| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Integration; |
| 4 |
|
| 5 |
use Code_Snippets\Core\DB; |
| 6 |
use Code_Snippets\Flat_Files\Snippet_Files; |
| 7 |
use Code_Snippets\Model\Snippet; |
| 8 |
use Code_Snippets\Utils\Code_Highlighter; |
| 9 |
use WP_Post; |
| 10 |
use function Code_Snippets\code_snippets; |
| 11 |
use function Code_Snippets\get_snippet; |
| 12 |
use function Code_Snippets\Settings\get_setting; |
| 13 |
|
| 14 |
/** |
| 15 |
* This class manages the shortcodes included with the plugin, |
| 16 |
* |
| 17 |
* @package Code_Snippets |
| 18 |
*/ |
| 19 |
class Shortcodes { |
| 20 |
|
| 21 |
/** |
| 22 |
* Name of the shortcode tag for rendering the code source |
| 23 |
*/ |
| 24 |
public const SOURCE_SHORTCODE = 'code_snippet_source'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Name of the shortcode tag for rendering content snippets |
| 28 |
*/ |
| 29 |
public const CONTENT_SHORTCODE = 'code_snippet'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Class constructor |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
add_action( 'the_posts', [ $this, 'enqueue_highlighting' ] ); |
| 36 |
|
| 37 |
add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] ); |
| 38 |
add_shortcode( self::SOURCE_SHORTCODE, [ $this, 'render_source_shortcode' ] ); |
| 39 |
|
| 40 |
add_filter( 'code_snippets/render_content_shortcode', 'trim' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Enqueue the syntax highlighting assets if they are required for the current posts |
| 45 |
* |
| 46 |
* @param array<WP_Post|int>|null|false $posts List of currently visible posts. |
| 47 |
* |
| 48 |
* @return array<WP_Post|int>|null|false Unchanged list of posts. |
| 49 |
*/ |
| 50 |
public function enqueue_highlighting( $posts ) { |
| 51 |
if ( empty( $posts ) || get_setting( 'general', 'disable_prism' ) ) { |
| 52 |
return $posts; |
| 53 |
} |
| 54 |
|
| 55 |
// Loop through the posts, checking for an existing shortcode, short-circuiting if possible. |
| 56 |
$found_shortcode_content = null; |
| 57 |
|
| 58 |
foreach ( $posts as $post ) { |
| 59 |
if ( false !== stripos( $post->post_content, '[' . self::SOURCE_SHORTCODE ) || |
| 60 |
false !== strpos( $post->post_content, '<!-- wp:code-snippets/source ' ) ) { |
| 61 |
$found_shortcode_content = $post->post_content; |
| 62 |
break; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Load assets on the appropriate hook if a matching shortcode was found. |
| 67 |
if ( null !== $found_shortcode_content ) { |
| 68 |
Code_Highlighter::register_prism_assets(); |
| 69 |
|
| 70 |
add_action( |
| 71 |
'wp_enqueue_scripts', |
| 72 |
function () { |
| 73 |
wp_enqueue_style( Code_Highlighter::PRISM_HANDLE ); |
| 74 |
wp_enqueue_script( Code_Highlighter::PRISM_HANDLE ); |
| 75 |
}, |
| 76 |
100 |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
return $posts; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Print a message to the user if the snippet ID attribute is invalid. |
| 85 |
* |
| 86 |
* @param int $id Snippet ID. |
| 87 |
* |
| 88 |
* @return string Warning message. |
| 89 |
*/ |
| 90 |
protected function invalid_id_warning( int $id ): string { |
| 91 |
// translators: %d: snippet ID. |
| 92 |
$text = esc_html__( 'Could not load snippet with an invalid ID: %d.', 'code-snippets' ); |
| 93 |
return current_user_can( 'edit_posts' ) ? sprintf( $text, $id ) : ''; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Allow boolean attributes to be provided without a value, similar to how React works. |
| 98 |
* |
| 99 |
* @param array<string|number, mixed> $atts Unfiltered shortcode attributes. |
| 100 |
* @param array<string> $boolean_flags List of attribute names with boolean values. |
| 101 |
* |
| 102 |
* @return array<string|number, mixed> Shortcode attributes with flags converted to attributes. |
| 103 |
*/ |
| 104 |
protected function convert_boolean_attribute_flags( array $atts, array $boolean_flags ): array { |
| 105 |
foreach ( $atts as $key => $value ) { |
| 106 |
if ( in_array( $value, $boolean_flags, true ) && ! isset( $atts[ $value ] ) ) { |
| 107 |
$atts[ $value ] = true; |
| 108 |
unset( $atts[ $key ] ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return $atts; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Build the file path for a snippet's flat file. |
| 117 |
* |
| 118 |
* @param string $table_name Table name for the snippet. |
| 119 |
* @param Snippet $snippet Snippet object. |
| 120 |
* |
| 121 |
* @return string Full file path for the snippet. |
| 122 |
*/ |
| 123 |
private function build_snippet_flat_file_path( string $table_name, Snippet $snippet ): string { |
| 124 |
$handler = code_snippets()->snippet_handler_registry->get_handler( $snippet->get_type() ); |
| 125 |
|
| 126 |
return Snippet_Files::get_base_dir( $table_name, $handler->get_dir_name() ) . '/' . $snippet->id . '.' . $handler->get_file_extension(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Evaluate the code from a content shortcode. |
| 131 |
* |
| 132 |
* @param Snippet $snippet Snippet. |
| 133 |
* @param array<string, mixed> $atts Shortcode attributes. |
| 134 |
* |
| 135 |
* @return string Evaluated shortcode content. |
| 136 |
*/ |
| 137 |
protected function evaluate_shortcode_content( Snippet $snippet, array $atts ): string { |
| 138 |
if ( empty( $atts['php'] ) ) { |
| 139 |
return $snippet->code; |
| 140 |
} |
| 141 |
|
| 142 |
if ( ! Snippet_Files::is_active() ) { |
| 143 |
return $this->evaluate_shortcode_from_db( $snippet, $atts ); |
| 144 |
} |
| 145 |
|
| 146 |
$network = DB::validate_network_param( $snippet->network ); |
| 147 |
$table_name = Snippet_Files::get_hashed_table_name( code_snippets()->db->get_table_name( $network ) ); |
| 148 |
$filepath = $this->build_snippet_flat_file_path( $table_name, $snippet ); |
| 149 |
|
| 150 |
return file_exists( $filepath ) |
| 151 |
? $this->evaluate_shortcode_from_flat_file( $filepath, $atts ) |
| 152 |
: $this->evaluate_shortcode_from_db( $snippet, $atts ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Evaluate a snippet by evaluating its code as a string. |
| 157 |
* |
| 158 |
* @param Snippet $snippet Snippet to execute. |
| 159 |
* @param array $atts Shortcode attributes. |
| 160 |
* |
| 161 |
* @return string Snippet output. |
| 162 |
* |
| 163 |
* phpcs:disable Squiz.PHP.Eval.Discouraged |
| 164 |
*/ |
| 165 |
private function evaluate_shortcode_from_db( Snippet $snippet, array $atts ): string { |
| 166 |
/** |
| 167 |
* Avoiding extract is typically recommended, however in this situation we want to make it easy for snippet |
| 168 |
* authors to use custom attributes. |
| 169 |
* |
| 170 |
* phpcs:disable WordPress.PHP.DontExtract.extract_extract |
| 171 |
*/ |
| 172 |
extract( $atts, EXTR_SKIP ); |
| 173 |
|
| 174 |
ob_start(); |
| 175 |
eval( "?>\n\n" . $snippet->code ); |
| 176 |
|
| 177 |
return ob_get_clean(); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Evaluate a snippet by loading its code from the filesystem. |
| 182 |
* |
| 183 |
* @param string $filepath Path to file to evaluate. |
| 184 |
* @param array $atts Shortcode attributes. |
| 185 |
* |
| 186 |
* @return string Snippet output. |
| 187 |
*/ |
| 188 |
private function evaluate_shortcode_from_flat_file( string $filepath, array $atts ): string { |
| 189 |
ob_start(); |
| 190 |
|
| 191 |
( function ( $atts ) use ( $filepath ) { |
| 192 |
/** |
| 193 |
* Avoiding extract is typically recommended, however in this situation we want to make it easy for snippet |
| 194 |
* authors to use custom attributes. |
| 195 |
* |
| 196 |
* phpcs:ignore WordPress.PHP.DontExtract.extract_extract |
| 197 |
*/ |
| 198 |
extract( $atts, EXTR_SKIP ); |
| 199 |
require_once $filepath; |
| 200 |
} )( $atts ); |
| 201 |
|
| 202 |
return ob_get_clean(); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Retrieve a content snippet from the filesystem or the database. |
| 207 |
* |
| 208 |
* @param int $id Snippet identifier. |
| 209 |
* @param bool $network Whether the snippet is network-wide. |
| 210 |
* |
| 211 |
* @return Snippet|null |
| 212 |
*/ |
| 213 |
private function get_content_snippet( int $id, bool $network ): ?Snippet { |
| 214 |
if ( ! Snippet_Files::is_active() ) { |
| 215 |
return get_snippet( $id, $network ); |
| 216 |
} |
| 217 |
|
| 218 |
$validated_network = DB::validate_network_param( $network ); |
| 219 |
$table_name = Snippet_Files::get_hashed_table_name( code_snippets()->db->get_table_name( $validated_network ) ); |
| 220 |
$handler = code_snippets()->snippet_handler_registry->get_handler( 'html' ); |
| 221 |
$config_filepath = Snippet_Files::get_base_dir( $table_name, $handler->get_dir_name() ) . '/index.php'; |
| 222 |
|
| 223 |
if ( file_exists( $config_filepath ) ) { |
| 224 |
$config = require_once $config_filepath; |
| 225 |
$snippet_data = $config[ $id ] ?? null; |
| 226 |
|
| 227 |
if ( $snippet_data ) { |
| 228 |
$snippet = new Snippet( $snippet_data ); |
| 229 |
return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $network ); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return get_snippet( $id, $network ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Render the value of a content shortcode |
| 238 |
* |
| 239 |
* @param array<string, mixed> $atts Shortcode attributes. |
| 240 |
* |
| 241 |
* @return string Shortcode content. |
| 242 |
*/ |
| 243 |
public function render_content_shortcode( array $atts ): string { |
| 244 |
$atts = $this->convert_boolean_attribute_flags( $atts, [ 'network', 'php', 'format', 'shortcodes', 'debug' ] ); |
| 245 |
$original_atts = $atts; |
| 246 |
|
| 247 |
$atts = shortcode_atts( |
| 248 |
[ |
| 249 |
'id' => 0, |
| 250 |
'snippet_id' => 0, |
| 251 |
'network' => false, |
| 252 |
'php' => false, |
| 253 |
'format' => false, |
| 254 |
'shortcodes' => false, |
| 255 |
'debug' => false, |
| 256 |
], |
| 257 |
$atts, |
| 258 |
self::CONTENT_SHORTCODE |
| 259 |
); |
| 260 |
|
| 261 |
$id = 0 !== intval( $atts['snippet_id'] ) ? intval( $atts['snippet_id'] ) : intval( $atts['id'] ); |
| 262 |
if ( ! $id ) { |
| 263 |
return $this->invalid_id_warning( $id ); |
| 264 |
} |
| 265 |
|
| 266 |
$snippet = $this->get_content_snippet( $id, (bool) $atts['network'] ); |
| 267 |
|
| 268 |
// Render the source code if this is not a shortcode snippet. |
| 269 |
if ( 'content' !== $snippet->scope ) { |
| 270 |
return $snippet->id ? $this->render_snippet_source( $snippet ) : $this->invalid_id_warning( $snippet->id ); |
| 271 |
} |
| 272 |
|
| 273 |
// If the snippet is inactive, either display a message or render nothing. |
| 274 |
if ( ! $snippet->active ) { |
| 275 |
if ( ! $atts['debug'] ) { |
| 276 |
return ''; |
| 277 |
} |
| 278 |
|
| 279 |
/* translators: 1: snippet name, 2: snippet edit link */ |
| 280 |
$text = __( '%1$s is currently inactive. You can <a href="%2$s">edit this snippet</a> to activate it and make it visible. This message will not appear in the published post.', 'code-snippets' ); |
| 281 |
$snippet_name = '<strong>' . $snippet->name . '</strong>'; |
| 282 |
$edit_url = add_query_arg( 'id', $snippet->id, code_snippets()->get_menu_url( 'edit' ) ); |
| 283 |
|
| 284 |
return wp_kses( |
| 285 |
sprintf( $text, $snippet_name, $edit_url ), |
| 286 |
[ |
| 287 |
'strong' => [], |
| 288 |
'a' => [ |
| 289 |
'href' => [], |
| 290 |
], |
| 291 |
] |
| 292 |
); |
| 293 |
} |
| 294 |
|
| 295 |
$content = $this->evaluate_shortcode_content( $snippet, $original_atts ); |
| 296 |
|
| 297 |
if ( $atts['format'] ) { |
| 298 |
$functions = [ 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'capital_P_dangit' ]; |
| 299 |
foreach ( $functions as $function ) { |
| 300 |
$content = call_user_func( $function, $content ); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
if ( $atts['shortcodes'] ) { |
| 305 |
// Temporarily remove this shortcode from the list to prevent recursion while executing do_shortcode. |
| 306 |
remove_shortcode( self::CONTENT_SHORTCODE ); |
| 307 |
$content = do_shortcode( $atts['format'] ? shortcode_unautop( $content ) : $content ); |
| 308 |
add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] ); |
| 309 |
} |
| 310 |
|
| 311 |
return apply_filters( 'code_snippets/content_shortcode', $content, $snippet, $atts, $original_atts ); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Converts a value and key into an HTML attribute pair. |
| 316 |
* |
| 317 |
* @param string $value Attribute value. |
| 318 |
* @param string $key Attribute name. |
| 319 |
* |
| 320 |
* @return void |
| 321 |
*/ |
| 322 |
private static function create_attribute_pair( string &$value, string $key ) { |
| 323 |
$value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) ); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Render the source code of a given snippet |
| 328 |
* |
| 329 |
* @param Snippet $snippet Snippet object. |
| 330 |
* @param array<string, mixed> $atts Shortcode attributes. |
| 331 |
* |
| 332 |
* @return string Shortcode content. |
| 333 |
*/ |
| 334 |
private function render_snippet_source( Snippet $snippet, array $atts = [] ): string { |
| 335 |
$atts = array_merge( |
| 336 |
array( |
| 337 |
'line_numbers' => false, |
| 338 |
'highlight_lines' => '', |
| 339 |
), |
| 340 |
$atts |
| 341 |
); |
| 342 |
|
| 343 |
$language = 'css' === $snippet->type ? 'css' : ( 'js' === $snippet->type ? 'js' : 'php' ); |
| 344 |
|
| 345 |
$pre_attributes = array( |
| 346 |
'id' => "code-snippet-source-$snippet->id", |
| 347 |
'class' => 'code-snippet-source', |
| 348 |
); |
| 349 |
|
| 350 |
$code_attributes = array( |
| 351 |
'class' => "language-$language", |
| 352 |
); |
| 353 |
|
| 354 |
if ( $atts['line_numbers'] ) { |
| 355 |
$code_attributes['class'] .= ' line-numbers'; |
| 356 |
$pre_attributes['class'] .= ' linkable-line-numbers'; |
| 357 |
} |
| 358 |
|
| 359 |
if ( $atts['highlight_lines'] ) { |
| 360 |
$pre_attributes['data-line'] = $atts['highlight_lines']; |
| 361 |
} |
| 362 |
|
| 363 |
$pre_attributes = apply_filters( 'code_snippets/prism_pre_attributes', $pre_attributes, $snippet, $atts ); |
| 364 |
$code_attributes = apply_filters( 'code_snippets/prism_code_attributes', $code_attributes, $snippet, $atts ); |
| 365 |
|
| 366 |
array_walk( $code_attributes, array( $this, 'create_attribute_pair' ) ); |
| 367 |
array_walk( $pre_attributes, array( $this, 'create_attribute_pair' ) ); |
| 368 |
|
| 369 |
$code = 'php' === $snippet->type ? "<?php\n\n$snippet->code" : $snippet->code; |
| 370 |
|
| 371 |
$output = sprintf( |
| 372 |
'<pre %s><code %s>%s</code></pre>', |
| 373 |
implode( ' ', $pre_attributes ), |
| 374 |
implode( ' ', $code_attributes ), |
| 375 |
esc_html( $code ) |
| 376 |
); |
| 377 |
|
| 378 |
return apply_filters( 'code_snippets/render_source_shortcode', $output, $snippet, $atts ); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Render the value of a source shortcode |
| 383 |
* |
| 384 |
* @param array<string, mixed> $atts Shortcode attributes. |
| 385 |
* |
| 386 |
* @return string Shortcode content. |
| 387 |
*/ |
| 388 |
public function render_source_shortcode( array $atts ): string { |
| 389 |
$atts = $this->convert_boolean_attribute_flags( $atts, [ 'network', 'line_numbers' ] ); |
| 390 |
|
| 391 |
$atts = shortcode_atts( |
| 392 |
array( |
| 393 |
'id' => 0, |
| 394 |
'snippet_id' => 0, |
| 395 |
'network' => false, |
| 396 |
'line_numbers' => false, |
| 397 |
'highlight_lines' => '', |
| 398 |
), |
| 399 |
$atts, |
| 400 |
self::SOURCE_SHORTCODE |
| 401 |
); |
| 402 |
|
| 403 |
$id = 0 !== intval( $atts['snippet_id'] ) ? intval( $atts['snippet_id'] ) : intval( $atts['id'] ); |
| 404 |
if ( ! $id ) { |
| 405 |
return $this->invalid_id_warning( $id ); |
| 406 |
} |
| 407 |
|
| 408 |
$snippet = $this->get_content_snippet( $id, (bool) $atts['network'] ); |
| 409 |
return $this->render_snippet_source( $snippet, $atts ); |
| 410 |
} |
| 411 |
} |
| 412 |
|