Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/js/insertcodes-admin.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => '0e0f7bc8e55c70c67db9');
20 changes: 1 addition & 19 deletions assets/js/insertcodes-admin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 34 additions & 2 deletions includes/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Admin {
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_menu' ) );
add_action( 'admin_menu', array( $this, 'code_snippets_menu' ) );
add_action( 'admin_menu', array( $this, 'settings_menu' ), 100 );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
Expand Down Expand Up @@ -49,6 +50,33 @@ public function add_menu() {
);
}

/**
* Add code snippets submenu.
*
* @since 1.2.0
* @return void
*/
public function code_snippets_menu() {
add_submenu_page(
'insert-codes',
__( 'Code Snippets', 'insert-codes' ),
__( 'Code Snippets', 'insert-codes' ),
'manage_options',
'insert-codes-snippets',
array( $this, 'code_snippets_page' ),
);
}

/**
* Render code snippets page.
*
* @since 1.2.0
* @return void
*/
public function code_snippets_page() {
include __DIR__ . '/views/snippets.php';
}

/**
* Add settings submenu.
*
Expand Down Expand Up @@ -96,14 +124,17 @@ public function render_page() {
public function enqueue_scripts( $hook ) {
$screens = array(
'toplevel_page_insert-codes',
'insert-codes_page_insert-codes-snippets',
'insert-codes_page_insert-codes-settings',
);

// Register the admin styles and scripts.
wp_register_style( 'insertcodes-admin', INSERTCODES_URL . 'assets/css/insertcodes-admin.css', array(), INSERTCODES_VERSION );
wp_register_script( 'insertcodes-admin', INSERTCODES_URL . 'assets/js/insertcodes-admin.js', array( 'jquery' ), INSERTCODES_VERSION, true );

if ( 'toplevel_page_insert-codes' === $hook ) {
$settings = wp_enqueue_code_editor( array( 'type' => 'text/html' ) );
// Conditionally enqueue the code editor and admin script.
if ( in_array( $hook, array( 'toplevel_page_insert-codes', 'insert-codes_page_insert-codes-snippets' ), true ) ) {
$settings = wp_enqueue_code_editor( array( 'type' => 'application/x-httpd-php' ) );

// Return if the editor was not enqueued.
if ( false === $settings ) {
Expand All @@ -113,6 +144,7 @@ public function enqueue_scripts( $hook ) {
wp_enqueue_script( 'insertcodes-admin' );
}

// Enqueue the admin style for the defined screens.
if ( in_array( $hook, $screens, true ) ) {
wp_enqueue_style( 'insertcodes-admin' );
}
Expand Down
99 changes: 99 additions & 0 deletions includes/Admin/views/snippets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Insert Code Snippets.
*
* @since 1.2.0
* @package InsertCodess
*/

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

