| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Core; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 6 |
use WPDeveloper\BetterDocs\Utils\Helper; |
| 7 |
use WPDeveloper\BetterDocs\Traits\EditorHelper; |
| 8 |
use WPDeveloper\BetterDocs\Admin\Customizer\Defaults; |
| 9 |
|
| 10 |
abstract class Shortcode extends Base { |
| 11 |
use EditorHelper; |
| 12 |
|
| 13 |
/** |
| 14 |
* Global Settings Reference. |
| 15 |
* @var Settings |
| 16 |
*/ |
| 17 |
protected $settings; |
| 18 |
/** |
| 19 |
* Global Query Reference. |
| 20 |
* @var Query |
| 21 |
*/ |
| 22 |
protected $query; |
| 23 |
/** |
| 24 |
* Utility Helper |
| 25 |
* @var Helper |
| 26 |
*/ |
| 27 |
protected $helper; |
| 28 |
/** |
| 29 |
* Customizer Defaults. |
| 30 |
* @var Defaults |
| 31 |
*/ |
| 32 |
protected $customizer; |
| 33 |
|
| 34 |
public function __construct( Settings $settings, Query $query, Helper $helper, Defaults $defaults ) { |
| 35 |
$this->settings = $settings; |
| 36 |
$this->query = $query; |
| 37 |
$this->helper = $helper; |
| 38 |
$this->customizer = $defaults; |
| 39 |
|
| 40 |
// add_action( 'betterdocs_before_shortcode_load', [$this, 'enqueue_scripts'], 11, 1 ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Shortcode Tag Name. |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
abstract public function get_name(); |
| 48 |
|
| 49 |
/** |
| 50 |
* A list of default attributes. |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
abstract public function default_attributes(); |
| 54 |
|
| 55 |
/** |
| 56 |
* Individual shortcode render callback. |
| 57 |
* @param mixed $atts |
| 58 |
* @param mixed $content |
| 59 |
* @return mixed |
| 60 |
*/ |
| 61 |
abstract public function render( $atts, $content = null ); |
| 62 |
|
| 63 |
public function get_style_depends() { |
| 64 |
return []; |
| 65 |
} |
| 66 |
|
| 67 |
public function get_script_depends() { |
| 68 |
return []; |
| 69 |
} |
| 70 |
|
| 71 |
public function enqueue_scripts() { |
| 72 |
foreach( $this->get_style_depends() as $handle ) { |
| 73 |
wp_enqueue_style( $handle ); |
| 74 |
} |
| 75 |
|
| 76 |
foreach( $this->get_script_depends() as $handle ) { |
| 77 |
wp_enqueue_script( $handle ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Shortcode render callback, which is hooked with add_shortcode function. |
| 83 |
* |
| 84 |
* @param mixed $atts |
| 85 |
* @param mixed $content |
| 86 |
* |
| 87 |
* @return bool|string |
| 88 |
*/ |
| 89 |
final public function render_with_hooks( $atts, $content = null ) { |
| 90 |
$this->enqueue_scripts(); |
| 91 |
|
| 92 |
$atts = $this->remove_deprecated_attributes( $atts ); |
| 93 |
$this->transform_attribute_types( $atts ); |
| 94 |
|
| 95 |
$this->client_attributes = empty( $atts ) ? [] : $atts; |
| 96 |
do_action( 'betterdocs_before_shortcode_load', $this ); |
| 97 |
$this->set_attributes( $atts ); |
| 98 |
|
| 99 |
// reset attributes; |
| 100 |
$this->reset_attributes(); |
| 101 |
|
| 102 |
do_action_ref_array( 'betterdocs_before_render', [ &$this, 'shortcode' ] ); |
| 103 |
|
| 104 |
ob_start(); |
| 105 |
$this->render( $atts, $content = null ); |
| 106 |
$content = ob_get_clean(); |
| 107 |
|
| 108 |
do_action_ref_array( 'betterdocs_after_render', [ &$this, 'shortcode' ] ); |
| 109 |
|
| 110 |
return $content; |
| 111 |
} |
| 112 |
|
| 113 |
protected function transform_attribute_types( &$atts ) { |
| 114 |
if( is_array( $atts ) ) { |
| 115 |
$_default_attributes = $this->default_attributes(); |
| 116 |
|
| 117 |
foreach( $atts as $key => $value ) { |
| 118 |
if( isset( $_default_attributes[ $key ] ) ) { |
| 119 |
$type = gettype( $_default_attributes[ $key ] ); |
| 120 |
|
| 121 |
if( $type === 'boolean' ) { |
| 122 |
$atts[ $key ] = $atts[ $key ] == 'false' || $atts[ $key ] == '' ? false : true; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* This method is responsive for set shortcode attributes in a property. |
| 131 |
* @param mixed $atts |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
private function set_attributes( $atts ) { |
| 135 |
$this->attributes = shortcode_atts( |
| 136 |
apply_filters( "betterdocs_shortcodes_default_atts", $this->default_attributes(), $this->get_name() ), |
| 137 |
$atts |
| 138 |
); |
| 139 |
|
| 140 |
return $this->attributes; |
| 141 |
} |
| 142 |
|
| 143 |
public function isset( $key, $value = null ) { |
| 144 |
if ( null === $value ) { |
| 145 |
return isset( $this->attributes[$key] ) && ! empty( $this->attributes[$key] ); |
| 146 |
} |
| 147 |
|
| 148 |
return isset( $this->attributes[$key] ) && $this->attributes[$key] == $value; |
| 149 |
} |
| 150 |
} |
| 151 |
|