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