説明なし

settings-api.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. /**
  3. * Settings API.
  4. *
  5. * @since 1.3.7
  6. */
  7. /**
  8. * Settings output wrapper.
  9. *
  10. * @since 1.3.9
  11. *
  12. * @param array $args
  13. *
  14. * @return string
  15. */
  16. function wpforms_settings_output_field( $args ) {
  17. // Define default callback for this field type.
  18. $callback = ! empty( $args['type'] ) && function_exists( 'wpforms_settings_' . $args['type'] . '_callback' ) ? 'wpforms_settings_' . $args['type'] . '_callback' : 'wpforms_settings_missing_callback';
  19. // Allow custom callback to be provided via arg.
  20. if ( ! empty( $args['callback'] ) && function_exists( $args['callback'] ) ) {
  21. $callback = $args['callback'];
  22. }
  23. // Store returned markup from callback.
  24. $field = call_user_func( $callback, $args );
  25. // Allow arg to bypass standard field wrap for custom display.
  26. if ( ! empty( $args['wrap'] ) ) {
  27. return $field;
  28. }
  29. // Custom row classes.
  30. $class = ! empty( $args['class'] ) ? wpforms_sanitize_classes( (array) $args['class'], true ) : '';
  31. // Allow hiding blocks on page load (useful for JS toggles).
  32. $display_none = ! empty( $args['is_hidden'] ) ? 'style="display:none;"' : '';
  33. // Build standard field markup and return.
  34. $output = '<div class="wpforms-setting-row wpforms-setting-row-' . sanitize_html_class( $args['type'] ) . ' wpforms-clear ' . $class . '" id="wpforms-setting-row-' . wpforms_sanitize_key( $args['id'] ) . '" ' . $display_none . '>';
  35. if ( ! empty( $args['name'] ) && empty( $args['no_label'] ) ) {
  36. $output .= '<span class="wpforms-setting-label">';
  37. $output .= '<label for="wpforms-setting-' . wpforms_sanitize_key( $args['id'] ) . '">' . esc_html( $args['name'] ) . '</label>';
  38. $output .= '</span>';
  39. }
  40. $output .= '<span class="wpforms-setting-field">';
  41. $output .= $field;
  42. if ( ! empty( $args['desc_after'] ) ) {
  43. $output .= '<div class="wpforms-clear">' . $args['desc_after'] . '</div>';
  44. }
  45. $output .= '</span>';
  46. $output .= '</div>';
  47. return $output;
  48. }
  49. /**
  50. * Missing Callback.
  51. *
  52. * If a function is missing for settings callbacks alert the user.
  53. *
  54. * @since 1.3.9
  55. *
  56. * @param array $args Arguments passed by the setting.
  57. *
  58. * @return string
  59. */
  60. function wpforms_settings_missing_callback( $args ) {
  61. return sprintf(
  62. /* translators: %s - ID of a setting. */
  63. esc_html__( 'The callback function used for the %s setting is missing.', 'wpforms-lite' ),
  64. '<strong>' . wpforms_sanitize_key( $args['id'] ) . '</strong>'
  65. );
  66. }
  67. /**
  68. * Settings content field callback.
  69. *
  70. * @since 1.3.9
  71. *
  72. * @param array $args
  73. *
  74. * @return string
  75. */
  76. function wpforms_settings_content_callback( $args ) {
  77. return ! empty( $args['content'] ) ? $args['content'] : '';
  78. }
  79. /**
  80. * Settings license field callback.
  81. *
  82. * @since 1.3.9
  83. *
  84. * @param array $args
  85. *
  86. * @return string
  87. */
  88. function wpforms_settings_license_callback( $args ) {
  89. // Lite users don't need to worry about license keys.
  90. if ( ! wpforms()->pro || ! class_exists( 'WPForms_License', false ) ) {
  91. $output = '<p>' . esc_html__( 'You\'re using WPForms Lite - no license needed. Enjoy!', 'wpforms-lite' ) . ' 🙂</p>';
  92. $output .=
  93. '<p>' .
  94. sprintf(
  95. wp_kses(
  96. /* translators: %s - WPForms.com upgrade URL. */
  97. __( 'To unlock more features consider <strong><a href="%s" target="_blank" rel="noopener noreferrer" class="wpforms-upgrade-modal">upgrading to PRO</a></strong>.', 'wpforms-lite' ),
  98. array(
  99. 'a' => array(
  100. 'href' => array(),
  101. 'class' => array(),
  102. 'target' => array(),
  103. 'rel' => array(),
  104. ),
  105. 'strong' => array(),
  106. )
  107. ),
  108. esc_url( wpforms_admin_upgrade_link( 'settings-license' ) )
  109. ) .
  110. '</p>';
  111. $output .=
  112. '<p class="discount-note">' .
  113. wp_kses(
  114. __( 'As a valued WPForms Lite user you receive <strong>50% off</strong>, automatically applied at checkout!', 'wpforms-lite' ),
  115. array(
  116. 'strong' => array(),
  117. 'br' => array(),
  118. )
  119. ) .
  120. '</p>';
  121. $output .= '<hr><p>' . esc_html__( 'Already purchased? Simply enter your license key below to connect with WPForms PRO!', 'wpforms-lite' ) . '</p>';
  122. $output .= '<p>';
  123. $output .= '<input type="password" id="wpforms-settings-upgrade-license-key" placeholder="' . esc_attr__( 'Paste license key here', 'wpforms-lite' ) . '" value="" />';
  124. $output .= '<button type="button" class="wpforms-btn wpforms-btn-md wpforms-btn-orange" id="wpforms-settings-connect-btn">' . esc_attr__( 'Connect', 'wpforms-lite' ) . '</button>';
  125. $output .= '</p>';
  126. return $output;
  127. }
  128. $license = (array) get_option( 'wpforms_license', [] );
  129. $key = ! empty( $license['key'] ) ? $license['key'] : '';
  130. $type = ! empty( $license['type'] ) ? $license['type'] : '';
  131. $is_valid_key = ! empty( $key ) &&
  132. ( isset( $license['is_expired'] ) && $license['is_expired'] === false ) &&
  133. ( isset( $license['is_disabled'] ) && $license['is_disabled'] === false ) &&
  134. ( isset( $license['is_invalid'] ) && $license['is_invalid'] === false );
  135. $output = '<span class="wpforms-setting-license-wrapper">';
  136. $output .= '<input type="password" id="wpforms-setting-license-key" value="' . esc_attr( $key ) . '"' . disabled( true, $is_valid_key, false ) . ' />';
  137. $output .= '</span>';
  138. $output .= '<button id="wpforms-setting-license-key-verify" class="wpforms-btn wpforms-btn-md wpforms-btn-orange">' . esc_html__( 'Verify Key', 'wpforms-lite' ) . '</button>';
  139. // Offer option to deactivate the key.
  140. $class = empty( $key ) ? 'wpforms-hide' : '';
  141. $output .= '<button id="wpforms-setting-license-key-deactivate" class="wpforms-btn wpforms-btn-md wpforms-btn-light-grey ' . $class . '">' . esc_html__( 'Deactivate Key', 'wpforms-lite' ) . '</button>';
  142. // If we have previously looked up the license type, display it.
  143. $class = empty( $type ) ? 'wpforms-hide' : '';
  144. $output .= '<p class="type ' . $class . '">' .
  145. sprintf(
  146. /* translators: $s - license type. */
  147. esc_html__( 'Your license key level is %s.', 'wpforms-lite' ),
  148. '<strong>' . esc_html( ucwords( $type ) ) . '</strong>'
  149. ) .
  150. '</p>';
  151. $output .= '<p class="desc ' . $class . '">' .
  152. sprintf( /* translators: %s - Refresh link. */
  153. esc_html__( 'If your license has been upgraded or is incorrect, then please %1$sforce a refresh%2$s.', 'wpforms-lite' ),
  154. '<a href="#" id="wpforms-setting-license-key-refresh">',
  155. '</a>'
  156. )
  157. . '</p>';
  158. return $output;
  159. }
  160. /**
  161. * Settings text input field callback.
  162. *
  163. * @since 1.3.9
  164. *
  165. * @param array $args
  166. *
  167. * @return string
  168. */
  169. function wpforms_settings_text_callback( $args ) {
  170. $default = isset( $args['default'] ) ? esc_html( $args['default'] ) : '';
  171. $value = wpforms_setting( $args['id'], $default );
  172. $id = wpforms_sanitize_key( $args['id'] );
  173. $output = '<input type="text" id="wpforms-setting-' . $id . '" name="' . $id . '" value="' . esc_attr( $value ) . '">';
  174. if ( ! empty( $args['desc'] ) ) {
  175. $output .= '<p class="desc">' . wp_kses_post( $args['desc'] ) . '</p>';
  176. }
  177. return $output;
  178. }
  179. /**
  180. * Settings number input field callback.
  181. *
  182. * @since 1.5.3
  183. *
  184. * @param array $args Setting field arguments.
  185. *
  186. * @return string
  187. */
  188. function wpforms_settings_number_callback( $args ) {
  189. $default = isset( $args['default'] ) ? esc_html( $args['default'] ) : '';
  190. $id = 'wpforms-setting-' . wpforms_sanitize_key( $args['id'] );
  191. $attr = array(
  192. 'value' => wpforms_setting( $args['id'], $default ),
  193. 'name' => wpforms_sanitize_key( $args['id'] ),
  194. );
  195. $data = ! empty( $args['data'] ) ? $args['data'] : array();
  196. if ( ! empty( $args['attr'] ) ) {
  197. $attr = array_merge( $attr, $args['attr'] );
  198. }
  199. $output = sprintf(
  200. '<input type="number" %s>',
  201. wpforms_html_attributes( $id, array(), $data, $attr )
  202. );
  203. if ( ! empty( $args['desc'] ) ) {
  204. $output .= '<p class="desc">' . wp_kses_post( $args['desc'] ) . '</p>';
  205. }
  206. return $output;
  207. }
  208. /**
  209. * Settings select field callback.
  210. *
  211. * @since 1.3.9
  212. *
  213. * @param array $args
  214. *
  215. * @return string
  216. */
  217. function wpforms_settings_select_callback( $args ) {
  218. $default = isset( $args['default'] ) ? esc_html( $args['default'] ) : '';
  219. $value = wpforms_setting( $args['id'], $default );
  220. $id = wpforms_sanitize_key( $args['id'] );
  221. $select_name = $id;
  222. $class = ! empty( $args['choicesjs'] ) ? 'choicesjs-select' : '';
  223. $choices = ! empty( $args['choicesjs'] ) ? true : false;
  224. $data = isset( $args['data'] ) ? (array) $args['data'] : array();
  225. $attr = isset( $args['attr'] ) ? (array) $args['attr'] : array();
  226. if ( $choices && ! empty( $args['search'] ) ) {
  227. $data['search'] = 'true';
  228. }
  229. if ( ! empty( $args['placeholder'] ) ) {
  230. $data['placeholder'] = $args['placeholder'];
  231. }
  232. if ( $choices && ! empty( $args['multiple'] ) ) {
  233. $attr[] = 'multiple';
  234. $select_name = $id . '[]';
  235. }
  236. foreach ( $data as $name => $val ) {
  237. $data[ $name ] = 'data-' . sanitize_html_class( $name ) . '="' . esc_attr( $val ) . '"';
  238. }
  239. $data = implode( ' ', $data );
  240. $attr = implode( ' ', array_map( 'sanitize_html_class', $attr ) );
  241. $output = $choices ? '<span class="choicesjs-select-wrap">' : '';
  242. $output .= '<select id="wpforms-setting-' . $id . '" name="' . $select_name . '" class="' . $class . '"' . $data . $attr . '>';
  243. foreach ( $args['options'] as $option => $name ) {
  244. if ( empty( $args['selected'] ) ) {
  245. $selected = selected( $value, $option, false );
  246. } else {
  247. $selected = is_array( $args['selected'] ) && in_array( $option, $args['selected'], true ) ? 'selected' : '';
  248. }
  249. $output .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>';
  250. }
  251. $output .= '</select>';
  252. $output .= $choices ? '</span>' : '';
  253. if ( ! empty( $args['desc'] ) ) {
  254. $output .= '<p class="desc">' . wp_kses_post( $args['desc'] ) . '</p>';
  255. }
  256. return $output;
  257. }
  258. /**
  259. * Settings checkbox field callback.
  260. *
  261. * @since 1.3.9
  262. *
  263. * @param array $args
  264. *
  265. * @return string
  266. */
  267. function wpforms_settings_checkbox_callback( $args ) {
  268. $value = wpforms_setting( $args['id'] );
  269. $id = wpforms_sanitize_key( $args['id'] );
  270. $checked = ! empty( $value ) ? checked( 1, $value, false ) : '';
  271. $output = '<input type="checkbox" id="wpforms-setting-' . $id . '" name="' . $id . '" ' . $checked . '>';
  272. if ( ! empty( $args['desc'] ) ) {
  273. $output .= '<p class="desc">' . wp_kses_post( $args['desc'] ) . '</p>';
  274. }
  275. return $output;
  276. }
  277. /**
  278. * Settings radio field callback.
  279. *
  280. * @since 1.3.9
  281. *
  282. * @param array $args
  283. *
  284. * @return string
  285. */
  286. function wpforms_settings_radio_callback( $args ) {
  287. $default = isset( $args['default'] ) ? esc_html( $args['default'] ) : '';
  288. $value = wpforms_setting( $args['id'], $default );
  289. $id = wpforms_sanitize_key( $args['id'] );
  290. $output = '';
  291. $x = 1;
  292. foreach ( $args['options'] as $option => $name ) {
  293. $checked = checked( $value, $option, false );
  294. $output .= '<input type="radio" id="wpforms-setting-' . $id . '[' . $x . ']" name="' . $id . '" value="' . esc_attr( $option ) . '" ' . $checked . '>';
  295. $output .= '<label for="wpforms-setting-' . $id . '[' . $x . ']" class="option-' . sanitize_html_class( $option ) . '">';
  296. $output .= esc_html( $name );
  297. $output .= '</label>';
  298. $x ++;
  299. }
  300. if ( ! empty( $args['desc'] ) ) {
  301. $output .= '<p class="desc">' . wp_kses_post( $args['desc'] ) . '</p>';
  302. }
  303. return $output;
  304. }
  305. /**
  306. * Settings image upload field callback.
  307. *
  308. * @since 1.3.9
  309. *
  310. * @param array $args
  311. *
  312. * @return string
  313. */
  314. function wpforms_settings_image_callback( $args ) {
  315. $default = isset( $args['default'] ) ? esc_html( $args['default'] ) : '';
  316. $value = wpforms_setting( $args['id'], $default );
  317. $id = wpforms_sanitize_key( $args['id'] );
  318. $output = '';
  319. if ( ! empty( $value ) ) {
  320. $output .= '<img src="' . esc_url_raw( $value ) . '">';
  321. }
  322. $output .= '<input type="text" id="wpforms-setting-' . $id . '" name="' . $id . '" value="' . esc_url_raw( $value ) . '">';
  323. $output .= '<button class="wpforms-btn wpforms-btn-md wpforms-btn-light-grey">' . esc_html__( 'Upload Image', 'wpforms-lite' ) . '</button>';
  324. if ( ! empty( $args['desc'] ) ) {
  325. $output .= '<p class="desc">' . wp_kses_post( $args['desc'] ) . '</p>';
  326. }
  327. return $output;
  328. }
  329. /**
  330. * Settings color picker field callback.
  331. *
  332. * @since 1.3.9
  333. *
  334. * @param array $args
  335. *
  336. * @return string
  337. */
  338. function wpforms_settings_color_callback( $args ) {
  339. $default = isset( $args['default'] ) ? esc_html( $args['default'] ) : '';
  340. $value = wpforms_setting( $args['id'], $default );
  341. $id = wpforms_sanitize_key( $args['id'] );
  342. $output = '<input type="text" id="wpforms-setting-' . $id . '" class="wpforms-color-picker" name="' . $id . '" value="' . esc_attr( $value ) . '">';
  343. if ( ! empty( $args['desc'] ) ) {
  344. $output .= '<p class="desc">' . wp_kses_post( $args['desc'] ) . '</p>';
  345. }
  346. return $output;
  347. }
  348. /**
  349. * Settings providers field callback - this is for the Integrations tab.
  350. *
  351. * @since 1.3.9
  352. *
  353. * @param array $args
  354. *
  355. * @return string
  356. */
  357. function wpforms_settings_providers_callback( $args ) {
  358. $active = wpforms_get_providers_available();
  359. $providers = wpforms_get_providers_options();
  360. $output = '<div id="wpforms-settings-providers">';
  361. ob_start();
  362. do_action( 'wpforms_settings_providers', $active, $providers );
  363. $output .= ob_get_clean();
  364. $output .= '</div>';
  365. return $output;
  366. }
  367. /**
  368. * Settings field columns callback.
  369. *
  370. * @since 1.5.8
  371. *
  372. * @param array $args Arguments passed by the setting.
  373. *
  374. * @return string
  375. */
  376. function wpforms_settings_columns_callback( $args ) {
  377. if ( empty( $args['columns'] ) || ! is_array( $args['columns'] ) ) {
  378. return '';
  379. }
  380. $output = '<div class="wpforms-setting-columns">';
  381. foreach ( $args['columns'] as $column ) {
  382. // Define default callback for this field type.
  383. $callback = ! empty( $column['type'] ) ? 'wpforms_settings_' . $column['type'] . '_callback' : '';
  384. // Allow custom callback to be provided via arg.
  385. if ( ! empty( $column['callback'] ) ) {
  386. $callback = $column['callback'];
  387. }
  388. $output .= '<div class="wpforms-setting-column">';
  389. if ( ! empty( $column['name'] ) ) {
  390. $output .= '<label><b>' . wp_kses_post( $column['name'] ) . '</b></label>';
  391. }
  392. if ( function_exists( $callback ) ) {
  393. $output .= call_user_func( $callback, $column );
  394. }
  395. $output .= '</div>';
  396. }
  397. $output .= '</div>';
  398. return $output;
  399. }