class-ad-groups-list.php
9 years ago
class-ad-type.php
9 years ago
class-licenses.php
9 years ago
class-menu.php
9 years ago
class-meta-box.php
9 years ago
class-notices.php
9 years ago
class-options.php
9 years ago
class-overview-widgets.php
9 years ago
class-settings.php
9 years ago
class-shortcode-creator.php
9 years ago
notices.php
9 years ago
shortcode-creator-l10n.php
10 years ago
class-meta-box.php
440 lines
| 1 | <?php |
| 2 | defined( 'ABSPATH' ) || exit; |
| 3 | |
| 4 | class Advanced_Ads_Admin_Meta_Boxes { |
| 5 | /** |
| 6 | * Instance of this class. |
| 7 | * |
| 8 | * @var object |
| 9 | */ |
| 10 | protected static $instance = null; |
| 11 | |
| 12 | /** |
| 13 | * meta box ids |
| 14 | * |
| 15 | * @since 1.7.4.2 |
| 16 | * @var array |
| 17 | */ |
| 18 | protected $meta_box_ids = array(); |
| 19 | |
| 20 | |
| 21 | private function __construct() { |
| 22 | add_action( 'add_meta_boxes_' . Advanced_Ads::POST_TYPE_SLUG, array( $this, 'add_meta_boxes' ) ); |
| 23 | // add meta box for post types edit pages |
| 24 | add_action( 'add_meta_boxes', array( $this, 'add_post_meta_box' ) ); |
| 25 | add_action( 'save_post', array( $this, 'save_post_meta_box' ) ); |
| 26 | // register dashboard widget |
| 27 | add_action( 'wp_dashboard_setup', array($this, 'add_dashboard_widget') ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Return an instance of this class. |
| 32 | * |
| 33 | * @return object A single instance of this class. |
| 34 | */ |
| 35 | public static function get_instance() { |
| 36 | // If the single instance hasn't been set, set it now. |
| 37 | if ( null == self::$instance ) { |
| 38 | self::$instance = new self; |
| 39 | } |
| 40 | |
| 41 | return self::$instance; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Add meta boxes |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | */ |
| 49 | public function add_meta_boxes() { |
| 50 | $post_type = Advanced_Ads::POST_TYPE_SLUG; |
| 51 | |
| 52 | add_meta_box( |
| 53 | 'ad-main-box', __( 'Ad Type', 'advanced-ads' ), array($this, 'markup_meta_boxes'), $post_type, 'normal', 'high' |
| 54 | ); |
| 55 | // use dynamic filter from to add close class to ad type meta box after saved first time |
| 56 | add_filter( 'postbox_classes_advanced_ads_ad-main-box', array( $this, 'close_ad_type_metabox' ) ); |
| 57 | |
| 58 | add_meta_box( |
| 59 | 'ad-parameters-box', __( 'Ad Parameters', 'advanced-ads' ), array($this, 'markup_meta_boxes'), $post_type, 'normal', 'high' |
| 60 | ); |
| 61 | add_meta_box( |
| 62 | 'ad-output-box', __( 'Layout / Output', 'advanced-ads' ), array($this, 'markup_meta_boxes'), $post_type, 'normal', 'high' |
| 63 | ); |
| 64 | add_meta_box( |
| 65 | 'ad-display-box', __( 'Display Conditions', 'advanced-ads' ), array($this, 'markup_meta_boxes'), $post_type, 'normal', 'high' |
| 66 | ); |
| 67 | add_meta_box( |
| 68 | 'ad-visitor-box', __( 'Visitor Conditions', 'advanced-ads' ), array($this, 'markup_meta_boxes'), $post_type, 'normal', 'high' |
| 69 | ); |
| 70 | |
| 71 | // register meta box ids |
| 72 | $this->meta_box_ids = array( |
| 73 | 'ad-main-box', |
| 74 | 'ad-parameters-box', |
| 75 | 'ad-output-box', |
| 76 | 'ad-display-box', |
| 77 | 'ad-visitor-box', |
| 78 | 'revisionsdiv', // revisions – only when activated |
| 79 | 'advanced_ads_groupsdiv' // automatically added by ad groups taxonomy |
| 80 | ); |
| 81 | |
| 82 | // force AA meta boxes to never be completely hidden by screen options |
| 83 | add_filter( 'hidden_meta_boxes', array( $this, 'unhide_meta_boxes' ), 10, 2 ); |
| 84 | |
| 85 | $whitelist = apply_filters( 'advanced-ads-ad-edit-allowed-metaboxes', array_merge( |
| 86 | $this->meta_box_ids, |
| 87 | array( |
| 88 | 'submitdiv', |
| 89 | 'slugdiv', |
| 90 | 'tracking-ads-box', |
| 91 | 'ad-layer-ads-box', // deprecated |
| 92 | ) |
| 93 | ) ); |
| 94 | |
| 95 | global $wp_meta_boxes; |
| 96 | // remove non-white-listed meta boxes |
| 97 | foreach ( array( 'normal', 'advanced', 'side' ) as $context ) { |
| 98 | if ( isset( $wp_meta_boxes[ $post_type ][ $context ] ) ) { |
| 99 | foreach ( array( 'high', 'sorted', 'core', 'default', 'low' ) as $priority ) { |
| 100 | if ( isset( $wp_meta_boxes[ $post_type ][ $context ][ $priority ]) ) { |
| 101 | foreach ( (array) $wp_meta_boxes[ $post_type ][ $context ][ $priority ] as $id => $box ) { |
| 102 | if ( ! in_array( $id, $whitelist ) ) { |
| 103 | unset( $wp_meta_boxes[ $post_type ][ $context ][ $priority ][ $id ] ); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * load templates for all meta boxes |
| 114 | * |
| 115 | * @since 1.0.0 |
| 116 | * @param obj $post |
| 117 | * @param array $box |
| 118 | * @todo move ad initialization to main function and just global it |
| 119 | */ |
| 120 | public function markup_meta_boxes($post, $box) { |
| 121 | $ad = new Advanced_Ads_Ad( $post->ID ); |
| 122 | |
| 123 | switch ( $box['id'] ) { |
| 124 | case 'ad-main-box': |
| 125 | $view = 'ad-main-metabox.php'; |
| 126 | $hndlelinks = '<a href="' . ADVADS_URL . 'manual/ad-types#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-ad-type" target="_blank">' . __('Manual', 'advanced-ads') . '</a>'; |
| 127 | break; |
| 128 | case 'ad-parameters-box': |
| 129 | $view = 'ad-parameters-metabox.php'; |
| 130 | break; |
| 131 | case 'ad-output-box': |
| 132 | $view = 'ad-output-metabox.php'; |
| 133 | break; |
| 134 | case 'ad-display-box': |
| 135 | $view = 'ad-display-metabox.php'; |
| 136 | $hndlelinks = '<a href="#" class="advads-video-link">' . __('Video', 'advanced-ads') . '</a>'; |
| 137 | $hndlelinks .= '<a href="' . ADVADS_URL . 'manual/display-conditions#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-display" target="_blank">' . __('Manual', 'advanced-ads') . '</a>'; |
| 138 | $videomarkup = '<iframe width="420" height="315" src="https://www.youtube-nocookie.com/embed/wVB6UpeyWNA?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>'; |
| 139 | break; |
| 140 | case 'ad-visitor-box': |
| 141 | $view = 'ad-visitor-metabox.php'; |
| 142 | $hndlelinks = '<a href="' . ADVADS_URL . 'manual/visitor-conditions#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-visitor" target="_blank">' . __('Manual', 'advanced-ads') . '</a>'; |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | if ( ! isset( $view ) ) { |
| 147 | return; |
| 148 | } |
| 149 | // markup moved to handle headline of the metabox |
| 150 | if( isset( $hndlelinks ) ){ |
| 151 | ?><span class="advads-hndlelinks hidden"><?php echo $hndlelinks; ?></span> |
| 152 | <?php |
| 153 | } |
| 154 | // show video markup |
| 155 | if( isset( $videomarkup ) ){ |
| 156 | echo '<div class="advads-video-link-container" data-videolink=\'' . $videomarkup . '\'></div>'; |
| 157 | } |
| 158 | /** |
| 159 | * list general notices |
| 160 | * |
| 161 | * elements in $warnings contain [text] and [class] attributes |
| 162 | */ |
| 163 | $warnings = array(); |
| 164 | // show warning if ad contains https in parameters box |
| 165 | if ( 'ad-parameters-box' === $box['id'] && $message = Advanced_Ads_Ad_Debug::is_https_and_http( $ad ) ) { |
| 166 | $warnings[] = array( |
| 167 | 'text' => $message, |
| 168 | 'class' =>'advads-ad-notice-https-missing error' |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | $warnings = apply_filters( 'advanced-ads-ad-notices', $warnings, $box, $post ); |
| 173 | echo '<ul id="' .$box['id'].'-notices" class="advads-metabox-notices">'; |
| 174 | foreach( $warnings as $_warning ){ |
| 175 | if( isset( $_warning['text'] ) ) : |
| 176 | $warning_class = isset( $_warning['class'] ) ? $_warning['class'] : ''; |
| 177 | echo '<li class="'. $warning_class . '">'; |
| 178 | echo $_warning['text']; |
| 179 | echo '</li>'; |
| 180 | endif; |
| 181 | } |
| 182 | echo '</ul>'; |
| 183 | include ADVADS_BASE_PATH . 'admin/views/' . $view; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * force all AA related meta boxes to stay visible |
| 188 | * |
| 189 | * @since 1.7.4.2 |
| 190 | * @param array $hidden An array of hidden meta boxes |
| 191 | * @param WP_Screen $screen WP_Screen object of the current screen |
| 192 | */ |
| 193 | public function unhide_meta_boxes( $hidden, $screen ){ |
| 194 | // only check on Advanced Ads edit screen |
| 195 | if ( ! isset( $screen->id ) || $screen->id !== 'advanced_ads' || !is_array( $this->meta_box_ids ) ) { |
| 196 | return $hidden; |
| 197 | } |
| 198 | |
| 199 | // return only hidden elements which are not among the Advanced Ads meta box ids |
| 200 | return array_diff( $hidden, $this->meta_box_ids ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * add a meta box to post type edit screens with ad settings |
| 205 | * |
| 206 | * @since 1.3.10 |
| 207 | * @param string $post_type current post type |
| 208 | */ |
| 209 | public function add_post_meta_box($post_type = ''){ |
| 210 | // don’t display for non admins |
| 211 | if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | // get public post types |
| 216 | $public_post_types = get_post_types( array('public' => true, 'publicly_queryable' => true), 'names', 'or' ); |
| 217 | |
| 218 | //limit meta box to public post types |
| 219 | if ( in_array( $post_type, $public_post_types ) ) { |
| 220 | add_meta_box( |
| 221 | 'advads-ad-settings', |
| 222 | __( 'Ad Settings', 'advanced-ads' ), |
| 223 | array( $this, 'render_post_meta_box' ), |
| 224 | $post_type, |
| 225 | 'advanced', |
| 226 | 'low' |
| 227 | ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * render meta box for ad settings on a per post basis |
| 233 | * |
| 234 | * @since 1.3.10 |
| 235 | * @param WP_Post $post The post object. |
| 236 | */ |
| 237 | public function render_post_meta_box( $post ) { |
| 238 | |
| 239 | // nonce field to check when we save the values |
| 240 | wp_nonce_field( 'advads_post_meta_box', 'advads_post_meta_box_nonce' ); |
| 241 | |
| 242 | // retrieve an existing value from the database. |
| 243 | $values = get_post_meta( $post->ID, '_advads_ad_settings', true ); |
| 244 | |
| 245 | // load the view |
| 246 | include ADVADS_BASE_PATH . 'admin/views/post-ad-settings-metabox.php'; |
| 247 | |
| 248 | do_action( 'advanced_ads_render_post_meta_box', $post, $values ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * save the ad meta when the post is saved. |
| 253 | * |
| 254 | * @since 1.3.10 |
| 255 | * @param int $post_id The ID of the post being saved. |
| 256 | */ |
| 257 | public function save_post_meta_box( $post_id ) { |
| 258 | |
| 259 | if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | // check nonce |
| 264 | if ( ! isset( $_POST['advads_post_meta_box_nonce'] ) ) { |
| 265 | return $post_id; } |
| 266 | |
| 267 | $nonce = $_POST['advads_post_meta_box_nonce']; |
| 268 | |
| 269 | // Verify that the nonce is valid. |
| 270 | if ( ! wp_verify_nonce( $nonce, 'advads_post_meta_box' ) ) { |
| 271 | return $post_id; } |
| 272 | |
| 273 | // don’t save on autosave |
| 274 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 275 | return $post_id; } |
| 276 | |
| 277 | // check the user's permissions. |
| 278 | if ( 'page' == $_POST['post_type'] ) { |
| 279 | if ( ! current_user_can( 'edit_page', $post_id ) ) { |
| 280 | return $post_id; } |
| 281 | } else { |
| 282 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 283 | return $post_id; } |
| 284 | } |
| 285 | |
| 286 | // Sanitize the user input. |
| 287 | $_data['disable_ads'] = isset($_POST['advanced_ads']['disable_ads']) ? absint( $_POST['advanced_ads']['disable_ads'] ) : 0; |
| 288 | |
| 289 | $_data = apply_filters( 'advanced_ads_save_post_meta_box', $_data ); |
| 290 | |
| 291 | // Update the meta field. |
| 292 | update_post_meta( $post_id, '_advads_ad_settings', $_data ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * add "close" class to collapse the ad-type metabox after ad was saved first |
| 297 | * |
| 298 | * @since 1.7.2 |
| 299 | * @param arr $classes |
| 300 | * @return arr $classes |
| 301 | */ |
| 302 | public function close_ad_type_metabox( $classes = array() ) { |
| 303 | global $post; |
| 304 | if ( isset( $post->ID ) && 'edit' === $post->filter ) { |
| 305 | if ( ! in_array( 'closed', $classes ) ) { |
| 306 | $classes[] = 'closed'; |
| 307 | } |
| 308 | } else { |
| 309 | $classes = array(); |
| 310 | } |
| 311 | return $classes; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * add dashboard widget with ad stats and additional information |
| 316 | * |
| 317 | * @since 1.3.12 |
| 318 | */ |
| 319 | public function add_dashboard_widget(){ |
| 320 | // display dashboard widget only to authors and higher roles |
| 321 | if( ! current_user_can('publish_posts') ) { |
| 322 | return; |
| 323 | } |
| 324 | add_meta_box( 'advads_dashboard_widget', __( 'Ads Dashboard', 'advanced-ads' ), array($this, 'dashboard_widget_function'), 'dashboard', 'side', 'high' ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * display widget functions |
| 329 | */ |
| 330 | public static function dashboard_widget_function($post, $callback_args){ |
| 331 | // load ad optimization feed |
| 332 | $feeds = array( |
| 333 | array( |
| 334 | 'link' => 'http://webgilde.com/en/ad-optimization/', |
| 335 | 'url' => 'http://webgilde.com/en/ad-optimization/feed/', |
| 336 | 'title' => __( 'From the ad optimization universe', 'advanced-ads' ), |
| 337 | 'items' => 2, |
| 338 | 'show_summary' => 0, |
| 339 | 'show_author' => 0, |
| 340 | 'show_date' => 0, |
| 341 | ), |
| 342 | array( |
| 343 | 'link' => ADVADS_URL, |
| 344 | 'url' => ADVADS_URL . 'feed/', |
| 345 | 'title' => __( 'Advanced Ads Tutorials', 'advanced-ads' ), |
| 346 | 'items' => 2, |
| 347 | 'show_summary' => 0, |
| 348 | 'show_author' => 0, |
| 349 | 'show_date' => 0, |
| 350 | ), |
| 351 | ); |
| 352 | |
| 353 | // get number of ads |
| 354 | $ads_count = Advanced_Ads::get_number_of_ads(); |
| 355 | if( current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 356 | echo '<p>'; |
| 357 | printf(__( '%d ads – <a href="%s">manage</a> - <a href="%s">new</a>', 'advanced-ads' ), |
| 358 | $ads_count, |
| 359 | 'edit.php?post_type='. Advanced_Ads::POST_TYPE_SLUG, |
| 360 | 'post-new.php?post_type='. Advanced_Ads::POST_TYPE_SLUG); |
| 361 | echo '</p>'; |
| 362 | } |
| 363 | |
| 364 | // get and display plugin version |
| 365 | $advads_plugin_data = get_plugin_data( ADVADS_BASE_PATH . 'advanced-ads.php' ); |
| 366 | if ( isset($advads_plugin_data['Version']) ){ |
| 367 | $version = $advads_plugin_data['Version']; |
| 368 | echo '<p><a href="'.ADVADS_URL.'#utm_source=advanced-ads&utm_medium=link&utm_campaign=dashboard" target="_blank" title="'. |
| 369 | __( 'plugin manual and homepage', 'advanced-ads' ).'">Advanced Ads</a> '. $version .'</p>'; |
| 370 | } |
| 371 | |
| 372 | $notice_options = Advanced_Ads_Admin_Notices::get_instance()->options(); |
| 373 | $_notice = 'nl_first_steps'; |
| 374 | if ( ! isset($notice_options['closed'][ $_notice ] ) ) { |
| 375 | ?><div class="advads-admin-notice"> |
| 376 | <p><button type="button" class="button-primary advads-notices-button-subscribe" data-notice="<?php echo $_notice ?>"><?php _e('Get the tutorial via email', 'advanced-ads'); ?></button></p> |
| 377 | </div><?php |
| 378 | } |
| 379 | |
| 380 | $_notice = 'nl_adsense'; |
| 381 | if ( ! isset($notice_options['closed'][ $_notice ] ) ) { |
| 382 | ?><div class="advads-admin-notice"> |
| 383 | <p><button type="button" class="button-primary advads-notices-button-subscribe" data-notice="<?php echo $_notice ?>"><?php _e('Get AdSense tips via email', 'advanced-ads'); ?></button></p> |
| 384 | </div><?php |
| 385 | } |
| 386 | |
| 387 | // rss feed |
| 388 | // $this->dashboard_widget_function_output('advads_dashboard_widget', $feed); |
| 389 | self::dashboard_cached_rss_widget( 'advads_dashboard_widget', array( 'self', 'dashboard_widget_function_output' ), array( 'advads' => $feeds ) ); |
| 390 | |
| 391 | // add markup for utm variables |
| 392 | // todo: move to js file |
| 393 | ?><script>jQuery('#advads_dashboard_widget .rss-widget a').each(function(){ this.href = this.href + '#utm_source=advanced-ads&utm_medium=rss-link&utm_campaign=dashboard'; })</script><?php |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * checks to see if there are feed urls in transient cache; if not, load them |
| 398 | * built using a lot of https://developer.wordpress.org/reference/functions/wp_dashboard_cached_rss_widget/ |
| 399 | * |
| 400 | * @since 1.3.12 |
| 401 | * @param string $widget_id |
| 402 | * @param callback $callback |
| 403 | * @param array $check_urls RSS feeds |
| 404 | * @return bool False on failure. True on success. |
| 405 | */ |
| 406 | static function dashboard_cached_rss_widget( $widget_id, $callback, $feeds = array() ) { |
| 407 | if ( empty($feeds) ) { |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | $cache_key = 'dash_' . md5( $widget_id ); |
| 412 | if ( false !== ( $output = get_transient( $cache_key ) ) ) { |
| 413 | echo $output; |
| 414 | return true; |
| 415 | } |
| 416 | if ( $callback && is_callable( $callback ) ) { |
| 417 | ob_start(); |
| 418 | call_user_func_array( $callback, $feeds ); |
| 419 | set_transient( $cache_key, ob_get_flush(), 48 * HOUR_IN_SECONDS ); // Default lifetime in cache of 48 hours |
| 420 | } |
| 421 | |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * create the rss output of the widget |
| 427 | * |
| 428 | * @param string $widget_id Widget ID. |
| 429 | * @param array $feeds Array of RSS feeds. |
| 430 | */ |
| 431 | static function dashboard_widget_function_output( $feeds ) { |
| 432 | foreach ( $feeds as $_feed ){ |
| 433 | echo '<div class="rss-widget">'; |
| 434 | echo '<h4>'.$_feed['title'].'</h4>'; |
| 435 | wp_widget_rss_output( $_feed['url'], $_feed ); |
| 436 | echo '</div>'; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | } |