PageRenderTime 16ms CodeModel.GetById 8ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/menu/class-admin-menu.php

https://bitbucket.org/carloskikea/helpet
PHP | 272 lines | 126 code | 35 blank | 111 comment | 16 complexity | ef6ce60f98cfe65ef1281d5d3135ff05 MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin\Menu
  6 */
  7
  8/**
  9 * Registers the admin menu on the left of the admin area.
 10 */
 11class WPSEO_Admin_Menu implements WPSEO_WordPress_Integration {
 12	/** @var WPSEO_Menu Menu */
 13	protected $menu;
 14
 15	/**
 16	 * Constructs the Admin Menu.
 17	 *
 18	 * @param WPSEO_Menu $menu Menu to use.
 19	 */
 20	public function __construct( WPSEO_Menu $menu ) {
 21		$this->menu = $menu;
 22	}
 23
 24	/**
 25	 * Registers all hooks to WordPress.
 26	 *
 27	 * @return void
 28	 */
 29	public function register_hooks() {
 30		// Needs the lower than default priority so other plugins can hook underneath it without issue.
 31		add_action( 'admin_menu', array( $this, 'register_settings_page' ), 5 );
 32	}
 33
 34	/**
 35	 * Registers the menu item submenus.
 36	 */
 37	public function register_settings_page() {
 38		$can_manage_options = WPSEO_Capability_Utils::current_user_can( $this->get_manage_capability() );
 39
 40		if ( $can_manage_options ) {
 41			/*
 42			 * The current user has the capability to control anything.
 43			 * This means that all submenus and dashboard can be shown.
 44			 */
 45			global $admin_page_hooks;
 46
 47			add_menu_page(
 48				'Yoast SEO: ' . __( 'Dashboard', 'wordpress-seo' ),
 49				__( 'SEO', 'wordpress-seo' ) . ' ' . $this->get_notification_counter(),
 50				$this->get_manage_capability(),
 51				$this->menu->get_page_identifier(),
 52				$this->get_admin_page_callback(),
 53				WPSEO_Utils::get_icon_svg(),
 54				'99.31337'
 55			);
 56
 57			$admin_page_hooks[ $this->menu->get_page_identifier() ] = 'seo'; // Wipe notification bits from hooks. R.
 58		}
 59
 60		// Get all submenu pages.
 61		$submenu_pages = $this->get_submenu_pages();
 62
 63		// Add submenu items to the main menu if possible.
 64		if ( $can_manage_options ) {
 65			$this->register_submenu_pages( $submenu_pages );
 66		}
 67
 68		/*
 69		 * If the user does not have the general manage options capability,
 70		 * we need to make sure the desired sub-item can be reached.
 71		 */
 72		if ( ! $can_manage_options ) {
 73			$this->register_menu_pages( $submenu_pages );
 74		}
 75	}
 76
 77	/**
 78	 * Registers submenu pages as menu pages.
 79	 *
 80	 * @param array $submenu_pages List of submenu pages.
 81	 */
 82	protected function register_menu_pages( $submenu_pages ) {
 83		if ( ! is_array( $submenu_pages ) || $submenu_pages === array() ) {
 84			return;
 85		}
 86
 87		// Loop through submenu pages and add them.
 88		foreach ( $submenu_pages as $submenu_page ) {
 89			if ( $submenu_page[3] === $this->get_manage_capability() ) {
 90				continue;
 91			}
 92
 93			// Register submenu as menu page.
 94			add_menu_page(
 95				'Yoast SEO: ' . $submenu_page[2],
 96				$submenu_page[2],
 97				$submenu_page[3],
 98				$submenu_page[4],
 99				$submenu_page[5],
100				WPSEO_Utils::get_icon_svg(),
101				'99.31337'
102			);
103		}
104	}
105
106	/**
107	 * Returns the list of registered submenu pages.
108	 *
109	 * @return array List of registered submenu pages.
110	 */
111	protected function get_submenu_pages() {
112		global $wpseo_admin;
113
114		/** WPSEO_Admin $wpseo_admin */
115		$admin_features = $wpseo_admin->get_admin_features();
116
117		// Submenu pages.
118		$submenu_pages = array(
119			$this->get_submenu_page( __( 'General', 'wordpress-seo' ), $this->menu->get_page_identifier() ),
120			$this->get_submenu_page( __( 'Search Appearance', 'wordpress-seo' ), 'wpseo_titles' ),
121			$this->get_submenu_page(
122				__( 'Search Console', 'wordpress-seo' ),
123				'wpseo_search_console',
124				array( $admin_features['google_search_console'], 'display' ),
125				array( array( $admin_features['google_search_console'], 'set_help' ) )
126			),
127			$this->get_submenu_page( __( 'Social', 'wordpress-seo' ), 'wpseo_social' ),
128			$this->get_submenu_page( __( 'Tools', 'wordpress-seo' ), 'wpseo_tools' ),
129			$this->get_submenu_page( $this->get_license_page_title(), 'wpseo_licenses' ),
130		);
131
132		/**
133		 * Filter: 'wpseo_submenu_pages' - Collects all submenus that need to be shown.
134		 *
135		 * @api array $submenu_pages List with all submenu pages.
136		 */
137		return (array) apply_filters( 'wpseo_submenu_pages', $submenu_pages );
138	}
139
140	/**
141	 * Creates a submenu formatted array.
142	 *
143	 * @param string     $page_title Page title to use.
144	 * @param string     $page_slug  Page slug to use.
145	 * @param callable   $callback   Optional. Callback which handles the page request.
146	 * @param callable[] $hook       Optional. Hook to trigger when the page is registered.
147	 *
148	 * @return array Formatted submenu.
149	 */
150	protected function get_submenu_page( $page_title, $page_slug, $callback = null, $hook = null ) {
151		if ( $callback === null ) {
152			$callback = $this->get_admin_page_callback();
153		}
154
155		return array(
156			$this->menu->get_page_identifier(),
157			'',
158			$page_title,
159			$this->get_manage_capability(),
160			$page_slug,
161			$callback,
162			$hook,
163		);
164	}
165
166	/**
167	 * Registers the submenu pages.
168	 *
169	 * This is only done when the user has the `wpseo_manage_options` capability,
170	 * thus all capabilities can be set to this capability.
171	 *
172	 * @param array $submenu_pages List of submenu pages to register.
173	 *
174	 * @return void
175	 */
176	protected function register_submenu_pages( $submenu_pages ) {
177		if ( ! is_array( $submenu_pages ) || $submenu_pages === array() ) {
178			return;
179		}
180
181		// Loop through submenu pages and add them.
182		foreach ( $submenu_pages as $submenu_page ) {
183			$page_title = $submenu_page[2];
184
185			// We cannot use $submenu_page[1] because add-ons define that, so hard-code this value.
186			if ( $submenu_page[4] === 'wpseo_licenses' ) {
187				$page_title = $this->get_license_page_title();
188			}
189
190			$page_title .= ' - Yoast SEO';
191
192			/*
193			 * Add submenu page.
194			 *
195			 * If we don't register this on `wpseo_manage_options`, admin users with only this capability
196			 * will not be able to see the submenus which are configured with something else,
197			 * thus all submenu pages are registered with the `wpseo_manage_options` capability here.
198			 */
199			$admin_page = add_submenu_page( $submenu_page[0], $page_title, $submenu_page[2], $this->get_manage_capability(), $submenu_page[4], $submenu_page[5] );
200
201			// Check if we need to hook.
202			if ( isset( $submenu_page[6] ) && ( is_array( $submenu_page[6] ) && $submenu_page[6] !== array() ) ) {
203				foreach ( $submenu_page[6] as $submenu_page_action ) {
204					add_action( 'load-' . $admin_page, $submenu_page_action );
205				}
206			}
207		}
208
209		// Use WordPress global $submenu to directly access it's properties.
210		global $submenu;
211		if ( isset( $submenu[ $this->menu->get_page_identifier() ] ) && WPSEO_Capability_Utils::current_user_can( $this->get_manage_capability() ) ) {
212			$submenu[ $this->menu->get_page_identifier() ][0][0] = __( 'General', 'wordpress-seo' );
213		}
214	}
215
216	/**
217	 * Returns the notification count in HTML format.
218	 *
219	 * @return string The notification count in HTML format.
220	 */
221	protected function get_notification_counter() {
222		$notification_center = Yoast_Notification_Center::get();
223		$notification_count  = $notification_center->get_notification_count();
224
225		// Add main page.
226		/* translators: %s: number of notifications */
227		$notifications = sprintf( _n( '%s notification', '%s notifications', $notification_count, 'wordpress-seo' ), number_format_i18n( $notification_count ) );
228
229		$counter = sprintf( '<span class="update-plugins count-%1$d"><span class="plugin-count" aria-hidden="true">%1$d</span><span class="screen-reader-text">%2$s</span></span>', $notification_count, $notifications );
230
231		return $counter;
232	}
233
234	/**
235	 * Returns the capability that is required to manage all options.
236	 *
237	 * @return string Capability to check against.
238	 */
239	protected function get_manage_capability() {
240		/**
241		 * Filter: 'wpseo_manage_options_capability' - Allow changing the capability users need to view the settings pages
242		 *
243		 * @deprecated 5.5
244		 * @api string unsigned The capability
245		 */
246		return apply_filters_deprecated( 'wpseo_manage_options_capability', array( 'wpseo_manage_options' ), 'WPSEO 5.5.0', false, 'Use the introduced wpseo_manage_options capability instead.' );
247	}
248
249	/**
250	 * Returns the page handler callback.
251	 *
252	 * @return array Callback page handler.
253	 */
254	protected function get_admin_page_callback() {
255		return array( $this->menu, 'load_page' );
256	}
257
258	/**
259	 * Returns the page title to use for the licenses page.
260	 *
261	 * @return string The title for the license page.
262	 */
263	protected function get_license_page_title() {
264		static $title = null;
265
266		if ( $title === null ) {
267			$title = __( 'Premium', 'wordpress-seo' );
268		}
269
270		return $title;
271	}
272}