Caldera_Forms_API_JsConfig

Abstraction for creating data to pass to wp_localize_script when working with Caldera Forms REST API.

Defined (1)

The class is defined in the following location(s).

/classes/api/jsconfig.php  
  1. class Caldera_Forms_API_JsConfig implements JsonSerializable{ 
  2.  
  3. /** 
  4. * Form config 
  5. * @since 1.5.0 
  6. * @var array 
  7. */ 
  8. protected $form; 
  9.  
  10. /** 
  11. * Data to return 
  12. * @since 1.5.0 
  13. * @var array 
  14. */ 
  15. protected $data; 
  16.  
  17. /** 
  18. * Caldera_Forms_API_JsConfig constructor. 
  19. * @param array $form Optional, form config with tying to a specific form 
  20. */ 
  21. public function __construct( array $form = array() ) { 
  22. $this->form = $form; 
  23.  
  24. /** 
  25. * @inheritdoc 
  26. * @since 1.5.0 
  27. */ 
  28. public function jsonSerialize() { 
  29. return $this->toArray(); 
  30.  
  31. /** 
  32. * Get data as array 
  33. * @since 1.5.0 
  34. * @return array 
  35. */ 
  36. public function toArray() { 
  37. if ( empty( $this->data ) ) { 
  38. $this->set_data(); 
  39.  
  40. return $this->data; 
  41.  
  42. /** 
  43. * Setup data to localize 
  44. * @since 1.5.0 
  45. */ 
  46. protected function set_data() { 
  47. $this->data = array( 
  48. 'dateFormat' => Caldera_Forms::time_format(),  
  49. 'api' => array( 
  50. 'root' => esc_url( trailingslashit( Caldera_Forms_API_Util::url() ) ),  
  51. 'form' => esc_url( trailingslashit( Caldera_Forms_API_Util::url( 'forms' ) ) ),  
  52. 'entries' => esc_url( trailingslashit( Caldera_Forms_API_Util::url( 'entries' ) ) ),  
  53. 'entrySettings' => esc_url( trailingslashit( Caldera_Forms_API_Util::url( 'settings/entries' ) ) ),  
  54. 'nonce' => wp_create_nonce( 'wp_rest' ),  
  55. 'token' => 'false',  
  56. ),  
  57. ); 
  58.  
  59. if( ! empty( $this->form ) ) { 
  60. $this->data[ 'formId' ] = $this->form[ 'ID' ]; 
  61.  
  62. /** 
  63. * Modify data to localize for API 
  64. * @since 1.5.0 
  65. * @param array $data The data 
  66. * @param array|null $form Form config if used, might be empty 
  67. */ 
  68. $this->data = apply_filters( 'caldera_forms_api_js_config', $this->data, $this->form ); 
  69.