PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/iamgraeme/royalmile
PHP | 452 lines | 202 code | 71 blank | 179 comment | 31 complexity | 08eb89f991c6ebf448053a1049196e2b MD5 | raw file
  1. <?php
  2. /**
  3. * @package WPSEO\Admin
  4. */
  5. /**
  6. * Class WPSEO_Admin_Pages
  7. *
  8. * Class with functionality for the Yoast SEO admin pages.
  9. */
  10. class WPSEO_Admin_Pages {
  11. /**
  12. * @var string $currentoption The option in use for the current admin page.
  13. */
  14. public $currentoption = 'wpseo';
  15. /**
  16. * Holds the asset manager.
  17. *
  18. * @var WPSEO_Admin_Asset_Manager
  19. */
  20. private $asset_manager;
  21. /**
  22. * Class constructor, which basically only hooks the init function on the init hook
  23. */
  24. function __construct() {
  25. add_action( 'init', array( $this, 'init' ), 20 );
  26. $this->asset_manager = new WPSEO_Admin_Asset_Manager();
  27. }
  28. /**
  29. * Make sure the needed scripts are loaded for admin pages
  30. */
  31. function init() {
  32. if ( filter_input( INPUT_GET, 'wpseo_reset_defaults' ) && wp_verify_nonce( filter_input( INPUT_GET, 'nonce' ), 'wpseo_reset_defaults' ) && current_user_can( 'manage_options' ) ) {
  33. WPSEO_Options::reset();
  34. wp_redirect( admin_url( 'admin.php?page=' . WPSEO_Admin::PAGE_IDENTIFIER ) );
  35. }
  36. if ( WPSEO_Utils::grant_access() ) {
  37. add_action( 'admin_enqueue_scripts', array( $this, 'config_page_scripts' ) );
  38. add_action( 'admin_enqueue_scripts', array( $this, 'config_page_styles' ) );
  39. }
  40. }
  41. /**
  42. * Loads the required styles for the config page.
  43. */
  44. function config_page_styles() {
  45. wp_enqueue_style( 'dashboard' );
  46. wp_enqueue_style( 'thickbox' );
  47. wp_enqueue_style( 'global' );
  48. wp_enqueue_style( 'wp-admin' );
  49. $this->asset_manager->enqueue_style( 'select2' );
  50. $this->asset_manager->enqueue_style( 'admin-css' );
  51. $this->asset_manager->enqueue_style( 'kb-search' );
  52. if ( is_rtl() ) {
  53. $this->asset_manager->enqueue_style( 'rtl' );
  54. }
  55. }
  56. /**
  57. * Loads the required scripts for the config page.
  58. */
  59. function config_page_scripts() {
  60. $this->asset_manager->enqueue_script( 'admin-script' );
  61. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-script', 'wpseoAdminL10n', $this->localize_admin_script() );
  62. wp_enqueue_script( 'dashboard' );
  63. wp_enqueue_script( 'thickbox' );
  64. $page = filter_input( INPUT_GET, 'page' );
  65. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-script', 'wpseoSelect2Locale', substr( get_locale(), 0, 2 ) );
  66. if ( in_array( $page, array( 'wpseo_social', WPSEO_Admin::PAGE_IDENTIFIER ) ) ) {
  67. wp_enqueue_media();
  68. $this->asset_manager->enqueue_script( 'admin-media' );
  69. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-media', 'wpseoMediaL10n', $this->localize_media_script() );
  70. }
  71. if ( 'wpseo_tools' === $page ) {
  72. $this->enqueue_tools_scripts();
  73. }
  74. }
  75. /**
  76. * Pass some variables to js for upload module.
  77. *
  78. * @return array
  79. */
  80. public function localize_media_script() {
  81. return array(
  82. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
  83. );
  84. }
  85. /**
  86. * Pass some variables to js for the admin JS module.
  87. *
  88. * %s is replaced with <code>%s</code> and replaced again in the javascript with the actual variable.
  89. *
  90. * @return array
  91. */
  92. public function localize_admin_script() {
  93. return array(
  94. /* translators: %s: '%%term_title%%' variable used in titles and meta's template that's not compatible with the given template */
  95. 'variable_warning' => sprintf( __( 'Warning: the variable %s cannot be used in this template.', 'wordpress-seo' ), '<code>%s</code>' ) . ' ' . __( 'See the help tab for more info.', 'wordpress-seo' ),
  96. 'locale' => get_locale(),
  97. 'kb_no_results' => __( 'No results found.', 'wordpress-seo' ),
  98. 'kb_heading' => __( 'Search the Yoast knowledge base', 'wordpress-seo' ),
  99. 'kb_error_message' => __( 'Something went wrong. Please try again later.', 'wordpress-seo' ),
  100. 'kb_loading_placeholder' => __( 'Loading...', 'wordpress-seo' ),
  101. 'kb_search' => __( 'search', 'wordpress-seo' ),
  102. 'kb_back' => __( 'Back', 'wordpress-seo' ),
  103. 'kb_open' => __( 'Open', 'wordpress-seo' ),
  104. );
  105. }
  106. /**
  107. * Enqueues and handles all the tool dependencies.
  108. */
  109. private function enqueue_tools_scripts() {
  110. $tool = filter_input( INPUT_GET, 'tool' );
  111. if ( empty( $tool ) ) {
  112. $this->asset_manager->enqueue_script( 'yoast-seo' );
  113. }
  114. if ( 'bulk-editor' === $tool ) {
  115. $this->asset_manager->enqueue_script( 'bulk-editor' );
  116. }
  117. if ( 'import-export' === $tool && filter_input( INPUT_POST, WPSEO_Export::NONCE_NAME ) !== null ) {
  118. $this->do_yoast_export();
  119. }
  120. }
  121. /**
  122. * Runs the yoast exporter class to possibly init the file download.
  123. */
  124. private function do_yoast_export() {
  125. check_admin_referer( WPSEO_Export::NONCE_ACTION, WPSEO_Export::NONCE_NAME );
  126. if ( ! current_user_can( 'manage_options' ) ) {
  127. return;
  128. }
  129. $wpseo_post = filter_input( INPUT_POST, 'wpseo' );
  130. $include_taxonomy = ! empty( $wpseo_post['include_taxonomy'] );
  131. $export = new WPSEO_Export( $include_taxonomy );
  132. if ( $export->has_error() ) {
  133. add_action( 'admin_notices', array( $export, 'set_error_hook' ) );
  134. }
  135. }
  136. /********************** DEPRECATED METHODS **********************/
  137. /**
  138. * Exports the current site's Yoast SEO settings.
  139. *
  140. * @param bool $include_taxonomy Whether to include the taxonomy metadata the plugin creates.
  141. *
  142. * @return bool|string $return False when failed, the URL to the export file when succeeded.
  143. */
  144. public function export_settings( $include_taxonomy ) {
  145. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>WPSEO_Export</code> class.' );
  146. $export = new WPSEO_Export( $include_taxonomy );
  147. if ( $export->success ) {
  148. return $export->export_zip_url;
  149. }
  150. return false;
  151. }
  152. /**
  153. * Generates the header for admin pages
  154. *
  155. * @deprecated 2.0
  156. *
  157. * @param bool $form Whether or not the form start tag should be included.
  158. * @param mixed $option_long_name The long name of the option to use for the current page.
  159. * @param string $option The short name of the option to use for the current page.
  160. * @param bool $contains_files Whether the form should allow for file uploads.
  161. */
  162. public function admin_header( $form = true, $option_long_name = false, $option = 'wpseo', $contains_files = false ) {
  163. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  164. Yoast_Form::get_instance()->admin_header( $form, $option, $contains_files, $option_long_name );
  165. }
  166. /**
  167. * Generates the footer for admin pages
  168. *
  169. * @deprecated 2.0
  170. *
  171. * @param bool $submit Whether or not a submit button and form end tag should be shown.
  172. * @param bool $show_sidebar Whether or not to show the banner sidebar - used by premium plugins to disable it.
  173. */
  174. public function admin_footer( $submit = true, $show_sidebar = true ) {
  175. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  176. Yoast_Form::get_instance()->admin_footer( $submit, $show_sidebar );
  177. }
  178. /**
  179. * Generates the sidebar for admin pages.
  180. *
  181. * @deprecated 2.0
  182. */
  183. public function admin_sidebar() {
  184. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  185. Yoast_Form::get_instance()->admin_sidebar();
  186. }
  187. /**
  188. * Create a Checkbox input field.
  189. *
  190. * @deprecated 2.0
  191. *
  192. * @param string $var The variable within the option to create the checkbox for.
  193. * @param string $label The label to show for the variable.
  194. * @param bool $label_left Whether the label should be left (true) or right (false).
  195. * @param string $option The option the variable belongs to.
  196. */
  197. public function checkbox( $var, $label, $label_left = false, $option = '' ) {
  198. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  199. if ( $option !== '' ) {
  200. Yoast_Form::get_instance()->set_option( $option );
  201. }
  202. Yoast_Form::get_instance()->checkbox( $var, $label, $label_left );
  203. }
  204. /**
  205. * Create a Text input field.
  206. *
  207. * @deprecated 2.0
  208. *
  209. * @param string $var The variable within the option to create the text input field for.
  210. * @param string $label The label to show for the variable.
  211. * @param string $option The option the variable belongs to.
  212. */
  213. function textinput( $var, $label, $option = '' ) {
  214. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  215. if ( $option !== '' ) {
  216. Yoast_Form::get_instance()->set_option( $option );
  217. }
  218. Yoast_Form::get_instance()->textinput( $var, $label );
  219. }
  220. /**
  221. * Create a textarea.
  222. *
  223. * @deprecated 2.0
  224. *
  225. * @param string $var The variable within the option to create the textarea for.
  226. * @param string $label The label to show for the variable.
  227. * @param string $option The option the variable belongs to.
  228. * @param array $attr The CSS class to assign to the textarea.
  229. */
  230. function textarea( $var, $label, $option = '', $attr = array() ) {
  231. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  232. if ( $option !== '' ) {
  233. Yoast_Form::get_instance()->set_option( $option );
  234. }
  235. Yoast_Form::get_instance()->textarea( $var, $label, $attr );
  236. }
  237. /**
  238. * Create a hidden input field.
  239. *
  240. * @deprecated 2.0
  241. *
  242. * @param string $var The variable within the option to create the hidden input for.
  243. * @param string $option The option the variable belongs to.
  244. */
  245. function hidden( $var, $option = '' ) {
  246. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  247. if ( $option !== '' ) {
  248. Yoast_Form::get_instance()->set_option( $option );
  249. }
  250. Yoast_Form::get_instance()->hidden( $var );
  251. }
  252. /**
  253. * Create a Select Box.
  254. *
  255. * @deprecated 2.0
  256. *
  257. * @param string $var The variable within the option to create the select for.
  258. * @param string $label The label to show for the variable.
  259. * @param array $values The select options to choose from.
  260. * @param string $option The option the variable belongs to.
  261. */
  262. function select( $var, $label, $values, $option = '' ) {
  263. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  264. if ( $option !== '' ) {
  265. Yoast_Form::get_instance()->set_option( $option );
  266. }
  267. Yoast_Form::get_instance()->select( $var, $label, $values );
  268. }
  269. /**
  270. * Create a File upload field.
  271. *
  272. * @deprecated 2.0
  273. *
  274. * @param string $var The variable within the option to create the file upload field for.
  275. * @param string $label The label to show for the variable.
  276. * @param string $option The option the variable belongs to.
  277. */
  278. function file_upload( $var, $label, $option = '' ) {
  279. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  280. if ( $option !== '' ) {
  281. Yoast_Form::get_instance()->set_option( $option );
  282. }
  283. Yoast_Form::get_instance()->file_upload( $var, $label );
  284. }
  285. /**
  286. * Media input
  287. *
  288. * @deprecated 2.0
  289. *
  290. * @param string $var Option name.
  291. * @param string $label Label message.
  292. * @param string $option Optional option key.
  293. */
  294. function media_input( $var, $label, $option = '' ) {
  295. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  296. if ( $option !== '' ) {
  297. Yoast_Form::get_instance()->set_option( $option );
  298. }
  299. Yoast_Form::get_instance()->media_input( $var, $label );
  300. }
  301. /**
  302. * Create a Radio input field.
  303. *
  304. * @deprecated 2.0
  305. *
  306. * @param string $var The variable within the option to create the file upload field for.
  307. * @param array $values The radio options to choose from.
  308. * @param string $label The label to show for the variable.
  309. * @param string $option The option the variable belongs to.
  310. */
  311. function radio( $var, $values, $label, $option = '' ) {
  312. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  313. if ( $option !== '' ) {
  314. Yoast_Form::get_instance()->set_option( $option );
  315. }
  316. Yoast_Form::get_instance()->radio( $var, $values, $label );
  317. }
  318. /**
  319. * Create a postbox widget.
  320. *
  321. * @deprecated 2.0
  322. *
  323. * @param string $id ID of the postbox.
  324. * @param string $title Title of the postbox.
  325. * @param string $content Content of the postbox.
  326. */
  327. function postbox( $id, $title, $content ) {
  328. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please re-implement the admin pages.' );
  329. ?>
  330. <div id="<?php echo esc_attr( $id ); ?>" class="yoastbox">
  331. <h1><?php echo $title; ?></h1>
  332. <?php echo $content; ?>
  333. </div>
  334. <?php
  335. }
  336. /**
  337. * Create a form table from an array of rows.
  338. *
  339. * @deprecated 2.0
  340. *
  341. * @param array $rows Rows to include in the table.
  342. *
  343. * @return string
  344. */
  345. function form_table( $rows ) {
  346. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please re-implement the admin pages.' );
  347. if ( ! is_array( $rows ) || $rows === array() ) {
  348. return '';
  349. }
  350. $content = '<table class="form-table">';
  351. foreach ( $rows as $row ) {
  352. $content .= '<tr><th scope="row">';
  353. if ( isset( $row['id'] ) && $row['id'] != '' ) {
  354. $content .= '<label for="' . esc_attr( $row['id'] ) . '">' . esc_html( $row['label'] ) . ':</label>';
  355. }
  356. else {
  357. $content .= esc_html( $row['label'] );
  358. }
  359. if ( isset( $row['desc'] ) && $row['desc'] != '' ) {
  360. $content .= '<br/><small>' . esc_html( $row['desc'] ) . '</small>';
  361. }
  362. $content .= '</th><td>';
  363. $content .= $row['content'];
  364. $content .= '</td></tr>';
  365. }
  366. $content .= '</table>';
  367. return $content;
  368. }
  369. /**
  370. * Resets the site to the default Yoast SEO settings and runs a title test to check
  371. * whether force rewrite needs to be on.
  372. *
  373. * @deprecated 1.5.0
  374. * @deprecated use WPSEO_Options::reset()
  375. * @see WPSEO_Options::reset()
  376. */
  377. function reset_defaults() {
  378. _deprecated_function( __METHOD__, 'WPSEO 1.5.0', 'WPSEO_Options::reset()' );
  379. WPSEO_Options::reset();
  380. }
  381. } /* End of class */