Guest User

admin

a guest
Aug 25th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.40 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  
  5.  * Handles all Admin access functionality.
  6.  
  7.  */
  8.  
  9. class Wdca_AdminPages {
  10.  
  11.  
  12.  
  13.     //function Wdca_AdminPages () { $this->__construct(); }
  14.  
  15.  
  16.  
  17.     function __construct () {
  18.  
  19.  
  20.  
  21.     }
  22.  
  23.  
  24.  
  25.     /**
  26.  
  27.      * Main entry point.
  28.  
  29.      *
  30.  
  31.      * @static
  32.  
  33.      */
  34.  
  35.     //function serve () {
  36.     public static function serve () {
  37.         $me = new Wdca_AdminPages;
  38.  
  39.         $me->add_hooks();
  40.  
  41.     }
  42.  
  43.  
  44.  
  45.     function create_admin_menu_entry () {
  46.  
  47.         if (@$_POST && isset($_POST['option_page'])) {
  48.  
  49.             $changed = false;
  50.  
  51.             $page = !empty($_GET['page']) ? $_GET['page'] : false;
  52.  
  53.             $key = Wdca_Data::AB_MODE_KEY == $page
  54.  
  55.                 ? Wdca_Data::AB_MODE_KEY
  56.  
  57.                 : Wdca_Data::get_valid_key($page)
  58.  
  59.             ;
  60.  
  61.             if ("{$key}-options" == @$_POST['option_page']) {
  62.  
  63.                 update_option($key, stripslashes_deep($_POST[$key]));
  64.  
  65.                 $changed = true;
  66.  
  67.             }
  68.  
  69.  
  70.  
  71.             if ($changed) {
  72.  
  73.                 $goback = add_query_arg('settings-updated', 'true',  wp_get_referer());
  74.  
  75.                 wp_redirect($goback);
  76.  
  77.                 die;
  78.  
  79.             }
  80.  
  81.         }
  82.  
  83.         /*$page = is_multisite() ? 'settings.php' : 'options-general.php';*/
  84.  
  85.         $page = "edit.php?post_type=" . Wdca_CustomAd::POST_TYPE;
  86.  
  87.         $perms = defined('WDCA_LEGACY_OPTIONS_ACCESS') && WDCA_LEGACY_OPTIONS_ACCESS
  88.  
  89.             ? is_multisite() ? 'manage_network_options' : 'manage_options'
  90.  
  91.             : (is_multisite() && !defined('WDCA_MINIMUM_ADMIN_CAPABILITY') ? 'manage_options' : (defined('WDCA_MINIMUM_ADMIN_CAPABILITY') ? WDCA_MINIMUM_ADMIN_CAPABILITY : 'manage_options'))
  92.  
  93.         ;
  94.  
  95.         if (!Wdca_Data::get_ab_option('enabled')) {
  96.  
  97.             add_submenu_page($page, __('Settings', 'wdca'), __('Settings', 'wdca'), $perms, 'wdca', array($this, 'create_admin_page'));
  98.  
  99.         } else {
  100.  
  101.             add_submenu_page($page, __('Settings (A)', 'wdca'), __('Settings (A)', 'wdca'), $perms, Wdca_Data::DEFAULT_KEY, array($this, 'create_admin_page'));
  102.  
  103.             add_submenu_page($page, __('Settings (B)', 'wdca'), __('Settings (B)', 'wdca'), $perms, Wdca_Data::B_GROUP_KEY, array($this, 'create_admin_page'));
  104.  
  105.         }
  106.  
  107.         add_submenu_page($page, __('A/B Settings', 'wdca'), __('A/B Settings', 'wdca'), $perms, Wdca_Data::AB_MODE_KEY, array($this, 'create_admin_page'));
  108.  
  109.  
  110.  
  111.     }
  112.  
  113.  
  114.  
  115.     function register_settings () {
  116.  
  117.         // Register AB settings
  118.  
  119.         $mode = Wdca_Data::AB_MODE_KEY;
  120.  
  121.         $form = new Wdca_AdminFormRenderer($mode);
  122.  
  123.         register_setting($mode, $mode);
  124.  
  125.         add_settings_section('wdca_settings', __('A/B mode setup', 'wdca'), array($form, 'create_ab_mode_setup_box'), "{$mode}-options");
  126.  
  127.         add_settings_field('wdca_enable', __('Enable A/B testing', 'wdca'), array($form, 'create_enabled_box'), "{$mode}-options", 'wdca_settings');
  128.  
  129.         add_settings_field('wdca_session', __('Track mode in sessions', 'wdca'), array($form, 'create_sessions_box'), "{$mode}-options", 'wdca_settings');
  130.  
  131.         add_settings_field('wdca_b_group_for_admins', __('Always show B group to admins', 'wdca'), array($form, 'create_b_group_for_admins_box'), "{$mode}-options", 'wdca_settings');
  132.  
  133.         add_settings_field('wdca_b_group_for_users', __('Always show B group to all my users', 'wdca'), array($form, 'create_b_group_for_users_box'), "{$mode}-options", 'wdca_settings');
  134.  
  135.         add_settings_field('wdca_get_override', __('GET key override', 'wdca'), array($form, 'create_get_key_override_box'), "{$mode}-options", 'wdca_settings');
  136.  
  137.  
  138.  
  139.         // ... that's done. Now, register mode settings:
  140.  
  141.         if (!Wdca_Data::get_ab_option('enabled')) return $this->register_mode_settings(Wdca_Data::DEFAULT_KEY);
  142.  
  143.  
  144.  
  145.         $this->register_mode_settings(Wdca_Data::DEFAULT_KEY);
  146.  
  147.         $this->register_mode_settings(Wdca_Data::B_GROUP_KEY);
  148.  
  149.     }
  150.  
  151.  
  152.  
  153.     function register_mode_settings ($mode) {
  154.  
  155.         $form = new Wdca_AdminFormRenderer($mode);
  156.  
  157.  
  158.  
  159.         register_setting($mode, $mode);
  160.  
  161.         add_settings_section('wdca_settings', __('Custom Ads', 'wdca'), create_function('', ''), "{$mode}-options");
  162.  
  163.         add_settings_field('wdca_enable', __('Enable Custom Ads', 'wdca'), array($form, 'create_enabled_box'), "{$mode}-options", 'wdca_settings');
  164.  
  165.         add_settings_field('wdca_test', __('Live mode', 'wdca'), array($form, 'create_live_mode_box'), "{$mode}-options", 'wdca_settings');
  166.  
  167.         add_settings_field('wdca_ad_count', __('Show this many Ads', 'wdca'), array($form, 'create_ad_count_box'), "{$mode}-options", 'wdca_settings');
  168.  
  169.         add_settings_field('wdca_ad_order', __('Order Ads by', 'wdca'), array($form, 'create_ad_order_box'), "{$mode}-options", 'wdca_settings');
  170.  
  171.         add_settings_field('wdca_p_first_count', __('Inject first Ad after this many paragraphs', 'wdca'), array($form, 'create_p_first_count_box'), "{$mode}-options", 'wdca_settings');
  172.  
  173.         add_settings_field('wdca_p_count', __('Inject subsequent Ads after this many paragraphs each', 'wdca'), array($form, 'create_p_count_box'), "{$mode}-options", 'wdca_settings');
  174.  
  175.         add_settings_field('wdca_ad_delay', __('Delayed Ads insertion', 'wdca'), array($form, 'create_ad_show_after_box'), "{$mode}-options", 'wdca_settings');
  176.  
  177.         add_settings_field('wdca_predefined_positions', __('Predefined positions', 'wdca'), array($form, 'create_predefined_positions_box'), "{$mode}-options", 'wdca_settings');
  178.  
  179.  
  180.  
  181.         add_settings_section('wdca_appearance', __('Appearance &amp; messages', 'wdca'), create_function('', ''), "{$mode}-options");
  182.  
  183.         add_settings_field('wdca_theme', __('Theme', 'wdca'), array($form, 'create_theme_box'), "{$mode}-options", 'wdca_appearance');
  184.  
  185.         add_settings_field('wdca_messages', __('Messages', 'wdca'), array($form, 'create_messages_box'), "{$mode}-options", 'wdca_appearance');
  186.  
  187.         add_settings_field('wdca_link', __('Link click', 'wdca'), array($form, 'create_link_box'), "{$mode}-options", 'wdca_appearance');
  188.  
  189.  
  190.  
  191.         add_settings_section('wdca_analytics', __('Google Analytics Integration', 'wdca'), array($form, 'create_ga_setup_box'), "{$mode}-options");
  192.  
  193.         add_settings_field('wdca_ga_integration', __('Enable Google Analytics integration', 'wdca'), array($form, 'create_ga_integration_box'), "{$mode}-options", 'wdca_analytics');
  194.  
  195.         add_settings_field('wdca_ga_category', __('Event category', 'wdca'), array($form, 'create_ga_category_box'), "{$mode}-options", 'wdca_analytics');
  196.  
  197.         add_settings_field('wdca_ga_label', __('Event label', 'wdca'), array($form, 'create_ga_label_box'), "{$mode}-options", 'wdca_analytics');
  198.  
  199.  
  200.  
  201.         add_settings_section('wdca_advanced', __('Advanced', 'wdca'), create_function('', ''), "{$mode}-options");
  202.  
  203.         add_settings_field('wdca_allow_post_types', __('Custom post types Ads', 'wdca'), array($form, 'create_cpt_ads_box'), "{$mode}-options", 'wdca_advanced');
  204.  
  205.         add_settings_field('wdca_post_metabox', __('Show post metabox', 'wdca'), array($form, 'create_post_metabox_box'), "{$mode}-options", 'wdca_advanced');
  206.  
  207.         add_settings_field('wdca_to_categories', __('Connect post categories', 'wdca'), array($form, 'create_categories_box'), "{$mode}-options", 'wdca_advanced');
  208.  
  209.         add_settings_field('wdca_to_tags', __('Connect post tags', 'wdca'), array($form, 'create_tags_box'), "{$mode}-options", 'wdca_advanced');
  210.  
  211.         add_settings_field('wdca_elements', __('Elements selector', 'wdca'), array($form, 'create_selector_box'), "{$mode}-options", 'wdca_advanced');
  212.  
  213.         add_settings_field('wdca_lazy_loading', __('Lazy loading', 'wdca'), array($form, 'create_lazy_loading_box'), "{$mode}-options", 'wdca_advanced');
  214.  
  215.     }
  216.  
  217.  
  218.  
  219.     function create_admin_page () {
  220.  
  221.         $option_key = $title = false;
  222.  
  223.         if (!empty($_GET['page']) && Wdca_Data::AB_MODE_KEY == $_GET['page']) {
  224.  
  225.             $option_key = Wdca_Data::AB_MODE_KEY;
  226.  
  227.             $title = __('A/B Settings', 'wdca');
  228.  
  229.         } else {
  230.  
  231.             $option_key = Wdca_Data::get_ab_option('enabled')
  232.  
  233.                 ? Wdca_Data::get_valid_key(@$_GET['page'])
  234.  
  235.                 : Wdca_Data::DEFAULT_KEY
  236.  
  237.             ;
  238.  
  239.             $title = !Wdca_Data::get_ab_option('enabled')
  240.  
  241.                 ? __('Settings', 'wdca')
  242.  
  243.                 : (Wdca_Data::DEFAULT_KEY == Wdca_Data::get_valid_key(@$_GET['page'])
  244.  
  245.                     ? __('Settings (A)', 'wdca')
  246.  
  247.                     : __('Settings (B)', 'wdca')
  248.  
  249.                 )
  250.  
  251.             ;
  252.  
  253.         }
  254.  
  255.         include(WDCA_PLUGIN_BASE_DIR . '/lib/forms/plugin_settings.php');
  256.  
  257.     }
  258.  
  259.  
  260.  
  261.     function js_print_scripts () {
  262.  
  263.         printf(
  264.  
  265.             '<script type="text/javascript">
  266.  
  267.                 var _wdca_data = {
  268.  
  269.                     "root_url": "%s",
  270.  
  271.                 };
  272.  
  273.             </script>',
  274.  
  275.             WDCA_PLUGIN_URL
  276.  
  277.         );
  278.  
  279.     }
  280.  
  281.  
  282.  
  283.     function js_editor_button () {
  284.  
  285.         wp_enqueue_script('wdca_editor', WDCA_PLUGIN_URL . '/js/wdca-button.js', array('jquery'));
  286.  
  287.         wp_localize_script('wdca_editor', 'l10nWdca', array(
  288.  
  289.             'add_ad' => __('Insert Ad', 'wdca'),
  290.  
  291.             'ad_title' => __('Title', 'wdca'),
  292.  
  293.             'ad_date' => __('Date', 'wdca'),
  294.  
  295.             'appearance' => __('Appearance', 'wdca'),
  296.  
  297.             'add_blank' => __('Insert blank placeholder for an ad', 'wdca'),
  298.  
  299.             'or_select_below' => __('or select an Ad to insert from the ones listed below', 'wdca'),
  300.  
  301.             'dflt' => __('Default', 'wdca'),
  302.  
  303.             'ad_size' => __('Size', 'wdca'),
  304.  
  305.             'small' => __('Small', 'wdca'),
  306.  
  307.             'medium' => __('Medium', 'wdca'),
  308.  
  309.             'large' => __('Large', 'wdca'),
  310.  
  311.             'ad_position' => __('Position', 'wdca'),
  312.  
  313.             'left' => __('Left', 'wdca'),
  314.  
  315.             'right' => __('Right', 'wdca'),
  316.  
  317.         ));
  318.  
  319.     }
  320.  
  321.  
  322.  
  323.     function css_print_styles () {
  324.  
  325.     }
  326.  
  327.  
  328.  
  329.     public function add_meta_boxes () {
  330.  
  331.         add_meta_box(
  332.  
  333.             'wdca_prevent_ad_insertion',
  334.  
  335.             __('In Post Ads', 'wdca'),
  336.  
  337.             array($this, 'render_prevent_ad_box'),
  338.  
  339.             'post',
  340.  
  341.             'side',
  342.  
  343.             'low'
  344.  
  345.         );
  346.  
  347.     }
  348.  
  349.  
  350.  
  351.     public function render_prevent_ad_box () {
  352.  
  353.         global $post;
  354.  
  355.         $post_id = wp_is_post_revision($post);
  356.  
  357.         $post_id = $post_id ? $post_id : $post->ID;
  358.  
  359.  
  360.  
  361.         $opts = get_option('wdca');
  362.  
  363.         $prevent_items = @$opts['prevent_items'];
  364.  
  365.         $prevent_items = is_array($prevent_items) ? $prevent_items : array();
  366.  
  367.         $checked = in_array($post_id, $prevent_items) ? 'checked="checked"' : '';
  368.  
  369.         echo "<p><input type='checkbox' {$checked} name='wdca_hide_box' id='wdca_hide_box' value='1' />";
  370.  
  371.         echo ' <label for="wdca_hide_box">' . __('Do not show In Post Ads in this post', 'wdca') . '</label></p>';
  372.  
  373.     }
  374.  
  375.  
  376.  
  377.     function save_meta () {
  378.  
  379.         global $post;
  380.  
  381.         $post_id = wp_is_post_revision($post);
  382.  
  383.         $post_id = $post_id ? $post_id : $post->ID;
  384.  
  385.  
  386.  
  387.         $opts = get_option('wdca');
  388.  
  389.         $opts = $opts ? $opts : array();
  390.  
  391.         $opts['prevent_items'] = @$opts['prevent_items'] ? $opts['prevent_items'] : array();
  392.  
  393.  
  394.  
  395.         if (@$_POST['wdca_hide_box']) {
  396.  
  397.             $opts['prevent_items'][] = $post_id;
  398.  
  399.         } else {
  400.  
  401.             $key = array_search($post_id, $opts['prevent_items']);
  402.  
  403.             if (false !== $key) unset($opts['prevent_items'][$key]);
  404.  
  405.         }
  406.  
  407.         $opts['prevent_items'] = array_unique($opts['prevent_items']);
  408.  
  409.         update_option('wdca', $opts);
  410.  
  411.     }
  412.  
  413.  
  414.  
  415.     /**
  416.  
  417.      * Handles ad listing requests.
  418.  
  419.      */
  420.  
  421.     function json_list_ads () {
  422.  
  423.         $ads = Wdca_CustomAd::get_all_ads();
  424.  
  425.         header('Content-type: application/json');
  426.  
  427.         echo json_encode($ads);
  428.  
  429.         exit();
  430.  
  431.     }
  432.  
  433.  
  434.  
  435.     function add_hooks () {
  436.  
  437.         add_action('admin_init', array($this, 'register_settings'));
  438.  
  439.         $hook = /*is_multisite() ? 'network_admin_menu' :*/ 'admin_menu';
  440.  
  441.         add_action($hook, array($this, 'create_admin_menu_entry'));
  442.  
  443.  
  444.  
  445.         add_action('admin_init', array($this, 'add_meta_boxes'));
  446.  
  447.         add_action('save_post', array($this, 'save_meta'));
  448.  
  449.  
  450.  
  451.         add_action('admin_print_scripts', array($this, 'js_print_scripts'));
  452.  
  453.         add_action('admin_print_styles', array($this, 'css_print_styles'));
  454.  
  455.  
  456.  
  457.         add_action('admin_print_scripts-post.php', array($this, 'js_editor_button'));
  458.  
  459.         add_action('admin_print_scripts-post-new.php', array($this, 'js_editor_button'));
  460.  
  461.  
  462.  
  463.         add_action('wp_ajax_wdca_list_ads', array($this, 'json_list_ads'));
  464.  
  465.     }
  466.  
  467. }
Add Comment
Please, Sign In to add comment