| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Template |
| 5 |
* |
| 6 |
* @since 3.3.0 |
| 7 |
*/ |
| 8 |
class LP_Abstract_Template { |
| 9 |
|
| 10 |
/** |
| 11 |
* @var LP_Template |
| 12 |
*/ |
| 13 |
protected static $instance = null; |
| 14 |
|
| 15 |
/** |
| 16 |
* LP_Template constructor. |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
} |
| 20 |
|
| 21 |
public function clearfix() { |
| 22 |
learn_press_get_template( 'global/clearfix' ); |
| 23 |
} |
| 24 |
|
| 25 |
public function callback( $template, $args = array() ) { |
| 26 |
return array( new LP_Template_Callback( $template, $args ), 'callback' ); |
| 27 |
} |
| 28 |
|
| 29 |
public function text( $text, $id = '' ) { |
| 30 |
return array( new LP_Template_Callback( $text, $id ), 'text' ); |
| 31 |
} |
| 32 |
|
| 33 |
public function __call( $name, $arguments ) { |
| 34 |
$log = sprintf( 'Template %s::%s doesn\'t exists.', get_class( $this ), $name ); |
| 35 |
error_log( $log ); |
| 36 |
|
| 37 |
if ( LP_Debug::is_debug() ) { |
| 38 |
echo sprintf( '<span title="%s" class="learn-press-template-warning"></span>', $log ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Return is callable method of self class. |
| 44 |
* |
| 45 |
* @since 3.3.0 |
| 46 |
* |
| 47 |
* @param string $callback |
| 48 |
* |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
public function func( $callback ) { |
| 52 |
return array( $this, $callback ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Add callable method of self class to a hook of template. |
| 57 |
* |
| 58 |
* @param string $name |
| 59 |
* @param string $callback |
| 60 |
* @param int $priority |
| 61 |
* @param int $number_args |
| 62 |
*/ |
| 63 |
public function hook( $name, $callback, $priority = 10, $number_args = 1 ) { |
| 64 |
add_action( $name, $this->func( $callback ), $priority, $number_args ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Remove hooked callable method. |
| 69 |
* |
| 70 |
* @param string $tag |
| 71 |
* @param string $function_to_remove - '*' will remove all methods. |
| 72 |
* @param int $priority |
| 73 |
* |
| 74 |
* Ex: Remove for text: LearnPress::instance()->template( 'course' )->remove( 'learn-press/course-content-summary', array( '<div class="course-detail-info"> <div class="lp-content-area"> <div class="course-info-left">', 'course-info-left-open' ), 10 ); |
| 75 |
*/ |
| 76 |
public function remove( $tag, $function_to_remove, $priority = 10 ) { |
| 77 |
global $wp_filter; |
| 78 |
|
| 79 |
try { |
| 80 |
if ( is_array( $function_to_remove ) ) { |
| 81 |
if ( empty( $wp_filter[ $tag ] ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$callbacks = $wp_filter[ $tag ]->callbacks; |
| 86 |
if ( ! $callbacks ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$priorities = array_keys( $callbacks ); |
| 91 |
|
| 92 |
foreach ( $priorities as $priority1 ) { |
| 93 |
|
| 94 |
if ( $priority !== '*' && $priority !== $priority1 ) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
if ( empty( $callbacks[ $priority1 ] ) ) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
foreach ( $callbacks[ $priority1 ] as $callback ) { |
| 103 |
|
| 104 |
if ( ! $callback['function'][0] instanceof LP_Template_Callback ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
if ( $callback['function'][0]->get_args() !== $function_to_remove[1] ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
remove_action( $tag, $callback['function'], $priority1 ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return; |
| 117 |
|
| 118 |
} |
| 119 |
} catch ( Throwable $e ) { |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* $function_to_remove === '*' will remove all functions hooked into a hook |
| 125 |
* in all priorities |
| 126 |
*/ |
| 127 |
if ( $function_to_remove === '*' ) { |
| 128 |
if ( ! empty( $wp_filter[ $tag ] ) ) { |
| 129 |
unset( $wp_filter[ $tag ] ); |
| 130 |
} |
| 131 |
|
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* $priority === '*' will remove all functions hooked into a hook |
| 137 |
* in |
| 138 |
*/ |
| 139 |
if ( $priority === '*' ) { |
| 140 |
|
| 141 |
if ( ! empty( $wp_filter[ $tag ]->callbacks ) ) { |
| 142 |
$priorities = array_keys( $wp_filter[ $tag ]->callbacks ); |
| 143 |
|
| 144 |
foreach ( $priorities as $priority ) { |
| 145 |
remove_action( $tag, $this->func( $function_to_remove ), $priority ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return; |
| 150 |
} |
| 151 |
remove_action( $tag, $this->func( $function_to_remove ), $priority ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Remove callback function. |
| 156 |
* |
| 157 |
* @param [type] $tag |
| 158 |
* @param [type] $function |
| 159 |
* @param [type] $priority |
| 160 |
* @return void |
| 161 |
* |
| 162 |
* @author Nhamdv <email@email.com> |
| 163 |
* |
| 164 |
* Ex: LearnPress::instance()->template( 'course' )->remove_callback( 'learn-press/course-content-summary', 'single-course/title', 10 ); |
| 165 |
* |
| 166 |
*/ |
| 167 |
public static function remove_callback( $tag, $function, $priority ) { |
| 168 |
global $wp_filter; |
| 169 |
|
| 170 |
if ( empty( $wp_filter[ $tag ] ) ) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
try { |
| 175 |
$callbacks = $wp_filter[ $tag ]->callbacks; |
| 176 |
|
| 177 |
if ( ! $callbacks ) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
$priorities = array_keys( $callbacks ); |
| 182 |
|
| 183 |
foreach ( $priorities as $priority1 ) { |
| 184 |
|
| 185 |
if ( $priority !== '*' && $priority !== $priority1 ) { |
| 186 |
continue; |
| 187 |
} |
| 188 |
|
| 189 |
if ( empty( $callbacks[ $priority1 ] ) ) { |
| 190 |
continue; |
| 191 |
} |
| 192 |
|
| 193 |
foreach ( $callbacks[ $priority1 ] as $callback ) { |
| 194 |
|
| 195 |
if ( ! $callback['function'][0] instanceof LP_Template_Callback ) { |
| 196 |
continue; |
| 197 |
} |
| 198 |
|
| 199 |
if ( $callback['function'][1] !== 'callback' ) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
if ( $callback['function'][0]->get_template() !== $function ) { |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
remove_action( $tag, $callback['function'], $priority1 ); |
| 208 |
} |
| 209 |
} |
| 210 |
} catch ( Throwable $e ) { |
| 211 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* @return LP_Template |
| 217 |
*/ |
| 218 |
public static function instance() { |
| 219 |
if ( ! self::$instance ) { |
| 220 |
self::$instance = new self(); |
| 221 |
} |
| 222 |
|
| 223 |
return self::$instance; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
class LP_Template_Callback { |
| 229 |
/** |
| 230 |
* @var string |
| 231 |
*/ |
| 232 |
protected $template = ''; |
| 233 |
|
| 234 |
/** |
| 235 |
* @var array |
| 236 |
*/ |
| 237 |
protected $args = array(); |
| 238 |
|
| 239 |
/** |
| 240 |
* LP_Template_Caller constructor. |
| 241 |
* |
| 242 |
* @param $template |
| 243 |
* @param array $args |
| 244 |
*/ |
| 245 |
public function __construct( $template, $args = array() ) { |
| 246 |
$this->template = $template; |
| 247 |
$this->args = $args; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* |
| 252 |
*/ |
| 253 |
public function callback() { |
| 254 |
$template_args = array(); |
| 255 |
|
| 256 |
if ( $this->args ) { |
| 257 |
foreach ( $this->args as $k => $v ) { |
| 258 |
if ( is_numeric( $k ) && is_string( $v ) ) { |
| 259 |
$template_args[ $v ] = func_get_arg( $k ); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
learn_press_get_template( $this->template, $template_args ); |
| 265 |
} |
| 266 |
|
| 267 |
public function text() { |
| 268 |
learn_press_echo_vuejs_write_on_php( $this->template ); |
| 269 |
} |
| 270 |
|
| 271 |
public function get_args() { |
| 272 |
return $this->args; |
| 273 |
} |
| 274 |
|
| 275 |
public function get_template() { |
| 276 |
return $this->template; |
| 277 |
} |
| 278 |
} |
| 279 |
|