calderawpcfeddfieldssetup

The Caldera Forms EDD calderawp cfeddfields setup class.

Defined (1)

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

/vendor/calderawp/cf-edd-fields/src/setup.php  
  1. class setup { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected static $slug = 'edd-licensed-downloads'; 
  7.  
  8. /** 
  9. * Add hooks 
  10. */ 
  11. public static function add_hooks() { 
  12. add_action( 'caldera_forms_pre_load_processors', [ __CLASS__, 'add_processor' ] ); 
  13. add_filter( 'caldera_forms_render_get_field', [ __CLASS__, 'init_license_field' ], 99, 2 ); 
  14.  
  15. /** 
  16. * Remove hooks 
  17. */ 
  18. public static function remove_hooks() { 
  19. remove_action( 'caldera_forms_pre_load_processors', [ __CLASS__, 'add_processor' ] ); 
  20. remove_filter( 'caldera_forms_render_get_field', [ __CLASS__, 'init_license_field' ], 99 ); 
  21.  
  22. /** 
  23. * Load the EDD SL processor 
  24. * @uses "caldera_forms_pre_load_processors" action 
  25. */ 
  26. public static function add_processor() { 
  27.  
  28. $config = [ 
  29. "name" => __( 'EDD: Licensed Downloads', 'cf-edd'),  
  30. "description" => __( 'Populate a select field with a user\'s licensed downloads.', 'cf-edd'),  
  31. "icon" => plugin_dir_url( __FILE__ ) . '/icon.png',  
  32. "author" => "Josh Pollock for CalderaWP LLC",  
  33. "author_url" => "https://CalderaWP.com",  
  34. "template" => __DIR__ . '/config-licensed-downloads.php',  
  35. ]; 
  36.  
  37. new processor( $config, self::processor_fields(), self::$slug ); 
  38.  
  39.  
  40. public static function processor_fields() { 
  41. return [ 
  42. 'id' => 'edd_licensed_downloads',  
  43. 'label' => __( 'Field', 'cf-edd' ),  
  44. 'type' => 'advanced',  
  45. 'desc' => __( 'Choose a select field to populate with licensed downloads for user specified in next option.', 'cf-edd' ),  
  46. 'allow_types' => [ 
  47. 'dropdown', 'select2', 'radio', 'checkbox' 
  48. ],  
  49. 'exclude' => [ 
  50. 'system', 'variables' 
  51.  
  52. ],  
  53. 'id' => 'edd_licensed_downloads_user',  
  54. 'label' => __( 'User ID', 'cf-edd' ),  
  55. 'desc' => __( 'User ID to get licensed downloads for. Leave empty for current user ID. ', 'cf-edd' ),  
  56. 'type' => 'text',  
  57. 'required' => false,  
  58. 'magic' => 'true',  
  59.  
  60. ],  
  61. 'id' => 'edd_licensed_downloads_none',  
  62. 'label' => __( 'Empty Message', 'cf-edd' ),  
  63. 'type' => 'text',  
  64. 'required' => false,  
  65. 'magic' => 'true',  
  66. 'desc' => __( 'No licensed Downloads Found Message', 'cf-form-connector' ),  
  67. ]; 
  68.  
  69. /** 
  70. * Setup license field for EDD SL processor 
  71. * @uses "caldera_forms_render_get_field" filter 
  72. * @param array $field 
  73. * @param array $form 
  74. * @return array 
  75. */ 
  76. public static function init_license_field( $field, $form ) { 
  77.  
  78. if( $processors = \Caldera_Forms::get_processor_by_type( 'edd-licensed-downloads', $form ) ) { 
  79. foreach( $processors as $processor ) { 
  80. if( $field['ID'] === $processor['config']['edd_licensed_downloads'] ) { 
  81. $user_id = null; 
  82. if ( ! empty( $config[ 'config' ][ 'edd_licensed_downloads_user' ] ) && 0 < absint( $config[ 'config' ][ 'edd_licensed_downloads_user' ] ) ) { 
  83. $user_id = $config[ 'config' ][ 'edd_licensed_downloads_user' ]; 
  84. }elseif ( is_user_logged_in() ) { 
  85. $user_id = get_current_user_id(); 
  86.  
  87. $downloads = license::get_downloads_by_licensed_user( $user_id ); 
  88. $field[ 'config' ][ 'option' ] = array(); 
  89. if ( ! empty( $downloads ) ) { 
  90. foreach( $downloads as $id => $title ) { 
  91. $field[ 'config' ][ 'option' ][] = array( 
  92. 'label' => esc_html( $title ),  
  93. 'value' => (string) $id,  
  94. ); 
  95.  
  96. //Worksround for https://github.com/CalderaWP/Caldera-Forms/issues/1074 
  97. $field[ 'config' ]['show_values'] = 1; 
  98.  
  99.  
  100. break; 
  101.  
  102.  
  103. return $field; 
  104.  
  105.