PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/classes/admin/settings/class-render.php

https://gitlab.com/BenjaminHolm/wp-rocket
PHP | 588 lines | 250 code | 66 blank | 272 comment | 18 complexity | 6be2adf950c3dc8fe2544cc39ce5e17d MD5 | raw file
  1. <?php
  2. namespace WP_Rocket\Admin\Settings;
  3. use WP_Rocket\Abstract_Render;
  4. defined( 'ABSPATH' ) || die( 'Cheatin&#8217; uh?' );
  5. /**
  6. * Handle rendering of HTML content for the settings page.
  7. *
  8. * @since 3.0
  9. * @author Remy Perona
  10. */
  11. class Render extends Abstract_render {
  12. /**
  13. * Settings array
  14. *
  15. * @since 3.0
  16. * @author Remy Perona
  17. *
  18. * @var array
  19. */
  20. private $settings = array();
  21. /**
  22. * Hidden settings array
  23. *
  24. * @since 3.0
  25. * @author Remy Perona
  26. *
  27. * @var array
  28. */
  29. private $hidden_settings;
  30. /**
  31. * Sets the settings value
  32. *
  33. * @since 3.0
  34. * @author Remy Perona
  35. *
  36. * @param array $settings Array of settings.
  37. * @return void
  38. */
  39. public function set_settings( $settings ) {
  40. $this->settings = (array) $settings;
  41. }
  42. /**
  43. * Sets the hidden settings value
  44. *
  45. * @since 3.0
  46. * @author Remy Perona
  47. *
  48. * @param array $hidden_settings Array of hidden settings.
  49. * @return void
  50. */
  51. public function set_hidden_settings( $hidden_settings ) {
  52. $this->hidden_settings = $hidden_settings;
  53. }
  54. /**
  55. * Renders the page sections navigation
  56. *
  57. * @since 3.0
  58. * @author Remy Perona
  59. *
  60. * @return void
  61. */
  62. public function render_navigation() {
  63. /**
  64. * Filters WP Rocket settings page navigation items.
  65. *
  66. * @since 3.0
  67. * @author Remy Perona
  68. *
  69. * @param array $navigation {
  70. * Items to populate the navigation.
  71. *
  72. * @type string $id Page section identifier.
  73. * @type string $title Menu item title.
  74. * @type string $menu_description Menu item description.
  75. * @type string $class Menu item classes
  76. * }
  77. */
  78. $navigation = apply_filters( 'rocket_settings_menu_navigation', $this->settings );
  79. $default = [
  80. 'id' => '',
  81. 'title' => '',
  82. 'menu_description' => '',
  83. 'class' => '',
  84. ];
  85. $navigation = array_map(
  86. function( array $item ) use ( $default ) {
  87. $item = wp_parse_args( $item, $default );
  88. if ( ! empty( $item['class'] ) ) {
  89. $item['class'] = implode( ' ', array_map( 'sanitize_html_class', $item['class'] ) );
  90. }
  91. unset( $item['sections'] );
  92. return $item;
  93. },
  94. $navigation
  95. );
  96. echo $this->generate( 'navigation', $navigation );
  97. }
  98. /**
  99. * Render the page sections.
  100. *
  101. * @since 3.0
  102. * @author Remy Perona
  103. */
  104. public function render_form_sections() {
  105. if ( ! isset( $this->settings ) ) {
  106. return;
  107. }
  108. foreach ( $this->settings as $id => $args ) {
  109. $default = [
  110. 'title' => '',
  111. 'menu_description' => '',
  112. 'class' => '',
  113. ];
  114. $args = wp_parse_args( $args, $default );
  115. $id = str_replace( '_', '-', $id );
  116. echo $this->generate( 'page-sections/' . $id, $args );
  117. }
  118. }
  119. /**
  120. * Render the Imagify page section.
  121. *
  122. * @since 3.2
  123. * @author Remy Perona
  124. */
  125. public function render_imagify_section() {
  126. echo $this->generate( 'page-sections/imagify' );
  127. }
  128. /**
  129. * Render the tools page section.
  130. *
  131. * @since 3.0
  132. * @author Remy Perona
  133. */
  134. public function render_tools_section() {
  135. echo $this->generate( 'page-sections/tools' );
  136. }
  137. /**
  138. * Renders the settings sections for a page section.
  139. *
  140. * @since 3.0
  141. * @author Remy Perona
  142. *
  143. * @param string $page Page section identifier.
  144. * @return void
  145. */
  146. public function render_settings_sections( $page ) {
  147. if ( ! isset( $this->settings[ $page ]['sections'] ) ) {
  148. return;
  149. }
  150. foreach ( $this->settings[ $page ]['sections'] as $args ) {
  151. $default = [
  152. 'type' => 'fields_container',
  153. 'title' => '',
  154. 'description' => '',
  155. 'class' => '',
  156. 'help' => '',
  157. 'helper' => '',
  158. 'page' => '',
  159. ];
  160. $args = wp_parse_args( $args, $default );
  161. if ( ! empty( $args['class'] ) ) {
  162. $args['class'] = implode( ' ', array_map( 'sanitize_html_class', $args['class'] ) );
  163. }
  164. call_user_func_array( array( $this, $args['type'] ), array( $args ) );
  165. }
  166. }
  167. /**
  168. * Renders the settings fields for a setting section and page.
  169. *
  170. * @since 3.0
  171. * @author Remy Perona
  172. *
  173. * @param string $page Page section identifier.
  174. * @param string $section Settings section identifier.
  175. * @return void
  176. */
  177. public function render_settings_fields( $page, $section ) {
  178. if ( ! isset( $this->settings[ $page ]['sections'][ $section ]['fields'] ) ) {
  179. return;
  180. }
  181. foreach ( $this->settings[ $page ]['sections'][ $section ]['fields'] as $args ) {
  182. $default = [
  183. 'type' => 'text',
  184. 'label' => '',
  185. 'description' => '',
  186. 'class' => '',
  187. 'container_class' => '',
  188. 'default' => '',
  189. 'helper' => '',
  190. 'placeholder' => '',
  191. 'parent' => '',
  192. 'section' => '',
  193. 'page' => '',
  194. 'sanitize_callback' => 'sanitize_text_field',
  195. 'input_attr' => '',
  196. 'warning' => [],
  197. ];
  198. $args = wp_parse_args( $args, $default );
  199. if ( ! empty( $args['input_attr'] ) ) {
  200. $input_attr = '';
  201. foreach ( $args['input_attr'] as $key => $value ) {
  202. if ( 'disabled' === $key ) {
  203. if ( 1 === $value ) {
  204. $input_attr .= ' disabled';
  205. }
  206. continue;
  207. }
  208. $input_attr .= ' ' . sanitize_key( $key ) . '="' . esc_attr( $value ) . '"';
  209. }
  210. $args['input_attr'] = $input_attr;
  211. }
  212. if ( ! empty( $args['parent'] ) ) {
  213. $args['parent'] = ' data-parent="' . esc_attr( $args['parent'] ) . '"';
  214. }
  215. if ( ! empty( $args['class'] ) ) {
  216. $args['class'] = implode( ' ', array_map( 'sanitize_html_class', $args['class'] ) );
  217. }
  218. if ( ! empty( $args['container_class'] ) ) {
  219. $args['container_class'] = implode( ' ', array_map( 'sanitize_html_class', $args['container_class'] ) );
  220. }
  221. call_user_func_array( array( $this, $args['type'] ), array( $args ) );
  222. }
  223. }
  224. /**
  225. * Renders hidden fields in the form.
  226. *
  227. * @since 3.0
  228. * @author Remy Perona
  229. *
  230. * @return void
  231. */
  232. public function render_hidden_fields() {
  233. foreach ( $this->hidden_settings as $setting ) {
  234. call_user_func_array( array( $this, 'hidden' ), array( $setting ) );
  235. }
  236. }
  237. /**
  238. * Displays the fields container section template.
  239. *
  240. * @since 3.0
  241. * @author Remy Perona
  242. *
  243. * @param array $args Array of arguments to populate the template.
  244. * @return void
  245. */
  246. public function fields_container( $args ) {
  247. echo $this->generate( 'sections/fields-container', $args );
  248. }
  249. /**
  250. * Displays the no container section template.
  251. *
  252. * @since 3.0
  253. * @author Remy Perona
  254. *
  255. * @param array $args Array of arguments to populate the template.
  256. * @return void
  257. */
  258. public function nocontainer( $args ) {
  259. echo $this->generate( 'sections/nocontainer', $args );
  260. }
  261. /**
  262. * Displays the add-ons container section template.
  263. *
  264. * @since 3.0
  265. * @author Remy Perona
  266. *
  267. * @param array $args Array of arguments to populate the template.
  268. * @return void
  269. */
  270. public function addons_container( $args ) {
  271. echo $this->generate( 'sections/addons-container', $args );
  272. }
  273. /**
  274. * Displays the text field template.
  275. *
  276. * @since 3.0
  277. * @author Remy Perona
  278. *
  279. * @param array $args Array of arguments to populate the template.
  280. * @return void
  281. */
  282. public function text( $args ) {
  283. echo $this->generate( 'fields/text', $args );
  284. }
  285. /**
  286. * Displays the checkbox field template.
  287. *
  288. * @since 3.0
  289. * @author Remy Perona
  290. *
  291. * @param array $args Array of arguments to populate the template.
  292. * @return void
  293. */
  294. public function checkbox( $args ) {
  295. echo $this->generate( 'fields/checkbox', $args );
  296. }
  297. /**
  298. * Displays the textarea field template.
  299. *
  300. * @since 3.0
  301. * @author Remy Perona
  302. *
  303. * @param array $args Array of arguments to populate the template.
  304. * @return void
  305. */
  306. public function textarea( $args ) {
  307. if ( is_array( $args['value'] ) ) {
  308. $args['value'] = implode( "\n", $args['value'] );
  309. }
  310. $args['value'] = empty( $args['value'] ) ? '' : $args['value'];
  311. echo $this->generate( 'fields/textarea', $args );
  312. }
  313. /**
  314. * Displays the sliding checkbox field template.
  315. *
  316. * @since 3.0
  317. * @author Remy Perona
  318. *
  319. * @param array $args Array of arguments to populate the template.
  320. * @return void
  321. */
  322. public function sliding_checkbox( $args ) {
  323. echo $this->generate( 'fields/sliding-checkbox', $args );
  324. }
  325. /**
  326. * Displays the number input field template.
  327. *
  328. * @since 3.0
  329. * @author Remy Perona
  330. *
  331. * @param array $args Array of arguments to populate the template.
  332. * @return void
  333. */
  334. public function number( $args ) {
  335. echo $this->generate( 'fields/number', $args );
  336. }
  337. /**
  338. * Displays the select field template.
  339. *
  340. * @since 3.0
  341. * @author Remy Perona
  342. *
  343. * @param array $args Array of arguments to populate the template.
  344. * @return void
  345. */
  346. public function select( $args ) {
  347. echo $this->generate( 'fields/select', $args );
  348. }
  349. /**
  350. * Displays the clear cache lifespan block template.
  351. *
  352. * @since 3.0
  353. * @author Remy Perona
  354. *
  355. * @param array $args Array of arguments to populate the template.
  356. * @return void
  357. */
  358. public function cache_lifespan( $args ) {
  359. echo $this->generate( 'fields/cache-lifespan', $args );
  360. }
  361. /**
  362. * Displays the hidden field template.
  363. *
  364. * @since 3.0
  365. * @author Remy Perona
  366. *
  367. * @param array $args Array of arguments to populate the template.
  368. * @return void
  369. */
  370. public function hidden( $args ) {
  371. echo $this->generate( 'fields/hidden', $args );
  372. }
  373. /**
  374. * Displays the CDN CNAMES template.
  375. *
  376. * @since 3.0
  377. * @author Remy Perona
  378. *
  379. * @param array $args Array of arguments to populate the template.
  380. * @return void
  381. */
  382. public function cnames( $args ) {
  383. echo $this->generate( 'fields/cnames', $args );
  384. }
  385. /**
  386. * Displays the one-click add-on field template.
  387. *
  388. * @since 3.0
  389. * @author Remy Perona
  390. *
  391. * @param array $args Array of arguments to populate the template.
  392. * @return void
  393. */
  394. public function one_click_addon( $args ) {
  395. echo $this->generate( 'fields/one-click-addon', $args );
  396. }
  397. /**
  398. * Displays the Rocket add-on field template.
  399. *
  400. * @since 3.0
  401. * @author Remy Perona
  402. *
  403. * @param array $args Array of arguments to populate the template.
  404. * @return void
  405. */
  406. public function rocket_addon( $args ) {
  407. echo $this->generate( 'fields/rocket-addon', $args );
  408. }
  409. /**
  410. * Displays the import form template.
  411. *
  412. * @since 3.0
  413. * @author Remy Perona
  414. *
  415. * @return void
  416. */
  417. public function render_import_form() {
  418. $args = array();
  419. /**
  420. * Filter the maximum allowed upload size for import files.
  421. *
  422. * @since (WordPress) 2.3.0
  423. *
  424. * @see wp_max_upload_size()
  425. *
  426. * @param int $max_upload_size Allowed upload size. Default 1 MB.
  427. */
  428. $args['bytes'] = apply_filters( 'import_upload_size_limit', wp_max_upload_size() ); // Filter from WP Core.
  429. $args['size'] = size_format( $args['bytes'] );
  430. $args['upload_dir'] = wp_upload_dir();
  431. $args['action'] = 'rocket_import_settings';
  432. $args['submit_text'] = __( 'Upload file and import settings', 'rocket' );
  433. echo $this->generate( 'fields/import-form', $args );
  434. }
  435. /**
  436. * Displays the button template.
  437. *
  438. * @since 3.0
  439. * @author Remy Perona
  440. *
  441. * @param string $type Type of button (can be button or link).
  442. * @param string $action Action to be performed.
  443. * @param array $args Optional array of arguments to populate the button attributes.
  444. * @return void
  445. */
  446. public function render_action_button( $type, $action, $args = array() ) {
  447. $default = [
  448. 'label' => '',
  449. 'action' => '',
  450. 'url' => '',
  451. 'parameter' => '',
  452. 'attributes' => '',
  453. ];
  454. $args = wp_parse_args( $args, $default );
  455. if ( ! empty( $args['attributes'] ) ) {
  456. $attributes = '';
  457. foreach ( $args['attributes'] as $key => $value ) {
  458. $attributes .= ' ' . sanitize_key( $key ) . '="' . esc_attr( $value ) . '"';
  459. }
  460. $args['attributes'] = $attributes;
  461. }
  462. switch ( $type ) {
  463. case 'link':
  464. switch ( $action ) {
  465. case 'ask_support':
  466. $args['url'] = rocket_get_external_url( 'support', array(
  467. 'utm_source' => 'wp_plugin',
  468. 'utm_medium' => 'wp_rocket',
  469. ) );
  470. break;
  471. case 'view_account':
  472. $args['url'] = rocket_get_external_url( 'account', array(
  473. 'utm_source' => 'wp_plugin',
  474. 'utm_medium' => 'wp_rocket',
  475. ) );
  476. break;
  477. case 'purge_cache':
  478. $url = admin_url( 'admin-post.php?action=' . $action );
  479. if ( isset( $args['parameters'] ) ) {
  480. $url = add_query_arg( $args['parameters'], $url );
  481. }
  482. $args['url'] = wp_nonce_url( $url, $action . '_all' );
  483. break;
  484. case 'preload':
  485. case 'rocket_purge_opcache':
  486. case 'rocket_purge_cloudflare':
  487. case 'rocket_purge_sucuri':
  488. case 'rocket_rollback':
  489. case 'rocket_export':
  490. case 'rocket_generate_critical_css':
  491. $url = admin_url( 'admin-post.php?action=' . $action );
  492. if ( ! empty( $args['parameters'] ) ) {
  493. $url = add_query_arg( $args['parameters'], $url );
  494. }
  495. $args['url'] = wp_nonce_url( $url, $action );
  496. break;
  497. case 'documentation':
  498. $args['url'] = get_rocket_documentation_url();
  499. break;
  500. }
  501. echo $this->generate( 'buttons/link', $args );
  502. break;
  503. default:
  504. $args['action'] = $action;
  505. echo $this->generate( 'buttons/button', $args );
  506. break;
  507. }
  508. }
  509. /**
  510. * Displays a partial template.
  511. *
  512. * @since 3.0
  513. * @author Remy Perona
  514. *
  515. * @param string $part Partial template name.
  516. *
  517. * @return void
  518. */
  519. public function render_part( $part ) {
  520. echo $this->generate( 'partials/' . $part );
  521. }
  522. }