class-ads-editing.php
3 months ago
class-ads.php
3 months ago
class-dashboard.php
3 months ago
class-groups.php
3 months ago
class-onboarding.php
3 months ago
class-placements.php
3 months ago
class-settings.php
3 months ago
class-support.php
3 months ago
class-tools.php
3 months ago
class-placements.php
169 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Placements screen. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.47.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin\Pages; |
| 11 | |
| 12 | use WP_Screen; |
| 13 | use AdvancedAds\Constants; |
| 14 | use AdvancedAds\Abstracts\Screen; |
| 15 | use AdvancedAds\Utilities\Conditional; |
| 16 | use AdvancedAds\Admin\Placements\List_Table; |
| 17 | use AdvancedAds\Admin\Placements\Create_Modal; |
| 18 | |
| 19 | defined( 'ABSPATH' ) || exit; |
| 20 | |
| 21 | /** |
| 22 | * Placements. |
| 23 | */ |
| 24 | class Placements extends Screen { |
| 25 | |
| 26 | /** |
| 27 | * Screen unique id. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public function get_id(): string { |
| 32 | return 'placements'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Register screen into WordPress admin area. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function register_screen(): void { |
| 41 | add_submenu_page( |
| 42 | ADVADS_SLUG, |
| 43 | __( 'Ad Placements', 'advanced-ads' ), |
| 44 | __( 'Placements', 'advanced-ads' ), |
| 45 | Conditional::user_cap( 'advanced_ads_manage_placements' ), |
| 46 | 'edit.php?post_type=' . Constants::POST_TYPE_PLACEMENT |
| 47 | ); |
| 48 | |
| 49 | // Keep the manual placements page around, but redirect it to the custom post type. |
| 50 | $old_placements_hook = add_submenu_page( |
| 51 | '', |
| 52 | '', |
| 53 | '', |
| 54 | Conditional::user_cap( 'advanced_ads_manage_placements' ), |
| 55 | ADVADS_SLUG . '-placements', |
| 56 | '__return_true' |
| 57 | ); |
| 58 | $this->set_hook( 'edit-' . Constants::POST_TYPE_PLACEMENT ); |
| 59 | add_action( 'current_screen', [ $this, 'load_placement_ui' ] ); |
| 60 | add_action( 'load-' . $old_placements_hook, [ $this, 'redirect_to_post_type' ] ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Enqueue assets |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | public function enqueue_assets(): void { |
| 69 | wp_advads()->registry->enqueue_style( 'screen-placements-listing' ); |
| 70 | wp_advads()->registry->enqueue_script( 'screen-placements-listing' ); |
| 71 | |
| 72 | wp_advads_json_add( |
| 73 | 'placements', |
| 74 | [ |
| 75 | 'updateItemNonce' => wp_create_nonce( 'placement-update-item'), |
| 76 | 'pickerUrl' => $this->get_placement_picker_url(), |
| 77 | ] |
| 78 | ); |
| 79 | |
| 80 | // Localize texts. |
| 81 | $i18n = [ |
| 82 | 'placements' => [ |
| 83 | 'created' => __( 'New placement created', 'advanced-ads' ), |
| 84 | 'updated' => __( 'Placement updated', 'advanced-ads' ), |
| 85 | 'closeSave' => __( 'Close and save', 'advanced-ads' ), |
| 86 | 'saveNew' => __( 'Save new placement', 'advanced-ads' ), |
| 87 | 'draft' => __( 'Draft', 'advanced-ads' ), |
| 88 | ], |
| 89 | ]; |
| 90 | wp_advads_json_add( 'i18n', $i18n ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Redirect old placement page to custom post type. |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | public function redirect_to_post_type(): void { |
| 99 | wp_safe_redirect( 'edit.php?post_type=' . Constants::POST_TYPE_PLACEMENT ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Load list table |
| 104 | * |
| 105 | * @param WP_Screen $screen Current screen instance. |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function load_placement_ui( WP_Screen $screen ): void { |
| 110 | if ( 'edit-' . Constants::POST_TYPE_PLACEMENT === $screen->id ) { |
| 111 | ( new List_Table() )->hooks(); |
| 112 | ( new Create_Modal() )->hooks(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get page header arguments |
| 118 | * |
| 119 | * @return array |
| 120 | */ |
| 121 | public function define_header_args(): array { |
| 122 | return [ |
| 123 | 'title' => __( 'Your Placements', 'advanced-ads' ), |
| 124 | 'breadcrumb_title' => __( 'Placements', 'advanced-ads' ), |
| 125 | 'manual_url' => 'https://wpadvancedads.com/manual/placements/', |
| 126 | ]; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Add actions to the header |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
| 134 | public function header_actions(): void { |
| 135 | ?> |
| 136 | <a href="#modal-placement-new" data-dialog="modal-placement-new" class="button button-primary advads-button"> |
| 137 | <span class="dashicons dashicons-plus -ml-1.5 leading-6"></span> |
| 138 | <span><?php esc_html_e( 'New Placement', 'advanced-ads' ); ?></span> |
| 139 | </a> |
| 140 | <?php |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the URL where the user is redirected after activating the frontend picker for a "Content" placement. |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | private function get_placement_picker_url() { |
| 149 | $location = false; |
| 150 | |
| 151 | if ( get_option( 'show_on_front' ) === 'posts' ) { |
| 152 | $recent_posts = wp_get_recent_posts( |
| 153 | [ |
| 154 | 'numberposts' => 1, |
| 155 | 'post_type' => 'post', |
| 156 | 'post_status' => 'publish', |
| 157 | ], |
| 158 | 'OBJECT' |
| 159 | ); |
| 160 | |
| 161 | if ( $recent_posts ) { |
| 162 | $location = get_permalink( $recent_posts[0] ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return $location ?? home_url(); |
| 167 | } |
| 168 | } |
| 169 |