conditions
3 years ago
events
3 years ago
methods
3 years ago
types
3 years ago
action-handler.php
3 years ago
action-localize.php
3 years ago
actions-tools.php
3 years ago
events-list.php
3 years ago
events-manager.php
3 years ago
manager.php
3 years ago
actions-tools.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Actions; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Actions\Types\Base; |
| 8 | use Jet_Form_Builder\Classes\Resources\File_Tools; |
| 9 | |
| 10 | class Actions_Tools { |
| 11 | |
| 12 | public static function run_flow( string $flow_path ) { |
| 13 | $actions = self::get_flow( $flow_path ); |
| 14 | |
| 15 | foreach ( $actions as $action ) { |
| 16 | jet_fb_action_handler()->process_single_action( $action ); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @param string $flow_path |
| 22 | * |
| 23 | * @return Base[] |
| 24 | */ |
| 25 | public static function get_flow( string $flow_path ): array { |
| 26 | $flow = self::load_flow( $flow_path ); |
| 27 | $actions = array(); |
| 28 | |
| 29 | foreach ( $flow as $action ) { |
| 30 | $current = jet_fb_action_handler()->add_hidden( $action['type'], $action ); |
| 31 | |
| 32 | if ( ! $current ) { |
| 33 | continue; |
| 34 | } |
| 35 | $current->settings = $action['settings'] ?? array(); |
| 36 | |
| 37 | $actions[] = $current; |
| 38 | } |
| 39 | |
| 40 | return $actions; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param string $flow_path |
| 45 | * |
| 46 | * @return array |
| 47 | */ |
| 48 | public static function load_flow( string $flow_path ): array { |
| 49 | if ( ! file_exists( $flow_path ) ) { |
| 50 | return array(); |
| 51 | } |
| 52 | |
| 53 | return is_file( $flow_path ) |
| 54 | ? self::load_flow_file( $flow_path ) |
| 55 | : self::load_flow_dir( $flow_path ); |
| 56 | } |
| 57 | |
| 58 | private static function load_flow_dir( string $flow_dir ) { |
| 59 | $flow_dir = trailingslashit( $flow_dir ); |
| 60 | |
| 61 | $paths = array( |
| 62 | $flow_dir . 'flow.php', |
| 63 | $flow_dir . 'flow.json' |
| 64 | ); |
| 65 | |
| 66 | foreach ( $paths as $path ) { |
| 67 | $flow = self::load_flow( $path ); |
| 68 | |
| 69 | if ( ! $flow ) { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | return $flow; |
| 74 | } |
| 75 | |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | private static function load_flow_file( string $flow_file ) { |
| 80 | $flow = false; |
| 81 | |
| 82 | switch ( File_Tools::get_file_ext( $flow_file ) ) { |
| 83 | case 'json': |
| 84 | $flow = self::load_flow_json( $flow_file ); |
| 85 | break; |
| 86 | case 'php': |
| 87 | $flow = self::load_flow_php( $flow_file ); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | if ( ! is_array( $flow ) || empty( $flow ) ) { |
| 92 | return array(); |
| 93 | } |
| 94 | |
| 95 | return $flow; |
| 96 | } |
| 97 | |
| 98 | private static function load_flow_json( string $flow_file ) { |
| 99 | return wp_json_file_decode( |
| 100 | $flow_file, |
| 101 | array( 'associative' => true ) |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | private static function load_flow_php( string $file_name ) { |
| 106 | if ( ! is_readable( $file_name ) ) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | return include_once $file_name; |
| 111 | } |
| 112 | |
| 113 | } |