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-groups.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Groups 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 AdvancedAds\Constants; |
| 13 | use AdvancedAds\Abstracts\Screen; |
| 14 | use AdvancedAds\Admin\Groups\Create_Modal; |
| 15 | use AdvancedAds\Utilities\Conditional; |
| 16 | use AdvancedAds\Admin\Groups\List_Table; |
| 17 | |
| 18 | defined( 'ABSPATH' ) || exit; |
| 19 | |
| 20 | /** |
| 21 | * Groups. |
| 22 | */ |
| 23 | class Groups extends Screen { |
| 24 | |
| 25 | /** |
| 26 | * Hold table object. |
| 27 | * |
| 28 | * @var null|List_Table |
| 29 | */ |
| 30 | private $list_table = null; |
| 31 | |
| 32 | /** |
| 33 | * Screen unique id. |
| 34 | * |
| 35 | * @return string |
| 36 | */ |
| 37 | public function get_id(): string { |
| 38 | return 'groups'; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Register screen into WordPress admin area. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function register_screen(): void { |
| 47 | $hook = add_submenu_page( |
| 48 | ADVADS_SLUG, |
| 49 | __( 'Ad Groups & Rotations', 'advanced-ads' ), |
| 50 | __( 'Groups & Rotation', 'advanced-ads' ), |
| 51 | Conditional::user_cap( 'advanced_ads_edit_ads' ), |
| 52 | ADVADS_SLUG . '-groups', |
| 53 | [ $this, 'display' ] |
| 54 | ); |
| 55 | |
| 56 | $this->set_hook( $hook ); |
| 57 | add_action( 'current_screen', [ $this, 'add_screen_options' ], 5 ); |
| 58 | add_action( 'current_screen', [ $this, 'get_list_table' ] ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Enqueue assets |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public function enqueue_assets(): void { |
| 67 | wp_advads()->registry->enqueue_style( 'screen-groups-listing' ); |
| 68 | wp_advads()->registry->enqueue_script( 'screen-groups-listing' ); |
| 69 | |
| 70 | // Localize texts. |
| 71 | $i18n = [ |
| 72 | 'groups' => [ |
| 73 | 'save' => __( 'Save', 'advanced-ads' ), |
| 74 | 'saveNew' => __( 'Save New Group', 'advanced-ads' ), |
| 75 | 'updated' => __( 'Group updated', 'advanced-ads' ), |
| 76 | 'deleted' => __( 'Group deleted', 'advanced-ads' ), |
| 77 | /* translators: an ad group title. */ |
| 78 | 'confirmation' => __( 'You are about to permanently delete %s', 'advanced-ads' ), |
| 79 | ], |
| 80 | ]; |
| 81 | wp_advads_json_add( 'i18n', $i18n ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Display screen content. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | public function display(): void { |
| 90 | global $wp_list_table; |
| 91 | |
| 92 | $wp_list_table = $this->get_list_table(); |
| 93 | ( new Create_Modal() )->hooks(); |
| 94 | |
| 95 | include_once ADVADS_ABSPATH . 'views/admin/groups/page.php'; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get list table object |
| 100 | * |
| 101 | * @return null|Groups_List_Table |
| 102 | */ |
| 103 | public function get_list_table() { |
| 104 | $is_screen = wp_advads()->screens->is_screen( $this->get_id() ); |
| 105 | $wp_screen = get_current_screen(); |
| 106 | if ( $is_screen && null === $this->list_table ) { |
| 107 | wp_advads()->registry->enqueue_script( 'groups' ); |
| 108 | $wp_screen->taxonomy = Constants::TAXONOMY_GROUP; |
| 109 | $wp_screen->post_type = Constants::POST_TYPE_AD; |
| 110 | $this->list_table = new List_Table(); |
| 111 | } |
| 112 | |
| 113 | return $this->list_table; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Add screen options. |
| 118 | * |
| 119 | * @return void |
| 120 | */ |
| 121 | public function add_screen_options(): void { |
| 122 | // Early bail!! |
| 123 | $is_screen = wp_advads()->screens->is_screen( $this->get_id() ); |
| 124 | if ( ! $is_screen ) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | add_screen_option( |
| 129 | 'per_page', |
| 130 | [ |
| 131 | 'default' => 20, |
| 132 | 'option' => 'edit_' . Constants::TAXONOMY_GROUP . '_per_page', |
| 133 | ] |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get page header arguments |
| 139 | * |
| 140 | * @return array |
| 141 | */ |
| 142 | public function define_header_args(): array { |
| 143 | return [ |
| 144 | 'title' => __( 'Your Groups', 'advanced-ads' ), |
| 145 | 'breadcrumb_title' => __( 'Groups', 'advanced-ads' ), |
| 146 | 'manual_url' => 'https://wpadvancedads.com/manual/ad-groups/', |
| 147 | ]; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Add actions to the header |
| 152 | * |
| 153 | * @return void |
| 154 | */ |
| 155 | public function header_actions(): void { |
| 156 | ?> |
| 157 | <a href="#modal-group-new" data-dialog="modal-group-new" class="button button-primary advads-button"> |
| 158 | <span class="dashicons dashicons-plus -ml-1.5 leading-6"></span> |
| 159 | <span><?php esc_html_e( 'New Ad Group', 'advanced-ads' ); ?></span> |
| 160 | </a> |
| 161 | <?php |
| 162 | } |
| 163 | } |
| 164 |