params.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * WPBakery WPBakery Page Builder shortcodes attributes class.
  7. *
  8. * This class and functions represents ability which will allow you to create attributes settings fields to
  9. * control new attributes.
  10. * New attributes can be added to shortcode settings by using param array in wp_map function
  11. *
  12. * @package WPBakeryPageBuilder
  13. *
  14. */
  15. /**
  16. * Shortcode params class allows to create new params types.
  17. * class WpbakeryShortcodeParams
  18. * @since 4.2
  19. */
  20. class WpbakeryShortcodeParams {
  21. /**
  22. * @since 4.2
  23. * @var array - store shortcode attributes types
  24. */
  25. protected static $params = array();
  26. /**
  27. * @since 4.2
  28. * @var array - store shortcode javascript files urls
  29. */
  30. protected static $scripts = array();
  31. /**
  32. * @since 4.7
  33. * @var array - store params not required to init
  34. */
  35. protected static $optional_init_params = array();
  36. /**
  37. * Get list of params that need to be initialized
  38. *
  39. * @return string[]
  40. */
  41. public static function getRequiredInitParams() {
  42. $all_params = array_keys( self::$params );
  43. $optional_params = apply_filters( 'vc_edit_form_fields_optional_params', self::$optional_init_params );
  44. $required_params = array_diff( $all_params, $optional_params );
  45. return $required_params;
  46. }
  47. /**
  48. * Create new attribute type
  49. *
  50. * @static
  51. * @param $name - attribute name
  52. * @param $form_field_callback - hook, will be called when settings form is shown and attribute added to shortcode
  53. * param list
  54. * @param $script_url - javascript file url which will be attached at the end of settings form.
  55. *
  56. * @return bool - return true if attribute type created
  57. * @since 4.2
  58. *
  59. */
  60. public static function addField( $name, $form_field_callback, $script_url = null ) {
  61. $result = false;
  62. if ( ! empty( $name ) && ! empty( $form_field_callback ) ) {
  63. self::$params[ $name ] = array(
  64. 'callbacks' => array(
  65. 'form' => $form_field_callback,
  66. ),
  67. );
  68. $result = true;
  69. if ( is_string( $script_url ) && ! in_array( $script_url, self::$scripts, true ) ) {
  70. self::$scripts[] = $script_url;
  71. }
  72. }
  73. return $result;
  74. }
  75. /**
  76. * Calls hook for attribute type
  77. * @param $name - attribute name
  78. * @param $param_settings - attribute settings from shortcode
  79. * @param $param_value - attribute value
  80. * @param $tag - attribute tag
  81. *
  82. * @return mixed|string - returns html which will be render in hook
  83. * @since 4.2
  84. * @static
  85. *
  86. */
  87. public static function renderSettingsField( $name, $param_settings, $param_value, $tag ) {
  88. if ( isset( self::$params[ $name ]['callbacks']['form'] ) ) {
  89. return call_user_func( self::$params[ $name ]['callbacks']['form'], $param_settings, $param_value, $tag );
  90. }
  91. return '';
  92. }
  93. /**
  94. * List of javascript files urls for shortcode attributes.
  95. * @return array - list of js scripts
  96. * @since 4.2
  97. * @static
  98. */
  99. public static function getScripts() {
  100. return self::$scripts;
  101. }
  102. }