Commit inicial powerservice
This commit is contained in:
+110
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Class to auto-insert snippets in the Admin area.
|
||||
*
|
||||
* @package wpcode
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class WPCode_Auto_Insert_Admin.
|
||||
*/
|
||||
class WPCode_Auto_Insert_Admin extends WPCode_Auto_Insert_Type {
|
||||
|
||||
/**
|
||||
* The category of this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category = 'global';
|
||||
|
||||
/**
|
||||
* Load the available options and labels.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init() {
|
||||
$this->locations = array(
|
||||
'admin_head' => array(),
|
||||
'admin_footer' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the label.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_label() {
|
||||
$this->label = __( 'Admin Area', 'insert-headers-and-footers' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the available locations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_locations() {
|
||||
$this->locations = array(
|
||||
'admin_head' => array(
|
||||
'label' => esc_html__( 'Admin header', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet in the wp-admin header area.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'admin_footer' => array(
|
||||
'label' => esc_html__( 'Admin footer', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet in the wp-admin footer.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we are on an archive page and we should be executing hooks.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function conditions() {
|
||||
return is_admin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add hooks specific to single posts.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hooks() {
|
||||
add_action( 'admin_head', array( $this, 'insert_admin_head' ), 9 );
|
||||
add_action( 'admin_footer', array( $this, 'insert_admin_footer' ), 9 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output snippet in the admin head.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_admin_head() {
|
||||
$this->output_location( 'admin_head' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output snippet in the admin footer.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_admin_footer() {
|
||||
$this->output_location( 'admin_footer' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the parent method to add the start hook specific to the admin.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function add_start_hook() {
|
||||
add_action( 'admin_init', array( $this, 'maybe_run_hooks' ) );
|
||||
}
|
||||
}
|
||||
|
||||
new WPCode_Auto_Insert_Admin();
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
/**
|
||||
* Class to auto-insert snippets on archive pages, taxonomies, etc.
|
||||
*
|
||||
* @package wpcode
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class WPCode_Auto_Insert_Archive.
|
||||
*/
|
||||
class WPCode_Auto_Insert_Archive extends WPCode_Auto_Insert_Type {
|
||||
/**
|
||||
* The type unique name (slug).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'archive';
|
||||
/**
|
||||
* The category of this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category = 'page';
|
||||
|
||||
/**
|
||||
* Load the available options and labels.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init() {
|
||||
$this->locations = array(
|
||||
'before_excerpt' => array(),
|
||||
'after_excerpt' => array(),
|
||||
'between_posts' => array(),
|
||||
'archive_before_post' => array(),
|
||||
'archive_after_post' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the label.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_label() {
|
||||
$this->label = __( 'Categories, Archives, Tags, Taxonomies', 'insert-headers-and-footers' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the available locations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_locations() {
|
||||
$this->locations = array(
|
||||
'before_excerpt' => array(
|
||||
'label' => __( 'Insert Before Excerpt', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Insert snippet above post summary.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'after_excerpt' => array(
|
||||
'label' => __( 'Insert After Excerpt', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Insert snippet below post summary.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'between_posts' => array(
|
||||
'label' => __( 'Between Posts', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Insert snippet between multiple posts.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'archive_before_post' => array(
|
||||
'label' => __( 'Before Post', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Insert snippet at the beginning of a post.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'archive_after_post' => array(
|
||||
'label' => __( 'After Post', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Insert snippet at the end of a post.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we are on an archive page and we should be executing hooks.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function conditions() {
|
||||
return is_archive() || is_home();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add hooks specific to single posts.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hooks() {
|
||||
add_filter( 'the_excerpt', array( $this, 'insert_before_excerpt' ) );
|
||||
add_filter( 'the_excerpt', array( $this, 'insert_after_excerpt' ) );
|
||||
add_action( 'the_post', array( $this, 'insert_between_posts' ), 10, 2 );
|
||||
add_action( 'the_post', array( $this, 'insert_before_after_post' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output snippet before excerpt on archive pages.
|
||||
*
|
||||
* @param string $excerpt The excerpt text.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function insert_before_excerpt( $excerpt ) {
|
||||
$snippets = $this->get_snippets_for_location( 'before_excerpt' );
|
||||
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$excerpt = wpcode()->execute->get_snippet_output( $snippet ) . $excerpt;
|
||||
}
|
||||
|
||||
return $excerpt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output snippet after excerpt on archive pages.
|
||||
*
|
||||
* @param string $excerpt The excerpt text.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function insert_after_excerpt( $excerpt ) {
|
||||
$snippets = $this->get_snippets_for_location( 'after_excerpt' );
|
||||
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$excerpt .= wpcode()->execute->get_snippet_output( $snippet );
|
||||
}
|
||||
|
||||
return $excerpt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output snippets between posts in a list of posts.
|
||||
*
|
||||
* @param WP_Post $post_data The post.
|
||||
* @param WP_Query $query The query.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_between_posts( $post_data, $query ) {
|
||||
// If the query has at least two posts to display snippets between.
|
||||
if ( $query->post_count < 2 ) {
|
||||
return;
|
||||
}
|
||||
// If the current post is not the first one in the list.
|
||||
if ( $query->current_post < 1 ) {
|
||||
return;
|
||||
}
|
||||
// If the current post is not the last one in the list.
|
||||
if ( $query->post_count <= $query->current_post ) {
|
||||
return;
|
||||
}
|
||||
$this->output_location( 'between_posts' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output snippets before or after posts.
|
||||
*
|
||||
* @param WP_Post $post_data The post.
|
||||
* @param WP_Query $query The query.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_before_after_post( $post_data, $query ) {
|
||||
$snippets = $this->get_snippets_for_location( 'archive_before_post' );
|
||||
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$insert_number = $snippet->get_auto_insert_number();
|
||||
if ( $query->current_post === $insert_number - 1 ) {
|
||||
echo wpcode()->execute->get_snippet_output( $snippet ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
}
|
||||
|
||||
$snippets = $this->get_snippets_for_location( 'archive_after_post' );
|
||||
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$insert_number = $snippet->get_auto_insert_number();
|
||||
if ( $query->current_post === $insert_number ) {
|
||||
echo wpcode()->execute->get_snippet_output( $snippet ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new WPCode_Auto_Insert_Archive();
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
/**
|
||||
* Class to auto-insert snippets site-wide.
|
||||
*
|
||||
* @package wpcode
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class WPCode_Auto_Insert_Single.
|
||||
*/
|
||||
class WPCode_Auto_Insert_Everywhere extends WPCode_Auto_Insert_Type {
|
||||
|
||||
/**
|
||||
* The type unique name (slug).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'everywhere';
|
||||
|
||||
/**
|
||||
* The category of this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category = 'global';
|
||||
|
||||
/**
|
||||
* This should is only available for PHP scripts.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $code_type = 'php';
|
||||
|
||||
/**
|
||||
* Load the available options and labels.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init() {
|
||||
$this->locations = array(
|
||||
'everywhere' => array(),
|
||||
'frontend_only' => array(),
|
||||
'admin_only' => array(),
|
||||
'frontend_cl' => array(),
|
||||
'on_demand' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the label.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_label() {
|
||||
$this->label = __( 'PHP Snippets Only', 'insert-headers-and-footers' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the available locations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_locations() {
|
||||
$this->locations = array(
|
||||
'everywhere' => array(
|
||||
'label' => esc_html__( 'Run Everywhere', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Snippet gets executed everywhere on your website.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'frontend_only' => array(
|
||||
'label' => esc_html__( 'Frontend Only', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Snippet gets executed only in the frontend of the website.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'admin_only' => array(
|
||||
'label' => esc_html__( 'Admin Only', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'The snippet only gets executed in the wp-admin area.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'frontend_cl' => array(
|
||||
'label' => esc_html__( 'Frontend Conditional Logic', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Ideal for running the snippet later with conditional logic rules in the frontend.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'on_demand' => array(
|
||||
'label' => esc_html__( 'On Demand', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Execute this snippet on demand or programmatically just when you need it.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute snippets.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run_snippets() {
|
||||
$snippets = $this->get_snippets_for_location( 'everywhere' );
|
||||
$line_reference = array();
|
||||
$code = array();
|
||||
$last_line = 0;
|
||||
$last_snippet = null;
|
||||
|
||||
if ( is_admin() ) {
|
||||
$snippets = array_merge( $snippets, $this->get_snippets_for_location( 'admin_only' ) );
|
||||
}
|
||||
|
||||
// Merge all the code into 1, so we can track on which line the error occurs, if any.
|
||||
foreach ( $snippets as $snippet ) {
|
||||
// Use the WPCode_Snippet_Execute_Type filters here for compatibility with class even though we're skipping it for these particular locations.
|
||||
$snippet_code = apply_filters( 'wpcode_snippet_output_php', $snippet->get_code(), $snippet );
|
||||
$snippet_code = apply_filters( 'wpcode_snippet_output', $snippet_code, $snippet );
|
||||
// Let's see how many lines the code has.
|
||||
$lines = substr_count( $snippet_code, PHP_EOL );
|
||||
// Let's keep a record of the start and end line of each snippet.
|
||||
$last_line ++;
|
||||
$line_reference[ $snippet->get_id() ] = array(
|
||||
'start' => $last_line,
|
||||
'end' => $last_line + $lines,
|
||||
);
|
||||
$last_line = $last_line + $lines;
|
||||
$code[] = $snippet_code;
|
||||
$last_snippet = $snippet;
|
||||
}
|
||||
if ( ! empty( $code ) ) {
|
||||
// Implode all the code and execute it.
|
||||
$code = implode( PHP_EOL, $code );
|
||||
// Execute the code.
|
||||
wpcode()->execute->safe_execute_php( $code, $last_snippet, $line_reference );
|
||||
}
|
||||
|
||||
if ( ! is_admin() ) {
|
||||
$snippets = $this->get_snippets_for_location( 'frontend_only' );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
wpcode()->execute->get_snippet_output( $snippet );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute snippets on the init hook to allow using more Conditional Logic options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run_init_snippets() {
|
||||
$snippets = $this->get_snippets_for_location( 'frontend_cl' );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
wpcode()->execute->get_snippet_output( $snippet );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the default hook and short-circuit any other conditions
|
||||
* checks as these snippets will run everywhere.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function add_start_hook() {
|
||||
add_action( 'plugins_loaded', array( $this, 'run_snippets' ), 5 );
|
||||
add_action( 'wp', array( $this, 'run_init_snippets' ) );
|
||||
}
|
||||
}
|
||||
|
||||
new WPCode_Auto_Insert_Everywhere();
|
||||
+281
@@ -0,0 +1,281 @@
|
||||
<?php
|
||||
/**
|
||||
* Class to auto-insert snippets on single posts.
|
||||
*
|
||||
* @package wpcode
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class WPCode_Auto_Insert_Single.
|
||||
*/
|
||||
class WPCode_Auto_Insert_Single extends WPCode_Auto_Insert_Type {
|
||||
|
||||
/**
|
||||
* The type unique name (slug).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'single';
|
||||
|
||||
/**
|
||||
* The category of this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category = 'page';
|
||||
|
||||
/**
|
||||
* Used to make sure we only output the before post code once.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $did_before_post_output = false;
|
||||
|
||||
/**
|
||||
* Load the available options and labels.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init() {
|
||||
$this->locations = array(
|
||||
'before_post' => array(),
|
||||
'after_post' => array(),
|
||||
'before_content' => array(),
|
||||
'after_content' => array(),
|
||||
'before_paragraph' => array(),
|
||||
'after_paragraph' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the label.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_label() {
|
||||
$this->label = __( 'Page, Post, Custom Post Type', 'insert-headers-and-footers' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the available locations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_locations() {
|
||||
$this->locations = array(
|
||||
'before_post' => array(
|
||||
'label' => esc_html__( 'Insert Before Post', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet at the beginning of a post.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'after_post' => array(
|
||||
'label' => esc_html__( 'Insert After Post', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet at the end of a post.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'before_content' => array(
|
||||
'label' => esc_html__( 'Insert Before Content', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet at the beginning of the post content.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'after_content' => array(
|
||||
'label' => esc_html__( 'Insert After Content', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet at the end of the post content.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'before_paragraph' => array(
|
||||
'label' => esc_html__( 'Insert Before Paragraph', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet before paragraph # of the post content.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'after_paragraph' => array(
|
||||
'label' => esc_html__( 'Insert After Paragraph', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet after paragraph # of the post content.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if we are on a singular page and we should be executing hooks.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function conditions() {
|
||||
return is_singular();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add hooks specific to single posts.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hooks() {
|
||||
add_action( 'the_post', array( $this, 'insert_before_post' ) );
|
||||
add_filter( 'render_block_core/template-part', array( $this, 'insert_before_post_fse' ), 15, 2 );
|
||||
add_action( 'the_content', array( $this, 'insert_after_post' ) );
|
||||
add_filter( 'the_content', array( $this, 'insert_before_content' ) );
|
||||
add_filter( 'the_content', array( $this, 'insert_after_content' ) );
|
||||
add_filter( 'the_content', array( $this, 'insert_after_before_paragraph' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert snippet before the post.
|
||||
*
|
||||
* @param WP_Post $post_object The post object being loaded.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_before_post( $post_object ) {
|
||||
if ( ! did_action( 'get_header' ) || get_the_ID() !== $post_object->ID || $this->did_before_post_output ) {
|
||||
return;
|
||||
}
|
||||
$this->output_location( 'before_post' );
|
||||
$this->did_before_post_output = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* In FSE themes there's no "get_header" action to check for, so we hook after the core template-part header block.
|
||||
*
|
||||
* @param string $block_content The normal block HTML that would be sent to the screen.
|
||||
* @param array $block An array of data about the block, and the way the user configured it.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function insert_before_post_fse( $block_content, $block ) {
|
||||
// If the get_header action ran we use the classic output method above.
|
||||
if ( did_action( 'get_header' ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
if ( ! isset( $block['attrs']['slug'] ) || 'header' !== $block['attrs']['slug'] ) {
|
||||
return $block_content;
|
||||
}
|
||||
$before_post = '';
|
||||
$snippets = $this->get_snippets_for_location( 'before_post' );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$before_post .= wpcode()->execute->get_snippet_output( $snippet );
|
||||
}
|
||||
|
||||
return $block_content . $before_post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert snippet output after the content.
|
||||
*
|
||||
* @param string $content The content of the post.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function insert_after_content( $content ) {
|
||||
$snippets = $this->get_snippets_for_location( 'after_content' );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$content .= wpcode()->execute->get_snippet_output( $snippet );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert snippet after the post
|
||||
*
|
||||
* @param string $content The post content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function insert_after_post( $content ) {
|
||||
$snippets = $this->get_snippets_for_location( 'after_post' );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$content .= wpcode()->execute->get_snippet_output( $snippet );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert snippets before the content.
|
||||
*
|
||||
* @param string $content The post content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function insert_before_content( $content ) {
|
||||
$snippets = $this->get_snippets_for_location( 'before_content' );
|
||||
$snippets_output = '';
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$snippets_output .= wpcode()->execute->get_snippet_output( $snippet );
|
||||
}
|
||||
|
||||
return $snippets_output . $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert content before or after paragraphs based on settings.
|
||||
*
|
||||
* @param string $content The post content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function insert_after_before_paragraph( $content ) {
|
||||
|
||||
$snippets = $this->get_snippets_for_location( 'before_paragraph' );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$auto_insert_number = $snippet->get_auto_insert_number();
|
||||
$auto_insert_number = empty( $auto_insert_number ) ? 1 : absint( $auto_insert_number );
|
||||
$snippet_output = wpcode()->execute->get_snippet_output( $snippet );
|
||||
$content = $this->insert_between_paragraphs( $snippet_output, $auto_insert_number, $content, 'before' );
|
||||
}
|
||||
|
||||
$snippets = $this->get_snippets_for_location( 'after_paragraph' );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$auto_insert_number = $snippet->get_auto_insert_number();
|
||||
$auto_insert_number = empty( $auto_insert_number ) ? 1 : absint( $auto_insert_number );
|
||||
$snippet_output = wpcode()->execute->get_snippet_output( $snippet );
|
||||
$content = $this->insert_between_paragraphs( $snippet_output, $auto_insert_number, $content, 'after' );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Insert snippet code before or after paragraphs in a post.
|
||||
*
|
||||
* @param string $content_to_insert The content to insert (snippet code output).
|
||||
* @param int $p_number The paragraph number.
|
||||
* @param string $content_to_add_to The content in which the content should be added.
|
||||
* @param string $before_or_after Add it before or after the paragraph.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function insert_between_paragraphs( $content_to_insert, $p_number, $content_to_add_to, $before_or_after = 'after' ) {
|
||||
if ( 'before' === $before_or_after ) {
|
||||
preg_match_all( '/<p(.*?)>/', $content_to_add_to, $matches );
|
||||
} else {
|
||||
preg_match_all( '/<\/p>/', $content_to_add_to, $matches );
|
||||
}
|
||||
$paragraphs = $matches[0];
|
||||
|
||||
// We don't have enough paragraphs to add the snippet.
|
||||
if ( count( $paragraphs ) < $p_number ) {
|
||||
return $content_to_add_to;
|
||||
}
|
||||
|
||||
$p_number = -- $p_number;
|
||||
$offset = 0;
|
||||
foreach ( $paragraphs as $p_index => $p ) {
|
||||
$position = strpos( $content_to_add_to, $p, $offset );
|
||||
if ( $p_index === $p_number ) {
|
||||
if ( 'before' === $before_or_after ) {
|
||||
$content_to_add_to = substr( $content_to_add_to, 0, $position ) . $content_to_insert . substr( $content_to_add_to, $position );
|
||||
} else {
|
||||
$content_to_add_to = substr( $content_to_add_to, 0, $position + 4 ) . $content_to_insert . substr( $content_to_add_to, $position + 4 );
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
$offset = $position + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $content_to_add_to;
|
||||
}
|
||||
}
|
||||
|
||||
new WPCode_Auto_Insert_Single();
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* Class to auto-insert snippets site-wide.
|
||||
*
|
||||
* @package wpcode
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class WPCode_Auto_Insert_Single.
|
||||
*/
|
||||
class WPCode_Auto_Insert_Site_Wide extends WPCode_Auto_Insert_Type {
|
||||
|
||||
/**
|
||||
* The type unique name (slug).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'site_wide';
|
||||
/**
|
||||
* The category of this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category = 'global';
|
||||
|
||||
/**
|
||||
* Load the available options and labels.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init() {
|
||||
$this->locations = array(
|
||||
'site_wide_header' => array(),
|
||||
'site_wide_body' => array(),
|
||||
'site_wide_footer' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the label.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_label() {
|
||||
$this->label = __( 'Site Wide (Frontend)', 'insert-headers-and-footers' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the available locations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_locations() {
|
||||
$this->locations = array(
|
||||
'site_wide_header' => array(
|
||||
'label' => esc_html__( 'Site Wide Header', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert snippet between the head tags of your website on all pages.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'site_wide_body' => array(
|
||||
'label' => esc_html__( 'Site Wide Body', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert the snippet just after the opening body tag.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
'site_wide_footer' => array(
|
||||
'label' => esc_html__( 'Site Wide Footer', 'insert-headers-and-footers' ),
|
||||
'description' => esc_html__( 'Insert the snippet in the footer just before the closing body tag.', 'insert-headers-and-footers' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add hooks specific to this type.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hooks() {
|
||||
add_action( 'wp_head', array( $this, 'insert_header' ) );
|
||||
add_action( 'wp_footer', array( $this, 'insert_footer' ) );
|
||||
add_action( 'wp_body_open', array( $this, 'insert_body' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert snippets in the header.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_header() {
|
||||
$this->output_location( 'site_wide_header' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert snippets in the footer.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_footer() {
|
||||
$this->output_location( 'site_wide_footer' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert snippets after the opening body tag.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function insert_body() {
|
||||
$this->output_location( 'site_wide_body' );
|
||||
}
|
||||
}
|
||||
|
||||
new WPCode_Auto_Insert_Site_Wide();
|
||||
+440
@@ -0,0 +1,440 @@
|
||||
<?php
|
||||
/**
|
||||
* Base class for of auto-insert options.
|
||||
*
|
||||
* @package wpcode
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract class WPCode_Auto_Insert_Type.
|
||||
*/
|
||||
abstract class WPCode_Auto_Insert_Type {
|
||||
/**
|
||||
* The auto-insert label.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* A unique name for this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* An array of locations.
|
||||
* This is an array of unique locations where snippets can be executed in the form
|
||||
* of key => label where the keys should be unique for all the options across
|
||||
* all child classes as those will be used as taxonomy terms to store the
|
||||
* relationship between snippets and their location.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $locations;
|
||||
|
||||
/**
|
||||
* Terms of the locations for this type.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $locations_terms;
|
||||
|
||||
/**
|
||||
* All the snippets for this location.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $snippets;
|
||||
|
||||
/**
|
||||
* For which code type this insert is available.
|
||||
* By default, all.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $code_type = 'all';
|
||||
|
||||
/**
|
||||
* If we should skip the cache set this to false.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $use_cache = true;
|
||||
|
||||
/**
|
||||
* Display a label next to the optgroup title.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $label_pill = '';
|
||||
|
||||
/**
|
||||
* Title of the upgrade prompt.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $upgrade_title = '';
|
||||
|
||||
/**
|
||||
* Text of the upgrade prompt.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $upgrade_text = '';
|
||||
|
||||
/**
|
||||
* URL of the upgrade prompt (CTA) with UTM.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $upgrade_link = '';
|
||||
|
||||
/**
|
||||
* Text for the CTA Button.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $upgrade_button = '';
|
||||
|
||||
/**
|
||||
* Category used for displaying this type in the admin.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category = '';
|
||||
|
||||
/**
|
||||
* Start the auto insertion.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->init();
|
||||
$this->register_type();
|
||||
/**
|
||||
* Constant to enable safe mode.
|
||||
* Filter to allow disabling auto insert.
|
||||
*/
|
||||
if ( defined( 'WPCODE_SAFE_MODE' ) && WPCODE_SAFE_MODE ) {
|
||||
return;
|
||||
}
|
||||
if ( ! apply_filters( 'wpcode_do_auto_insert', true ) ) {
|
||||
return;
|
||||
}
|
||||
// If we're in headers & footers mode prevent execution of any type of snippet.
|
||||
if ( WPCode()->settings->get_option( 'headers_footers_mode' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
add_action( 'admin_init', array( $this, 'load_upgrade_strings' ), 140 );
|
||||
|
||||
$this->add_start_hook();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this instance to the global auto-insert types.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function register_type() {
|
||||
wpcode()->auto_insert->register_type( $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Init function that is specific to each auto-insert type.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function init();
|
||||
|
||||
/**
|
||||
* Give child classes a chance to load on a different hook.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function add_start_hook() {
|
||||
add_action( 'wp', array( $this, 'maybe_run_hooks' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if conditions are met before calling the hooks.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_run_hooks() {
|
||||
if ( ! $this->conditions() ) {
|
||||
return;
|
||||
}
|
||||
// Go through relevant hooks and output based on settings.
|
||||
$this->hooks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Conditions that have to be met for the class to do its thing.
|
||||
* For example, in the single post class we'll check if is_single
|
||||
* and only then will we change the_content.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function conditions() {
|
||||
// Most types only run on the frontend.
|
||||
return ! is_admin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks specific to this type of auto-insertion.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hooks() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of options for the admin.
|
||||
* Check if the insert type is compatible with the code type.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_locations() {
|
||||
$this->load_locations();
|
||||
|
||||
return isset( $this->locations ) ? $this->locations : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the locations for this type.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function load_locations();
|
||||
|
||||
/**
|
||||
* Load the label for this type.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function load_label();
|
||||
|
||||
/**
|
||||
* Query snippets by location.
|
||||
*
|
||||
* @param string $location The location slug.
|
||||
*
|
||||
* @return WPCode_Snippet[]
|
||||
*/
|
||||
public function get_snippets_for_location( $location ) {
|
||||
$snippets = $this->get_snippets();
|
||||
|
||||
$snippets_for_location = isset( $snippets[ $location ] ) ? $snippets[ $location ] : array();
|
||||
|
||||
return apply_filters(
|
||||
'wpcode_get_snippets_for_location',
|
||||
wpcode()->conditional_logic->check_snippets_conditions( $snippets_for_location ),
|
||||
$location
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the snippets for this type and query on demand.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_snippets() {
|
||||
if ( ! isset( $this->snippets ) ) {
|
||||
$this->load_all_snippets_for_type();
|
||||
}
|
||||
|
||||
return $this->snippets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all the snippets for this type and group them by location.
|
||||
* This can be further improved by separating the snippet loading and loading
|
||||
* all the relevant snippets for a screen at once (regardless of type) or just loading all the
|
||||
* active snippets in 1 query.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_all_snippets_for_type() {
|
||||
|
||||
if ( $this->use_cache() ) {
|
||||
$this->snippets = $this->get_snippets_from_cache();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->snippets = array();
|
||||
$args = array(
|
||||
'post_type' => wpcode_get_post_type(),
|
||||
'posts_per_page' => - 1,
|
||||
'post_status' => 'publish',
|
||||
// We don't want to cache this query ever as it should only run when snippets are preloaded in case of an error it will provide false values if cached.
|
||||
'cache_results' => false,
|
||||
// We don't want to allow any filters to be applied to this query.
|
||||
'suppress_filters' => true,
|
||||
);
|
||||
$snippets_query = new WP_Query( $args );
|
||||
$snippets = $snippets_query->posts;
|
||||
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$snippet_locations = wp_get_post_terms( $snippet->ID, 'wpcode_location', array( 'fields' => 'slugs' ) );
|
||||
if ( empty( $snippet_locations ) || is_wp_error( $snippet_locations ) ) {
|
||||
continue;
|
||||
}
|
||||
$location_key = $snippet_locations[0];
|
||||
$this->snippets[ $location_key ][] = wpcode_get_snippet( $snippet );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get snippets from cache split by relevant locations for this type.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_snippets_from_cache() {
|
||||
$cached_snippets = wpcode()->cache->get_cached_snippets();
|
||||
$type_snippets = array();
|
||||
foreach ( $this->locations as $location => $label ) {
|
||||
if ( array_key_exists( $location, $cached_snippets ) ) {
|
||||
$type_snippets[ $location ] = $cached_snippets[ $location ];
|
||||
} else {
|
||||
$type_snippets[ $location ] = array();
|
||||
}
|
||||
}
|
||||
|
||||
return $type_snippets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ids of the loaded location terms.
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
public function get_locations_ids() {
|
||||
$terms = $this->get_location_terms();
|
||||
$ids = array();
|
||||
foreach ( $terms as $term ) {
|
||||
$ids[] = $term->term_id;
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the location terms.
|
||||
*
|
||||
* @return WP_Term[]
|
||||
*/
|
||||
public function get_location_terms() {
|
||||
if ( ! isset( $this->locations_terms ) ) {
|
||||
$this->load_locations_terms();
|
||||
}
|
||||
|
||||
return $this->locations_terms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the location terms using get_terms and store them in the instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_locations_terms() {
|
||||
$terms = get_terms(
|
||||
array(
|
||||
'taxonomy' => 'wpcode_location',
|
||||
'slug' => array_keys( $this->locations ),
|
||||
)
|
||||
);
|
||||
|
||||
$this->locations_terms = array();
|
||||
|
||||
if ( is_wp_error( $terms ) ) {
|
||||
// If the terms don't exist, bail early.
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $terms as $term ) {
|
||||
$term_slug = $term->slug ?? '';
|
||||
$this->locations_terms[ $term_slug ] = $term;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the clauses for our specific query to include the term id in the resulting
|
||||
* WP_Post object so that we can group the results by the type locations.
|
||||
*
|
||||
* @param array $clauses Array of clauses for the SQL query.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function include_term_in_post( $clauses ) {
|
||||
global $wpdb;
|
||||
|
||||
$clauses['fields'] .= ", {$wpdb->term_relationships}.term_taxonomy_id";
|
||||
$clauses['groupby'] = '';
|
||||
|
||||
return $clauses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the type label.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_label() {
|
||||
if ( ! isset( $this->label ) ) {
|
||||
$this->load_label();
|
||||
}
|
||||
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab the use cache value allowing filtering.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function use_cache() {
|
||||
return boolval( apply_filters( 'wpcode_use_auto_insert_cache', $this->use_cache ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the snippets for a location and echo them executed.
|
||||
*
|
||||
* @param string $location_name The location to grab snippets for.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function output_location( $location_name ) {
|
||||
$snippets = $this->get_snippets_for_location( $location_name );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
echo wpcode()->execute->get_snippet_output( $snippet ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the snippets for a location and return them executed.
|
||||
*
|
||||
* @param string $location_name The location to grab snippets for.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_location( $location_name ) {
|
||||
$content = '';
|
||||
$snippets = $this->get_snippets_for_location( $location_name );
|
||||
foreach ( $snippets as $snippet ) {
|
||||
$content .= wpcode()->execute->get_snippet_output( $snippet );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the strings for the upgrade prompt, if any.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_upgrade_strings() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user