WPSEO_Admin_Pages

Class WPSEO_Admin_Pages.

Defined (1)

The class is defined in the following location(s).

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