manage_options(); break; } } public function admin_loader() { $this->wp_ajax_action( 'yop_poll_add_new_template_config' ); } public function manage_load_templates() { wp_enqueue_script( 'yop-poll-admin-templates-js', YOP_POLL_URL . "js/yop-poll-admin-templates.js", array( 'jquery' ), YOP_POLL_VERSION, true ); wp_enqueue_style( 'yop-poll-timepicker', YOP_POLL_URL . "css/timepicker.css", array(), YOP_POLL_VERSION ); wp_enqueue_style( 'yop-poll-jquery-ui', YOP_POLL_URL . "css/jquery-ui.css", array(), YOP_POLL_VERSION ); } public function id_used_template($id){ global $wpdb; $results = $wpdb->get_results( $wpdb->prepare( " SELECT * FROM " . $wpdb->yop_pollmeta ) ); foreach($results as $result){ $meta=unserialize($result->meta_value); // yop_poll_dump($result ); if($meta['template']==$id||$meta['widget']==$id){ return false; } } return true; } private function manage_options() { global $page, $action, $message; switch ( $action ) { case "after-buy": YOP_POLL_Pro_Admin::after_buy(); break; case "do-buy": YOP_POLL_Pro_Admin::do_buy(); break; case 'add-new-template': { self:: view_add_edit_templates( 'add-new' ); break; } case 'delete': { $message = $this->delete_templates( $_GET ); $this->view_templates(); break; } case 'clone': { $message = $this->clone_templates( $_POST ); $this->view_templates(); break; } case 'edit': { self:: view_add_edit_templates( 'edit' ); break; } default: { $this->view_templates(); break; } } } public function view_templates() { wp_enqueue_script( 'yop-poll-admin-js', YOP_POLL_URL . '/js/polls/yop-poll-admin.js', array( 'jquery', 'jquery-ui-datepicker' ), YOP_POLL_VERSION, true ); wp_enqueue_style( 'yop-poll-slider-css', YOP_POLL_URL . 'css/yop-poll-slider.css', array(), YOP_POLL_VERSION ); wp_enqueue_script( 'yop-poll-slider-js', YOP_POLL_URL . 'js/yop-poll-slider.js', array( 'jquery', 'jquery-ui-dialog' ), YOP_POLL_VERSION, true ); global $wpdb, $message; global $page, $action, $orderby, $order, $current_user; $orderby = ( empty ( $GLOBALS['orderby'] ) ) ? 'name' : $GLOBALS['orderby']; $order = ( empty ( $GLOBALS['order'] ) ) ? 'desc' : $GLOBALS['order']; $data['per_page'] = ( isset ( $_REQUEST ['per_page'] ) ? intval( $_REQUEST ['per_page'] ) : 100 ); $data['page_no'] = isset ( $_REQUEST ['page_no'] ) ? ( int )$_REQUEST ['page_no'] : 1; $order_fields = array( 'name', 'last_modified' ); $data['title'] = __yop_poll( "Templates" ); $data['REQUEST'] = $_REQUEST; $data['poll_url'] =YOP_POLL_URL; $data['orderby'] = ( empty ( $data['REQUEST']['orderby'] ) ) ? 'name' : $data['REQUEST']['orderby']; $data['order'] = ( empty ( $data['REQUEST']['order'] ) ) ? 'desc' : $data['REQUEST']['order']; $data['order_direction'] = $this->make_order_array( $order_fields, 'asc', $orderby, ( 'desc' == $order ) ? 'asc' : 'desc' ); $data['order_sortable'] = $this->make_order_array( $order_fields, 'sortable', $orderby, 'sorted' ); $data['order_direction_reverse'] = $this->make_order_array( $order_fields, 'desc', $orderby, ( 'desc' == $order ) ? 'desc' : 'asc' ); $data['search'] = array( 'fields' => array( 'name', 'last_modified' ), 'value' => isset ( $_REQUEST['s'] ) ? $_REQUEST ['s'] : '' ); $data['yop_poll_templates'] = Yop_Poll_Model::get_yop_poll_templates_search( $data['orderby'], $data['order'], $data['search'], $data['page_no'] - 1, $data['per_page'] ); $data['total_templates'] = self::count_yop_poll_templates_search( $data['orderby'], $data['order'], $data['search'] ); $data['message'] = array( 'error' => $message['error'], 'succes' => $message['success'] ); $data['templates'] = YOP_POLL_MODEL::get_yop_poll_templates_search( 'id', 'desc' ); $n = count( $data['templates'] ); for( $i = 0; $i < $n; $i ++ ) { $data['templates'][$i]['preview'] = YOP_POLL_Poll_Model::return_template_preview_html( $data['templates'][$i]['id'] ); } if ( intval( $data['page_no'] ) > intval( $data['total_templates'] ) ){ $data['page_no'] = 1; } $args = array( 'base' => remove_query_arg( 'page_no', $_SERVER ['REQUEST_URI'] ) . '%_%', 'format' => '&page_no=%#%', 'current' => max( 1, $data['page_no'] ), 'total' => ceil( $data['total_templates'] / $data['per_page'] ), 'prev_next' => true, 'prev_text' => __( '«' ), 'next_text' => __( '»' ) ); $data['pagination'] = paginate_links( $args ); $_SERVER ['REQUEST_URI'] = remove_query_arg( array( 'action' ), $_SERVER ['REQUEST_URI'] ); $data['request']['uri'] = $_SERVER["REQUEST_URI"]; $this->display( 'templates.html', $data ); } private function view_add_edit_templates( $act ) { global $action, $current_user; add_filter( 'wp_default_editor', 'force_default_editor' ); $data['title'] = __yop_poll( "Templates" ); $data['page_name'] = __yop_poll( "Add New Poll Template" ); $data['action_type'] = $action; $data['template_id'] = ''; if ( 'edit' == $data['action_type'] ){ $data['template_id'] = ( isset ( $_GET ['id'] ) ? intval( $_GET ['id'] ) : 0 ); } $data['current_template'] = self::get_poll_template_from_database_by_id( $data['template_id'] ); $data['template_author_name'] = self::get_poll_template_field_from_database_by_id( 1 ); $this->display( 'templates_add_edit.html', $data ); } public static function count_yop_poll_templates_search( $orderby = 'last_modified', $order = 'desc', $search = array( 'fields' => array(), 'value' => NULL ) ) { global $wpdb; $sql = "SELECT COUNT(*) AS templates_no FROM " . $wpdb->yop_poll_templates; $sql_search = ''; if ( count( $search['fields'] ) > 0 ){ $sql_search .= ' ( '; foreach ( $search['fields'] as $field ) { $sql_search .= $wpdb->prepare( ' `' . $field . '` like \'%%%s%%\' OR', $search['value'] ); } $sql_search = trim( $sql_search, 'OR' ); $sql_search .= ' ) '; } if ( count( $search['fields'] ) > 0 ){ $sql .= ' WHERE ' . $sql_search; } $sql .= ' ORDER BY ' . $orderby . ' ' . $order; $result = $wpdb->get_var( $sql ); return $result; } private function delete_templates( $request ) { global $message; $message['success'] = ""; $message['error'] = ""; if ( isset ( $_REQUEST ['templatecheck'] ) ){ $bulktemplates = ( array )$_REQUEST ['templatecheck']; foreach ( $bulktemplates as $template_id ) { $template_id = ( int )$template_id; $message = self::delete_template_from_db( $template_id ); } return $message; } else { if( check_admin_referer('yop-poll-templates-add-edit-action', 'yop-poll-templates-add-edit-name') ) { if ( isset ( $_REQUEST ['id'] ) ){ if(self::id_used_template( $_REQUEST ['id'])){ self::delete_template_from_db( $_REQUEST['id'] ); } else $message['error']='This template is used to display a poll'; } } else { wp_die( __yop_poll('You do not have enough permission to add a template')); } } return $message; } private function delete_template_from_db( $template_id ) { global $wpdb; $response['success'] = ""; $response['error'] = ""; if( check_admin_referer('yop-poll-templates-add-edit-action', 'yop-poll-templates-add-edit-name') ) { if ( current_user_can( 'delete_own_yop_polls_templates' ) ){ $sql = $wpdb->query( $wpdb->prepare( " DELETE FROM $wpdb->yop_poll_templates WHERE id = %d", $template_id ) ); if ( $sql ){ $response['success'] = __yop_poll( 'Template deleted' ); } else { $response['error'] = __yop_poll( 'Could not delete template from database! Please try again!' ); } } else { $response['error'] = __yop_poll( 'You do not have enough permission to delete a template' ); } return $response; } else { wp_die( __yop_poll('You do not have enough permission to add a template')); } } private function clone_templates( $request ) { global $message; $message['success'] = ""; $message['error'] = ""; if ( isset ( $_REQUEST ['templatecheck'] ) ){ $bulktemplates = ( array )$_REQUEST ['templatecheck']; foreach ( $bulktemplates as $template_id ) { $template_details = self::get_poll_template_from_database_by_id( $template_id ); $clone_number = self::count_poll_template_from_database_like_name( $template_details['name'] . ' - clone' ); if ( $template_details ){ $template = array( 'name' => $template_details['name'] . ' - clone' . ( 0 == $clone_number ? '' : $clone_number ), 'before_vote_template' => $template_details['before_vote_template'], 'after_vote_template' => $template_details['after_vote_template'], 'before_start_date_template' => $template_details['before_start_date_template'], 'after_end_date_template' => $template_details['after_end_date_template'], 'css' => $template_details['css'], 'js' => $template_details['js'], 'status' => ( 'default' == $template_details['status'] ) ? 'other' : $template_details['status'], 'date_added' => NULL, 'last_modified' => NULL ); $message = self::insert_poll_template_to_database( $template ); } } $this->view_templates(); } else { if ( isset ( $_REQUEST ['id'] ) ){ $template_details = self::get_poll_template_from_database_by_id( $_REQUEST ['id'] ); $clone_number = self::count_poll_template_from_database_like_name( $template_details['name'] . ' - clone' ); if ( $template_details ){ $template = array( 'name' => $template_details['name'] . ' - clone' . ( 0 == $clone_number ? '' : $clone_number ), 'before_vote_template' => $template_details['before_vote_template'], 'after_vote_template' => $template_details['after_vote_template'], 'before_start_date_template' => $template_details['before_start_date_template'], 'after_end_date_template' => $template_details['after_end_date_template'], 'css' => $template_details['css'], 'js' => $template_details['js'], 'status' => ( 'default' == $template_details['status'] ) ? 'other' : $template_details['status'], 'date_added' => NULL, 'last_modified' => NULL ); return self::insert_poll_template_to_database( $template ); } } $this->view_templates(); } $this->view_templates(); } private static function get_poll_template_from_database_by_id( $template_id = 0 ) { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( " SELECT * FROM " . $wpdb->yop_poll_templates . " WHERE id = %d LIMIT 0,1 ", $template_id ), ARRAY_A ); return $result; } private static function count_poll_template_from_database_like_name( $template_name ) { global $wpdb; $result = $wpdb->get_var( $wpdb->prepare( " SELECT count(*) FROM " . $wpdb->yop_poll_templates . " WHERE name like %s ", $template_name . '%' ) ); return $result; } private static function insert_poll_template_to_database( $template ) { $response['success'] = ""; $response['error'] = ""; global $wpdb; global $current_user; wp_get_current_user(); if( check_admin_referer('yop-poll-templates-add-edit-action', 'yop-poll-templates-add-edit-name') ) { if ( current_user_can( 'clone_own_yop_polls_templates' ) ){ $sql = $wpdb->query( $wpdb->prepare( " INSERT INTO " . $wpdb->yop_poll_templates . " SET template_author = %d, name = %s, before_vote_template = %s, after_vote_template = %s, before_start_date_template = %s, after_end_date_template = %s, css = %s, js = %s, date_added = %s, last_modified = %s, status = %s ", $current_user->ID, $template['name'], $template['before_vote_template'], $template['after_vote_template'], $template['before_start_date_template'], $template['after_end_date_template'], $template['css'], $template['js'], current_time( 'mysql' ), current_time( 'mysql' ), $template['status'] ) ); if ( $sql ){ $response['success'] = __yop_poll( 'Template cloned!' ); } else { $response['error'] = __yop_poll( 'Could not clone template from database! Please try again!' ); } } else { $response['error'] = __yop_poll( 'You do not have enough permission to clone a template' ); } return $response; } else { wp_die( __yop_poll( 'You do not have enough permission to add a template' ) ); } } public function do_add_edit_template() { if ( 'add-new' == $_POST['action'] ){ if ( ( !current_user_can( 'edit_yop_polls_templates' ) ) && ( !current_user_can( 'edit_yop_polls' ) ) ){ wp_die( __yop_poll( 'You are not allowed to edit this item.' ) ); } } self::save_template(); } public function do_reset_template() { if ( ( !current_user_can( 'edit_yop_polls_templates' ) ) && ( !current_user_can( 'edit_yop_polls' ) ) ){ wp_die( __yop_poll( 'You are not allowed to reset this item.' ) ); } self::reset_template(); } private static function reset_template() { $template['name'] = $_POST['yop_poll_template_name']; $template['before_vote_template'] = $_POST['yop-poll-before-vote-template-input']; $template['after_vote_template'] = $_POST['yop-poll-after-vote-template-input']; $template['before_start_date_template'] = $_POST['yop-poll-template-before-start-date-input']; $template['after_end_date_template'] = $_POST['yop-poll-template-after-end-date-input']; $template['css'] = $_POST['yop-poll-template-css-input']; $template['js'] = $_POST['yop-poll-template-js-input']; $template['status'] = "other"; $template['id'] = $_POST['template_id']; $data['action'] = $_POST['action']; $data['name'] = $_POST['yop_poll_reset_template_id']; $result = self::add_update_templates_in_database( $data['action'], $data['name'], $template['name'] ); if ( $result ){ wp_die( __yop_poll( 'Template reseted!' ) ); } else { wp_die( __yop_poll( 'Could not resete template in database! Please try again!' ) ); } } private static function save_template() { $template['name'] = $_POST['yop_poll_template_name']; $template['before_vote_template'] = $_POST['yop-poll-before-vote-template-input']; $template['after_vote_template'] = $_POST['yop-poll-after-vote-template-input']; $template['before_start_date_template'] = $_POST['yop-poll-template-before-start-date-input']; $template['after_end_date_template'] = $_POST['yop-poll-template-after-end-date-input']; $template['css'] = $_POST['yop-poll-template-css-input']; $template['js'] = $_POST['yop-poll-template-js-input']; $template['status'] = "other"; $template['id'] = $_POST['template_id']; if ( $_POST['action_type'] == "add-new-template" ){ if ( $template['name'] != "" ){ $result = self::get_poll_template_from_database_by_name( $template['name'] ); if ( $result ){ wp_die( __yop_poll( 'This template already exists! Please choose another name!' ) ); } } else { wp_die( __yop_poll( 'Please enter a template name!' ) ); } $result = self:: insert_poll_template_to_database( $template ); if ( $result ){ wp_die( __yop_poll( 'Template added!' ) ); } else { wp_die( __yop_poll( 'Could not add template in database! Please try again!' ) ); } } else { if ( $_POST['action_type'] == "edit" ){ if ( $template['name'] == "" ){ wp_die( __yop_poll( 'Please enter a template name!' ) ); } else { $result = self:: update_poll_template_in_database( $template ); if ( $result ){ wp_die( __yop_poll( 'Template edited!' ) ); } else { wp_die( __yop_poll( 'Could not edit template in database! Please try again!' ) ); } } } } } private static function get_poll_template_field_from_database_by_id( $template_id ) { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( " SELECT user_nicename FROM $wpdb->users WHERE ID = %d LIMIT 0,1 ", $template_id ), ARRAY_A ); return $result; } private static function get_poll_template_from_database_by_name( $template_name ) { global $wpdb; $sql = $wpdb->query( $wpdb->prepare( " SELECT * FROM $wpdb->yop_poll_templates WHERE name = %s ", $template_name ) ); return $sql; } private static function update_poll_template_in_database( $template ) { global $wpdb; $sql = $wpdb->query( $wpdb->prepare( " UPDATE " . $wpdb->yop_poll_templates . " SET name = %s, before_vote_template = %s, after_vote_template = %s, before_start_date_template = %s, after_end_date_template = %s, css = %s, js = %s, last_modified = %s WHERE id = %d ", $template['name'], $template['before_vote_template'], $template['after_vote_template'], $template['before_start_date_template'], $template['after_end_date_template'], $template['css'], $template['js'], current_time( 'mysql' ), $template['id'] ) ); return $sql; } private static function add_update_templates_in_database( $action = 'add', $template_name, $template_to_reset ) { global $wpdb; global $current_user; wp_get_current_user(); $sql = "UPDATE `" . $wpdb->yop_poll_templates . "` SET "; $before_vote_template = << [QUESTION_CONTAINER]
%POLL-QUESTION%
    [ANSWER_CONTAINER]
  • %POLL-ANSWER-CHECK-INPUT% %POLL-ANSWER-LABEL% [ANSWER_DESCRIPTION_CONTAINER] %ANSWER-DESCRIPTION% [/ANSWER_DESCRIPTION_CONTAINER] [ANSWER_RESULT_CONTAINER] %POLL-ANSWER-RESULT-LABEL% %POLL-ANSWER-RESULT-BAR% [/ANSWER_RESULT_CONTAINER]
  • [/ANSWER_CONTAINER] [OTHER_ANSWER_CONTAINER]
  • %POLL-OTHER-ANSWER-CHECK-INPUT% %POLL-OTHER-ANSWER-LABEL% %POLL-ANSWER-RESULT-LABEL% %POLL-OTHER-ANSWER-TEXT-INPUT% %POLL-ANSWER-RESULT-BAR%
  • [/OTHER_ANSWER_CONTAINER]
    [CUSTOM_FIELD_CONTAINER]
  • %POLL-CUSTOM-FIELD-LABEL% %POLL-CUSTOM-FIELD-TEXT-INPUT%
  • [/CUSTOM_FIELD_CONTAINER]
[/QUESTION_CONTAINER][CAPTCHA_CONTAINER]
%CAPTCHA-IMAGE%
%RELOAD-CAPTCHA-IMAGE%
%CAPTCHA-LABEL%
%CAPTCHA-INPUT%
[/CAPTCHA_CONTAINER]NOWDOC; $after_vote_template = << [QUESTION_CONTAINER]
%POLL-QUESTION%
    [ANSWER_RESULT_CONTAINER]
  • %POLL-ANSWER-RESULT-BAR%
  • [/ANSWER_RESULT_CONTAINER]
[/QUESTION_CONTAINER]NOWDOC; $after_vote_template = addslashes( $after_vote_template ); $before_start_date_template = << start on %POLL-START-DATE%
and will end on %POLL-END-DATE%
NOWDOC; $before_start_date_template = addslashes( $before_start_date_template ); $after_end_date_template = <<Start date %POLL-START-DATE%
End date %POLL-END-DATE%
Poll Results:
[QUESTION_CONTAINER]
%POLL-QUESTION%
    [ANSWER_RESULT_CONTAINER]
  • %POLL-ANSWER-RESULT-BAR%
  • [/ANSWER_RESULT_CONTAINER]
[/QUESTION_CONTAINER]
NOWDOC; $after_end_date_template = addslashes( $after_end_date_template ); $js = << 0) { jQuery(this).width( parseInt( jQuery(this).width() ) - parseInt(jQuery(this).css("border-left-width")) - parseInt(jQuery(this).css("border-right-width")) ); } else { jQuery(this).css("border-left-width", "0px"); jQuery(this).css("border-right-width", "0px"); } });}function stripPadding_%POLL-ID%(object) { object.each(function() { jQuery(this).width( parseInt( jQuery(this).width() ) - parseInt(jQuery(this).css("padding-left")) - parseInt(jQuery(this).css("padding-left")) ); });}function strip_results_%POLL-ID%() { stripPadding_%POLL-ID%( jQuery("#yop-poll-container-%POLL-ID% .yop_poll_li_result-%POLL-ID%") ); stripBorder_%POLL-ID%( jQuery("#yop-poll-container-%POLL-ID% .yop-poll-result-bar-%POLL-ID%") );}jQuery(document).ready(function(e) { jQuery('.yop-poll-forms').removeClass('yop-poll-forms-display'); if(typeof window.strip_results_%POLL-ID% == "function") strip_results_%POLL-ID%(); if(typeof window.tabulate_answers_%POLL-ID% == "function") tabulate_answers_%POLL-ID%(); if(typeof window.tabulate_results_%POLL-ID% == "function") tabulate_results_%POLL-ID%();});function equalWidth_%POLL-ID%(obj, cols, findWidest ) { findWidest = typeof findWidest !== "undefined" ? findWidest : false; var quest=0; if ( findWidest ) { obj.each(function() { var thisWidth = jQuery(this).width(); width = parseInt(thisWidth / cols); jQuery(this).width(width); jQuery(this).css("float", "left"); }); } else { var widest = 0; var count = 0; var poz_each_question=0; obj.each(function() { count++; cols[quest][2]=(jQuery('#yop-poll-answers-%POLL-ID%-'+ cols[quest][3] +' li').length); var thisWidth = jQuery(this).width(); if(thisWidth > widest) { widest = thisWidth; } if(count widest) { widest = thisWidth; } if(countprepare( ' WHERE name = %s', $template_to_reset ); return $wpdb->query( $sql ); }}function force_default_editor() { return 'html';}?>