This commit is contained in:
2026-10-06 18:51:23 -06:00
parent 8424dacd03
commit e2c26b7f21
4223 changed files with 737986 additions and 53 deletions
@@ -0,0 +1,56 @@
<?php
/**
* Execute CSS snippets and return their output.
*
* @package wpcode
*/
/**
* WPCode_Snippet_Execute_CSS class.
*/
class WPCode_Snippet_Execute_CSS extends WPCode_Snippet_Execute_Type {
/**
* The snippet type, JavaScript for this one.
*
* @var string
*/
public $type = 'css';
/**
* Grab snippet code and return its output.
*
* @return string
*/
protected function prepare_snippet_output() {
$code = $this->get_snippet_code();
$snippet_id = $this->snippet->get_id();
$style_id = 'wpcode-snippet-css-' . $snippet_id;
// Detect live preview context for this snippet.
$is_live_preview = false;
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( is_user_logged_in() && current_user_can( 'wpcode_edit_snippets' ) && isset( $_GET['wpcode_preview'], $_GET['snippet_id'] ) && '1' === $_GET['wpcode_preview'] && absint( $_GET['snippet_id'] ) === $snippet_id ) {
$is_live_preview = true;
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
// For normal output, only render when code is not empty.
// For live preview of this snippet, always output a style tag so the editor can target it,
// inserting a harmless comment placeholder when the CSS is empty.
if ( '' === trim( $code ) ) {
if ( ! $is_live_preview ) {
return '';
}
$code = '/* inserted by WPCode live preview */';
}
// In live preview add a deterministic id for targeting; otherwise keep original behavior (no id).
if ( $is_live_preview ) {
return '<style id="' . esc_attr( $style_id ) . '" class="wpcode-css-snippet">' . $code . '</style>';
}
return '<style class="wpcode-css-snippet">' . $code . '</style>';
}
}
@@ -0,0 +1,29 @@
<?php
/**
* Execute html snippets and return their output.
* This is probably the simplest one.
*
* @package wpcode
*/
/**
* WPCode_Snippet_Execute_HTML class.
*/
class WPCode_Snippet_Execute_HTML extends WPCode_Snippet_Execute_Type {
/**
* The snippet type, HTML for this one.
*
* @var string
*/
public $type = 'html';
/**
* Grab snippet code and return its output.
*
* @return string
*/
protected function prepare_snippet_output() {
return $this->get_snippet_code();
}
}
@@ -0,0 +1,35 @@
<?php
/**
* Execute JavaScript snippets and return their output.
*
* @package wpcode
*/
/**
* WPCode_Snippet_Execute_JS class.
*/
class WPCode_Snippet_Execute_JS extends WPCode_Snippet_Execute_Type {
/**
* The snippet type, JavaScript for this one.
*
* @var string
*/
public $type = 'js';
/**
* Grab snippet code and return its output.
*
* @return string
*/
protected function prepare_snippet_output() {
$code = $this->get_snippet_code();
if ( ! empty( $code ) ) {
// Wrap our code in a script tag.
$code = '<script>' . $code . '</script>';
}
return $code;
}
}
@@ -0,0 +1,34 @@
<?php
/**
* Execute php snippets and return their output.
*
* @package wpcode
*/
/**
* WPCode_Snippet_Execute_PHP class.
*/
class WPCode_Snippet_Execute_PHP extends WPCode_Snippet_Execute_Type {
/**
* The snippet type, PHP for this one.
*
* @var string
*/
public $type = 'php';
/**
* Grab snippet code and return its output.
*
* @return string
*/
protected function prepare_snippet_output() {
$code = $this->get_snippet_code();
// If we're doing the activation, unslash the code similar to how it will be unslashed before saving in wp_insert_post.
if ( wpcode()->execute->is_doing_activation() && isset( $_POST['wpcode_snippet_code'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$code = wp_unslash( $code );
}
return wpcode()->execute->safe_execute_php( $code, $this->snippet );
}
}
@@ -0,0 +1,33 @@
<?php
/**
* Execute text snippets and return their output.
*
* @package wpcode
*/
/**
* WPCode_Snippet_Execute_Text class.
*/
class WPCode_Snippet_Execute_Text extends WPCode_Snippet_Execute_Type {
/**
* The snippet type, Text for this one.
*
* @var string
*/
public $type = 'text';
/**
* Grab snippet code and return its output.
*
* @return string
*/
protected function prepare_snippet_output() {
// There's nothing to prepare here at this point.
if ( apply_filters( 'wpcode_text_execute_shortcodes', true ) ) {
return do_shortcode( $this->get_snippet_code() );
}
return $this->get_snippet_code();
}
}
@@ -0,0 +1,97 @@
<?php
/**
* Abstract class for executing different type of snippets.
*
* @package wpcode
*/
/**
* WPCode_Snippet_Execute_Type class.
*/
abstract class WPCode_Snippet_Execute_Type {
/**
* The type of snippet.
*
* @var string
*/
public $type;
/**
* Loaded post data.
*
* @var WPCode_Snippet
*/
public $snippet;
/**
* Load the snippet by id or post object.
*
* @param WPCode_Snippet $snippet The snippet post or the id.
*/
public function __construct( $snippet ) {
if ( empty( $snippet->attributes ) ) {
$shortcode_attributes = $snippet->get_shortcode_attributes();
foreach ( $shortcode_attributes as $attribute ) {
$snippet->set_attribute( $attribute, '' );
}
}
$this->snippet = $snippet;
}
/**
* Get the snippet prepared code and run it through a filter
* before returning it.
*
* @return string
*/
public function get_output() {
if ( ! $this->has_snippet() ) {
return '';
}
$code = $this->prepare_snippet_output();
$output = apply_filters( "wpcode_snippet_output_{$this->type}", $code, $this->snippet );
return apply_filters( 'wpcode_snippet_output', $output, $this->snippet );
}
/**
* Check if the snippet object is set.
*
* @return bool
*/
public function has_snippet() {
return isset( $this->snippet );
}
/**
* Override this in child classes to add specific logic for each snippet type.
*
* @return string
*/
protected function prepare_snippet_output() {
return '';
}
/**
* Get the snippet code.
*
* @return string
*/
public function get_snippet_code() {
return $this->snippet->get_code();
}
/**
* Get the scss snippet code.
*
* @return string
*/
public function get_snippet_compiled_code() {
return $this->snippet->get_compiled_code();
}
}
@@ -0,0 +1,38 @@
<?php
/**
* Execute universal snippets and return their output.
* This type handles both HTML and PHP at the same time in the same way
* you can write both in a .php file.
*
* @package wpcode
*/
/**
* WPCode_Snippet_Execute_Universal class.
*/
class WPCode_Snippet_Execute_Universal extends WPCode_Snippet_Execute_Type {
/**
* The snippet type, Universal for this one.
*
* @var string
*/
public $type = 'universal';
/**
* Grab snippet code and return its output.
*
* @return string
*/
protected function prepare_snippet_output() {
$code = $this->get_snippet_code();
// If we're doing the activation, unslash the code similar to how it will be unslashed before saving in wp_insert_post.
if ( wpcode()->execute->is_doing_activation() && isset( $_POST['wpcode_snippet_code'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$code = wp_unslash( $code );
}
// Wrap code with PHP tags, so it gets executed correctly.
return wpcode()->execute->safe_execute_php( '?>' . $code . '<?php ', $this->snippet );
}
}