pages
2 years ago
class-action-links.php
2 years ago
class-admin-menu.php
2 years ago
class-assets.php
1 year ago
class-groups-list-table.php
2 years ago
class-header.php
2 years ago
class-page-quick-edit.php
1 year ago
class-tinymce.php
2 years ago
index.php
2 years ago
class-admin-menu.php
151 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The class is responsible for adding menu and submenu pages for the plugin in the WordPress admin area. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.47.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin; |
| 11 | |
| 12 | use Advanced_Ads; |
| 13 | use Advanced_Ads_Ad_Health_Notices; |
| 14 | use Advanced_Ads_Checks; |
| 15 | use AdvancedAds\Entities; |
| 16 | use AdvancedAds\Admin\Pages\Ads; |
| 17 | use AdvancedAds\Admin\Pages\Dashboard; |
| 18 | use AdvancedAds\Admin\Pages\Groups; |
| 19 | use AdvancedAds\Admin\Pages\Placements; |
| 20 | use AdvancedAds\Admin\Pages\Settings; |
| 21 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 22 | use AdvancedAds\Utilities\WordPress; |
| 23 | |
| 24 | defined( 'ABSPATH' ) || exit; |
| 25 | |
| 26 | /** |
| 27 | * Admin Admin Menu. |
| 28 | */ |
| 29 | class Admin_Menu implements Integration_Interface { |
| 30 | |
| 31 | /** |
| 32 | * Hold screens |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | private $screens = []; |
| 37 | |
| 38 | /** |
| 39 | * Hook into WordPress. |
| 40 | * |
| 41 | * @return void |
| 42 | */ |
| 43 | public function hooks(): void { |
| 44 | add_action( 'admin_menu', [ $this, 'add_pages' ] ); |
| 45 | add_action( 'admin_head', [ $this, 'highlight_menu_item' ] ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Register the administration menu for this plugin into the WordPress Dashboard menu. |
| 50 | * |
| 51 | * @since 1.0.0 |
| 52 | * |
| 53 | * @return void |
| 54 | */ |
| 55 | public function add_pages(): void { |
| 56 | foreach ( $this->get_screens() as $renderer ) { |
| 57 | $renderer->register_screen(); |
| 58 | } |
| 59 | |
| 60 | $this->register_forward_links(); |
| 61 | |
| 62 | /** |
| 63 | * Allows extensions to insert sub menu pages. |
| 64 | * |
| 65 | * @since untagged Added the `$hidden_page_slug` parameter. |
| 66 | * |
| 67 | * @param string $plugin_slug The slug slug used to add a visible page. |
| 68 | * @param string $hidden_page_slug The slug slug used to add a hidden page. |
| 69 | */ |
| 70 | do_action( 'advanced-ads-submenu-pages', ADVADS_SLUG, 'advanced_ads_hidden_page_slug' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Register forward links |
| 75 | * |
| 76 | * @return void |
| 77 | */ |
| 78 | private function register_forward_links(): void { |
| 79 | global $submenu; |
| 80 | |
| 81 | $has_ads = Advanced_Ads::get_number_of_ads( [ 'any', 'trash' ] ); |
| 82 | $notices = Advanced_Ads_Ad_Health_Notices::get_number_of_notices(); |
| 83 | $notice_alert = ' <span class="update-plugins count-' . $notices . '"><span class="update-count">' . $notices . '</span></span>'; |
| 84 | |
| 85 | // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited |
| 86 | if ( current_user_can( WordPress::user_cap( 'advanced_ads_manage_options' ) ) ) { |
| 87 | $submenu['advanced-ads'][] = [ |
| 88 | __( 'Support', 'advanced-ads' ), |
| 89 | WordPress::user_cap( 'advanced_ads_manage_options' ), |
| 90 | admin_url( 'admin.php?page=advanced-ads-settings#top#support' ), |
| 91 | __( 'Support', 'advanced-ads' ), |
| 92 | ]; |
| 93 | |
| 94 | if ( $has_ads ) { |
| 95 | $submenu['advanced-ads'][0][0] .= $notice_alert; |
| 96 | } else { |
| 97 | $submenu['advanced-ads'][1][0] .= $notice_alert; |
| 98 | } |
| 99 | |
| 100 | // Link to license tab if they are invalid. |
| 101 | if ( Advanced_Ads_Checks::licenses_invalid() ) { |
| 102 | $submenu['advanced-ads'][] = [ |
| 103 | __( 'Licenses', 'advanced-ads' ) |
| 104 | . ' <span class="update-plugins count-1"><span class="update-count">!</span></span>', |
| 105 | WordPress::user_cap( 'advanced_ads_manage_options' ), |
| 106 | admin_url( 'admin.php?page=advanced-ads-settings#top#licenses' ), |
| 107 | __( 'Licenses', 'advanced-ads' ), |
| 108 | ]; |
| 109 | } |
| 110 | } |
| 111 | // phpcs:enable |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get screens |
| 116 | * |
| 117 | * @return array |
| 118 | */ |
| 119 | private function get_screens(): array { |
| 120 | if ( ! empty( $this->screens ) ) { |
| 121 | return $this->screens; |
| 122 | } |
| 123 | |
| 124 | $this->screens['dashboard'] = new Dashboard(); |
| 125 | $this->screens['ads'] = new Ads(); |
| 126 | $this->screens['groups'] = new Groups(); |
| 127 | $this->screens['placements'] = new Placements(); |
| 128 | $this->screens['settings'] = new Settings(); |
| 129 | |
| 130 | return $this->screens; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Highlights the 'Advanced Ads->Ads' item in the menu when an ad edit page is open |
| 135 | * |
| 136 | * @see the 'parent_file' and the 'submenu_file' filters for reference |
| 137 | * |
| 138 | * @return void |
| 139 | */ |
| 140 | public function highlight_menu_item(): void { |
| 141 | global $parent_file, $submenu_file, $post_type; |
| 142 | |
| 143 | // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited |
| 144 | if ( Entities::POST_TYPE_AD === $post_type ) { |
| 145 | $parent_file = ADVADS_SLUG; |
| 146 | $submenu_file = 'edit.php?post_type=' . Entities::POST_TYPE_AD; |
| 147 | } |
| 148 | // phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited |
| 149 | } |
| 150 | } |
| 151 |