| 1 |
<?php |
| 2 |
/** |
| 3 |
* Callback block. |
| 4 |
* |
| 5 |
* @package HivePress\Blocks |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace HivePress\Blocks; |
| 9 |
|
| 10 |
use HivePress\Helpers as hp; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Renders the callback result. |
| 17 |
*/ |
| 18 |
class Callback extends Block { |
| 19 |
|
| 20 |
/** |
| 21 |
* Callback name. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $callback; |
| 26 |
|
| 27 |
/** |
| 28 |
* Callback parameters. |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $params = []; |
| 33 |
|
| 34 |
/** |
| 35 |
* Return the output? |
| 36 |
* |
| 37 |
* @var bool |
| 38 |
*/ |
| 39 |
protected $return = false; |
| 40 |
|
| 41 |
/** |
| 42 |
* Renders block HTML. |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function render() { |
| 47 |
$output = ''; |
| 48 |
|
| 49 |
if ( function_exists( $this->callback ) ) { |
| 50 |
if ( $this->return ) { |
| 51 |
$output = call_user_func_array( $this->callback, $this->params ); |
| 52 |
} else { |
| 53 |
ob_start(); |
| 54 |
|
| 55 |
call_user_func_array( $this->callback, $this->params ); |
| 56 |
$output = ob_get_contents(); |
| 57 |
|
| 58 |
ob_end_clean(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return $output; |
| 63 |
} |
| 64 |
} |
| 65 |
|