PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/ngochuynh1991/cuacuon
PHP | 414 lines | 179 code | 62 blank | 173 comment | 31 complexity | 42dd7a35721eb19078eefa3070d75335 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_dashboard' ) );
  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. if ( is_rtl() ) {
  52. $this->asset_manager->enqueue_style( 'rtl' );
  53. }
  54. }
  55. /**
  56. * Loads the required scripts for the config page.
  57. */
  58. function config_page_scripts() {
  59. $this->asset_manager->enqueue_script( 'admin-script' );
  60. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'admin-script', 'wpseoAdminL10n', $this->localize_admin_script() );
  61. wp_enqueue_script( 'dashboard' );
  62. wp_enqueue_script( 'thickbox' );
  63. $page = filter_input( INPUT_GET, 'page' );
  64. $tool = filter_input( INPUT_GET, 'tool' );
  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_dashboard' ) ) ) {
  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 && empty( $tool ) ) {
  72. $this->asset_manager->enqueue_script( 'yoast-seo' );
  73. }
  74. if ( 'wpseo_tools' === $page && 'bulk-editor' === $tool ) {
  75. $this->asset_manager->enqueue_script( 'bulk-editor' );
  76. }
  77. if ( 'wpseo_tools' === $page && 'import-export' === $tool ) {
  78. $this->asset_manager->enqueue_script( 'export' );
  79. }
  80. }
  81. /**
  82. * Pass some variables to js for upload module.
  83. *
  84. * @return array
  85. */
  86. public function localize_media_script() {
  87. return array(
  88. 'choose_image' => __( 'Use Image', 'wordpress-seo' ),
  89. );
  90. }
  91. /**
  92. * Pass some variables to js for the admin JS module.
  93. *
  94. * %s is replaced with <code>%s</code> and replaced again in the javascript with the actual variable.
  95. *
  96. * @return array
  97. */
  98. public function localize_admin_script() {
  99. return array(
  100. /* translators: %s: '%%term_title%%' variable used in titles and meta's template that's not compatible with the given template */
  101. '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' ),
  102. 'locale' => get_locale(),
  103. );
  104. }
  105. /********************** DEPRECATED METHODS **********************/
  106. /**
  107. * Exports the current site's Yoast SEO settings.
  108. *
  109. * @param bool $include_taxonomy Whether to include the taxonomy metadata the plugin creates.
  110. *
  111. * @return bool|string $return False when failed, the URL to the export file when succeeded.
  112. */
  113. public function export_settings( $include_taxonomy ) {
  114. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>WPSEO_Export</code> class.' );
  115. $export = new WPSEO_Export( $include_taxonomy );
  116. if ( $export->success ) {
  117. return $export->export_zip_url;
  118. }
  119. else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * Generates the header for admin pages
  125. *
  126. * @deprecated 2.0
  127. *
  128. * @param bool $form Whether or not the form start tag should be included.
  129. * @param mixed $option_long_name The long name of the option to use for the current page.
  130. * @param string $option The short name of the option to use for the current page.
  131. * @param bool $contains_files Whether the form should allow for file uploads.
  132. */
  133. public function admin_header( $form = true, $option_long_name = false, $option = 'wpseo', $contains_files = false ) {
  134. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  135. Yoast_Form::get_instance()->admin_header( $form, $option, $contains_files, $option_long_name );
  136. }
  137. /**
  138. * Generates the footer for admin pages
  139. *
  140. * @deprecated 2.0
  141. *
  142. * @param bool $submit Whether or not a submit button and form end tag should be shown.
  143. * @param bool $show_sidebar Whether or not to show the banner sidebar - used by premium plugins to disable it.
  144. */
  145. public function admin_footer( $submit = true, $show_sidebar = true ) {
  146. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  147. Yoast_Form::get_instance()->admin_footer( $submit, $show_sidebar );
  148. }
  149. /**
  150. * Generates the sidebar for admin pages.
  151. *
  152. * @deprecated 2.0
  153. */
  154. public function admin_sidebar() {
  155. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  156. Yoast_Form::get_instance()->admin_sidebar();
  157. }
  158. /**
  159. * Create a Checkbox input field.
  160. *
  161. * @deprecated 2.0
  162. *
  163. * @param string $var The variable within the option to create the checkbox for.
  164. * @param string $label The label to show for the variable.
  165. * @param bool $label_left Whether the label should be left (true) or right (false).
  166. * @param string $option The option the variable belongs to.
  167. */
  168. public function checkbox( $var, $label, $label_left = false, $option = '' ) {
  169. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  170. if ( $option !== '' ) {
  171. Yoast_Form::get_instance()->set_option( $option );
  172. }
  173. Yoast_Form::get_instance()->checkbox( $var, $label, $label_left );
  174. }
  175. /**
  176. * Create a Text input field.
  177. *
  178. * @deprecated 2.0
  179. *
  180. * @param string $var The variable within the option to create the text input field for.
  181. * @param string $label The label to show for the variable.
  182. * @param string $option The option the variable belongs to.
  183. */
  184. function textinput( $var, $label, $option = '' ) {
  185. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  186. if ( $option !== '' ) {
  187. Yoast_Form::get_instance()->set_option( $option );
  188. }
  189. Yoast_Form::get_instance()->textinput( $var, $label );
  190. }
  191. /**
  192. * Create a textarea.
  193. *
  194. * @deprecated 2.0
  195. *
  196. * @param string $var The variable within the option to create the textarea for.
  197. * @param string $label The label to show for the variable.
  198. * @param string $option The option the variable belongs to.
  199. * @param array $attr The CSS class to assign to the textarea.
  200. */
  201. function textarea( $var, $label, $option = '', $attr = array() ) {
  202. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  203. if ( $option !== '' ) {
  204. Yoast_Form::get_instance()->set_option( $option );
  205. }
  206. Yoast_Form::get_instance()->textarea( $var, $label, $attr );
  207. }
  208. /**
  209. * Create a hidden input field.
  210. *
  211. * @deprecated 2.0
  212. *
  213. * @param string $var The variable within the option to create the hidden input for.
  214. * @param string $option The option the variable belongs to.
  215. */
  216. function hidden( $var, $option = '' ) {
  217. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  218. if ( $option !== '' ) {
  219. Yoast_Form::get_instance()->set_option( $option );
  220. }
  221. Yoast_Form::get_instance()->hidden( $var );
  222. }
  223. /**
  224. * Create a Select Box.
  225. *
  226. * @deprecated 2.0
  227. *
  228. * @param string $var The variable within the option to create the select for.
  229. * @param string $label The label to show for the variable.
  230. * @param array $values The select options to choose from.
  231. * @param string $option The option the variable belongs to.
  232. */
  233. function select( $var, $label, $values, $option = '' ) {
  234. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  235. if ( $option !== '' ) {
  236. Yoast_Form::get_instance()->set_option( $option );
  237. }
  238. Yoast_Form::get_instance()->select( $var, $label, $values );
  239. }
  240. /**
  241. * Create a File upload field.
  242. *
  243. * @deprecated 2.0
  244. *
  245. * @param string $var The variable within the option to create the file upload field for.
  246. * @param string $label The label to show for the variable.
  247. * @param string $option The option the variable belongs to.
  248. */
  249. function file_upload( $var, $label, $option = '' ) {
  250. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  251. if ( $option !== '' ) {
  252. Yoast_Form::get_instance()->set_option( $option );
  253. }
  254. Yoast_Form::get_instance()->file_upload( $var, $label );
  255. }
  256. /**
  257. * Media input
  258. *
  259. * @deprecated 2.0
  260. *
  261. * @param string $var Option name.
  262. * @param string $label Label message.
  263. * @param string $option Optional option key.
  264. */
  265. function media_input( $var, $label, $option = '' ) {
  266. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  267. if ( $option !== '' ) {
  268. Yoast_Form::get_instance()->set_option( $option );
  269. }
  270. Yoast_Form::get_instance()->media_input( $var, $label );
  271. }
  272. /**
  273. * Create a Radio input field.
  274. *
  275. * @deprecated 2.0
  276. *
  277. * @param string $var The variable within the option to create the file upload field for.
  278. * @param array $values The radio options to choose from.
  279. * @param string $label The label to show for the variable.
  280. * @param string $option The option the variable belongs to.
  281. */
  282. function radio( $var, $values, $label, $option = '' ) {
  283. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please use the <code>Yoast_Form</code> class.' );
  284. if ( $option !== '' ) {
  285. Yoast_Form::get_instance()->set_option( $option );
  286. }
  287. Yoast_Form::get_instance()->radio( $var, $values, $label );
  288. }
  289. /**
  290. * Create a postbox widget.
  291. *
  292. * @deprecated 2.0
  293. *
  294. * @param string $id ID of the postbox.
  295. * @param string $title Title of the postbox.
  296. * @param string $content Content of the postbox.
  297. */
  298. function postbox( $id, $title, $content ) {
  299. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please re-implement the admin pages.' );
  300. ?>
  301. <div id="<?php echo esc_attr( $id ); ?>" class="yoastbox">
  302. <h1><?php echo $title; ?></h1>
  303. <?php echo $content; ?>
  304. </div>
  305. <?php
  306. }
  307. /**
  308. * Create a form table from an array of rows.
  309. *
  310. * @deprecated 2.0
  311. *
  312. * @param array $rows Rows to include in the table.
  313. *
  314. * @return string
  315. */
  316. function form_table( $rows ) {
  317. _deprecated_function( __METHOD__, 'WPSEO 2.0', 'This method is deprecated, please re-implement the admin pages.' );
  318. if ( ! is_array( $rows ) || $rows === array() ) {
  319. return '';
  320. }
  321. $content = '<table class="form-table">';
  322. foreach ( $rows as $row ) {
  323. $content .= '<tr><th scope="row">';
  324. if ( isset( $row['id'] ) && $row['id'] != '' ) {
  325. $content .= '<label for="' . esc_attr( $row['id'] ) . '">' . esc_html( $row['label'] ) . ':</label>';
  326. }
  327. else {
  328. $content .= esc_html( $row['label'] );
  329. }
  330. if ( isset( $row['desc'] ) && $row['desc'] != '' ) {
  331. $content .= '<br/><small>' . esc_html( $row['desc'] ) . '</small>';
  332. }
  333. $content .= '</th><td>';
  334. $content .= $row['content'];
  335. $content .= '</td></tr>';
  336. }
  337. $content .= '</table>';
  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. *
  344. * @deprecated 1.5.0
  345. * @deprecated use WPSEO_Options::reset()
  346. * @see WPSEO_Options::reset()
  347. */
  348. function reset_defaults() {
  349. _deprecated_function( __METHOD__, 'WPSEO 1.5.0', 'WPSEO_Options::reset()' );
  350. WPSEO_Options::reset();
  351. }
  352. } /* End of class */