ID && Advanced_Ads_AdSense_Data::get_instance()->is_setup() && ! Advanced_Ads_AdSense_Data::get_instance()->is_hide_stats() ) { add_meta_box( 'advads-gadsense-box', __( 'AdSense Earnings', 'advanced-ads' ), [ $this, 'markup_meta_boxes' ], $post_type, 'normal', 'high' ); } // use dynamic filter from to add close class to ad type meta box after saved first time. add_filter( 'postbox_classes_advanced_ads_ad-main-box', [ $this, 'close_ad_type_metabox' ] ); // show the Usage box for saved ads if ( $post->filter === 'edit' ) { add_meta_box( 'ad-usage-box', __( 'Usage', 'advanced-ads' ), [ $this, 'markup_meta_boxes' ], $post_type, 'normal', 'high' ); } add_meta_box( 'ad-parameters-box', __( 'Ad Parameters', 'advanced-ads' ), [ $this, 'markup_meta_boxes' ], $post_type, 'normal', 'high' ); add_meta_box( 'ad-output-box', __( 'Layout / Output', 'advanced-ads' ), [ $this, 'markup_meta_boxes' ], $post_type, 'normal', 'high' ); add_meta_box( 'ad-targeting-box', __( 'Targeting', 'advanced-ads' ), [ $this, 'markup_meta_boxes' ], $post_type, 'normal', 'default' ); if ( ! defined( 'AAP_VERSION' ) ) { add_meta_box( 'advads-pro-pitch', __( 'Increase your ad revenue', 'advanced-ads' ), [ $this, 'markup_meta_boxes' ], $post_type, 'side', 'low' ); } if ( ! defined( 'AAT_VERSION' ) ) { add_meta_box( 'advads-tracking-pitch', __( 'Statistics', 'advanced-ads' ), [ $this, 'markup_meta_boxes' ], $post_type, 'normal', 'low' ); } // register meta box ids. $this->meta_box_ids = [ 'ad-main-box', 'advads-gadsense-box', 'ad-parameters-box', 'ad-output-box', 'ad-targeting-box', 'advads-pro-pitch', 'advads-tracking-pitch', 'revisionsdiv', // revisions – only when activated. 'advanced_ads_groupsdiv', // automatically added by ad groups taxonomy. ]; // force AA meta boxes to never be completely hidden by screen options. add_filter( 'hidden_meta_boxes', [ $this, 'unhide_meta_boxes' ], 10, 2 ); // hide the checkboxes for "unhideable" meta boxes within screen options via CSS. add_action( 'admin_head', [ $this, 'unhide_meta_boxes_style' ] ); $whitelist = apply_filters( 'advanced-ads-ad-edit-allowed-metaboxes', array_merge( $this->meta_box_ids, [ // meta boxes in this array can be hidden using Screen Option 'submitdiv', 'slugdiv', 'ad-usage-box', 'authordiv', 'tracking-ads-box', 'ad-layer-ads-box', // deprecated. ] ) ); global $wp_meta_boxes; // remove non-white-listed meta boxes. foreach ( [ 'normal', 'advanced', 'side' ] as $context ) { if ( isset( $wp_meta_boxes[ $post_type ][ $context ] ) ) { foreach ( [ 'high', 'sorted', 'core', 'default', 'low' ] as $priority ) { if ( isset( $wp_meta_boxes[ $post_type ][ $context ][ $priority ] ) ) { foreach ( (array) $wp_meta_boxes[ $post_type ][ $context ][ $priority ] as $id => $box ) { if ( ! in_array( $id, $whitelist ) ) { unset( $wp_meta_boxes[ $post_type ][ $context ][ $priority ][ $id ] ); } } } } } } } /** * Load templates for all meta boxes * * @param WP_Post $post WP_Post object. * @param array $box meta box information. * @todo move ad initialization to main function and just global it */ public function markup_meta_boxes( $post, $box ) { $ad = \Advanced_Ads\Ad_Repository::get( $post->ID ); switch ( $box['id'] ) { case 'ad-main-box': $view = 'ad-main-metabox.php'; $hndlelinks = '' . __( 'Manual', 'advanced-ads' ) . ''; break; case 'ad-usage-box': $view = 'ad-usage-metabox.php'; break; case 'ad-parameters-box': $view = 'ad-parameters-metabox.php'; break; case 'ad-output-box': $positioning = ( new Advanced_Ads_Ad_Positioning( $ad ) )->return_admin_view(); $wrapper_id = $ad->options( 'output.wrapper-id', '' ); $wrapper_class = $ad->options( 'output.wrapper-class', '' ); $debug_mode_enabled = (bool) $ad->options( 'output.debugmode', false ); $view = 'ad-output-metabox.php'; $hndlelinks = '' . __( 'Manual', 'advanced-ads' ) . ''; break; case 'ad-targeting-box': $view = 'conditions/ad-targeting-metabox.php'; $hndlelinks = '' . __( 'Video', 'advanced-ads' ) . ''; $hndlelinks .= '' . __( 'Display Conditions', 'advanced-ads' ) . ''; $hndlelinks .= '' . __( 'Visitor Conditions', 'advanced-ads' ) . ''; $videomarkup = ''; $display_conditions = $ad->options( 'conditions', [] ); $visitor_conditions = $ad->options( 'visitors', [] ); $display_conditions_available = ( empty( $display_conditions ) ); $visitor_conditions_available = ( empty( $visitor_conditions ) ); break; case 'advads-pro-pitch': $view = 'upgrades/all-access.php'; // $hndlelinks = '' . __('Manual', 'advanced-ads') . ''; break; case 'advads-tracking-pitch': $view = 'upgrades/tracking.php'; // $hndlelinks = '' . __('Manual', 'advanced-ads') . ''; break; case 'advads-gadsense-box': $unit_code = null; if ( $ad && isset( $ad->type ) && 'adsense' === $ad->type ) { if ( isset( $ad->content ) ) { $json_content = json_decode( $ad->content ); // phpcs:ignore if ( isset( $json_content->slotId ) ) { // phpcs:ignore $unit_code = $json_content->slotId; } } } $report_type = 'unit'; $report_filter = $unit_code; $advads_gadsense_options['hidden'] = ! $unit_code; $view = 'gadsense-dashboard.php'; $hndlelinks = '' . __( 'Disable', 'advanced-ads' ) . ''; break; } if ( ! isset( $view ) ) { return; } // markup moved to handle headline of the metabox. if ( isset( $hndlelinks ) ) { ?> [ 'width' => [], 'height' => [], 'src' => [], 'frameborder' => [], 'allowfullscreen' => [], ], ] ) . '\'>'; } /** * List general notices * elements in $warnings contain [text] and [class] attributes. */ $warnings = []; // show warning if ad contains https in parameters box. $https_message = Advanced_Ads_Ad_Debug::is_https_and_http( $ad ); if ( 'ad-parameters-box' === $box['id'] && $https_message ) { $warnings[] = [ 'text' => $https_message, 'class' => 'advads-ad-notice-https-missing advads-notice-inline advads-error', ]; } if ( 'ad-parameters-box' === $box['id'] ) { $warnings[] = [ 'text' => Advanced_Ads_AdSense_Admin::get_auto_ads_messages()[ Advanced_Ads_AdSense_Data::get_instance()->is_page_level_enabled() ? 'enabled' : 'disabled' ], 'class' => 'advads-auto-ad-in-ad-content hidden advads-notice-inline advads-error', ]; } // Let users know that they could use the Google AdSense ad type when they enter an AdSense code. if ( 'ad-parameters-box' === $box['id'] && Advanced_Ads_Ad_Type_Adsense::content_is_adsense( $ad->content ) && in_array( $ad->type, [ 'plain', 'content' ], true ) ) { if ( false === strpos( $ad->content, 'enable_page_level_ads' ) && ! preg_match( '/script[^>]+data-ad-client=/', $ad->content ) ) { $adsense_auto_ads = Advanced_Ads_AdSense_Data::get_instance()->is_page_level_enabled(); $warnings[] = [ 'class' => 'advads-adsense-found-in-content advads-notice-inline advads-error', 'text' => sprintf( // translators: %1$s opening button tag, %2$s closing button tag. esc_html__( 'This looks like an AdSense ad. Switch the ad type to “AdSense ad” to make use of more features. %1$sSwitch to AdSense ad%2$s.', 'advanced' ), '' ), ]; } } $warnings = apply_filters( 'advanced-ads-ad-notices', $warnings, $box, $post ); echo '
'; include ADVADS_BASE_PATH . 'admin/views/' . $view; } /** * Force all AA related meta boxes to stay visible * * @param array $hidden An array of hidden meta boxes. * @param WP_Screen $screen WP_Screen object of the current screen. * * @return array */ public function unhide_meta_boxes( $hidden, $screen ) { // only check on Advanced Ads edit screen. if ( ! isset( $screen->id ) || 'advanced_ads' !== $screen->id || ! is_array( $this->meta_box_ids ) ) { return $hidden; } // return only hidden elements which are not among the Advanced Ads meta box ids. return array_diff( $hidden, (array) apply_filters( 'advanced-ads-unhide-meta-boxes', $this->meta_box_ids ) ); } /** * Add dynamic CSS for un-hideable meta boxes. */ public function unhide_meta_boxes_style() { $screen = get_current_screen(); if ( empty( $screen ) || ! isset( $screen->id ) || 'advanced_ads' !== $screen->id ) { return; } $meta_boxes = (array) apply_filters( 'advanced-ads-unhide-meta-boxes', $this->meta_box_ids ); if ( empty( $meta_boxes ) ) { return; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we don't need to escape the string we just concatenated. printf( '', implode( ', ', array_reduce( $meta_boxes, function( $styles, $box_id ) { $styles[] = sprintf( 'label[for="%s-hide"]', $box_id ); return $styles; }, [] ) ) ); } /** * Add a meta box to post type edit screens with ad settings * * @param string $post_type current post type. */ public function add_post_meta_box( $post_type = '' ) { // don’t display for non admins. if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) ) { return; } // get public post types. $public_post_types = get_post_types( [ 'public' => true, 'publicly_queryable' => true, ], 'names', 'or' ); // limit meta box to public post types. if ( in_array( $post_type, $public_post_types ) ) { add_meta_box( 'advads-ad-settings', __( 'Ad Settings', 'advanced-ads' ), [ $this, 'render_post_meta_box' ], $post_type, 'side', 'low' ); } } /** * Render meta box for ad settings on a per post basis * * @param WP_Post $post The post object. */ public function render_post_meta_box( $post ) { // nonce field to check when we save the values. wp_nonce_field( 'advads_post_meta_box', 'advads_post_meta_box_nonce' ); // retrieve an existing value from the database. $values = get_post_meta( $post->ID, '_advads_ad_settings', true ); // load the view. include ADVADS_BASE_PATH . 'admin/views/post-ad-settings-metabox.php'; do_action( 'advanced_ads_render_post_meta_box', $post, $values ); } /** * Save the ad meta when the post is saved. * * @param int $post_id The ID of the post being saved. * * @return mixed empty or post ID. */ public function save_post_meta_box( $post_id ) { if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) ) { return; } // check nonce. if ( ! isset( $_POST['advads_post_meta_box_nonce'] ) ) { return $post_id; } $nonce = $_POST['advads_post_meta_box_nonce']; // Verify that the nonce is valid. if ( ! wp_verify_nonce( $nonce, 'advads_post_meta_box' ) ) { return $post_id; } // don’t save on autosave. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; } // check the user's permissions. if ( 'page' === $_POST['post_type'] ) { if ( ! current_user_can( 'edit_page', $post_id ) ) { return $post_id; } } else { if ( ! current_user_can( 'edit_post', $post_id ) ) { return $post_id; } } // sanitize the user input. $_data['disable_ads'] = isset( $_POST['advanced_ads']['disable_ads'] ) ? absint( $_POST['advanced_ads']['disable_ads'] ) : 0; $_data = apply_filters( 'advanced_ads_save_post_meta_box', $_data ); // update the meta field. update_post_meta( $post_id, '_advads_ad_settings', $_data ); } /** * Add "close" class to collapse the ad-type metabox after ad was saved first * * @param array $classes class attributes. * @return array $classes */ public function close_ad_type_metabox( $classes = [] ) { global $post; if ( isset( $post->ID ) && 'publish' === $post->post_status ) { if ( ! in_array( 'closed', $classes, true ) ) { $classes[] = 'closed'; } } else { $classes = []; } return $classes; } /** * Add dashboard widget with ad stats and additional information */ public function add_dashboard_widget() { // display dashboard widget only to authors and higher roles. if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface' ) ) ) { return; } add_meta_box( 'advads_dashboard_widget', __( 'Dashboard', 'advanced-ads' ), [ $this, 'dashboard_widget_function' ], 'dashboard', 'side', 'high' ); } /** * Display widget functions * * @param WP_Post $post post object. * @param array $callback_args callback arguments. */ public static function dashboard_widget_function( $post, $callback_args ) { // get number of ads. $ads_count = Advanced_Ads::get_number_of_ads(); if ( current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) ) { echo ''; printf( // translators: %1$d is the number of ads, %2$s and %3$s are URLs. wp_kses( __( '%1$d ads – manage - new', 'advanced-ads' ), [ 'a' => [ 'href' => [] ] ] ), absint( $ads_count ), 'edit.php?post_type=' . esc_attr( Advanced_Ads::POST_TYPE_SLUG ), 'post-new.php?post_type=' . esc_attr( Advanced_Ads::POST_TYPE_SLUG ) ); echo '
'; } $notice_options = Advanced_Ads_Admin_Notices::get_instance()->options(); $_notice = 'nl_first_steps'; if ( ! isset( $notice_options['closed'][ $_notice ] ) ) { ?>