?>
<div class="insertcodes-container">
<div class="wrap insertcodes-wrap">
<div class="insertcodes__header">
<h1 class="wp-heading-inline">
<?php esc_html_e( 'Insert Code Snippets', 'insert-codes' ); ?>
</h1>
<p><?php esc_html_e( 'Bellow are the code editor fields for inserting code snippets inside the functions.php file.', 'insert-codes' ); ?></p>
</div>
<hr class="wp-header-end">
<div class="insertcodes__body">
<div class="insertcodes__content">
<form id="insertcodes-form" method="post" action="<?php echo esc_html( admin_url( 'admin-post.php' ) ); ?>">
<div class="field-group filed-section">
<h3><?php esc_html_e( 'PHP Code Snippets', 'insert-codes' ); ?></h3>
</div>
<div class="field-group field-editor">
<div class="field-label">
<label for="insertcodes_php"><strong><?php esc_html_e( 'PHP Code Snippets:', 'insert-codes' ); ?></strong></label>
</div>
<div class="field">
<textarea type="text" name="insertcodes_php" id="insertcodes_php"><?php echo esc_textarea( get_option( 'insertcodes_php' ) ); ?></textarea>
<p class="description"><?php esc_html_e( 'These scripts will be executed in the PHP context.', 'insert-codes' ); ?></p>
</div>
</div>
<div class="field-group filed-section">
<h3><?php esc_html_e( 'Snippets Settings', 'insert-codes' ); ?></h3>
</div>
<div class="field-group">
<div class="field-label">
<strong><?php esc_html_e( 'Enable Snippets:', 'insert-codes' ); ?></strong>
</div>
<div class="field">
<label for="insertcodes_enable_snippets">
<input name="insertcodes_enable_snippets" id="insertcodes_enable_snippets" type="checkbox" value="yes" <?php checked( get_option( 'insertcodes_enable_snippets' ), 'yes' ); ?>>
<?php esc_html_e( 'Enable php code snippet', 'insert-codes' ); ?>
</label>
<p class="description"><?php esc_html_e( 'Enabling this will execute the PHP code snippets.', 'insert-codes' ); ?></p>
</div>
</div>
<div class="field-group">
<div class="field-label">
<label for="insertcodes_snippets_location"><strong><?php esc_html_e( 'Location:', 'insert-codes' ); ?></strong></label>
</div>
<div class="field">
<select name="insertcodes_snippets_location" id="insertcodes_snippets_location" class="regular-text">
<option value="everywhere" <?php selected( get_option( 'insertcodes_snippets_location' ), 'everywhere' ); ?>><?php esc_html_e( 'Everywhere', 'insert-codes' ); ?></option>
<option value="admin_only" <?php selected( get_option( 'insertcodes_snippets_location' ), 'admin_only' ); ?>><?php esc_html_e( 'Admin only', 'insert-codes' ); ?></option>
<option value="frontend_only" <?php selected( get_option( 'insertcodes_snippets_location' ), 'frontend_only' ); ?>><?php esc_html_e( 'Frontend only', 'insert-codes' ); ?></option>
</select>
<p class="description"><?php esc_html_e( 'Select where the code snippet should execute.', 'insert-codes' ); ?></p>
</div>
</div>
<div class="field-group is-last-item">
<input type="hidden" name="action" value="insertcodes_snippets">
<?php wp_nonce_field( 'insertcodes_snippets' ); ?>
<div class="field-submit-btn">
<?php submit_button(); ?>
</div>
</div>
</form>
</div>
<div class="insertcodes__aside aside__items">
<div class="aside__item">
<div class="aside__item__header">
<h4><?php esc_html_e( 'Support & Rating', 'insert-codes' ); ?></h4>
</div>
<div class="aside__item__body">
<ul>
<li>
<p>
<?php esc_html_e( 'If you need help, please visit the support forum.', 'insert-codes' ); ?>
<a href="https://wordpress.org/support/plugin/insert-codes/" target="_blank"><?php esc_html_e( 'Get Support', 'insert-codes' ); ?></a>
</p>
</li>
<li>
<p>
<?php esc_html_e( 'If you like the plugin, please rate it on WordPress.org.', 'insert-codes' ); ?>
<a href="https://wordpress.org/plugins/insert-codes/" target="_blank"><?php esc_html_e( 'Give a Rating', 'insert-codes' ); ?></a>
</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
61 changes: 61 additions & 0 deletions includes/Controllers/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Actions {
*/
public function __construct() {
add_action( 'admin_post_insertcodes_hbf_scripts', array( __CLASS__, 'handle_hbf_scripts' ) );
add_action( 'admin_post_insertcodes_snippets', array( __CLASS__, 'handle_snippets' ) );
add_action( 'admin_post_insertcodes_settings', array( __CLASS__, 'handle_settings' ) );
}

Expand All @@ -29,6 +30,11 @@ public function __construct() {
public static function handle_hbf_scripts() {
check_admin_referer( 'insertcodes_hbf_scripts' );

// User capability check. You must have manage_options capability to perform this action.
if ( ! current_user_can( 'manage_options' ) ) {
insertcodes()->add_flash_notice( __( 'You do not have sufficient permissions to perform this action.', 'insert-codes' ) );
}

$header_scripts = isset( $_POST['insertcodes_header'] ) ? wp_kses( wp_unslash( $_POST['insertcodes_header'] ), insertcodes_get_allowed_html() ) : '';
$body_scripts = isset( $_POST['insertcodes_body'] ) ? wp_kses( wp_unslash( $_POST['insertcodes_body'] ), insertcodes_get_allowed_html() ) : '';
$footer_scripts = isset( $_POST['insertcodes_footer'] ) ? wp_kses( wp_unslash( $_POST['insertcodes_footer'] ), insertcodes_get_allowed_html() ) : '';
Expand All @@ -43,6 +49,42 @@ public static function handle_hbf_scripts() {
exit();
}

/**
* Updating PHP code snippets.
*
* @since 1.2.0
* @return void
*/
public static function handle_snippets() {
check_admin_referer( 'insertcodes_snippets' );

// User capability check. You must have manage_options capability to perform this action.
if ( ! current_user_can( 'manage_options' ) ) {
insertcodes()->add_flash_notice( __( 'You do not have sufficient permissions to perform this action.', 'insert-codes' ) );
}

// Get the sanitized PHP code snippets.
$php_snippets = self::sanitize_snippet( $_POST );

// Get settings value.
$enable_snippets = isset( $_POST['insertcodes_enable_snippets'] ) ? sanitize_key( wp_unslash( $_POST['insertcodes_enable_snippets'] ) ) : '';
$location = isset( $_POST['insertcodes_snippets_location'] ) ? sanitize_key( wp_unslash( $_POST['insertcodes_snippets_location'] ) ) : '';

// If '<?php' is present beginning of the code, then remove it.
if ( 0 === strpos( $php_snippets, '<?php' ) ) {
$php_snippets = substr( $php_snippets, 5 );
}

// Updating options.
update_option( 'insertcodes_php', $php_snippets );
update_option( 'insertcodes_enable_snippets', $enable_snippets );
update_option( 'insertcodes_snippets_location', $location );

insertcodes()->add_flash_notice( __( 'PHP code snippets saved successfully.', 'insert-codes' ) );
wp_safe_redirect( wp_get_referer() );
exit();
}

/**
* Updating settings.
*
Expand All @@ -52,6 +94,11 @@ public static function handle_hbf_scripts() {
public static function handle_settings() {
check_admin_referer( 'insertcodes_settings' );

// User capability check. You must have manage_options capability to perform this action.
if ( ! current_user_can( 'manage_options' ) ) {
insertcodes()->add_flash_notice( __( 'You do not have sufficient permissions to perform this action.', 'insert-codes' ) );
}

$headers_priority = isset( $_POST['insertcodes_header_priority'] ) ? intval( wp_unslash( $_POST['insertcodes_header_priority'] ) ) : intval( '10' );
$body_priority = isset( $_POST['insertcodes_body_priority'] ) ? intval( wp_unslash( $_POST['insertcodes_body_priority'] ) ) : intval( '10' );
$footers_priority = isset( $_POST['insertcodes_footer_priority'] ) ? intval( wp_unslash( $_POST['insertcodes_footer_priority'] ) ) : intval( '10' );
Expand All @@ -67,4 +114,18 @@ public static function handle_settings() {
wp_safe_redirect( wp_get_referer() );
exit();
}

/**
* Sanitize PHP codes.
*
* @param array $data POST data.
*
* @since 1.2.0
* @return string $codes Sanitized PHP codes.
*/
public static function sanitize_snippet( $data ) {
$codes = isset( $data['insertcodes_php'] ) ? wp_unslash( $data['insertcodes_php'] ) : '';

return $codes;
}
}
72 changes: 72 additions & 0 deletions includes/Controllers/ExecutableCodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace InsertCodes\Controllers;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
* Class ExecutableCodes.
* Handles the executable codes.
*
* @since 1.2.0
* @package InsertCodes\Controllers
*/
class ExecutableCodes {
/**
* Constructor.
*/
public function __construct() {
add_action( 'init', array( $this, 'execute_init' ) );
}

/**
* Execute the code snippet.
*
* @since 1.2.0
* @return void
*/
public function execute_init() {
// Snippets is not enabled the return.
if ( 'yes' !== get_option( 'insertcodes_enable_snippets', 'no' ) ) {
return;
}

// Arguments to pass to the code snippet.
$args = array(
'type' => 'php',
'location' => get_option( 'insertcodes_snippets_location', 'everywhere' ),
'priority' => 'default',
'hook' => 'init',
);

// Get the executable code snippets.
$code_snippets = get_option( 'insertcodes_php', '' );

// Check if the code snippets is empty.
if ( empty( $code_snippets ) ) {
return;
}

// phpcs:disable
// TODO: We should Implode all the code and execute it when we supported multiple Code Snippets.
// Example: $code_snippets = implode( PHP_EOL, $code_snippets );.
// Loop through the code snippets.
/*
foreach ( $code_snippets as $code_snippet ) {
// Execute the code snippet.
$this->execute(
$code_snippet['code'],
$code_snippet['type'],
$code_snippet['location'],
$code_snippet['priority'],
$code_snippet['hook'],
$code_snippet['args']
);
}
*/
// phpcs:enable

// Execute the code snippets.
new Execute( $code_snippets, $args );
}
}
Loading