controls.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor settings controls.
  8. *
  9. * Elementor settings controls handler class responsible for creating the final
  10. * HTML for various input field types used in Elementor settings pages.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Settings_Controls {
  15. /**
  16. * Render settings control.
  17. *
  18. * Generates the final HTML on the frontend for any given field based on
  19. * the field type (text, select, checkbox, raw HTML, etc.).
  20. *
  21. * @since 1.0.0
  22. * @access public
  23. * @static
  24. *
  25. * @param array $field Optional. Field data. Default is an empty array.
  26. */
  27. public static function render( $field = [] ) {
  28. if ( empty( $field ) || empty( $field['id'] ) ) {
  29. return;
  30. }
  31. $defaults = [
  32. 'type' => '',
  33. 'attributes' => [],
  34. 'std' => '',
  35. 'desc' => '',
  36. ];
  37. $field = array_merge( $defaults, $field );
  38. $method_name = $field['type'];
  39. if ( ! method_exists( __CLASS__, $method_name ) ) {
  40. $method_name = 'text';
  41. }
  42. self::$method_name( $field );
  43. }
  44. /**
  45. * Render text control.
  46. *
  47. * Generates the final HTML for text controls.
  48. *
  49. * @since 2.0.0
  50. * @access private
  51. * @static
  52. *
  53. * @param array $field Field data.
  54. */
  55. private static function text( array $field ) {
  56. $attributes = [];
  57. if ( empty( $field['attributes']['class'] ) ) {
  58. $field['attributes']['class'] = 'regular-text';
  59. }
  60. foreach ( $field['attributes'] as $attribute_key => $attribute_values ) {
  61. $attributes[] = sprintf( '%1$s="%2$s"', $attribute_key, esc_attr( $attribute_values ) );
  62. }
  63. $attributes = implode( ' ', $attributes );
  64. ?>
  65. <input type="<?php echo esc_attr( $field['type'] ); ?>" id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo esc_attr( $field['id'] ); ?>" value="<?php echo esc_attr( get_option( $field['id'], $field['std'] ) ); ?>" <?php echo $attributes; ?>/>
  66. <?php
  67. if ( ! empty( $field['sub_desc'] ) ) :
  68. echo $field['sub_desc'];
  69. endif;
  70. ?>
  71. <?php if ( ! empty( $field['desc'] ) ) : ?>
  72. <p class="description"><?php echo $field['desc']; ?></p>
  73. <?php
  74. endif;
  75. }
  76. /**
  77. * Render checkbox control.
  78. *
  79. * Generates the final HTML for checkbox controls.
  80. *
  81. * @since 2.0.0
  82. * @access private
  83. * @static
  84. *
  85. * @param array $field Field data.
  86. */
  87. private static function checkbox( array $field ) {
  88. ?>
  89. <label>
  90. <input type="<?php echo esc_attr( $field['type'] ); ?>" id="<?php echo esc_attr( $field['id'] ); ?>" name="<?php echo esc_attr( $field['id'] ); ?>" value="<?php echo $field['value']; ?>"<?php checked( $field['value'], get_option( $field['id'], $field['std'] ) ); ?> />
  91. <?php
  92. if ( ! empty( $field['sub_desc'] ) ) :
  93. echo $field['sub_desc'];
  94. endif;
  95. ?>
  96. </label>
  97. <?php if ( ! empty( $field['desc'] ) ) : ?>
  98. <p class="description"><?php echo $field['desc']; ?></p>
  99. <?php
  100. endif;
  101. }
  102. /**
  103. * Render checkbox list control.
  104. *
  105. * Generates the final HTML for checkbox list controls.
  106. *
  107. * @since 2.0.0
  108. * @access private
  109. * @static
  110. *
  111. * @param array $field Field data.
  112. */
  113. private static function checkbox_list( array $field ) {
  114. $old_value = get_option( $field['id'], $field['std'] );
  115. if ( ! is_array( $old_value ) ) {
  116. $old_value = [];
  117. }
  118. foreach ( $field['options'] as $option_key => $option_value ) :
  119. ?>
  120. <label>
  121. <input type="checkbox" name="<?php echo $field['id']; ?>[]" value="<?php echo $option_key; ?>"<?php checked( in_array( $option_key, $old_value ), true ); ?> />
  122. <?php echo $option_value; ?>
  123. </label><br />
  124. <?php endforeach; ?>
  125. <?php if ( ! empty( $field['desc'] ) ) : ?>
  126. <p class="description"><?php echo $field['desc']; ?></p>
  127. <?php
  128. endif;
  129. }
  130. /**
  131. * Render select control.
  132. *
  133. * Generates the final HTML for select controls.
  134. *
  135. * @since 2.0.0
  136. * @access private
  137. * @static
  138. *
  139. * @param array $field Field data.
  140. */
  141. private static function select( array $field ) {
  142. $old_value = get_option( $field['id'], $field['std'] );
  143. ?>
  144. <select name="<?php echo esc_attr( $field['id'] ); ?>">
  145. <?php if ( ! empty( $field['show_select'] ) ) : ?>
  146. <option value="">— <?php echo __( 'Select', 'elementor' ); ?> —</option>
  147. <?php endif; ?>
  148. <?php foreach ( $field['options'] as $value => $label ) : ?>
  149. <option value="<?php echo esc_attr( $value ); ?>"<?php selected( $value, $old_value ); ?>><?php echo $label; ?></option>
  150. <?php endforeach; ?>
  151. </select>
  152. <?php if ( ! empty( $field['desc'] ) ) : ?>
  153. <p class="description"><?php echo $field['desc']; ?></p>
  154. <?php
  155. endif;
  156. }
  157. /**
  158. * Render checkbox list control for CPT.
  159. *
  160. * Generates the final HTML for checkbox list controls populated with Custom Post Types.
  161. *
  162. * @since 2.0.0
  163. * @access private
  164. * @static
  165. *
  166. * @param array $field Field data.
  167. */
  168. private static function checkbox_list_cpt( array $field ) {
  169. $defaults = [
  170. 'exclude' => [],
  171. ];
  172. $field = array_merge( $defaults, $field );
  173. $post_types_objects = get_post_types(
  174. [
  175. 'public' => true,
  176. ], 'objects'
  177. );
  178. $field['options'] = [];
  179. foreach ( $post_types_objects as $cpt_slug => $post_type ) {
  180. if ( in_array( $cpt_slug, $field['exclude'] ) ) {
  181. continue;
  182. }
  183. $field['options'][ $cpt_slug ] = $post_type->labels->name;
  184. }
  185. self::checkbox_list( $field );
  186. }
  187. /**
  188. * Render checkbox list control for user roles.
  189. *
  190. * Generates the final HTML for checkbox list controls populated with user roles.
  191. *
  192. * @since 2.0.0
  193. * @access private
  194. * @static
  195. *
  196. * @param array $field Field data.
  197. */
  198. private static function checkbox_list_roles( array $field ) {
  199. $defaults = [
  200. 'exclude' => [],
  201. ];
  202. $field = array_merge( $defaults, $field );
  203. $field['options'] = [];
  204. foreach ( get_editable_roles() as $role_slug => $role_data ) {
  205. if ( in_array( $role_slug, $field['exclude'] ) ) {
  206. continue;
  207. }
  208. $field['options'][ $role_slug ] = $role_data['name'];
  209. }
  210. self::checkbox_list( $field );
  211. }
  212. /**
  213. * Render raw HTML control.
  214. *
  215. * Generates the final HTML for raw HTML controls.
  216. *
  217. * @since 2.0.0
  218. * @access private
  219. * @static
  220. *
  221. * @param array $field Field data.
  222. */
  223. private static function raw_html( array $field ) {
  224. if ( empty( $field['html'] ) ) {
  225. return;
  226. }
  227. ?>
  228. <div id="<?php echo $field['id']; ?>">
  229. <div><?php echo $field['html']; ?></div>
  230. <?php
  231. if ( ! empty( $field['sub_desc'] ) ) :
  232. echo $field['sub_desc'];
  233. endif;
  234. ?>
  235. <?php if ( ! empty( $field['desc'] ) ) : ?>
  236. <p class="description"><?php echo $field['desc']; ?></p>
  237. <?php endif; ?>
  238. </div>
  239. <?php
  240. }
  241. }