Skip to content

Commit 319f70a

Browse files
authored
Merge pull request #4 from wpfresher/feature/php
Add php code snippets support
2 parents e08c46a + e9746d7 commit 319f70a

14 files changed

Lines changed: 655 additions & 48 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array(), 'version' => '0e0f7bc8e55c70c67db9');

assets/js/insertcodes-admin.js

Lines changed: 1 addition & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/Admin/Admin.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Admin {
1717
*/
1818
public function __construct() {
1919
add_action( 'admin_menu', array( $this, 'add_menu' ) );
20+
add_action( 'admin_menu', array( $this, 'code_snippets_menu' ) );
2021
add_action( 'admin_menu', array( $this, 'settings_menu' ), 100 );
2122
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
2223
}
@@ -49,6 +50,33 @@ public function add_menu() {
4950
);
5051
}
5152

53+
/**
54+
* Add code snippets submenu.
55+
*
56+
* @since 1.2.0
57+
* @return void
58+
*/
59+
public function code_snippets_menu() {
60+
add_submenu_page(
61+
'insert-codes',
62+
__( 'Code Snippets', 'insert-codes' ),
63+
__( 'Code Snippets', 'insert-codes' ),
64+
'manage_options',
65+
'insert-codes-snippets',
66+
array( $this, 'code_snippets_page' ),
67+
);
68+
}
69+
70+
/**
71+
* Render code snippets page.
72+
*
73+
* @since 1.2.0
74+
* @return void
75+
*/
76+
public function code_snippets_page() {
77+
include __DIR__ . '/views/snippets.php';
78+
}
79+
5280
/**
5381
* Add settings submenu.
5482
*
@@ -96,14 +124,17 @@ public function render_page() {
96124
public function enqueue_scripts( $hook ) {
97125
$screens = array(
98126
'toplevel_page_insert-codes',
127+
'insert-codes_page_insert-codes-snippets',
99128
'insert-codes_page_insert-codes-settings',
100129
);
101130

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

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

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

147+
// Enqueue the admin style for the defined screens.
116148
if ( in_array( $hook, $screens, true ) ) {
117149
wp_enqueue_style( 'insertcodes-admin' );
118150
}

includes/Admin/views/snippets.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Insert Code Snippets.
4+
*
5+
* @since 1.2.0
6+
* @package InsertCodess
7+
*/
8+
9+
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
10+
11+
?>
12+
<div class="insertcodes-container">
13+
<div class="wrap insertcodes-wrap">
14+
<div class="insertcodes__header">
15+
<h1 class="wp-heading-inline">
16+
<?php esc_html_e( 'Insert Code Snippets', 'insert-codes' ); ?>
17+
</h1>
18+
<p><?php esc_html_e( 'Bellow are the code editor fields for inserting code snippets inside the functions.php file.', 'insert-codes' ); ?></p>
19+
</div>
20+
<hr class="wp-header-end">
21+
<div class="insertcodes__body">
22+
<div class="insertcodes__content">
23+
<form id="insertcodes-form" method="post" action="<?php echo esc_html( admin_url( 'admin-post.php' ) ); ?>">
24+
<div class="field-group filed-section">
25+
<h3><?php esc_html_e( 'PHP Code Snippets', 'insert-codes' ); ?></h3>
26+
</div>
27+
<div class="field-group field-editor">
28+
<div class="field-label">
29+
<label for="insertcodes_php"><strong><?php esc_html_e( 'PHP Code Snippets:', 'insert-codes' ); ?></strong></label>
30+
</div>
31+
<div class="field">
32+
<textarea type="text" name="insertcodes_php" id="insertcodes_php"><?php echo esc_textarea( get_option( 'insertcodes_php' ) ); ?></textarea>
33+
<p class="description"><?php esc_html_e( 'These scripts will be executed in the PHP context.', 'insert-codes' ); ?></p>
34+
</div>
35+
</div>
36+
<div class="field-group filed-section">
37+
<h3><?php esc_html_e( 'Snippets Settings', 'insert-codes' ); ?></h3>
38+
</div>
39+
<div class="field-group">
40+
<div class="field-label">
41+
<strong><?php esc_html_e( 'Enable Snippets:', 'insert-codes' ); ?></strong>
42+
</div>
43+
<div class="field">
44+
<label for="insertcodes_enable_snippets">
45+
<input name="insertcodes_enable_snippets" id="insertcodes_enable_snippets" type="checkbox" value="yes" <?php checked( get_option( 'insertcodes_enable_snippets' ), 'yes' ); ?>>
46+
<?php esc_html_e( 'Enable php code snippet', 'insert-codes' ); ?>
47+
</label>
48+
<p class="description"><?php esc_html_e( 'Enabling this will execute the PHP code snippets.', 'insert-codes' ); ?></p>
49+
</div>
50+
</div>
51+
<div class="field-group">
52+
<div class="field-label">
53+
<label for="insertcodes_snippets_location"><strong><?php esc_html_e( 'Location:', 'insert-codes' ); ?></strong></label>
54+
</div>
55+
<div class="field">
56+
<select name="insertcodes_snippets_location" id="insertcodes_snippets_location" class="regular-text">
57+
<option value="everywhere" <?php selected( get_option( 'insertcodes_snippets_location' ), 'everywhere' ); ?>><?php esc_html_e( 'Everywhere', 'insert-codes' ); ?></option>
58+
<option value="admin_only" <?php selected( get_option( 'insertcodes_snippets_location' ), 'admin_only' ); ?>><?php esc_html_e( 'Admin only', 'insert-codes' ); ?></option>
59+
<option value="frontend_only" <?php selected( get_option( 'insertcodes_snippets_location' ), 'frontend_only' ); ?>><?php esc_html_e( 'Frontend only', 'insert-codes' ); ?></option>
60+
</select>
61+
<p class="description"><?php esc_html_e( 'Select where the code snippet should execute.', 'insert-codes' ); ?></p>
62+
</div>
63+
</div>
64+
<div class="field-group is-last-item">
65+
<input type="hidden" name="action" value="insertcodes_snippets">
66+
<?php wp_nonce_field( 'insertcodes_snippets' ); ?>
67+
<div class="field-submit-btn">
68+
<?php submit_button(); ?>
69+
</div>
70+
</div>
71+
</form>
72+
</div>
73+
<div class="insertcodes__aside aside__items">
74+
<div class="aside__item">
75+
<div class="aside__item__header">
76+
<h4><?php esc_html_e( 'Support & Rating', 'insert-codes' ); ?></h4>
77+
</div>
78+
<div class="aside__item__body">
79+
<ul>
80+
<li>
81+
<p>
82+
<?php esc_html_e( 'If you need help, please visit the support forum.', 'insert-codes' ); ?>
83+
<a href="https://wordpress.org/support/plugin/insert-codes/" target="_blank"><?php esc_html_e( 'Get Support', 'insert-codes' ); ?></a>
84+
</p>
85+
</li>
86+
<li>
87+
<p>
88+
<?php esc_html_e( 'If you like the plugin, please rate it on WordPress.org.', 'insert-codes' ); ?>
89+
<a href="https://wordpress.org/plugins/insert-codes/" target="_blank"><?php esc_html_e( 'Give a Rating', 'insert-codes' ); ?></a>
90+
</p>
91+
</li>
92+
</ul>
93+
</div>
94+
</div>
95+
</div>
96+
</div>
97+
</div>
98+
</div>
99+
<?php

