settings-page.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor settings page.
  8. *
  9. * An abstract class that provides the needed properties and methods to handle
  10. * WordPress dashboard settings pages in inheriting classes.
  11. *
  12. * @since 1.0.0
  13. * @abstract
  14. */
  15. abstract class Settings_Page {
  16. /**
  17. * Settings page ID.
  18. */
  19. const PAGE_ID = '';
  20. /**
  21. * Tabs.
  22. *
  23. * Holds the settings page tabs, sections and fields.
  24. *
  25. * @access private
  26. *
  27. * @var array
  28. */
  29. private $tabs;
  30. /**
  31. * Create tabs.
  32. *
  33. * Return the settings page tabs, sections and fields.
  34. *
  35. * @since 1.5.0
  36. * @access protected
  37. * @abstract
  38. */
  39. abstract protected function create_tabs();
  40. /**
  41. * Get settings page title.
  42. *
  43. * Retrieve the title for the settings page.
  44. *
  45. * @since 1.5.0
  46. * @access protected
  47. * @abstract
  48. */
  49. abstract protected function get_page_title();
  50. /**
  51. * Get settings page URL.
  52. *
  53. * Retrieve the URL of the settings page.
  54. *
  55. * @since 1.5.0
  56. * @access public
  57. * @static
  58. *
  59. * @return string Settings page URL.
  60. */
  61. final public static function get_url() {
  62. return admin_url( 'admin.php?page=' . static::PAGE_ID );
  63. }
  64. /**
  65. * Settings page constructor.
  66. *
  67. * Initializing Elementor settings page.
  68. *
  69. * @since 1.5.0
  70. * @access public
  71. */
  72. public function __construct() {
  73. if ( ! empty( $_POST['option_page'] ) && static::PAGE_ID === $_POST['option_page'] ) {
  74. add_action( 'admin_init', [ $this, 'register_settings_fields' ] );
  75. }
  76. }
  77. /**
  78. * Get tabs.
  79. *
  80. * Retrieve the settings page tabs, sections and fields.
  81. *
  82. * @since 1.5.0
  83. * @access public
  84. *
  85. * @return array Settings page tabs, sections and fields.
  86. */
  87. final public function get_tabs() {
  88. $this->ensure_tabs();
  89. return $this->tabs;
  90. }
  91. /**
  92. * Add tab.
  93. *
  94. * Register a new tab to a settings page.
  95. *
  96. * @since 1.5.0
  97. * @access public
  98. *
  99. * @param string $tab_id Tab ID.
  100. * @param array $tab_args Optional. Tab arguments. Default is an empty array.
  101. */
  102. final public function add_tab( $tab_id, array $tab_args = [] ) {
  103. $this->ensure_tabs();
  104. if ( isset( $this->tabs[ $tab_id ] ) ) {
  105. // Don't override an existing tab
  106. return;
  107. }
  108. if ( ! isset( $tab_args['sections'] ) ) {
  109. $tab_args['sections'] = [];
  110. }
  111. $this->tabs[ $tab_id ] = $tab_args;
  112. }
  113. /**
  114. * Add section.
  115. *
  116. * Register a new section to a tab.
  117. *
  118. * @since 1.5.0
  119. * @access public
  120. *
  121. * @param string $tab_id Tab ID.
  122. * @param string $section_id Section ID.
  123. * @param array $section_args Optional. Section arguments. Default is an
  124. * empty array.
  125. */
  126. final public function add_section( $tab_id, $section_id, array $section_args = [] ) {
  127. $this->ensure_tabs();
  128. if ( ! isset( $this->tabs[ $tab_id ] ) ) {
  129. // If the requested tab doesn't exists, use the first tab
  130. $tab_id = key( $this->tabs );
  131. }
  132. if ( isset( $this->tabs[ $tab_id ]['sections'][ $section_id ] ) ) {
  133. // Don't override an existing section
  134. return;
  135. }
  136. if ( ! isset( $section_args['fields'] ) ) {
  137. $section_args['fields'] = [];
  138. }
  139. $this->tabs[ $tab_id ]['sections'][ $section_id ] = $section_args;
  140. }
  141. /**
  142. * Add field.
  143. *
  144. * Register a new field to a section.
  145. *
  146. * @since 1.5.0
  147. * @access public
  148. *
  149. * @param string $tab_id Tab ID.
  150. * @param string $section_id Section ID.
  151. * @param string $field_id Field ID.
  152. * @param array $field_args Field arguments.
  153. */
  154. final public function add_field( $tab_id, $section_id, $field_id, array $field_args ) {
  155. $this->ensure_tabs();
  156. if ( ! isset( $this->tabs[ $tab_id ] ) ) {
  157. // If the requested tab doesn't exists, use the first tab
  158. $tab_id = key( $this->tabs );
  159. }
  160. if ( ! isset( $this->tabs[ $tab_id ]['sections'][ $section_id ] ) ) {
  161. // If the requested section doesn't exists, use the first section
  162. $section_id = key( $this->tabs[ $tab_id ]['sections'] );
  163. }
  164. if ( isset( $this->tabs[ $tab_id ]['sections'][ $section_id ]['fields'][ $field_id ] ) ) {
  165. // Don't override an existing field
  166. return;
  167. }
  168. $this->tabs[ $tab_id ]['sections'][ $section_id ]['fields'][ $field_id ] = $field_args;
  169. }
  170. /**
  171. * Add fields.
  172. *
  173. * Register multiple fields to a section.
  174. *
  175. * @since 1.5.0
  176. * @access public
  177. *
  178. * @param string $tab_id Tab ID.
  179. * @param string $section_id Section ID.
  180. * @param array $fields {
  181. * An array of fields.
  182. *
  183. * @type string $field_id Field ID.
  184. * @type array $field_args Field arguments.
  185. * }
  186. */
  187. final public function add_fields( $tab_id, $section_id, array $fields ) {
  188. foreach ( $fields as $field_id => $field_args ) {
  189. $this->add_field( $tab_id, $section_id, $field_id, $field_args );
  190. }
  191. }
  192. /**
  193. * Register settings fields.
  194. *
  195. * In each tab register his inner sections, and in each section register his
  196. * inner fields.
  197. *
  198. * @since 1.5.0
  199. * @access public
  200. */
  201. final public function register_settings_fields() {
  202. $controls_class_name = __NAMESPACE__ . '\Settings_Controls';
  203. $tabs = $this->get_tabs();
  204. foreach ( $tabs as $tab_id => $tab ) {
  205. foreach ( $tab['sections'] as $section_id => $section ) {
  206. $full_section_id = 'elementor_' . $section_id . '_section';
  207. $label = isset( $section['label'] ) ? $section['label'] : '';
  208. $section_callback = isset( $section['callback'] ) ? $section['callback'] : '__return_empty_string';
  209. add_settings_section( $full_section_id, $label, $section_callback, static::PAGE_ID );
  210. foreach ( $section['fields'] as $field_id => $field ) {
  211. $full_field_id = ! empty( $field['full_field_id'] ) ? $field['full_field_id'] : 'elementor_' . $field_id;
  212. $field['field_args']['id'] = $full_field_id;
  213. $field_classes = [ $full_field_id ];
  214. if ( ! empty( $field['class'] ) ) {
  215. $field_classes[] = $field['field_args']['class'];
  216. }
  217. $field['field_args']['class'] = implode( ' ', $field_classes );
  218. add_settings_field(
  219. $full_field_id,
  220. isset( $field['label'] ) ? $field['label'] : '',
  221. [ $controls_class_name, 'render' ],
  222. static::PAGE_ID,
  223. $full_section_id,
  224. $field['field_args']
  225. );
  226. $setting_args = [];
  227. if ( ! empty( $field['setting_args'] ) ) {
  228. $setting_args = $field['setting_args'];
  229. }
  230. register_setting( static::PAGE_ID, $full_field_id, $setting_args );
  231. }
  232. }
  233. }
  234. }
  235. /**
  236. * Display settings page.
  237. *
  238. * Output the content for the settings page.
  239. *
  240. * @since 1.5.0
  241. * @access public
  242. */
  243. public function display_settings_page() {
  244. $this->register_settings_fields();
  245. $tabs = $this->get_tabs();
  246. ?>
  247. <div class="wrap">
  248. <h1><?php echo $this->get_page_title(); ?></h1>
  249. <div id="elementor-settings-tabs-wrapper" class="nav-tab-wrapper">
  250. <?php
  251. foreach ( $tabs as $tab_id => $tab ) {
  252. if ( empty( $tab['sections'] ) ) {
  253. continue;
  254. }
  255. $active_class = '';
  256. if ( 'general' === $tab_id ) {
  257. $active_class = ' nav-tab-active';
  258. }
  259. echo "<a id='elementor-settings-tab-{$tab_id}' class='nav-tab{$active_class}' href='#tab-{$tab_id}'>{$tab['label']}</a>";
  260. }
  261. ?>
  262. </div>
  263. <form id="elementor-settings-form" method="post" action="options.php">
  264. <?php
  265. settings_fields( static::PAGE_ID );
  266. foreach ( $tabs as $tab_id => $tab ) {
  267. if ( empty( $tab['sections'] ) ) {
  268. continue;
  269. }
  270. $active_class = '';
  271. if ( 'general' === $tab_id ) {
  272. $active_class = ' elementor-active';
  273. }
  274. echo "<div id='tab-{$tab_id}' class='elementor-settings-form-page{$active_class}'>";
  275. foreach ( $tab['sections'] as $section_id => $section ) {
  276. $full_section_id = 'elementor_' . $section_id . '_section';
  277. if ( ! empty( $section['label'] ) ) {
  278. echo "<h2>{$section['label']}</h2>";
  279. }
  280. if ( ! empty( $section['callback'] ) ) {
  281. $section['callback']();
  282. }
  283. echo '<table class="form-table">';
  284. do_settings_fields( static::PAGE_ID, $full_section_id );
  285. echo '</table>';
  286. }
  287. echo '</div>';
  288. }
  289. submit_button();
  290. ?>
  291. </form>
  292. </div><!-- /.wrap -->
  293. <?php
  294. }
  295. /**
  296. * Ensure tabs.
  297. *
  298. * Make sure the settings page has tabs before inserting any new sections or
  299. * fields.
  300. *
  301. * @since 1.5.0
  302. * @access private
  303. */
  304. private function ensure_tabs() {
  305. if ( null === $this->tabs ) {
  306. $this->tabs = $this->create_tabs();
  307. $page_id = static::PAGE_ID;
  308. /**
  309. * After create settings.
  310. *
  311. * Fires after the settings are created in Elementor admin page.
  312. *
  313. * The dynamic portion of the hook name, `$page_id`, refers to the current page ID.
  314. *
  315. * @since 1.0.0
  316. *
  317. * @param Settings_Page $this The settings page.
  318. */
  319. do_action( "elementor/admin/after_create_settings/{$page_id}", $this );
  320. }
  321. }
  322. }