| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MWCODE_API |
| 4 |
{ |
| 5 |
private $core = null; |
| 6 |
private $snippet = null; |
| 7 |
|
| 8 |
|
| 9 |
public function __construct( $core, $snippet ) { |
| 10 |
$this->core = $core; |
| 11 |
$this->snippet = $snippet; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Get a function by its ID. |
| 16 |
* |
| 17 |
* The options are used to filter the functions: |
| 18 |
* - 'php_ready_args' (bool): If false, the arguments will not be formatted for PHP. (no $ before the names). |
| 19 |
* |
| 20 |
* @param $id |
| 21 |
* @param array $options |
| 22 |
* |
| 23 |
* @return mixed |
| 24 |
*/ |
| 25 |
public function get_function( $id, $options = [] ) { |
| 26 |
$snippet = $this->snippet->get_function( $id, $options ); |
| 27 |
return $snippet; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get all functions. |
| 32 |
* |
| 33 |
* @return mixed |
| 34 |
* |
| 35 |
*/ |
| 36 |
public function get_functions( $safe = true ) { |
| 37 |
$snippets = $this->snippet->get_functions(); |
| 38 |
|
| 39 |
if ( $safe ) { |
| 40 |
|
| 41 |
$snippets = array_filter( $snippets, function( $snippet ) { |
| 42 |
$name = $snippet['name']; |
| 43 |
if ( !preg_match( '/^[a-zA-Z0-9_-]{1,64}$/', $name ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
return true; |
| 48 |
} ); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
return $snippets; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Execute a function by its ID. |
| 57 |
* The arguments should be an associative array with the argument names as keys. |
| 58 |
* Example: [ "$city" => " 'Tokyo' ", "$date" => "1999" ] |
| 59 |
* |
| 60 |
* @param $id |
| 61 |
* @param $args |
| 62 |
* |
| 63 |
* @return mixed |
| 64 |
*/ |
| 65 |
public function execute_function( $id, $args ) { |
| 66 |
$output = $this->core->run_snippet( $id, $args ); |
| 67 |
return $output; |
| 68 |
} |
| 69 |
} |
| 70 |
|