* @since 1.47.0 */ namespace AdvancedAds\Admin\Pages; use AdvancedAds\Abstracts\Screen; use AdvancedAds\Constants; use AdvancedAds\Framework\Utilities\Params; use AdvancedAds\Utilities\Conditional; use AdvancedAds\Utilities\WordPress; use WP_Post; defined( 'ABSPATH' ) || exit; /** * Ads editing screen. */ class Ads_Editing extends Screen { /** * Screen unique id. * * @return string */ public function get_id(): string { return 'ads-editing'; } /** * Register screen into WordPress admin area. * * @return void */ public function register_screen(): void { $this->set_hook( Constants::POST_TYPE_AD ); add_action( 'dbx_post_sidebar', [ $this, 'render_support_callout' ] ); add_action( 'edit_form_top', [ $this, 'edit_form_above_title' ] ); add_action( 'post_submitbox_misc_actions', [ $this, 'add_submit_box_meta' ] ); } /** * Enqueue assets * * @return void */ public function enqueue_assets(): void { $wp_screen = get_current_screen(); if ( 'post' !== $wp_screen->base || Constants::POST_TYPE_AD !== $wp_screen->post_type ) { return; } wp_advads()->registry->enqueue_script( 'screen-ads-editing' ); wp_advads()->registry->enqueue_style( 'screen-ads-editing' ); // Ad positioning. wp_advads()->registry->enqueue_style( 'ad-positioning' ); wp_advads()->registry->enqueue_script( 'ad-positioning' ); // Enqueue code editor and settings for manipulating HTML. $settings = wp_enqueue_code_editor( [ 'type' => 'application/x-httpd-php' ] ); // Only if CodeMirror is enabled. if ( false !== $settings ) { wp_advads_json_add( 'admin', [ 'codeMirror' => [ 'settings' => $settings, ], ] ); } // Localize texts. $i18n = [ 'ads' => [ 'afterParagraphPrompt' => __( 'After which paragraph?', 'advanced-ads' ), ], ]; wp_advads_json_add( 'i18n', $i18n ); } /** * Define header args. * * @return array */ public function define_header_args(): array { return [ 'manual_url' => $this->get_manual_url( '/first-ad/' ), ]; } /** * Add actions to the header * * @return void */ public function header_actions(): void { $wp_screen = get_current_screen(); if ( 'post' !== $wp_screen->base || 'add' === $wp_screen->action ) { return; } $url = admin_url( 'post-new.php?post_type=' . Constants::POST_TYPE_AD ); ?> post_type || ! Conditional::is_any_addon_activated() ) { return; } include_once ADVADS_ABSPATH . 'views/admin/ads/info-bottom.php'; } /** * Add information above the ad title * * @since 1.5.6 * * @param object $post WordPress post type object. * * @return void */ public function edit_form_above_title( $post ): void { if ( ! isset( $post->post_type ) || Constants::POST_TYPE_AD !== $post->post_type ) { return; } // Don’t show placement options if this is an ad translated with WPML since the placement might exist already. if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { $trid = apply_filters( 'wpml_element_trid', null, $post->ID ); $translations = apply_filters( 'wpml_get_element_translations', null, $trid, 'Advanced_Ads' ); if ( is_array( $translations ) && count( $translations ) > 1 ) { return; } } /** * Display ad injection information after ad is created — set `advanced-ads-ad-edit-show-placement-injection` to false if you want to prevent the box from appearing. */ if ( 6 === Params::get( 'message', 0, FILTER_VALIDATE_INT ) && apply_filters( 'advanced-ads-ad-edit-show-placement-injection', true ) ) { $ad = wp_advads_get_ad( $post->ID ); $placements = wp_advads_get_placements(); $latest_post = $this->get_latest_post(); include_once ADVADS_ABSPATH . 'admin/views/placement-injection-top.php'; } } /** * Add meta values below submit box * * @since 1.3.15 * * @param WP_Post $post WordPress post type object. * * @return void */ public function add_submit_box_meta( $post ): void { global $wp_locale; // Early bail!! if ( Constants::POST_TYPE_AD !== $post->post_type ) { return; } $ad = wp_advads_get_ad( $post->ID ); // Get time set for ad or current timestamp (both GMT). $utc_ts = $ad->get_expiry_date() ?: current_time( 'timestamp', true ); // phpcs:ignore $local_time = ( new \DateTimeImmutable( '@' . $utc_ts ) )->setTimezone( WordPress::get_timezone() ); [ $curr_year, $curr_month, $curr_day, $curr_hour, $curr_minute ] = explode( '-', $local_time->format( 'Y-m-d-H-i' ) ); $enabled = (int) ! empty( $ad->get_expiry_date() ); include ADVADS_ABSPATH . 'views/admin/ads/submitbox-meta.php'; } /** * Load latest blog post * * @return \WP_Post|null */ private function get_latest_post(): ?\WP_Post { $posts = wp_get_recent_posts( [ 'numberposts' => 1 ], OBJECT ); return $posts[0] ?? null; } }