class-ad-groups-list.php
8 years ago
class-ad-type.php
8 years ago
class-licenses.php
8 years ago
class-menu.php
8 years ago
class-meta-box.php
8 years ago
class-notices.php
8 years ago
class-options.php
9 years ago
class-overview-widgets.php
8 years ago
class-settings.php
8 years ago
class-shortcode-creator.php
9 years ago
notices.php
8 years ago
shortcode-creator-l10n.php
10 years ago
class-ad-type.php
638 lines
| 1 | <?php |
| 2 | defined( 'ABSPATH' ) || exit; |
| 3 | |
| 4 | class Advanced_Ads_Admin_Ad_Type { |
| 5 | /** |
| 6 | * Instance of this class. |
| 7 | * |
| 8 | * @var object |
| 9 | */ |
| 10 | protected static $instance = null; |
| 11 | |
| 12 | /** |
| 13 | * post type slug |
| 14 | * |
| 15 | * @since 1.0.0 |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $post_type = ''; |
| 19 | |
| 20 | private function __construct() { |
| 21 | // registering custom columns needs to work with and without DOING_AJAX |
| 22 | add_filter( 'manage_advanced_ads_posts_columns', array($this, 'ad_list_columns_head') ); // extra column |
| 23 | add_filter( 'manage_advanced_ads_posts_custom_column', array($this, 'ad_list_columns_content'), 10, 2 ); // extra column |
| 24 | add_filter( 'manage_advanced_ads_posts_custom_column', array($this, 'ad_list_columns_timing'), 10, 2 ); // extra column |
| 25 | add_action( 'restrict_manage_posts', array( $this, 'ad_list_add_filters') ); |
| 26 | |
| 27 | // ad updated messages |
| 28 | add_filter( 'bulk_post_updated_messages', array($this, 'ad_bulk_update_messages'), 10, 2 ); |
| 29 | |
| 30 | // handling (ad) lists |
| 31 | add_filter( 'request', array($this, 'ad_list_request') ); // order ads by title, not ID |
| 32 | add_action( 'all_admin_notices', array($this, 'no_ads_yet_notice') ); |
| 33 | |
| 34 | // save ads post type |
| 35 | add_action( 'save_post', array($this, 'save_ad') ); |
| 36 | // delete ads post type |
| 37 | add_action( 'delete_post', array($this, 'delete_ad') ); |
| 38 | |
| 39 | // on post/ad edit screen |
| 40 | add_action( 'edit_form_top', array($this, 'edit_form_above_title') ); |
| 41 | add_action( 'edit_form_after_title', array($this, 'edit_form_below_title') ); |
| 42 | add_action( 'dbx_post_sidebar', array($this, 'edit_form_end') ); |
| 43 | add_action( 'post_submitbox_misc_actions', array($this, 'add_submit_box_meta') ); |
| 44 | add_action( 'admin_enqueue_scripts', array($this, 'use_code_editor') ); |
| 45 | |
| 46 | // ad updated messages |
| 47 | add_filter( 'post_updated_messages', array($this, 'ad_update_messages') ); |
| 48 | |
| 49 | $this->post_type = constant( 'Advanced_Ads::POST_TYPE_SLUG' ); |
| 50 | |
| 51 | add_filter( 'gettext', array( $this, 'replace_cheating_message' ), 20, 2 ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Return an instance of this class. |
| 56 | * |
| 57 | * @return object A single instance of this class. |
| 58 | */ |
| 59 | public static function get_instance() { |
| 60 | // If the single instance hasn't been set, set it now. |
| 61 | if ( null == self::$instance ) { |
| 62 | self::$instance = new self; |
| 63 | } |
| 64 | |
| 65 | return self::$instance; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * add heading for extra column of ads list |
| 70 | * remove the date column |
| 71 | * |
| 72 | * @since 1.3.3 |
| 73 | * @param arr $columns |
| 74 | */ |
| 75 | public function ad_list_columns_head( $columns ){ |
| 76 | $new_columns = array(); |
| 77 | if( is_array( $columns ) ){ |
| 78 | foreach( $columns as $key => $value ) { |
| 79 | $new_columns[ $key ] = $value; |
| 80 | if ( $key == 'title' ){ |
| 81 | $new_columns[ 'ad_details' ] = __( 'Ad Details', 'advanced-ads' ); |
| 82 | $new_columns[ 'ad_timing' ] = __( 'Ad Planning', 'advanced-ads' ); |
| 83 | } |
| 84 | } |
| 85 | } else { |
| 86 | $new_columns[ 'ad_details' ] = __( 'Ad Details', 'advanced-ads' ); |
| 87 | $new_columns[ 'ad_timing' ] = __( 'Ad Planning', 'advanced-ads' ); |
| 88 | } |
| 89 | |
| 90 | // white-listed columns |
| 91 | $whitelist = apply_filters( 'advanced-ads-ad-list-allowed-columns', array( |
| 92 | 'cb', // checkbox |
| 93 | 'title', |
| 94 | 'ad_details', |
| 95 | 'ad_timing', |
| 96 | 'taxonomy-advanced_ads_groups', |
| 97 | ) ); |
| 98 | |
| 99 | // remove non-white-listed columns |
| 100 | foreach( $new_columns as $_key => $_value ){ |
| 101 | if( ! in_array( $_key, $whitelist ) ){ |
| 102 | unset( $new_columns[ $_key ] ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return $new_columns; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * display ad details in ads list |
| 111 | * |
| 112 | * @since 1.3.3 |
| 113 | * @param string $column_name name of the column |
| 114 | * @param int $ad_id id of the ad |
| 115 | */ |
| 116 | public function ad_list_columns_content($column_name, $ad_id) { |
| 117 | if ( $column_name == 'ad_details' ) { |
| 118 | $ad = new Advanced_Ads_Ad( $ad_id ); |
| 119 | |
| 120 | // load ad type title |
| 121 | $types = Advanced_Ads::get_instance()->ad_types; |
| 122 | $type = ( ! empty($types[$ad->type]->title)) ? $types[$ad->type]->title : 0; |
| 123 | |
| 124 | // load ad size |
| 125 | $size = 0; |
| 126 | if ( ! empty($ad->width) || ! empty($ad->height) ) { |
| 127 | $size = sprintf( '%d x %d', $ad->width, $ad->height ); |
| 128 | } |
| 129 | |
| 130 | $size = apply_filters( 'advanced-ads-list-ad-size', $size, $ad ); |
| 131 | |
| 132 | include ADVADS_BASE_PATH . 'admin/views/ad-list-details-column.php'; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * display ad details in ads list |
| 138 | * |
| 139 | * @since 1.6.11 |
| 140 | * @param string $column_name name of the column |
| 141 | * @param int $ad_id id of the ad |
| 142 | */ |
| 143 | public function ad_list_columns_timing($column_name, $ad_id) { |
| 144 | if ( $column_name == 'ad_timing' ) { |
| 145 | $ad = new Advanced_Ads_Ad( $ad_id ); |
| 146 | |
| 147 | $expiry = false; |
| 148 | $post_future = false; |
| 149 | $post_start = get_post_time('U', true, $ad->id ); |
| 150 | $html_classes = 'advads-filter-timing'; |
| 151 | $expiry_date_format = get_option( 'date_format' ). ', ' . get_option( 'time_format' ); |
| 152 | |
| 153 | if( isset( $ad->expiry_date ) && $ad->expiry_date ){ |
| 154 | $html_classes .= ' advads-filter-any-exp-date'; |
| 155 | |
| 156 | $expiry = $ad->expiry_date; |
| 157 | if( $ad->expiry_date < time() ){ |
| 158 | $html_classes .= ' advads-filter-expired'; |
| 159 | } |
| 160 | } |
| 161 | if( $post_start > time() ){ |
| 162 | $post_future = $post_start; |
| 163 | $html_classes .= ' advads-filter-future'; |
| 164 | } |
| 165 | |
| 166 | ob_start(); |
| 167 | do_action_ref_array( 'advanced-ads-ad-list-timing-column-after', array( $ad, &$html_classes ) ); |
| 168 | $content_after = ob_get_clean(); |
| 169 | |
| 170 | include ADVADS_BASE_PATH . 'admin/views/ad-list-timing-column.php'; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * adds filter dropdowns before the 'Filter' button on the ad list table |
| 176 | */ |
| 177 | function ad_list_add_filters() { |
| 178 | $screen = get_current_screen(); |
| 179 | if ( ! isset( $screen->id ) || $screen->id !== 'edit-advanced_ads' ) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | $timing_filter = apply_filters( 'advanced-ads-ad-list-timing-column-filter', array( |
| 184 | 'advads-filter-expired' => __( 'expired', 'advanced-ads' ), |
| 185 | 'advads-filter-any-exp-date' => __( 'any expiry date', 'advanced-ads' ), |
| 186 | 'advads-filter-future' => __( 'planned', 'advanced-ads' ) |
| 187 | ) ); |
| 188 | |
| 189 | include ADVADS_BASE_PATH . 'admin/views/ad-list-filters.php'; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * edit ad bulk update messages |
| 194 | * |
| 195 | * @since 1.4.7 |
| 196 | * @param arr $messages existing bulk update messages |
| 197 | * @param arr $counts numbers of updated ads |
| 198 | * @return arr $messages |
| 199 | * |
| 200 | * @see wp-admin/edit.php |
| 201 | */ |
| 202 | public function ad_bulk_update_messages(array $messages, array $counts){ |
| 203 | $post = get_post(); |
| 204 | |
| 205 | $messages[Advanced_Ads::POST_TYPE_SLUG] = array( |
| 206 | 'updated' => _n( '%s ad updated.', '%s ads updated.', $counts['updated'], 'advanced-ads' ), |
| 207 | 'locked' => _n( '%s ad not updated, somebody is editing it.', '%s ads not updated, somebody is editing them.', $counts['locked'], 'advanced-ads' ), |
| 208 | 'deleted' => _n( '%s ad permanently deleted.', '%s ads permanently deleted.', $counts['deleted'], 'advanced-ads' ), |
| 209 | 'trashed' => _n( '%s ad moved to the Trash.', '%s ads moved to the Trash.', $counts['trashed'], 'advanced-ads' ), |
| 210 | 'untrashed' => _n( '%s ad restored from the Trash.', '%s ads restored from the Trash.', $counts['untrashed'], 'advanced-ads' ), |
| 211 | ); |
| 212 | |
| 213 | return $messages; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * order ads by title on ads list |
| 218 | * |
| 219 | * @since 1.3.18 |
| 220 | * @param arr $vars array with request vars |
| 221 | */ |
| 222 | public function ad_list_request($vars){ |
| 223 | |
| 224 | // order ads by title on ads list |
| 225 | if ( is_admin() && empty( $vars['orderby'] ) && $this->post_type == $vars['post_type'] ) { |
| 226 | $vars = array_merge( $vars, array( |
| 227 | 'orderby' => 'title', |
| 228 | 'order' => 'ASC' |
| 229 | ) ); |
| 230 | } |
| 231 | |
| 232 | return $vars; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * show instructions to create first ad above the ad list |
| 237 | * |
| 238 | * @return type |
| 239 | */ |
| 240 | public function no_ads_yet_notice(){ |
| 241 | $screen = get_current_screen(); |
| 242 | if ( ! isset( $screen->id ) || $screen->id !== 'edit-advanced_ads' ) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | // get number of ads |
| 247 | $existing_ads = Advanced_Ads::get_number_of_ads(); |
| 248 | |
| 249 | // only display if there are no more than 2 ads |
| 250 | if( 3 > $existing_ads ){ |
| 251 | echo '<div class="advads-ad-metabox postbox" style="clear: both; margin: 10px 20px 0 2px;">'; |
| 252 | include ADVADS_BASE_PATH . 'admin/views/ad-list-no-ads.php'; |
| 253 | echo '</div>'; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /** |
| 259 | * prepare the ad post type to be saved |
| 260 | * |
| 261 | * @since 1.0.0 |
| 262 | * @param int $post_id id of the post |
| 263 | * @todo handling this more dynamic based on ad type |
| 264 | */ |
| 265 | public function save_ad($post_id) { |
| 266 | |
| 267 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) |
| 268 | // only use for ads, no other post type |
| 269 | || ! isset($_POST['post_type']) |
| 270 | || $this->post_type != $_POST['post_type'] |
| 271 | || ! isset($_POST['advanced_ad']['type']) |
| 272 | || wp_is_post_revision( $post_id ) ) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | // get ad object |
| 277 | $ad = new Advanced_Ads_Ad( $post_id ); |
| 278 | if ( ! $ad instanceof Advanced_Ads_Ad ) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | // filter to allow change of submitted ad settings |
| 283 | $_POST['advanced_ad'] = apply_filters( 'advanced-ads-ad-settings-pre-save', $_POST['advanced_ad'] ); |
| 284 | |
| 285 | $ad->type = $_POST['advanced_ad']['type']; |
| 286 | if ( isset($_POST['advanced_ad']['output']) ) { |
| 287 | $ad->set_option( 'output', $_POST['advanced_ad']['output'] ); |
| 288 | } else { |
| 289 | $ad->set_option( 'output', array() ); |
| 290 | } |
| 291 | /** |
| 292 | * deprecated since introduction of "visitors" in 1.5.4 |
| 293 | */ |
| 294 | if ( isset($_POST['advanced_ad']['visitor']) ) { |
| 295 | $ad->set_option( 'visitor', $_POST['advanced_ad']['visitor'] ); |
| 296 | } else { |
| 297 | $ad->set_option( 'visitor', array() ); |
| 298 | } |
| 299 | // visitor conditions |
| 300 | if ( isset($_POST['advanced_ad']['visitors']) ) { |
| 301 | $ad->set_option( 'visitors', $_POST['advanced_ad']['visitors'] ); |
| 302 | } else { |
| 303 | $ad->set_option( 'visitors', array() ); |
| 304 | } |
| 305 | $ad->url = 0; |
| 306 | if ( isset($_POST['advanced_ad']['url']) ) { |
| 307 | $ad->url = esc_url( $_POST['advanced_ad']['url'] ); |
| 308 | } |
| 309 | // save size |
| 310 | $ad->width = 0; |
| 311 | if ( isset($_POST['advanced_ad']['width']) ) { |
| 312 | $ad->width = absint( $_POST['advanced_ad']['width'] ); |
| 313 | } |
| 314 | $ad->height = 0; |
| 315 | if ( isset($_POST['advanced_ad']['height']) ) { |
| 316 | $ad->height = absint( $_POST['advanced_ad']['height'] ); |
| 317 | } |
| 318 | |
| 319 | if ( ! empty($_POST['advanced_ad']['description']) ) { |
| 320 | $ad->description = esc_textarea( $_POST['advanced_ad']['description'] ); } |
| 321 | else { $ad->description = ''; } |
| 322 | |
| 323 | if ( ! empty($_POST['advanced_ad']['content']) ) { |
| 324 | $ad->content = $_POST['advanced_ad']['content']; } |
| 325 | else { $ad->content = ''; } |
| 326 | |
| 327 | if ( ! empty($_POST['advanced_ad']['conditions']) ){ |
| 328 | $ad->conditions = $_POST['advanced_ad']['conditions']; |
| 329 | } else { |
| 330 | $ad->conditions = array(); |
| 331 | } |
| 332 | // prepare expiry date |
| 333 | if ( isset($_POST['advanced_ad']['expiry_date']['enabled']) ) { |
| 334 | $year = absint( $_POST['advanced_ad']['expiry_date']['year'] ); |
| 335 | $month = absint( $_POST['advanced_ad']['expiry_date']['month'] ); |
| 336 | $day = absint( $_POST['advanced_ad']['expiry_date']['day'] ); |
| 337 | $hour = absint( $_POST['advanced_ad']['expiry_date']['hour'] ); |
| 338 | $minute = absint( $_POST['advanced_ad']['expiry_date']['minute'] ); |
| 339 | |
| 340 | $expiration_date = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $hour, $minute, '00' ); |
| 341 | $valid_date = wp_checkdate( $month, $day, $year, $expiration_date ); |
| 342 | |
| 343 | if ( !$valid_date ) { |
| 344 | $ad->expiry_date = 0; |
| 345 | } else { |
| 346 | $_gmDate = date_create( $expiration_date, Advanced_Ads_Admin::get_wp_timezone() ); |
| 347 | $_gmDate->setTimezone( new DateTimeZone( 'UTC' ) ); |
| 348 | $gmDate = $_gmDate->format( 'Y-m-d-H-i' ); |
| 349 | list( $year, $month, $day, $hour, $minute ) = explode( '-', $gmDate ); |
| 350 | $ad->expiry_date = gmmktime($hour, $minute, 0, $month, $day, $year); |
| 351 | } |
| 352 | } else { |
| 353 | $ad->expiry_date = 0; |
| 354 | } |
| 355 | |
| 356 | $image_id = ( isset( $_POST['advanced_ad']['output']['image_id'] ) ) ? absint( $_POST['advanced_ad']['output']['image_id'] ) : 0; |
| 357 | if ( $image_id ) { |
| 358 | $all_posts_id = get_post_meta( $image_id, '_advanced-ads_parent_id' ); |
| 359 | |
| 360 | if ( ! in_array ( $post_id, $all_posts_id ) ) { |
| 361 | add_post_meta( $image_id, '_advanced-ads_parent_id', $post_id, false ); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | $ad->save(); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * prepare the ad post type to be removed |
| 370 | * |
| 371 | * @param int $post_id id of the post |
| 372 | */ |
| 373 | public function delete_ad( $post_id ) { |
| 374 | global $wpdb; |
| 375 | |
| 376 | if ( ! current_user_can( 'delete_posts' ) ) { |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | if ( $post_id > 0 ) { |
| 381 | $post_type = get_post_type( $post_id ); |
| 382 | if ( $post_type == $this->post_type ) { |
| 383 | $wpdb->query( |
| 384 | $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %d", '_advanced-ads_parent_id', $post_id ) |
| 385 | ); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * add information above the ad title |
| 392 | * |
| 393 | * @since 1.5.6 |
| 394 | * @param obj $post |
| 395 | */ |
| 396 | public function edit_form_above_title($post){ |
| 397 | if ( ! isset($post->post_type) || $post->post_type != $this->post_type ) { |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | // highlight Dummy ad if this is the first ad |
| 402 | if( ! Advanced_Ads::get_instance()->get_number_of_ads() ){ |
| 403 | ?><style>.advanced-ads-type-list-dummy { font-weight: bold; }</style><?php |
| 404 | } |
| 405 | |
| 406 | |
| 407 | $ad = new Advanced_Ads_Ad( $post->ID ); |
| 408 | |
| 409 | $placement_types = Advanced_Ads_Placements::get_placement_types(); |
| 410 | $placements = Advanced_Ads::get_ad_placements_array(); // -TODO use model |
| 411 | |
| 412 | // display general and wizard information |
| 413 | include ADVADS_BASE_PATH . 'admin/views/ad-info-top.php'; |
| 414 | // display ad injection information |
| 415 | include ADVADS_BASE_PATH . 'admin/views/placement-injection-top.php'; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * add information about the ad below the ad title |
| 420 | * |
| 421 | * @since 1.1.0 |
| 422 | * @param obj $post |
| 423 | */ |
| 424 | public function edit_form_below_title($post){ |
| 425 | if ( ! isset($post->post_type) || $post->post_type != $this->post_type ) { |
| 426 | return; |
| 427 | } |
| 428 | $ad = new Advanced_Ads_Ad( $post->ID ); |
| 429 | |
| 430 | include ADVADS_BASE_PATH . 'admin/views/ad-info.php'; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * add information below the ad edit form |
| 435 | * |
| 436 | * @since 1.7.3 |
| 437 | * @param obj $post |
| 438 | */ |
| 439 | public function edit_form_end($post){ |
| 440 | if ( ! isset($post->post_type) || $post->post_type != $this->post_type ) { |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | include ADVADS_BASE_PATH . 'admin/views/ad-info-bottom.php'; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * add meta values below submit box |
| 449 | * |
| 450 | * @since 1.3.15 |
| 451 | */ |
| 452 | public function add_submit_box_meta(){ |
| 453 | global $post, $wp_locale; |
| 454 | |
| 455 | if ( $post->post_type !== Advanced_Ads::POST_TYPE_SLUG ) { return; } |
| 456 | |
| 457 | $ad = new Advanced_Ads_Ad( $post->ID ); |
| 458 | |
| 459 | // get time set for ad or current timestamp (both GMT) |
| 460 | $utc_ts = $ad->expiry_date ? $ad->expiry_date : time(); |
| 461 | $utc_time = date_create( '@' . $utc_ts ); |
| 462 | $tz_option = get_option( 'timezone_string' ); |
| 463 | $exp_time = clone $utc_time; |
| 464 | |
| 465 | if ( $tz_option ) { |
| 466 | $exp_time->setTimezone( Advanced_Ads_Admin::get_wp_timezone() ); |
| 467 | } else { |
| 468 | $tz_name = Advanced_Ads_Admin::timezone_get_name( Advanced_Ads_Admin::get_wp_timezone() ); |
| 469 | $tz_offset = substr( $tz_name, 3 ); |
| 470 | $off_time = date_create( $utc_time->format( 'Y-m-d\TH:i:s' ) . $tz_offset ); |
| 471 | $offset_in_sec = date_offset_get( $off_time ); |
| 472 | $exp_time = date_create( '@' . ( $utc_ts + $offset_in_sec ) ); |
| 473 | } |
| 474 | |
| 475 | list( $curr_year, $curr_month, $curr_day, $curr_hour, $curr_minute ) = explode( '-', $exp_time->format( 'Y-m-d-H-i' ) ); |
| 476 | $enabled = 1 - empty($ad->expiry_date); |
| 477 | |
| 478 | include ADVADS_BASE_PATH . 'admin/views/ad-submitbox-meta.php'; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * use CodeMirror for plain text input field |
| 483 | * |
| 484 | * needs WordPress 4.9 and higher |
| 485 | * |
| 486 | * @since 1.8.15 |
| 487 | */ |
| 488 | public function use_code_editor(){ |
| 489 | global $wp_version; |
| 490 | if ( 'advanced_ads' !== get_current_screen()->id |
| 491 | || defined( 'ADVANCED_ADS_DISABLE_CODE_HIGHLIGHTING' ) |
| 492 | || -1 === version_compare( $wp_version, '4.9' ) ) { |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | // Enqueue code editor and settings for manipulating HTML. |
| 497 | $settings = wp_enqueue_code_editor( array( 'type' => 'application/x-httpd-php' ) ); |
| 498 | |
| 499 | // Bail if user disabled CodeMirror. |
| 500 | if ( false === $settings ) { |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | wp_add_inline_script( |
| 505 | 'code-editor', |
| 506 | sprintf( |
| 507 | 'jQuery( function() { if( jQuery( "#advads-ad-content-plain" ).length ){ wp.codeEditor.initialize( "advads-ad-content-plain", %s ); } } );', |
| 508 | wp_json_encode( $settings ) |
| 509 | ) |
| 510 | ); |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * edit ad update messages |
| 515 | * |
| 516 | * @since 1.4.7 |
| 517 | * @param arr $messages existing post update messages |
| 518 | * @return arr $messages |
| 519 | * |
| 520 | * @see wp-admin/edit-form-advanced.php |
| 521 | */ |
| 522 | public function ad_update_messages($messages = array()){ |
| 523 | $post = get_post(); |
| 524 | |
| 525 | // added to hide error message caused by third party code that uses post_updated_messages filter wrong |
| 526 | if( ! is_array( $messages )){ |
| 527 | return $messages; |
| 528 | } |
| 529 | |
| 530 | $messages[Advanced_Ads::POST_TYPE_SLUG] = array( |
| 531 | 0 => '', // Unused. Messages start at index 1. |
| 532 | 1 => __( 'Ad updated.', 'advanced-ads' ), |
| 533 | 4 => __( 'Ad updated.', 'advanced-ads' ), |
| 534 | /* translators: %s: date and time of the revision */ |
| 535 | 5 => isset( $_GET['revision'] ) ? sprintf( __( 'Ad restored to revision from %s', 'advanced-ads' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
| 536 | 6 => __( 'Ad saved.', 'advanced-ads' ), // published |
| 537 | 7 => __( 'Ad saved.', 'advanced-ads' ), // saved |
| 538 | 8 => __( 'Ad submitted.', 'advanced-ads' ), |
| 539 | 9 => sprintf( |
| 540 | __( 'Ad scheduled for: <strong>%1$s</strong>.', 'advanced-ads' ), |
| 541 | // translators: Publish box date format, see http://php.net/date |
| 542 | date_i18n( __( 'M j, Y @ G:i', 'advanced-ads' ), strtotime( $post->post_date ) ) |
| 543 | ), |
| 544 | 10 => __( 'Ad draft updated.', 'advanced-ads' ) |
| 545 | ); |
| 546 | return $messages; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * whether to show the wizard welcome message or not |
| 551 | * |
| 552 | * @since 1.7.4 |
| 553 | * @return bool true, if wizard welcome message should be displayed |
| 554 | */ |
| 555 | public function show_wizard_welcome(){ |
| 556 | $user_id = get_current_user_id(); |
| 557 | if( ! $user_id ) { |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | $hide_wizard = get_user_meta( $user_id, 'advanced-ads-hide-wizard', true ); |
| 562 | global $post; |
| 563 | |
| 564 | return ( ! $hide_wizard && 'edit' !== $post->filter ) ? true : false; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * whether to start the wizard by default or not |
| 569 | * |
| 570 | * @since 1.7.4 |
| 571 | * return bool true, if wizard should start automatically |
| 572 | */ |
| 573 | public function start_wizard_automatically(){ |
| 574 | $user_id = get_current_user_id(); |
| 575 | if( ! $user_id ) { |
| 576 | return true; |
| 577 | } |
| 578 | |
| 579 | $hide_wizard = get_user_meta( $user_id, 'advanced-ads-hide-wizard', true ); |
| 580 | global $post; |
| 581 | |
| 582 | // true if the wizard was never started or closed |
| 583 | return ( ( ! $hide_wizard && 'edit' !== $post->filter ) || 'false'=== $hide_wizard ) ? true : false; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Check if an ad is not valid for 'Post Content' placement |
| 588 | * |
| 589 | * @param obj $ad Advanced_Ads_Ad object |
| 590 | * @return string with error if not valid, empty string if valid |
| 591 | */ |
| 592 | public static function check_ad_dom_is_not_valid( Advanced_Ads_Ad $ad ) { |
| 593 | $adContent = ( isset( $ad->content ) ) ? $ad->content : ''; |
| 594 | if ( ! extension_loaded( 'dom' ) || ! $adContent ) { |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | $wpCharset = get_bloginfo('charset'); |
| 599 | $adDom = new DOMDocument('1.0', $wpCharset); |
| 600 | |
| 601 | $libxml_previous_state = libxml_use_internal_errors( true ); |
| 602 | // clear existing errors |
| 603 | libxml_clear_errors(); |
| 604 | // source for this regex: http://stackoverflow.com/questions/17852537/preg-replace-only-specific-part-of-string |
| 605 | $adContent = preg_replace('#(document.write.+)</(.*)#', '$1<\/$2', $adContent); // escapes all closing html tags |
| 606 | $adDom->loadHtml('<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html; charset=' . $wpCharset . '" /><body>' . $adContent); |
| 607 | |
| 608 | $errors = ''; |
| 609 | foreach( libxml_get_errors() as $_error ) { |
| 610 | // continue, if there is '&' symbol, but not HTML entity |
| 611 | if ( false === stripos( $_error->message, 'htmlParseEntityRef:' ) ) { |
| 612 | $errors .= print_r( $_error, true ); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | libxml_use_internal_errors( $libxml_previous_state ); |
| 617 | return $errors; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Replace 'Cheatin’ uh?' message if user role does not have required permissions. |
| 622 | * |
| 623 | * @param string $translation Translated text. |
| 624 | * @param string $text Text to translate. |
| 625 | * @return string $translation Translated text. |
| 626 | */ |
| 627 | public function replace_cheating_message( $translated_text, $untranslated_text ) { |
| 628 | global $typenow; |
| 629 | |
| 630 | if ( isset( $typenow ) && $untranslated_text === 'Cheatin’ uh?' && $typenow === $this->post_type ) { |
| 631 | $translated_text = __( 'You don’t have access to ads. Please deactivate and re-enable Advanced Ads again to fix this.', 'advanced-ads' ); |
| 632 | } |
| 633 | |
| 634 | return $translated_text; |
| 635 | } |
| 636 | |
| 637 | } |
| 638 |