PluginProbe
Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript / 4.80
Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript v4.80
trunk 1.0 2.0 3.0 3.5 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.71 4.80 5.0
add-custom-codes / add-custom-codes.php

add-custom-codes.php in Add Custom Codes – Insert Header, Footer, Custom PHP Snippets, CSS, Javascript 4.80, at add-custom-codes.php

595 lines 18.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Add Custom Codes - Insert Header, Footer, Custom Code Snippets
4 * Description: Light-weight plugin to add Custom PHP Functions, HTML, CSS, Javascript, Google Analytics, Search console verification tags and other custom code snippets to your Wordpress website.
5 * Version: 4.80
6 * Author: Saifudheen Mak
7 * Author URI: https://maktalseo.com
8 * License: GPL2
9 * Text Domain: add-custom-codes
10 */
11
12 // If this file was called directly, abort.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 add_action( 'init', 'accodes_load_textdomain' );
18 function accodes_load_textdomain() {
19 load_plugin_textdomain( 'add-custom-codes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
20 }
21
22 /*----------------
23 plugin links 'Plugins' page
24 ------------------*/
25 add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'accodes_action_links' );
26
27 function accodes_action_links ( $actions ) {
28 $mylinks = array(
29 '<a href="' . admin_url( 'edit.php?post_type=accodes_snippets&page=accodes-about' ) . '">Donate us</a>',
30 );
31 $actions = array_merge( $actions, $mylinks );
32 return $actions;
33 }
34
35 add_filter( 'plugin_row_meta', 'accodes_row_meta', 10, 2 );
36
37 function accodes_row_meta( $links, $file ) {
38 if ( plugin_basename( __FILE__ ) == $file ) {
39 $row_meta = array(
40 'wrt-review' => '<a href="' . esc_url( 'https://wordpress.org/support/plugin/add-custom-codes/reviews/#new-post' ) . '" target="_blank" style="">' . esc_html__( 'Rate this plugin', 'add-custom-codes' ) . '</a>',
41 'acc-buy-coffee' => '<a href="' . esc_url( 'https://maktalseo.com' ) . '" target="_blank" style="color:green;">' . esc_html__( 'Hire us!', 'add-custom-codes' ) . '</a>'
42 );
43 return array_merge( $links, $row_meta );
44 }
45 return (array) $links;
46 }
47
48 /*---------------------------------
49 styles and scripts for plugin page
50 -----------------------------------------*/
51
52 add_action('admin_enqueue_scripts', 'accodes_codemirror_scripts');
53
54 function accodes_codemirror_scripts($hook) {
55
56 wp_enqueue_code_editor([]);
57 wp_enqueue_script('wp-codemirror');
58 wp_enqueue_style('wp-codemirror');
59
60 // Enqueue CodeMirror theme if editing snippet
61 $screen = get_current_screen();
62 if ($screen->post_type === 'accodes_snippets') {
63 wp_enqueue_style(
64 'accodes-codemirror-material',
65 plugins_url('add-custom-codes/assets/css/codemirror-material.css'),
66 array(),
67 '1.0'
68 );
69 }
70
71
72 wp_register_style( 'accodes-css', plugins_url( 'add-custom-codes/assets/css/style43.css' ), '', '4.259' );
73 wp_enqueue_style( 'accodes-css' );
74
75 wp_register_script('accodes-js', plugins_url('add-custom-codes/assets/js/scripts.js'), ['jquery', 'jquery-ui-autocomplete'], '4.79', true);
76 wp_enqueue_script( 'accodes-js' );
77
78 // Fetch tags for autocomplete
79 $tags = get_terms([
80 'taxonomy' => 'accodes_tag',
81 'hide_empty' => false,
82 ]);
83 $tag_names = wp_list_pluck($tags, 'name');
84
85 // Localize script once
86 wp_localize_script('accodes-js', 'accodes_data', [
87 'accodes_toggle_snippet_nonce' => wp_create_nonce('accodes_toggle_snippet'),
88 'accodes_tag_suggestions' => $tag_names,
89 ]);
90
91 //localization of script for status toggle in snippet listing page
92 wp_localize_script('accodes-js', 'accodes_toggle_snippet_nonce', wp_create_nonce('accodes_toggle_snippet'));
93 }
94
95 //enque style for about page
96 add_action('admin_enqueue_scripts', function ($hook) {
97 if ($hook === 'accodes_snippets_page_accodes-about') {
98 wp_enqueue_style(
99 'accodes-about-css',
100 plugins_url('assets/css/about.css', __FILE__),
101 [],
102 '1.2'
103 );
104 }
105 });
106
107
108 function accodes_settings_page() {
109 if (!current_user_can('administrator')) {
110 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'add-custom-codes'));
111 }
112
113 include('global-form.php');
114 }
115
116 /*---------------------------
117 define items in settings page
118 -------------------------------*/
119
120 function accodes_sanitize_raw_input($input) {
121 return $input;
122 }
123
124 // Register individual settings with static callbacks
125 add_action('admin_init', 'accodes_settings');
126
127 function accodes_settings() {
128 // Custom CSS field
129 register_setting(
130 'accodes-settings-group',
131 'custom-css-codes-input',
132 [
133 'type' => 'string',
134 'sanitize_callback' => 'accodes_sanitize_raw_input',
135 'default' => ''
136 ]
137 );
138
139 // Custom footer codes field
140 register_setting(
141 'accodes-settings-group',
142 'custom-footer-codes-input',
143 [
144 'type' => 'string',
145 'sanitize_callback' => 'accodes_sanitize_raw_input',
146 'default' => ''
147 ]
148 );
149
150 // Custom header codes field
151 register_setting(
152 'accodes-settings-group',
153 'custom-header-codes-input',
154 [
155 'type' => 'string',
156 'sanitize_callback' => 'accodes_sanitize_raw_input',
157 'default' => ''
158 ]
159 );
160
161 // Boolean option for global CSS in footer
162 register_setting(
163 'accodes-settings-group',
164 'accodes_global_css_on_footer',
165 [
166 'type' => 'boolean',
167 'sanitize_callback' => 'rest_sanitize_boolean',
168 'default' => false
169 ]
170 );
171 }
172
173
174 /*----------------
175 functions
176 ---------------*/
177 function get_current_page_id()
178 {
179 global $post;
180 $current_page_id = false;
181 if ( class_exists( 'WooCommerce' ) && is_shop() ) {
182 $current_page_id = wc_get_page_id( 'shop' );
183 }
184 else {
185 // get page id of individual only
186 if ( is_singular() ) {
187 return get_queried_object_id();
188 }
189 }
190 return $current_page_id;
191 }
192
193 function get_current_taxonomy_id() {
194 if (is_tax() || is_category() || is_tag()) {
195 $term = get_queried_object();
196 if (!empty($term) && !is_wp_error($term)) {
197 return $term->term_id;
198 }
199 }
200 return false;
201 }
202
203 /*---------------------------------
204 output Global CSS
205 ----------------*/
206 function get_global_custom_css()
207 {
208 $accodes_global_css = '';
209 $options = get_option( 'custom-css-codes-input' );
210 if($options!='')
211 {
212 $accodes_global_css = $options;
213 }
214 return $accodes_global_css;
215 }
216
217
218 add_action( 'wp_head', 'accodes_css_output_header' );
219 function accodes_css_output_header() {
220 //check if global css to be added before footer
221 $css_on_footer = (bool) get_option('accodes_global_css_on_footer', false);
222 //put css in footer not ticked
223 if(!$css_on_footer)
224 {
225 $accodes_global_css = get_global_custom_css();
226 //escape
227 echo '<!-- Global CSS by Add Custom Codes --> <style type="text/css"> '.wp_strip_all_tags($accodes_global_css).' </style> <!-- End - Global CSS by Add Custom Codes -->';
228 }
229 }
230
231 add_action( 'wp_footer', 'accodes_css_output_footer' );
232 function accodes_css_output_footer() {
233 //check if global css to be added before footer
234 $css_on_footer = (bool) get_option('accodes_global_css_on_footer', false);
235 //put css in footer ticked
236 if($css_on_footer)
237 {
238 $accodes_global_css = get_global_custom_css();
239 //escape
240 echo '<!-- Global CSS by Add Custom Codes --> <style type="text/css"> '.wp_strip_all_tags($accodes_global_css).' </style> <!-- End - Global CSS by Add Custom Codes -->';
241 }
242 }
243
244 /*---------------------------------
245 output Global Header Codes
246 ----------------*/
247 add_action( 'wp_head', 'accodes_header_output' );
248 function accodes_header_output() {
249
250 $hide_global_header = '';
251 $current_page_id = get_current_page_id();
252 $current_taxonomy_id = get_current_taxonomy_id();
253 $global_header_codes ='';
254 $get_single_header_codes = '';
255 $single_header_codes = '';
256 $get_taxonomy_header_codes = '';
257 $taxonomy_header_codes = '';
258 $output = '';
259
260
261
262 //check if single page
263 if ( $current_page_id ) {
264 //value is 'on' if checked
265 $hide_global_header = get_post_meta( $current_page_id, 'accodes_hide_header', true );
266 //get Single Header Codes
267 $get_single_header_codes = get_post_meta( $current_page_id , '_accodes_header_metabox', true );
268 if($get_single_header_codes !='')
269 {
270
271 $single_header_codes = PHP_EOL.'<!-- Single header Scripts by Add Custom Codes -->'. PHP_EOL;
272 $single_header_codes .= $get_single_header_codes;
273 $single_header_codes .= PHP_EOL.'<!-- End of Single header Scripts by Add Custom Codes -->'. PHP_EOL;
274 }
275 }
276
277 //check if taxonomy page
278 if ( $current_taxonomy_id ) {
279 //value is 'on' if checked
280 $hide_global_header = get_term_meta( $current_taxonomy_id, 'accodes_hide_header', true );
281 //get Single Header Codes
282 $get_taxonomy_header_codes = get_term_meta( $current_taxonomy_id , '_accodes_header_metabox', true );
283 if($get_taxonomy_header_codes !='')
284 {
285 $taxonomy_header_codes = PHP_EOL.'<!-- Taxonomy header Scripts by Add Custom Codes -->'. PHP_EOL;
286 $taxonomy_header_codes .= $get_taxonomy_header_codes;
287 $taxonomy_header_codes .= PHP_EOL.'<!-- End of Taxonomy header Scripts by Add Custom Codes -->'. PHP_EOL;
288 }
289 }
290
291 //get global header - if not set to hide
292 if($hide_global_header != 'on')
293 {
294 //get Global Header Codes
295 $get_global_header_codes = get_option( 'custom-header-codes-input' );
296 if($get_global_header_codes!='')
297 {
298 $global_header_codes = ' <!-- Global Header Scripts by Add Custom Codes --> ';
299 $global_header_codes .= $get_global_header_codes;
300 $global_header_codes .= ' <!-- End - Global Header Scripts by Add Custom Codes --> ';
301 }
302 }
303 $output .= $global_header_codes;
304 $output .= $single_header_codes;
305 $output .= $taxonomy_header_codes;
306
307 if($output !='')
308 {
309 //escape
310 echo $output;
311 }
312
313 }
314
315
316
317 /*---------------------------------
318 output Global Footer Codes
319 ----------------*/
320 add_action( 'wp_footer', 'accodes_footer_output' );
321 function accodes_footer_output() {
322
323 $hide_global_footer = '';
324 $current_page_id = get_current_page_id();
325 $current_taxonomy_id = get_current_taxonomy_id();
326 $global_footer_codes ='';
327 $single_footer_codes = '';
328 $taxonomy_footer_codes = '';
329 $output = '';
330
331 if ( $current_page_id ) {
332 //value is 'on' if checked
333 $hide_global_footer = get_post_meta( $current_page_id, 'accodes_hide_footer', true );
334 //get Footer Codes for Single
335 $get_single_footer_codes = get_post_meta( $current_page_id , '_accodes_footer_metabox', true );
336 if($get_single_footer_codes !='')
337 {
338 $single_footer_codes = PHP_EOL.'<!-- Single Footer Scripts by Add Custom Codes -->'. PHP_EOL;
339 $single_footer_codes .= $get_single_footer_codes;
340 $single_footer_codes .= PHP_EOL.'<!-- End - Single Footer Scripts by Add Custom Codes -->'. PHP_EOL;
341 }
342 }
343
344 //check if taxonomy page
345 if ( $current_taxonomy_id ) {
346 //value is 'on' if checked
347 $hide_global_footer = get_term_meta( $current_taxonomy_id, 'accodes_hide_footer', true );
348 //get Single Header Codes
349 $get_taxonomy_footer_codes = get_term_meta( $current_taxonomy_id , '_accodes_footer_metabox', true );
350 if($get_taxonomy_footer_codes !='')
351 {
352 $taxonomy_footer_codes = PHP_EOL.'<!-- Taxonomy footer Scripts by Add Custom Codes -->'. PHP_EOL;
353 $taxonomy_footer_codes .= $get_taxonomy_footer_codes;
354 $taxonomy_footer_codes .= PHP_EOL.'<!-- End of Taxonomy footer Scripts by Add Custom Codes -->'. PHP_EOL;
355 }
356 }
357
358 //get global footer - if not set to hide
359 if($hide_global_footer != 'on')
360 {
361 //get Global Footer codes
362 $get_global_footer_codes = get_option( 'custom-footer-codes-input' );
363 if($get_global_footer_codes!='')
364 {
365 $global_footer_codes = PHP_EOL.'<!-- Global Footer Scripts by Add Custom Codes -->'. PHP_EOL;
366 $global_footer_codes .= $get_global_footer_codes;
367 $global_footer_codes .= PHP_EOL.'<!-- End - Global Footer Scripts by Add Custom Codes -->'. PHP_EOL;
368 }
369 }
370 $output .= $global_footer_codes;
371 $output .= $single_footer_codes;
372 $output .= $taxonomy_footer_codes;
373
374 if($output !='')
375 {
376 //escape
377 echo $output;
378 }
379
380 }
381
382
383
384 /*----------------
385 * Individual pages
386 * Create the meta boxes for Single post, page, product any other custom post type
387 ------------------------------------*/
388 function _accodes_create_metabox_single() {
389
390 $post_types = get_post_types( '', 'names' );
391 $post_types = array_merge( $post_types, array( 'post', 'page' ) );
392
393 foreach ( $post_types as $post_type ) {
394
395 if ($post_type === 'accodes_snippets') {
396 continue;
397 }
398
399 add_meta_box(
400 '_accodes_metabox',
401 'Add Custom Codes by Mak',
402 '_accodes_render_metabox',
403 $post_type,
404 'normal',
405 'default'
406 );
407 }
408
409 }
410 add_action( 'add_meta_boxes', '_accodes_create_metabox_single' );
411
412 /*-------------------------------
413 * Display Meta Boxes for Single
414 * ----------------------------*/
415 function _accodes_render_metabox() {
416 // Variables
417 global $post; // Get the current post data
418 $header_script = get_post_meta( $post->ID, '_accodes_header_metabox', true ); // Get the saved values
419 $footer_script = get_post_meta( $post->ID, '_accodes_footer_metabox', true ); // Get the saved values
420
421 $hide_header = get_post_meta( $post->ID, 'accodes_hide_header', true );
422 $hide_footer = get_post_meta( $post->ID, 'accodes_hide_footer', true );
423
424 include plugin_dir_path(__FILE__) . 'single-meta.php';
425
426 }
427
428
429 /*-------------------------
430 * update data on post save - Single
431 ---------------------------*/
432 function _accodes_save_metabox_single( $post_id, $post ) {
433
434
435 // Verify nonce for security
436 if ( !isset($_POST['accodes_meta_nonce']) || !wp_verify_nonce($_POST['accodes_meta_nonce'], 'accodes_save_meta') ) {
437 return $post_id;
438 }
439
440 // Verify user has permission to edit post
441 if ( !current_user_can( 'edit_post', $post->ID )) {
442 return $post->ID;
443 }
444
445 // Check if we're in the WordPress REST API request (used by Gutenberg)
446 if (defined('REST_REQUEST') && REST_REQUEST) {
447 return $post_id;
448 }
449
450 // Check if it's an autosave or a revision. If so, return early.
451 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
452 return $post_id;
453 }
454 // Skip revisions
455 if (wp_is_post_revision($post_id)) {
456 return $post_id;
457 }
458
459
460
461 $allowed_html = [
462 'script' => ['type' => [], 'src' => [], 'async' => [], 'defer' => []],
463 'style' => ['type' => []],
464 'meta' => ['name' => [], 'content' => [], 'charset' => []],
465 'link' => ['rel' => [], 'href' => [], 'type' => []],
466 'div' => ['class' => [], 'id' => [], 'style' => []],
467 'span' => ['class' => [], 'style' => []],
468 'p' => ['class' => [], 'style' => []],
469 'a' => ['href' => [], 'title' => [], 'target' => []],
470 'img' => ['src' => [], 'alt' => [], 'width' => [], 'height' => []],
471 'iframe' => ['src' => [], 'width' => [], 'height' => [], 'frameborder' => [], 'allowfullscreen' => []],
472 ];
473
474
475
476 $accodes_header_metabox = isset($_POST['_accodes_header_metabox'])
477 ? wp_kses(wp_unslash($_POST['_accodes_header_metabox']), $allowed_html)
478 : '';
479
480 $accodes_footer_metabox = isset($_POST['_accodes_footer_metabox'])
481 ? wp_kses(wp_unslash($_POST['_accodes_footer_metabox']), $allowed_html)
482 : '';
483
484 // Get values of checkbox
485 $hide_header = isset( $_POST['accodes_hide_header'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_header']) ): '';
486 $hide_footer = isset( $_POST['accodes_hide_footer'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_footer']) ): '';
487
488 //Update values of meta boxes
489 update_post_meta( $post->ID, '_accodes_header_metabox', $accodes_header_metabox );
490 update_post_meta( $post->ID, '_accodes_footer_metabox', $accodes_footer_metabox );
491
492 //Update values of check boxes
493 update_post_meta( $post->ID, 'accodes_hide_header', $hide_header );
494 update_post_meta( $post->ID, 'accodes_hide_footer', $hide_footer );
495
496 }
497 add_action( 'save_post', '_accodes_save_metabox_single', 10, 2 );
498
499
500 /*
501 * single meta end
502 * -----------------------------------------------*/
503
504 function get_current_term_id() {
505 if (isset($_GET['tag_ID'])) {
506 return intval($_GET['tag_ID']);
507 }
508 return 0;
509 }
510
511 // Register meta box
512 function accodes_render_taxonomy_meta_box() {
513 $taxonomies = get_taxonomies();
514 foreach ($taxonomies as $taxonomy) {
515 add_action("{$taxonomy}_edit_form", function($tag) use ($taxonomy) {
516 $term_id = get_current_term_id();
517 $header_script = get_term_meta( $term_id, '_accodes_header_metabox', true );
518 $footer_script = get_term_meta( $term_id, '_accodes_footer_metabox', true );
519 $hide_header = get_term_meta( $term_id, 'accodes_hide_header', true );
520 $hide_footer = get_term_meta( $term_id, 'accodes_hide_footer', true );
521 include('taxonomy-meta.php');
522 });
523 }
524
525 }
526 add_action('admin_init', 'accodes_render_taxonomy_meta_box');
527
528 // Save meta box data of taxonomies
529 function accodes_save_taxonomy_meta_data($term_id) {
530
531 // Verify nonce for security
532 if (!isset($_POST['accodes_tax_meta_nonce']) || !wp_verify_nonce($_POST['accodes_tax_meta_nonce'], 'accodes_save_tax_meta')) {
533 return $term_id;
534 }
535
536 $allowed_html = [
537 'script' => ['type' => [], 'src' => [], 'async' => [], 'defer' => []],
538 'style' => ['type' => []],
539 'meta' => ['name' => [], 'content' => [], 'charset' => []],
540 'link' => ['rel' => [], 'href' => [], 'type' => []],
541 'div' => ['class' => [], 'id' => [], 'style' => []],
542 'span' => ['class' => [], 'style' => []],
543 'p' => ['class' => [], 'style' => []],
544 'a' => ['href' => [], 'title' => [], 'target' => []],
545 'img' => ['src' => [], 'alt' => [], 'width' => [], 'height' => []],
546 'iframe' => ['src' => [], 'width' => [], 'height' => [], 'frameborder' => [], 'allowfullscreen' => []],
547 ];
548
549
550 //get value of meta boxes
551 $accodes_header_metabox = isset($_POST['_accodes_header_metabox'])
552 ? wp_kses(wp_unslash($_POST['_accodes_header_metabox']), $allowed_html)
553 : '';
554
555 $accodes_footer_metabox = isset($_POST['_accodes_footer_metabox'])
556 ? wp_kses(wp_unslash($_POST['_accodes_footer_metabox']), $allowed_html)
557 : '';
558
559 // Get values of checkbox
560 $hide_header = isset( $_POST['accodes_hide_header'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_header'])) : '';
561 $hide_footer = isset( $_POST['accodes_hide_footer'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_footer']) ): '';
562
563 //Update values of meta boxes
564 update_term_meta( $term_id, '_accodes_header_metabox', $accodes_header_metabox );
565 update_term_meta( $term_id, '_accodes_footer_metabox', $accodes_footer_metabox );
566
567 //Update values of check boxes
568 update_term_meta( $term_id, 'accodes_hide_header', $hide_header );
569 update_term_meta( $term_id, 'accodes_hide_footer', $hide_footer );
570 }
571 add_action('edited_term', 'accodes_save_taxonomy_meta_data');
572 add_action('create_term', 'accodes_save_taxonomy_meta_data');
573
574 // Global form callback function
575 function accodes_global_page_callback() {
576 include plugin_dir_path(__FILE__) . 'global-form.php';
577 }
578
579
580 //import export
581 include plugin_dir_path(__FILE__) . 'includes/import-export.php';
582
583
584 //custom snippets
585 include plugin_dir_path(__FILE__) . 'custom-snippets.php';
586
587 //about
588 function accodes_about_page_callback() {
589 include plugin_dir_path(__FILE__) . 'includes/about-accodes.php';
590 }
591
592 //deactivation
593 include plugin_dir_path(__FILE__) . 'deactivate/deactivate.php';
594
595