PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br
PHP | 379 lines | 206 code | 59 blank | 114 comment | 25 complexity | 19a5b515f786631067e254a9e3728640 MD5 | raw file
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. use Yoast\WP\SEO\Helpers\Wordpress_Helper;
  8. /**
  9. * Class that holds most of the admin functionality for Yoast SEO.
  10. */
  11. class WPSEO_Admin {
  12. /**
  13. * The page identifier used in WordPress to register the admin page.
  14. *
  15. * !DO NOT CHANGE THIS!
  16. *
  17. * @var string
  18. */
  19. const PAGE_IDENTIFIER = 'wpseo_dashboard';
  20. /**
  21. * Array of classes that add admin functionality.
  22. *
  23. * @var array
  24. */
  25. protected $admin_features;
  26. /**
  27. * Class constructor.
  28. */
  29. public function __construct() {
  30. $integrations = [];
  31. global $pagenow;
  32. $wpseo_menu = new WPSEO_Menu();
  33. $wpseo_menu->register_hooks();
  34. if ( is_multisite() ) {
  35. WPSEO_Options::maybe_set_multisite_defaults( false );
  36. }
  37. if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
  38. add_action( 'created_category', [ $this, 'schedule_rewrite_flush' ] );
  39. add_action( 'edited_category', [ $this, 'schedule_rewrite_flush' ] );
  40. add_action( 'delete_category', [ $this, 'schedule_rewrite_flush' ] );
  41. }
  42. if ( WPSEO_Options::get( 'disable-attachment' ) === true ) {
  43. add_filter( 'wpseo_accessible_post_types', [ 'WPSEO_Post_Type', 'filter_attachment_post_type' ] );
  44. }
  45. add_filter( 'plugin_action_links_' . WPSEO_BASENAME, [ $this, 'add_action_link' ], 10, 2 );
  46. add_filter( 'network_admin_plugin_action_links_' . WPSEO_BASENAME, [ $this, 'add_action_link' ], 10, 2 );
  47. add_action( 'admin_enqueue_scripts', [ $this, 'config_page_scripts' ] );
  48. add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_style' ] );
  49. add_filter( 'user_contactmethods', [ $this, 'update_contactmethods' ], 10, 1 );
  50. add_action( 'after_switch_theme', [ $this, 'switch_theme' ] );
  51. add_action( 'switch_theme', [ $this, 'switch_theme' ] );
  52. add_filter( 'set-screen-option', [ $this, 'save_bulk_edit_options' ], 10, 3 );
  53. add_action( 'admin_init', [ 'WPSEO_Plugin_Conflict', 'hook_check_for_plugin_conflicts' ], 10, 1 );
  54. add_action( 'admin_init', [ $this, 'map_manage_options_cap' ] );
  55. WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'wpseo' );
  56. WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'home' );
  57. if ( YoastSEO()->helpers->current_page->is_yoast_seo_page() ) {
  58. add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
  59. }
  60. $this->set_upsell_notice();
  61. $this->initialize_cornerstone_content();
  62. if ( WPSEO_Utils::is_plugin_network_active() ) {
  63. $integrations[] = new Yoast_Network_Admin();
  64. }
  65. $this->admin_features = [
  66. 'dashboard_widget' => new Yoast_Dashboard_Widget(),
  67. ];
  68. if ( WPSEO_Metabox::is_post_overview( $pagenow ) || WPSEO_Metabox::is_post_edit( $pagenow ) ) {
  69. $this->admin_features['primary_category'] = new WPSEO_Primary_Term_Admin();
  70. }
  71. $integrations[] = new WPSEO_Yoast_Columns();
  72. $integrations[] = new WPSEO_Statistic_Integration();
  73. $integrations[] = new WPSEO_Capability_Manager_Integration( WPSEO_Capability_Manager_Factory::get() );
  74. $integrations[] = new WPSEO_Admin_Gutenberg_Compatibility_Notification();
  75. $integrations[] = new WPSEO_Expose_Shortlinks();
  76. $integrations[] = new WPSEO_MyYoast_Proxy();
  77. $integrations[] = new WPSEO_Schema_Person_Upgrade_Notification();
  78. $integrations[] = new WPSEO_Tracking( 'https://tracking.yoast.com/stats', ( WEEK_IN_SECONDS * 2 ) );
  79. $integrations[] = new WPSEO_Admin_Settings_Changed_Listener();
  80. $integrations = array_merge(
  81. $integrations,
  82. $this->get_admin_features(),
  83. $this->initialize_cornerstone_content()
  84. );
  85. foreach ( $integrations as $integration ) {
  86. $integration->register_hooks();
  87. }
  88. }
  89. /**
  90. * Schedules a rewrite flush to happen at shutdown.
  91. */
  92. public function schedule_rewrite_flush() {
  93. // Bail if this is a multisite installation and the site has been switched.
  94. if ( is_multisite() && ms_is_switched() ) {
  95. return;
  96. }
  97. add_action( 'shutdown', 'flush_rewrite_rules' );
  98. }
  99. /**
  100. * Returns all the classes for the admin features.
  101. *
  102. * @return array
  103. */
  104. public function get_admin_features() {
  105. return $this->admin_features;
  106. }
  107. /**
  108. * Register assets needed on admin pages.
  109. */
  110. public function enqueue_assets() {
  111. if ( filter_input( INPUT_GET, 'page' ) === 'wpseo_licenses' ) {
  112. $asset_manager = new WPSEO_Admin_Asset_Manager();
  113. $asset_manager->enqueue_style( 'extensions' );
  114. }
  115. }
  116. /**
  117. * Returns the manage_options capability.
  118. *
  119. * @return string The capability to use.
  120. */
  121. public function get_manage_options_cap() {
  122. /**
  123. * Filter: 'wpseo_manage_options_capability' - Allow changing the capability users need to view the settings pages.
  124. *
  125. * @api string unsigned The capability.
  126. */
  127. return apply_filters( 'wpseo_manage_options_capability', 'wpseo_manage_options' );
  128. }
  129. /**
  130. * Maps the manage_options cap on saving an options page to wpseo_manage_options.
  131. */
  132. public function map_manage_options_cap() {
  133. // phpcs:ignore WordPress.Security -- The variable is only used in strpos and thus safe to not unslash or sanitize.
  134. $option_page = ! empty( $_POST['option_page'] ) ? $_POST['option_page'] : '';
  135. if ( strpos( $option_page, 'yoast_wpseo' ) === 0 ) {
  136. add_filter( 'option_page_capability_' . $option_page, [ $this, 'get_manage_options_cap' ] );
  137. }
  138. }
  139. /**
  140. * Adds the ability to choose how many posts are displayed per page
  141. * on the bulk edit pages.
  142. */
  143. public function bulk_edit_options() {
  144. $option = 'per_page';
  145. $args = [
  146. 'label' => __( 'Posts', 'wordpress-seo' ),
  147. 'default' => 10,
  148. 'option' => 'wpseo_posts_per_page',
  149. ];
  150. add_screen_option( $option, $args );
  151. }
  152. /**
  153. * Saves the posts per page limit for bulk edit pages.
  154. *
  155. * @param int $status Status value to pass through.
  156. * @param string $option Option name.
  157. * @param int $value Count value to check.
  158. *
  159. * @return int
  160. */
  161. public function save_bulk_edit_options( $status, $option, $value ) {
  162. if ( $option && ( $value > 0 && $value < 1000 ) === 'wpseo_posts_per_page' ) {
  163. return $value;
  164. }
  165. return $status;
  166. }
  167. /**
  168. * Adds links to Premium Support and FAQ under the plugin in the plugin overview page.
  169. *
  170. * @param array $links Array of links for the plugins, adapted when the current plugin is found.
  171. * @param string $file The filename for the current plugin, which the filter loops through.
  172. *
  173. * @return array
  174. */
  175. public function add_action_link( $links, $file ) {
  176. if ( $file === WPSEO_BASENAME && WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
  177. if ( is_network_admin() ) {
  178. $settings_url = network_admin_url( 'admin.php?page=' . self::PAGE_IDENTIFIER );
  179. }
  180. else {
  181. $settings_url = admin_url( 'admin.php?page=' . self::PAGE_IDENTIFIER );
  182. }
  183. $settings_link = '<a href="' . esc_url( $settings_url ) . '">' . __( 'Settings', 'wordpress-seo' ) . '</a>';
  184. array_unshift( $links, $settings_link );
  185. }
  186. // Add link to docs.
  187. $faq_link = '<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/1yc' ) ) . '" target="_blank">' . __( 'FAQ', 'wordpress-seo' ) . '</a>';
  188. array_unshift( $links, $faq_link );
  189. $addon_manager = new WPSEO_Addon_Manager();
  190. if ( YoastSEO()->helpers->product->is_premium() ) {
  191. // Remove Free 'deactivate' link if Premium is active as well. We don't want users to deactivate Free when Premium is active.
  192. unset( $links['deactivate'] );
  193. $no_deactivation_explanation = '<span style="color: #32373c">' . sprintf(
  194. /* translators: %s expands to Yoast SEO Premium. */
  195. __( 'Required by %s', 'wordpress-seo' ),
  196. 'Yoast SEO Premium'
  197. ) . '</span>';
  198. array_unshift( $links, $no_deactivation_explanation );
  199. if ( $addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) {
  200. return $links;
  201. }
  202. // Add link to where premium can be activated.
  203. $activation_link = '<a style="font-weight: bold;" href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/activate-my-yoast' ) ) . '" target="_blank">' . __( 'Activate your subscription', 'wordpress-seo' ) . '</a>';
  204. array_unshift( $links, $activation_link );
  205. return $links;
  206. }
  207. // Add link to premium landing page.
  208. $premium_link = '<a style="font-weight: bold;" href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/1yb' ) ) . '" target="_blank">' . __( 'Get Premium', 'wordpress-seo' ) . '</a>';
  209. array_unshift( $links, $premium_link );
  210. return $links;
  211. }
  212. /**
  213. * Enqueues the (tiny) global JS needed for the plugin.
  214. */
  215. public function config_page_scripts() {
  216. $asset_manager = new WPSEO_Admin_Asset_Manager();
  217. $asset_manager->enqueue_script( 'admin-global' );
  218. $asset_manager->localize_script( 'admin-global', 'wpseoAdminGlobalL10n', $this->localize_admin_global_script() );
  219. }
  220. /**
  221. * Enqueues the (tiny) global stylesheet needed for the plugin.
  222. */
  223. public function enqueue_global_style() {
  224. $asset_manager = new WPSEO_Admin_Asset_Manager();
  225. $asset_manager->enqueue_style( 'admin-global' );
  226. }
  227. /**
  228. * Filter the $contactmethods array and add a set of social profiles.
  229. *
  230. * These are used with the Facebook author, rel="author" and Twitter cards implementation.
  231. *
  232. * @param array $contactmethods Currently set contactmethods.
  233. *
  234. * @return array Contactmethods with added contactmethods.
  235. */
  236. public function update_contactmethods( $contactmethods ) {
  237. $contactmethods['facebook'] = __( 'Facebook profile URL', 'wordpress-seo' );
  238. $contactmethods['instagram'] = __( 'Instagram profile URL', 'wordpress-seo' );
  239. $contactmethods['linkedin'] = __( 'LinkedIn profile URL', 'wordpress-seo' );
  240. $contactmethods['myspace'] = __( 'MySpace profile URL', 'wordpress-seo' );
  241. $contactmethods['pinterest'] = __( 'Pinterest profile URL', 'wordpress-seo' );
  242. $contactmethods['soundcloud'] = __( 'SoundCloud profile URL', 'wordpress-seo' );
  243. $contactmethods['tumblr'] = __( 'Tumblr profile URL', 'wordpress-seo' );
  244. $contactmethods['twitter'] = __( 'Twitter username (without @)', 'wordpress-seo' );
  245. $contactmethods['youtube'] = __( 'YouTube profile URL', 'wordpress-seo' );
  246. $contactmethods['wikipedia'] = __( 'Wikipedia page about you', 'wordpress-seo' ) . '<br/><small>' . __( '(if one exists)', 'wordpress-seo' ) . '</small>';
  247. return $contactmethods;
  248. }
  249. /**
  250. * Log the updated timestamp for user profiles when theme is changed.
  251. */
  252. public function switch_theme() {
  253. $wordpress_helper = new Wordpress_Helper();
  254. $wordpress_version = $wordpress_helper->get_wordpress_version();
  255. // Capability queries were only introduced in WP 5.9.
  256. if ( version_compare( $wordpress_version, '5.8.99', '<' ) ) {
  257. $users = get_users( [ 'who' => 'authors' ] );
  258. }
  259. else {
  260. $users = get_users( [ 'capability' => [ 'edit_posts' ] ] );
  261. }
  262. if ( is_array( $users ) && $users !== [] ) {
  263. foreach ( $users as $user ) {
  264. update_user_meta( $user->ID, '_yoast_wpseo_profile_updated', time() );
  265. }
  266. }
  267. }
  268. /**
  269. * Localization for the dismiss urls.
  270. *
  271. * @return array
  272. */
  273. private function localize_admin_global_script() {
  274. return array_merge(
  275. [
  276. 'isRtl' => is_rtl(),
  277. 'variable_warning' => sprintf(
  278. /* translators: %1$s: '%%term_title%%' variable used in titles and meta's template that's not compatible with the given template, %2$s: expands to 'HelpScout beacon' */
  279. __( 'Warning: the variable %1$s cannot be used in this template. See the %2$s for more info.', 'wordpress-seo' ),
  280. '<code>%s</code>',
  281. 'HelpScout beacon'
  282. ),
  283. /* translators: %s: expends to Yoast SEO */
  284. 'help_video_iframe_title' => sprintf( __( '%s video tutorial', 'wordpress-seo' ), 'Yoast SEO' ),
  285. 'scrollable_table_hint' => __( 'Scroll to see the table content.', 'wordpress-seo' ),
  286. 'wincher_is_logged_in' => WPSEO_Options::get( 'wincher_integration_active', true ) ? YoastSEO()->helpers->wincher->login_status() : false,
  287. ],
  288. YoastSEO()->helpers->wincher->get_admin_global_links()
  289. );
  290. }
  291. /**
  292. * Sets the upsell notice.
  293. */
  294. protected function set_upsell_notice() {
  295. $upsell = new WPSEO_Product_Upsell_Notice();
  296. $upsell->dismiss_notice_listener();
  297. $upsell->initialize();
  298. }
  299. /**
  300. * Whether we are on the admin dashboard page.
  301. *
  302. * @return bool
  303. */
  304. protected function on_dashboard_page() {
  305. return $GLOBALS['pagenow'] === 'index.php';
  306. }
  307. /**
  308. * Loads the cornerstone filter.
  309. *
  310. * @return WPSEO_WordPress_Integration[] The integrations to initialize.
  311. */
  312. protected function initialize_cornerstone_content() {
  313. if ( ! WPSEO_Options::get( 'enable_cornerstone_content' ) ) {
  314. return [];
  315. }
  316. return [
  317. 'cornerstone_filter' => new WPSEO_Cornerstone_Filter(),
  318. ];
  319. }
  320. }