| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Shortcodes; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class declaration |
| 7 |
*/ |
| 8 |
class ShortcodesExecutor { |
| 9 |
|
| 10 |
private $shortcodes = []; |
| 11 |
|
| 12 |
private $data = [ |
| 13 |
'is_sample' => true, |
| 14 |
]; |
| 15 |
|
| 16 |
public function __construct( $shortcodes = [], $data = [] ) { |
| 17 |
if ( ! isset( $shortcodes ) || ! is_array( $shortcodes ) ) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Extra shortcodes: order meta... |
| 23 |
*/ |
| 24 |
$this->shortcodes = apply_filters( 'yaymail_extra_shortcodes', $shortcodes, $data ); |
| 25 |
|
| 26 |
if ( isset( $data ) && is_array( $data ) ) { |
| 27 |
$this->data = $data; |
| 28 |
} |
| 29 |
|
| 30 |
$this->initialize_shortcodes(); |
| 31 |
} |
| 32 |
|
| 33 |
public function initialize_shortcodes() { |
| 34 |
if ( empty( $this->shortcodes ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
foreach ( $this->shortcodes as $shortcode_information ) { |
| 39 |
$callback = isset( $shortcode_information['callback'] ) ? $shortcode_information['callback'] : ''; |
| 40 |
$callback_args = isset( $shortcode_information['callback_args'] ) ? $shortcode_information['callback_args'] : ''; |
| 41 |
|
| 42 |
$data = ! empty( $callback_args ) && is_array( $callback_args ) ? array_merge( $this->data, $callback_args ) : $this->data; |
| 43 |
|
| 44 |
if ( is_callable( $callback ) ) { |
| 45 |
add_shortcode( |
| 46 |
$shortcode_information['name'], |
| 47 |
function( $shortcode_atts ) use ( $callback, $data ) { |
| 48 |
return call_user_func( $callback, $data, $shortcode_atts ); |
| 49 |
} |
| 50 |
); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public function get_shortcodes_content() { |
| 56 |
if ( empty( $this->shortcodes ) ) { |
| 57 |
return []; |
| 58 |
} |
| 59 |
|
| 60 |
$result = []; |
| 61 |
|
| 62 |
foreach ( $this->shortcodes as $shortcode_information ) { |
| 63 |
$shortcode_name = '[' . $shortcode_information['name'] . ']'; |
| 64 |
$result[] = array_merge( |
| 65 |
$shortcode_information, |
| 66 |
[ |
| 67 |
'content' => do_shortcode( $shortcode_name ), |
| 68 |
] |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
return $result; |
| 73 |
} |
| 74 |
} |
| 75 |
|