assets
11 years ago
includes
11 years ago
views
11 years ago
class-advanced-ads-admin.php
11 years ago
class-advanced-ads-admin.php
935 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Advanced Ads. |
| 5 | * |
| 6 | * @package Advanced_Ads_Admin |
| 7 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 8 | * @license GPL-2.0+ |
| 9 | * @link http://webgilde.com |
| 10 | * @copyright 2013 Thomas Maier, webgilde GmbH |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Plugin class. This class should ideally be used to work with the |
| 15 | * administrative side of the WordPress site. |
| 16 | * |
| 17 | * |
| 18 | * @package Advanced_Ads_Admin |
| 19 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 20 | */ |
| 21 | class Advanced_Ads_Admin { |
| 22 | |
| 23 | /** |
| 24 | * Instance of this class. |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | * @var object |
| 28 | */ |
| 29 | protected static $instance = null; |
| 30 | |
| 31 | /** |
| 32 | * Slug of the settings page |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | * @var string |
| 36 | */ |
| 37 | public $plugin_screen_hook_suffix = null; |
| 38 | |
| 39 | /** |
| 40 | * Slug of the ad group page |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | * @var string |
| 44 | */ |
| 45 | protected $ad_group_hook_suffix = null; |
| 46 | |
| 47 | /** |
| 48 | * general plugin slug |
| 49 | * |
| 50 | * @since 1.0.0 |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $plugin_slug = ''; |
| 54 | |
| 55 | /** |
| 56 | * post type slug |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | * @var string |
| 60 | */ |
| 61 | protected $post_type = ''; |
| 62 | |
| 63 | /** |
| 64 | * Initialize the plugin by loading admin scripts & styles and adding a |
| 65 | * settings page and menu. |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | private function __construct() { |
| 70 | |
| 71 | /* |
| 72 | * Call $plugin_slug from public plugin class. |
| 73 | * |
| 74 | */ |
| 75 | $plugin = Advanced_Ads::get_instance(); |
| 76 | $this->plugin_slug = $plugin->get_plugin_slug(); |
| 77 | $this->post_type = constant("Advanced_Ads::POST_TYPE_SLUG"); |
| 78 | |
| 79 | // Load admin style sheet and JavaScript. |
| 80 | add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles')); |
| 81 | add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts')); |
| 82 | |
| 83 | // Add menu items |
| 84 | add_action('admin_menu', array($this, 'add_plugin_admin_menu')); |
| 85 | |
| 86 | // on post/ad edit screen |
| 87 | add_action('edit_form_after_title', array($this, 'edit_form_below_title')); |
| 88 | add_action('admin_init', array($this, 'add_meta_boxes')); |
| 89 | |
| 90 | // save ads post type |
| 91 | add_action('save_post', array($this, 'save_ad')); |
| 92 | |
| 93 | // handling extra columns on ad lists |
| 94 | add_filter('manage_advanced_ads_posts_columns', array($this, 'ad_list_columns_head')); |
| 95 | add_filter('manage_advanced_ads_posts_custom_column', array($this, 'ad_list_columns_content'), 10, 2); |
| 96 | |
| 97 | // settings handling |
| 98 | add_action('admin_init', array($this, 'settings_init')); |
| 99 | |
| 100 | // admin notices |
| 101 | add_action('admin_notices', array($this, 'admin_notices')); |
| 102 | |
| 103 | // Add an action link pointing to the options page. |
| 104 | $plugin_basename = plugin_basename(plugin_dir_path('__DIR__') . $this->plugin_slug . '.php'); |
| 105 | add_filter('plugin_action_links_' . $plugin_basename, array($this, 'add_action_links')); |
| 106 | |
| 107 | // add meta box for post types edit pages |
| 108 | add_action( 'add_meta_boxes', array( $this, 'add_post_meta_box' ) ); |
| 109 | add_action( 'save_post', array( $this, 'save_post_meta_box' ) ); |
| 110 | |
| 111 | // register dashboard widget |
| 112 | add_action('wp_dashboard_setup', array($this, 'add_dashboard_widget')); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Return an instance of this class. |
| 118 | * |
| 119 | * @since 1.0.0 |
| 120 | * |
| 121 | * @return object A single instance of this class. |
| 122 | */ |
| 123 | public static function get_instance() { |
| 124 | |
| 125 | // If the single instance hasn't been set, set it now. |
| 126 | if (null == self::$instance) { |
| 127 | self::$instance = new self; |
| 128 | } |
| 129 | |
| 130 | return self::$instance; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Register and enqueue admin-specific style sheet. |
| 135 | * |
| 136 | * @since 1.0.0 |
| 137 | * |
| 138 | * @return null Return early if no settings page is registered. |
| 139 | */ |
| 140 | public function enqueue_admin_styles() { |
| 141 | |
| 142 | global $post; |
| 143 | if (!isset($this->plugin_screen_hook_suffix) && isset($post) && Advanced_Ads::POST_TYPE_SLUG != $post->type) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | wp_enqueue_style($this->plugin_slug . '-admin-styles', plugins_url('assets/css/admin.css', __FILE__), array(), Advanced_Ads::VERSION); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Register and enqueue admin-specific JavaScript. |
| 152 | * |
| 153 | * @since 1.0.0 |
| 154 | * |
| 155 | * @return null Return early if no settings page is registered. |
| 156 | */ |
| 157 | public function enqueue_admin_scripts() { |
| 158 | |
| 159 | global $post; |
| 160 | if (!isset($this->plugin_screen_hook_suffix) && isset($post) && Advanced_Ads::POST_TYPE_SLUG != $post->type) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | wp_enqueue_script($this->plugin_slug . '-admin-script', plugins_url('assets/js/admin.js', __FILE__), array('jquery', 'jquery-ui-autocomplete'), Advanced_Ads::VERSION); |
| 165 | |
| 166 | // just register this script for later inclusion on ad group list page |
| 167 | wp_register_script('inline-edit-group-ads', plugins_url('assets/js/inline-edit-group-ads.js', __FILE__), array('jquery'), Advanced_Ads::VERSION); |
| 168 | |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * display admin notices |
| 173 | * |
| 174 | * @since 1.2.1 |
| 175 | */ |
| 176 | public function admin_notices() |
| 177 | { |
| 178 | |
| 179 | // display notice in case there are still old ad injections |
| 180 | $old_injections = get_option('advads-ads-injections', array()); |
| 181 | |
| 182 | // display ad before the content |
| 183 | if(isset($old_injections) && count($old_injections) > 0){ |
| 184 | $_injection_ids = array(); |
| 185 | foreach($old_injections as $_inj){ |
| 186 | $_injection_ids = array_merge($_injection_ids, $_inj); |
| 187 | } |
| 188 | $ad_links = array(); |
| 189 | foreach($_injection_ids as $_inj_id){ |
| 190 | $ad_links[] = '<a href="' . get_edit_post_link($_inj_id) . '">'.$_inj_id.'</a>'; |
| 191 | } |
| 192 | ?> |
| 193 | <div class="error"><p><?php printf(__('Advanced Ads Update: Auto injections are now managed through placements. Please convert these ads with auto injections: %s', ADVADS_SLUG), implode(', ', $ad_links));?></p></div> |
| 194 | <?php |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Register the administration menu for this plugin into the WordPress Dashboard menu. |
| 200 | * |
| 201 | * @since 1.0.0 |
| 202 | */ |
| 203 | public function add_plugin_admin_menu() { |
| 204 | |
| 205 | // add main menu item with overview page |
| 206 | add_menu_page( |
| 207 | __('Overview', ADVADS_SLUG), __('Advanced Ads', ADVADS_SLUG), 'manage_options', $this->plugin_slug, array($this, 'display_overview_page'), 'dashicons-chart-line', '58.74' |
| 208 | ); |
| 209 | |
| 210 | add_submenu_page( |
| 211 | $this->plugin_slug, __('Ads', ADVADS_SLUG), __('Ads', ADVADS_SLUG), 'manage_options', 'edit.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG |
| 212 | ); |
| 213 | |
| 214 | $this->ad_group_hook_suffix = add_submenu_page( |
| 215 | $this->plugin_slug, __('Ad Groups', ADVADS_SLUG), __('Groups', ADVADS_SLUG), 'manage_options', $this->plugin_slug . '-groups', array($this, 'ad_group_admin_page') |
| 216 | ); |
| 217 | |
| 218 | // add placements page |
| 219 | add_submenu_page( |
| 220 | $this->plugin_slug, __('Ad Placements', ADVADS_SLUG), __('Placements', ADVADS_SLUG), 'manage_options', $this->plugin_slug . '-placements', array($this, 'display_placements_page') |
| 221 | ); |
| 222 | // add settings page |
| 223 | $this->plugin_screen_hook_suffix = add_submenu_page( |
| 224 | $this->plugin_slug, __('Advanced Ads Settings', ADVADS_SLUG), __('Settings', ADVADS_SLUG), 'manage_options', $this->plugin_slug . '-settings', array($this, 'display_plugin_settings_page') |
| 225 | ); |
| 226 | add_submenu_page( |
| 227 | null, __('Advanced Ads Debugging', ADVADS_SLUG), __('Debug', ADVADS_SLUG), 'manage_options', $this->plugin_slug . '-debug', array($this, 'display_plugin_debug_page') |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Render the overview page |
| 233 | * |
| 234 | * @since 1.2.2 |
| 235 | */ |
| 236 | public function display_overview_page() { |
| 237 | $recent_ads = Advanced_Ads::get_ads(); |
| 238 | $groups = Advanced_Ads::get_ad_groups(); |
| 239 | $placements = Advanced_Ads::get_ad_placements_array(); |
| 240 | include_once( 'views/overview.php' ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Render the settings page |
| 245 | * |
| 246 | * @since 1.0.0 |
| 247 | */ |
| 248 | public function display_plugin_settings_page() { |
| 249 | include_once( 'views/settings.php' ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Render the placements page |
| 254 | * |
| 255 | * @since 1.1.0 |
| 256 | */ |
| 257 | public function display_placements_page() { |
| 258 | // sace new placement |
| 259 | if(isset($_POST['advads']['placement'])){ |
| 260 | $return = Advads_Ad_Placements::save_new_placement($_POST['advads']['placement']); |
| 261 | } |
| 262 | // save placement data |
| 263 | if(isset($_POST['advads']['placements'])){ |
| 264 | $return = Advads_Ad_Placements::save_placements($_POST['advads']['placements']); |
| 265 | } |
| 266 | $error = false; |
| 267 | $success = false; |
| 268 | if(isset($return) && $return !== true) { |
| 269 | $error = $return; |
| 270 | } elseif(isset($return) && $return === true){ |
| 271 | $success = __('Placements updated', ADVADS_SLUG); |
| 272 | } |
| 273 | $placement_types = Advads_Ad_Placements::get_placement_types(); |
| 274 | $placements = Advanced_Ads::get_ad_placements_array(); |
| 275 | // load ads and groups for select field |
| 276 | |
| 277 | // display view |
| 278 | include_once( 'views/placements.php' ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Render the debug page |
| 283 | * |
| 284 | * @since 1.0.1 |
| 285 | */ |
| 286 | public function display_plugin_debug_page() { |
| 287 | // load array with ads by condition |
| 288 | $plugin = Advanced_Ads::get_instance(); |
| 289 | $plugin_options = $plugin->options(); |
| 290 | $ads_by_conditions = $plugin->get_ads_by_conditions_array(); |
| 291 | $ad_placements = Advanced_Ads::get_ad_placements_array(); |
| 292 | |
| 293 | include_once( 'views/debug.php' ); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Render the ad group page |
| 298 | * |
| 299 | * @since 1.0.0 |
| 300 | */ |
| 301 | public function ad_group_admin_page() { |
| 302 | |
| 303 | $taxonomy = Advanced_Ads::AD_GROUP_TAXONOMY; |
| 304 | $post_type = Advanced_Ads::POST_TYPE_SLUG; |
| 305 | $tax = get_taxonomy($taxonomy); |
| 306 | |
| 307 | $action = $this->current_action(); |
| 308 | |
| 309 | // handle new and updated groups |
| 310 | if ($action == 'editedgroup') { |
| 311 | $group_id = (int) $_POST['group_id']; |
| 312 | check_admin_referer('update-group_' . $group_id); |
| 313 | |
| 314 | if (!current_user_can($tax->cap->edit_terms)) |
| 315 | wp_die(__('Sorry, you are not allowed to access this feature.', ADVADS_SLUG)); |
| 316 | |
| 317 | // handle new groups |
| 318 | if ($group_id == 0) { |
| 319 | $ret = wp_insert_term($_POST['name'], $taxonomy, $_POST); |
| 320 | if ($ret && !is_wp_error($ret)) |
| 321 | $forced_message = 1; |
| 322 | else |
| 323 | $forced_message = 4; |
| 324 | // handle group updates |
| 325 | } else { |
| 326 | $tag = get_term($group_id, $taxonomy); |
| 327 | if (!$tag) |
| 328 | wp_die(__('You attempted to edit an ad group that doesn’t exist. Perhaps it was deleted?', ADVADS_SLUG)); |
| 329 | |
| 330 | $ret = wp_update_term($group_id, $taxonomy, $_POST); |
| 331 | if ($ret && !is_wp_error($ret)) |
| 332 | $forced_message = 3; |
| 333 | else |
| 334 | $forced_message = 5; |
| 335 | } |
| 336 | // deleting items |
| 337 | } elseif($action == 'delete'){ |
| 338 | $group_id = (int) $_REQUEST['group_id']; |
| 339 | check_admin_referer('delete-tag_' . $group_id); |
| 340 | |
| 341 | if (!current_user_can($tax->cap->delete_terms)) |
| 342 | wp_die(__('Sorry, you are not allowed to access this feature.', ADVADS_SLUG)); |
| 343 | |
| 344 | wp_delete_term($group_id, $taxonomy); |
| 345 | |
| 346 | $forced_message = 2; |
| 347 | } |
| 348 | |
| 349 | // handle views |
| 350 | switch ($action) { |
| 351 | case 'edit' : |
| 352 | $title = $tax->labels->edit_item; |
| 353 | if (isset($_REQUEST['group_id'])) { |
| 354 | $group_id = absint($_REQUEST['group_id']); |
| 355 | $tag = get_term($group_id, $taxonomy, OBJECT, 'edit'); |
| 356 | } else { |
| 357 | $group_id = 0; |
| 358 | $tag = false; |
| 359 | } |
| 360 | |
| 361 | require_once( 'views/ad-group-edit.php' ); |
| 362 | break; |
| 363 | |
| 364 | default : |
| 365 | $title = $tax->labels->name; |
| 366 | // load needed classes |
| 367 | include_once( 'includes/class-list-table.php' ); |
| 368 | include_once( 'includes/class-ad-groups-list-table.php' ); |
| 369 | // load template |
| 370 | include_once( 'views/ad-group.php' ); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * returns a link to the ad group list page |
| 376 | * |
| 377 | * @since 1.0.0 |
| 378 | * @param arr $args additional arguments, e.g. action or group_id |
| 379 | * @return string admin url |
| 380 | */ |
| 381 | static function group_page_url($args = array()) { |
| 382 | $plugin = Advanced_Ads::get_instance(); |
| 383 | |
| 384 | $defaultargs = array( |
| 385 | // 'post_type' => constant("Advanced_Ads::POST_TYPE_SLUG"), |
| 386 | 'page' => 'advanced-ads-groups', |
| 387 | ); |
| 388 | $args = $args + $defaultargs; |
| 389 | |
| 390 | return add_query_arg($args, admin_url('admin.php')); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Add settings action link to the plugins page. |
| 395 | * |
| 396 | * @since 1.0.0 |
| 397 | */ |
| 398 | public function add_action_links($links) { |
| 399 | |
| 400 | return array_merge( |
| 401 | array( |
| 402 | 'settings' => '<a href="' . admin_url('edit.php?post_type=advanced_ads&page=advanced-ads-settings') . '">' . __('Settings', ADVADS_SLUG) . '</a>' |
| 403 | ), $links |
| 404 | ); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * add information about the ad below the ad title |
| 409 | * |
| 410 | * @since 1.1.0 |
| 411 | * @param obj $post |
| 412 | */ |
| 413 | public function edit_form_below_title($post){ |
| 414 | if (!isset($post->post_type) || $post->post_type != $this->post_type) { |
| 415 | return; |
| 416 | } |
| 417 | require_once('views/ad_info.php'); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Add meta boxes |
| 422 | * |
| 423 | * @since 1.0.0 |
| 424 | */ |
| 425 | public function add_meta_boxes() { |
| 426 | add_meta_box( |
| 427 | 'ad-main-box', __('Ad Type', ADVADS_SLUG), array($this, 'markup_meta_boxes'), Advanced_Ads::POST_TYPE_SLUG, 'normal', 'high' |
| 428 | ); |
| 429 | add_meta_box( |
| 430 | 'ad-parameters-box', __('Ad Parameters', ADVADS_SLUG), array($this, 'markup_meta_boxes'), Advanced_Ads::POST_TYPE_SLUG, 'normal', 'high' |
| 431 | ); |
| 432 | add_meta_box( |
| 433 | 'ad-output-box', __('Layout / Output', ADVADS_SLUG), array($this, 'markup_meta_boxes'), Advanced_Ads::POST_TYPE_SLUG, 'normal', 'high' |
| 434 | ); |
| 435 | add_meta_box( |
| 436 | 'ad-display-box', __('Display Conditions', ADVADS_SLUG), array($this, 'markup_meta_boxes'), Advanced_Ads::POST_TYPE_SLUG, 'normal', 'high' |
| 437 | ); |
| 438 | add_meta_box( |
| 439 | 'ad-visitor-box', __('Visitor Conditions', ADVADS_SLUG), array($this, 'markup_meta_boxes'), Advanced_Ads::POST_TYPE_SLUG, 'normal', 'high' |
| 440 | ); |
| 441 | add_meta_box( |
| 442 | 'ad-inject-box', __('Auto injection', ADVADS_SLUG), array($this, 'markup_meta_boxes'), Advanced_Ads::POST_TYPE_SLUG, 'normal', 'high' |
| 443 | ); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * load templates for all meta boxes |
| 448 | * |
| 449 | * @since 1.0.0 |
| 450 | * @param obj $post |
| 451 | * @param array $box |
| 452 | * @todo move ad initialization to main function and just global it |
| 453 | */ |
| 454 | public function markup_meta_boxes($post, $box) { |
| 455 | $ad = new Advads_Ad($post->ID); |
| 456 | |
| 457 | switch ($box['id']) { |
| 458 | case 'ad-main-box': |
| 459 | $view = 'ad-main-metabox.php'; |
| 460 | break; |
| 461 | case 'ad-parameters-box': |
| 462 | $view = 'ad-parameters-metabox.php'; |
| 463 | break; |
| 464 | case 'ad-output-box': |
| 465 | $view = 'ad-output-metabox.php'; |
| 466 | break; |
| 467 | case 'ad-display-box': |
| 468 | $view = 'ad-display-metabox.php'; |
| 469 | break; |
| 470 | case 'ad-visitor-box': |
| 471 | $view = 'ad-visitor-metabox.php'; |
| 472 | break; |
| 473 | case 'ad-inject-box': |
| 474 | $view = 'ad-inject-metabox.php'; |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | if (empty($view)) |
| 479 | return; |
| 480 | $view = plugin_dir_path(__FILE__) . 'views/' . $view; |
| 481 | if (is_file($view)) { |
| 482 | require_once( $view ); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * prepare the ad post type to be saved |
| 488 | * |
| 489 | * @since 1.0.0 |
| 490 | * @param int $post_id id of the post |
| 491 | * @todo handling this more dynamic based on ad type |
| 492 | */ |
| 493 | public function save_ad($post_id) { |
| 494 | |
| 495 | // only use for ads, no other post type |
| 496 | if (!isset($_POST['post_type']) || $this->post_type != $_POST['post_type'] || !isset($_POST['advanced_ad']['type'])) { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | // don’t do this on revisions |
| 501 | if ( wp_is_post_revision( $post_id ) ) |
| 502 | return; |
| 503 | |
| 504 | // get ad object |
| 505 | $ad = new Advads_Ad($post_id); |
| 506 | if (!$ad instanceof Advads_Ad) |
| 507 | return; |
| 508 | |
| 509 | $ad->type = $_POST['advanced_ad']['type']; |
| 510 | if(isset($_POST['advanced_ad']['output'])) { |
| 511 | $ad->set_option('output', $_POST['advanced_ad']['output']); |
| 512 | } else { |
| 513 | $ad->set_option('output', array()); |
| 514 | } |
| 515 | if(isset($_POST['advanced_ad']['visitor'])) { |
| 516 | $ad->set_option('visitor', $_POST['advanced_ad']['visitor']); |
| 517 | } else { |
| 518 | $ad->set_option('visitor', array()); |
| 519 | } |
| 520 | if(isset($_POST['advanced_ad']['injection'])) { |
| 521 | $ad->set_option('injection', $_POST['advanced_ad']['injection']); |
| 522 | } else { |
| 523 | $ad->set_option('injection', array()); |
| 524 | } |
| 525 | // save size |
| 526 | $ad->width = 0; |
| 527 | if(isset($_POST['advanced_ad']['width'])) { |
| 528 | $ad->width = absint($_POST['advanced_ad']['width']); |
| 529 | } |
| 530 | $ad->height = 0; |
| 531 | if(isset($_POST['advanced_ad']['height'])) { |
| 532 | $ad->height = absint($_POST['advanced_ad']['height']); |
| 533 | } |
| 534 | |
| 535 | if(!empty($_POST['advanced_ad']['content'])) |
| 536 | $ad->content = $_POST['advanced_ad']['content']; |
| 537 | else $ad->content = ''; |
| 538 | if(!empty($_POST['advanced_ad']['conditions'])){ |
| 539 | $ad->conditions = $_POST['advanced_ad']['conditions']; |
| 540 | } else { |
| 541 | $ad->conditions = array(); |
| 542 | } |
| 543 | |
| 544 | $ad->save(); |
| 545 | |
| 546 | // update global ad information |
| 547 | $this->update_global_injection_array(); |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * get action from the params |
| 552 | * |
| 553 | * @since 1.0.0 |
| 554 | */ |
| 555 | public function current_action() { |
| 556 | if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) |
| 557 | return $_REQUEST['action']; |
| 558 | |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * initialize settings |
| 564 | * |
| 565 | * @since 1.0.1 |
| 566 | */ |
| 567 | public function settings_init(){ |
| 568 | |
| 569 | // get settings page hook |
| 570 | $hook = $this->plugin_screen_hook_suffix; |
| 571 | |
| 572 | // register settings |
| 573 | register_setting($hook, ADVADS_SLUG); |
| 574 | |
| 575 | // add new section |
| 576 | add_settings_section( |
| 577 | 'advanced_ads_setting_section', |
| 578 | __('General', ADVADS_SLUG), |
| 579 | array($this, 'render_settings_section_callback'), |
| 580 | $hook |
| 581 | ); |
| 582 | |
| 583 | // add setting fields to disable ads |
| 584 | add_settings_field( |
| 585 | 'disable-ads', |
| 586 | __('Disable ads', ADVADS_SLUG), |
| 587 | array($this, 'render_settings_disable_ads'), |
| 588 | $hook, |
| 589 | 'advanced_ads_setting_section' |
| 590 | ); |
| 591 | // add setting fields for user role |
| 592 | add_settings_field( |
| 593 | 'hide-for-user-role', |
| 594 | __('Hide ads for logged in users', ADVADS_SLUG), |
| 595 | array($this, 'render_settings_hide_for_users'), |
| 596 | $hook, |
| 597 | 'advanced_ads_setting_section' |
| 598 | ); |
| 599 | // add setting fields for advanced ads |
| 600 | add_settings_field( |
| 601 | 'activate-advanced-js', |
| 602 | __('Use advanced JavaScript', ADVADS_SLUG), |
| 603 | array($this, 'render_settings_advanced_js'), |
| 604 | $hook, |
| 605 | 'advanced_ads_setting_section' |
| 606 | ); |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * render settings section |
| 611 | * |
| 612 | * @since 1.1.1 |
| 613 | */ |
| 614 | public function render_settings_section_callback(){ |
| 615 | // for whatever purpose there might come |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * options to disable ads |
| 620 | * |
| 621 | * @since 1.3.11 |
| 622 | */ |
| 623 | public function render_settings_disable_ads(){ |
| 624 | $options = Advanced_Ads::get_instance()->options(); |
| 625 | |
| 626 | // set the variables |
| 627 | $disable_all = isset($options['disabled-ads']['all']) ? 1 : 0; |
| 628 | $disable_404 = isset($options['disabled-ads']['404']) ? 1 : 0; |
| 629 | $disable_archives = isset($options['disabled-ads']['archives']) ? 1 : 0; |
| 630 | |
| 631 | // load the template |
| 632 | $view = plugin_dir_path(__FILE__) . 'views/settings_disable_ads.php'; |
| 633 | if (is_file($view)) { |
| 634 | require( $view ); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * render setting to hide ads from logged in users |
| 640 | * |
| 641 | * @since 1.1.1 |
| 642 | */ |
| 643 | public function render_settings_hide_for_users(){ |
| 644 | $options = Advanced_Ads::get_instance()->options(); |
| 645 | $current_capability_role = isset($options['hide-for-user-role']) ? $options['hide-for-user-role'] : 0; |
| 646 | |
| 647 | |
| 648 | $capability_roles = array( |
| 649 | '' => __('(display to all)', ADVADS_SLUG), |
| 650 | 'read' => __('Subscriber', ADVADS_SLUG), |
| 651 | 'delete_posts' => __('Contributor', ADVADS_SLUG), |
| 652 | 'edit_posts' => __('Author', ADVADS_SLUG), |
| 653 | 'edit_pages' => __('Editor', ADVADS_SLUG), |
| 654 | 'activate_plugins' => __('Admin', ADVADS_SLUG), |
| 655 | ); |
| 656 | echo '<select name="'.ADVADS_SLUG.'[hide-for-user-role]">'; |
| 657 | foreach($capability_roles as $_capability => $_role) { |
| 658 | echo '<option value="'.$_capability.'" '.selected($_capability, $current_capability_role, false).'>'.$_role.'</option>'; |
| 659 | } |
| 660 | echo '</select>'; |
| 661 | |
| 662 | echo '<p class="description">'. __('Choose the lowest role a user must have in order to not see any ads.', ADVADS_SLUG) .'</p>'; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * render setting to display advanced js file |
| 667 | * |
| 668 | * @since 1.2.3 |
| 669 | */ |
| 670 | public function render_settings_advanced_js(){ |
| 671 | $options = Advanced_Ads::get_instance()->options(); |
| 672 | $checked = (!empty($options['advanced-js'])) ? 1 : 0; |
| 673 | |
| 674 | echo '<input id="advanced-ads-advanced-js" type="checkbox" value="1" name="'.ADVADS_SLUG.'[advanced-js]" '.checked($checked, 1, false).'>'; |
| 675 | echo '<p class="description">'. sprintf(__('Only enable this if you can and want to use the advanced JavaScript functions described <a href="%s">here</a>.', ADVADS_SLUG), 'http://wpadvancedads.com/javascript-functions/') .'</p>'; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * save a global array with ad injection information |
| 680 | * runs every time for all ads a single ad is saved (but not on autosave) |
| 681 | * |
| 682 | * @since 1.1.0 |
| 683 | */ |
| 684 | public function update_global_injection_array(){ |
| 685 | // get all public ads |
| 686 | $ad_posts = Advanced_Ads::get_ads(); |
| 687 | |
| 688 | // merge ad injection settings by type (place => ad id) |
| 689 | $all_injections = array(); |
| 690 | if(is_array($ad_posts)) foreach($ad_posts as $_ad){ |
| 691 | // load the ad |
| 692 | $ad = new Advads_Ad($_ad->ID); |
| 693 | // get injection post meta |
| 694 | $injection_options = $ad->options('injection'); |
| 695 | // add injection settings to global array |
| 696 | if(isset($injection_options)) foreach($injection_options as $_iokey => $_io){ |
| 697 | $all_injections[$_iokey][] = $_ad->ID; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | // save global injection array to WP options table or remove it |
| 702 | if(is_array($all_injections) && count($all_injections) > 0) |
| 703 | update_option('advads-ads-injections', $all_injections); |
| 704 | else |
| 705 | delete_option ('advads-ads-injections'); |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * add heading for extra column of ads list |
| 710 | * remove the date column |
| 711 | * |
| 712 | * @since 1.3.3 |
| 713 | * @param arr $defaults |
| 714 | */ |
| 715 | public function ad_list_columns_head($defaults){ |
| 716 | |
| 717 | $offset = array_search('title', array_keys($defaults)) + 1; |
| 718 | |
| 719 | $defaults = array_merge |
| 720 | ( |
| 721 | array_slice($defaults, 0, $offset), |
| 722 | array('ad_details' => __('Ad Details', ADVADS_SLUG)), |
| 723 | array_slice($defaults, $offset, null) |
| 724 | ); |
| 725 | |
| 726 | // remove the date |
| 727 | unset($defaults['date']); |
| 728 | |
| 729 | return $defaults; |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * display ad details in ads list |
| 734 | * |
| 735 | * @since 1.3.3 |
| 736 | * @param string $column_name name of the column |
| 737 | * @param int $ad_id id of the ad |
| 738 | */ |
| 739 | public function ad_list_columns_content($column_name, $ad_id) { |
| 740 | if ($column_name == 'ad_details') { |
| 741 | $ad = new Advads_Ad($ad_id); |
| 742 | |
| 743 | // load ad type title |
| 744 | $types = Advanced_Ads::get_instance()->ad_types; |
| 745 | $type = (!empty($types[$ad->type]->title)) ? $types[$ad->type]->title : 0; |
| 746 | |
| 747 | // load ad size |
| 748 | $size = 0; |
| 749 | if(!empty($ad->width) || !empty($ad->height)){ |
| 750 | $size = sprintf('%d x %d', $ad->width, $ad->height); |
| 751 | } |
| 752 | |
| 753 | $view = plugin_dir_path(__FILE__) . 'views/ad_list_details_column.php'; |
| 754 | if (is_file($view)) { |
| 755 | require( $view ); |
| 756 | } |
| 757 | |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * add a meta box to post type edit screens with ad settings |
| 763 | * |
| 764 | * @since 1.3.10 |
| 765 | * @param string $post_type current post type |
| 766 | */ |
| 767 | public function add_post_meta_box($post_type = ""){ |
| 768 | // get public post types |
| 769 | $public_post_types = get_post_types(array('public' => true, 'publicly_queryable' => true), 'names', 'or'); |
| 770 | |
| 771 | //limit meta box to public post types |
| 772 | if ( in_array( $post_type, $public_post_types )) { |
| 773 | add_meta_box( |
| 774 | 'advads-ad-settings', |
| 775 | __( 'Ad Settings', ADVADS_SLUG), |
| 776 | array( $this, 'render_post_meta_box' ), |
| 777 | $post_type, |
| 778 | 'advanced', |
| 779 | 'low' |
| 780 | ); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * render meta box for ad settings on a per post basis |
| 786 | * |
| 787 | * @since 1.3.10 |
| 788 | * @param WP_Post $post The post object. |
| 789 | */ |
| 790 | public function render_post_meta_box( $post ) { |
| 791 | |
| 792 | // nonce field to check when we save the values |
| 793 | wp_nonce_field( 'advads_post_meta_box', 'advads_post_meta_box_nonce' ); |
| 794 | |
| 795 | // retrieve an existing value from the database. |
| 796 | $values = get_post_meta( $post->ID, '_advads_ad_settings', true ); |
| 797 | |
| 798 | // load the view |
| 799 | $view = plugin_dir_path(__FILE__) . 'views/post_ad_settings_metabox.php'; |
| 800 | if (is_file($view)) { |
| 801 | require( $view ); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * save the ad meta when the post is saved. |
| 807 | * |
| 808 | * @since 1.3.10 |
| 809 | * @param int $post_id The ID of the post being saved. |
| 810 | */ |
| 811 | public function save_post_meta_box( $post_id ) { |
| 812 | |
| 813 | // check nonce |
| 814 | if ( ! isset( $_POST['advads_post_meta_box_nonce'] ) ) |
| 815 | return $post_id; |
| 816 | |
| 817 | $nonce = $_POST['advads_post_meta_box_nonce']; |
| 818 | |
| 819 | // Verify that the nonce is valid. |
| 820 | if ( ! wp_verify_nonce( $nonce, 'advads_post_meta_box' ) ) |
| 821 | return $post_id; |
| 822 | |
| 823 | // don’t save on autosave |
| 824 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
| 825 | return $post_id; |
| 826 | |
| 827 | // check the user's permissions. |
| 828 | if ( 'page' == $_POST['post_type'] ) { |
| 829 | if ( ! current_user_can( 'edit_page', $post_id ) ) |
| 830 | return $post_id; |
| 831 | } else { |
| 832 | if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 833 | return $post_id; |
| 834 | } |
| 835 | |
| 836 | // Sanitize the user input. |
| 837 | $_data['disable_ads'] = isset($_POST['advanced_ads']['disable_ads']) ? absint($_POST['advanced_ads']['disable_ads']) : 0; |
| 838 | |
| 839 | // Update the meta field. |
| 840 | update_post_meta( $post_id, '_advads_ad_settings', $_data ); |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * add dashboard widget with ad stats and additional information |
| 845 | * |
| 846 | * @since 1.3.12 |
| 847 | */ |
| 848 | public function add_dashboard_widget(){ |
| 849 | // wp_add_dashboard_widget('advads_dashboard_widget', __('Ads Dashboard', ADVADS_SLUG), array($this, 'dashboard_widget_function')); |
| 850 | add_meta_box('advads_dashboard_widget', __('Ads Dashboard', ADVADS_SLUG), array($this, 'dashboard_widget_function'), 'dashboard', 'side', 'high'); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * display widget functions |
| 855 | */ |
| 856 | public function dashboard_widget_function($post, $callback_args){ |
| 857 | // load ad optimization feed |
| 858 | $feed = array( |
| 859 | 'link' => 'http://webgilde.com/en/ad-optimization/', |
| 860 | 'url' => 'http://webgilde.com/en/ad-optimization/feed/', |
| 861 | 'title' => __('Tutorials and News'), |
| 862 | 'items' => 3, |
| 863 | 'show_summary' => 0, |
| 864 | 'show_author' => 0, |
| 865 | 'show_date' => 0, |
| 866 | ); |
| 867 | |
| 868 | // get number of ads |
| 869 | $recent_ads = Advanced_Ads::get_ads(); |
| 870 | echo '<p>'; |
| 871 | printf(__('%d ads – <a href="%s">manage</a> – <a href="%s">new</a>', ADVADS_SLUG), |
| 872 | count($recent_ads), |
| 873 | 'edit.php?post_type='. Advanced_Ads::POST_TYPE_SLUG, |
| 874 | 'post-new.php?post_type='. Advanced_Ads::POST_TYPE_SLUG); |
| 875 | echo '</p>'; |
| 876 | |
| 877 | // get and display plugin version |
| 878 | $advads_plugin_data = get_plugin_data(ADVADS_BASE_PATH . 'advanced-ads.php'); |
| 879 | if(isset($advads_plugin_data['Version'])){ |
| 880 | $version = $advads_plugin_data['Version']; |
| 881 | echo '<p><a href="http://wpadvancedads.com" target="_blank" title="'. |
| 882 | __('plugin manual and homepage', ADVADS_SLUG).'">Advanced Ads</a> '. $version .'</p>'; |
| 883 | } |
| 884 | |
| 885 | // rss feed |
| 886 | echo '<h4>' . __('From the ad optimization universe', ADVADS_SLUG) . '</h4>'; |
| 887 | // $this->dashboard_widget_function_output('advads_dashboard_widget', $feed); |
| 888 | $this->dashboard_cached_rss_widget( 'advads_dashboard_widget', array($this, 'dashboard_widget_function_output'), array('advads' => $feed) ); |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * checks to see if there are feed urls in transient cache; if not, load them |
| 893 | * built using a lot of https://developer.wordpress.org/reference/functions/wp_dashboard_cached_rss_widget/ |
| 894 | * |
| 895 | * @since 1.3.12 |
| 896 | * @param string $widget_id |
| 897 | * @param callback $callback |
| 898 | * @param array $check_urls RSS feeds |
| 899 | * @return bool False on failure. True on success. |
| 900 | */ |
| 901 | function dashboard_cached_rss_widget( $widget_id, $callback, $feed = array() ) { |
| 902 | if ( empty($feed) ) { |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | $cache_key = 'dash_' . md5( $widget_id ); |
| 907 | if ( false !== ( $output = get_transient( $cache_key ) ) ) { |
| 908 | echo $output; |
| 909 | return true; |
| 910 | } |
| 911 | |
| 912 | if ( $callback && is_callable( $callback ) ) { |
| 913 | ob_start(); |
| 914 | call_user_func_array( $callback, $feed ); |
| 915 | set_transient( $cache_key, ob_get_flush(), 12 * HOUR_IN_SECONDS ); // Default lifetime in cache of 12 hours (same as the feeds) |
| 916 | } |
| 917 | |
| 918 | return true; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * create the rss output of the widget |
| 923 | * |
| 924 | * @param string $widget_id Widget ID. |
| 925 | * @param array $feeds Array of RSS feeds. |
| 926 | */ |
| 927 | function dashboard_widget_function_output( $feed ) { |
| 928 | |
| 929 | echo '<div class="rss-widget">'; |
| 930 | wp_widget_rss_output( $feed['url'], $feed ); |
| 931 | echo "</div>"; |
| 932 | } |
| 933 | |
| 934 | } |
| 935 |