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
send-email-hooks.php
3 years ago
actions-tools.php
104 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 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 10 | |
| 11 | class Actions_Tools { |
| 12 | |
| 13 | /** |
| 14 | * @param string $flow_path |
| 15 | * |
| 16 | * @return \Generator |
| 17 | */ |
| 18 | public static function get_flow( string $flow_path ): \Generator { |
| 19 | $flow = self::load_flow( $flow_path ); |
| 20 | |
| 21 | foreach ( $flow as $action ) { |
| 22 | try { |
| 23 | $current = jet_form_builder()->actions->get_action( $action['type'] ); |
| 24 | } catch ( Repository_Exception $exception ) { |
| 25 | continue; |
| 26 | } |
| 27 | |
| 28 | $current->settings = $action['settings'] ?? array(); |
| 29 | |
| 30 | yield array( $current, $action ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param string $flow_path |
| 36 | * |
| 37 | * @return array |
| 38 | */ |
| 39 | public static function load_flow( string $flow_path ): array { |
| 40 | if ( ! file_exists( $flow_path ) ) { |
| 41 | return array(); |
| 42 | } |
| 43 | |
| 44 | return is_file( $flow_path ) |
| 45 | ? self::load_flow_file( $flow_path ) |
| 46 | : self::load_flow_dir( $flow_path ); |
| 47 | } |
| 48 | |
| 49 | private static function load_flow_dir( string $flow_dir ) { |
| 50 | $flow_dir = trailingslashit( $flow_dir ); |
| 51 | |
| 52 | $paths = array( |
| 53 | $flow_dir . 'flow.php', |
| 54 | $flow_dir . 'flow.json' |
| 55 | ); |
| 56 | |
| 57 | foreach ( $paths as $path ) { |
| 58 | $flow = self::load_flow( $path ); |
| 59 | |
| 60 | if ( ! $flow ) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | return $flow; |
| 65 | } |
| 66 | |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | private static function load_flow_file( string $flow_file ) { |
| 71 | $flow = false; |
| 72 | |
| 73 | switch ( File_Tools::get_file_ext( $flow_file ) ) { |
| 74 | case 'json': |
| 75 | $flow = self::load_flow_json( $flow_file ); |
| 76 | break; |
| 77 | case 'php': |
| 78 | $flow = self::load_flow_php( $flow_file ); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | if ( ! is_array( $flow ) || empty( $flow ) ) { |
| 83 | return array(); |
| 84 | } |
| 85 | |
| 86 | return $flow; |
| 87 | } |
| 88 | |
| 89 | private static function load_flow_json( string $flow_file ) { |
| 90 | return wp_json_file_decode( |
| 91 | $flow_file, |
| 92 | array( 'associative' => true ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | private static function load_flow_php( string $file_name ) { |
| 97 | if ( ! is_readable( $file_name ) ) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | return include_once $file_name; |
| 102 | } |
| 103 | |
| 104 | } |