includes/Controllers/Actions.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Actions {
1717
*/
1818
public function __construct() {
1919
add_action( 'admin_post_insertcodes_hbf_scripts', array( __CLASS__, 'handle_hbf_scripts' ) );
20+
add_action( 'admin_post_insertcodes_snippets', array( __CLASS__, 'handle_snippets' ) );
2021
add_action( 'admin_post_insertcodes_settings', array( __CLASS__, 'handle_settings' ) );
2122
}
2223

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

33+
// User capability check. You must have manage_options capability to perform this action.
34+
if ( ! current_user_can( 'manage_options' ) ) {
35+
insertcodes()->add_flash_notice( __( 'You do not have sufficient permissions to perform this action.', 'insert-codes' ) );
36+
}
37+
3238
$header_scripts = isset( $_POST['insertcodes_header'] ) ? wp_kses( wp_unslash( $_POST['insertcodes_header'] ), insertcodes_get_allowed_html() ) : '';
3339
$body_scripts = isset( $_POST['insertcodes_body'] ) ? wp_kses( wp_unslash( $_POST['insertcodes_body'] ), insertcodes_get_allowed_html() ) : '';
3440
$footer_scripts = isset( $_POST['insertcodes_footer'] ) ? wp_kses( wp_unslash( $_POST['insertcodes_footer'] ), insertcodes_get_allowed_html() ) : '';
@@ -43,6 +49,42 @@ public static function handle_hbf_scripts() {
4349
exit();
4450
}
4551

52+
/**
53+
* Updating PHP code snippets.
54+
*
55+
* @since 1.2.0
56+
* @return void
57+
*/
58+
public static function handle_snippets() {
59+
check_admin_referer( 'insertcodes_snippets' );
60+
61+
// User capability check. You must have manage_options capability to perform this action.
62+
if ( ! current_user_can( 'manage_options' ) ) {
63+
insertcodes()->add_flash_notice( __( 'You do not have sufficient permissions to perform this action.', 'insert-codes' ) );
64+
}
65+
66+
// Get the sanitized PHP code snippets.
67+
$php_snippets = self::sanitize_snippet( $_POST );
68+
69+
// Get settings value.
70+
$enable_snippets = isset( $_POST['insertcodes_enable_snippets'] ) ? sanitize_key( wp_unslash( $_POST['insertcodes_enable_snippets'] ) ) : '';
71+
$location = isset( $_POST['insertcodes_snippets_location'] ) ? sanitize_key( wp_unslash( $_POST['insertcodes_snippets_location'] ) ) : '';
72+
73+
// If '<?php' is present beginning of the code, then remove it.
74+
if ( 0 === strpos( $php_snippets, '<?php' ) ) {
75+
$php_snippets = substr( $php_snippets, 5 );
76+
}
77+
78+
// Updating options.
79+
update_option( 'insertcodes_php', $php_snippets );
80+
update_option( 'insertcodes_enable_snippets', $enable_snippets );
81+
update_option( 'insertcodes_snippets_location', $location );
82+
83+
insertcodes()->add_flash_notice( __( 'PHP code snippets saved successfully.', 'insert-codes' ) );
84+
wp_safe_redirect( wp_get_referer() );
85+
exit();
86+
}
87+
4688
/**
4789
* Updating settings.
4890
*
@@ -52,6 +94,11 @@ public static function handle_hbf_scripts() {
5294
public static function handle_settings() {
5395
check_admin_referer( 'insertcodes_settings' );
5496

97+
// User capability check. You must have manage_options capability to perform this action.
98+
if ( ! current_user_can( 'manage_options' ) ) {
99+
insertcodes()->add_flash_notice( __( 'You do not have sufficient permissions to perform this action.', 'insert-codes' ) );
100+
}
101+
55102
$headers_priority = isset( $_POST['insertcodes_header_priority'] ) ? intval( wp_unslash( $_POST['insertcodes_header_priority'] ) ) : intval( '10' );
56103
$body_priority = isset( $_POST['insertcodes_body_priority'] ) ? intval( wp_unslash( $_POST['insertcodes_body_priority'] ) ) : intval( '10' );
57104
$footers_priority = isset( $_POST['insertcodes_footer_priority'] ) ? intval( wp_unslash( $_POST['insertcodes_footer_priority'] ) ) : intval( '10' );
@@ -67,4 +114,18 @@ public static function handle_settings() {
67114
wp_safe_redirect( wp_get_referer() );
68115
exit();
69116
}
117+
118+
/**
119+
* Sanitize PHP codes.
120+
*
121+
* @param array $data POST data.
122+
*
123+
* @since 1.2.0
124+
* @return string $codes Sanitized PHP codes.
125+
*/
126+
public static function sanitize_snippet( $data ) {
127+
$codes = isset( $data['insertcodes_php'] ) ? wp_unslash( $data['insertcodes_php'] ) : '';
128+
129+
return $codes;
130+
}
70131
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace InsertCodes\Controllers;
4+
5+
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6+
7+
/**
8+
* Class ExecutableCodes.
9+
* Handles the executable codes.
10+
*
11+
* @since 1.2.0
12+
* @package InsertCodes\Controllers
13+
*/
14+
class ExecutableCodes {
15+
/**
16+
* Constructor.
17+
*/
18+
public function __construct() {
19+
add_action( 'init', array( $this, 'execute_init' ) );
20+
}
21+
22+
/**
23+
* Execute the code snippet.
24+
*
25+
* @since 1.2.0
26+
* @return void
27+
*/
28+
public function execute_init() {
29+
// Snippets is not enabled the return.
30+
if ( 'yes' !== get_option( 'insertcodes_enable_snippets', 'no' ) ) {
31+
return;
32+
}
33+
34+
// Arguments to pass to the code snippet.
35+
$args = array(
36+
'type' => 'php',
37+
'location' => get_option( 'insertcodes_snippets_location', 'everywhere' ),
38+
'priority' => 'default',
39+
'hook' => 'init',
40+
);
41+
42+
// Get the executable code snippets.
43+
$code_snippets = get_option( 'insertcodes_php', '' );
44+
45+
// Check if the code snippets is empty.
46+
if ( empty( $code_snippets ) ) {
47+
return;
48+
}
49+
50+
// phpcs:disable
51+
// TODO: We should Implode all the code and execute it when we supported multiple Code Snippets.
52+
// Example: $code_snippets = implode( PHP_EOL, $code_snippets );.
53+
// Loop through the code snippets.
54+
/*
55+
foreach ( $code_snippets as $code_snippet ) {
56+
// Execute the code snippet.
57+
$this->execute(
58+
$code_snippet['code'],
59+
$code_snippet['type'],
60+
$code_snippet['location'],
61+
$code_snippet['priority'],
62+
$code_snippet['hook'],
63+
$code_snippet['args']
64+
);
65+
}
66+
*/
67+
// phpcs:enable
68+
69+
// Execute the code snippets.
70+
new Execute( $code_snippets, $args );
71+
}
72+
}

0 commit comments

Comments
 (0)