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