Donate us', ); $actions = array_merge( $actions, $mylinks ); return $actions; } add_filter( 'plugin_row_meta', 'accodes_row_meta', 10, 2 ); function accodes_row_meta( $links, $file ) { if ( plugin_basename( __FILE__ ) == $file ) { $row_meta = array( 'wrt-review' => '' . esc_html__( 'Rate this plugin', 'add-custom-codes' ) . '', 'acc-buy-coffee' => '' . esc_html__( 'Hire us!', 'add-custom-codes' ) . '' ); return array_merge( $links, $row_meta ); } return (array) $links; } /*--------------------------------- styles and scripts for plugin page -----------------------------------------*/ add_action('admin_enqueue_scripts', 'accodes_codemirror_scripts'); function accodes_codemirror_scripts($hook) { wp_enqueue_code_editor([]); wp_enqueue_script('wp-codemirror'); wp_enqueue_style('wp-codemirror'); // Enqueue CodeMirror theme if editing snippet $screen = get_current_screen(); if ($screen->post_type === 'accodes_snippets') { wp_enqueue_style( 'accodes-codemirror-material', plugins_url('add-custom-codes/assets/css/codemirror-material.css'), array(), '1.0' ); } wp_register_style( 'accodes-css', plugins_url( 'add-custom-codes/assets/css/style43.css' ), '', '4.259' ); wp_enqueue_style( 'accodes-css' ); wp_register_script('accodes-js', plugins_url('add-custom-codes/assets/js/scripts.js'), ['jquery', 'jquery-ui-autocomplete'], '4.82', true); wp_enqueue_script( 'accodes-js' ); // Fetch tags for autocomplete $tags = get_terms([ 'taxonomy' => 'accodes_tag', 'hide_empty' => false, ]); $tag_names = wp_list_pluck($tags, 'name'); // Localize script once wp_localize_script('accodes-js', 'accodes_data', [ 'accodes_toggle_snippet_nonce' => wp_create_nonce('accodes_toggle_snippet'), 'accodes_tag_suggestions' => $tag_names, ]); //localization of script for status toggle in snippet listing page wp_localize_script('accodes-js', 'accodes_toggle_snippet_nonce', wp_create_nonce('accodes_toggle_snippet')); } //enque style for about page add_action('admin_enqueue_scripts', function ($hook) { if ($hook === 'accodes_snippets_page_accodes-about') { wp_enqueue_style( 'accodes-about-css', plugins_url('assets/css/about.css', __FILE__), [], '1.2' ); } }); function accodes_settings_page() { if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'add-custom-codes')); } include('global-form.php'); } /*--------------------------- define items in settings page -------------------------------*/ function accodes_sanitize_raw_input($input) { return $input; } // Register individual settings with static callbacks add_action('admin_init', 'accodes_settings'); function accodes_settings() { // Custom CSS field register_setting( 'accodes-settings-group', 'custom-css-codes-input', [ 'type' => 'string', 'sanitize_callback' => 'accodes_sanitize_raw_input', 'default' => '' ] ); // Custom footer codes field register_setting( 'accodes-settings-group', 'custom-footer-codes-input', [ 'type' => 'string', 'sanitize_callback' => 'accodes_sanitize_raw_input', 'default' => '' ] ); // Custom header codes field register_setting( 'accodes-settings-group', 'custom-header-codes-input', [ 'type' => 'string', 'sanitize_callback' => 'accodes_sanitize_raw_input', 'default' => '' ] ); // Boolean option for global CSS in footer register_setting( 'accodes-settings-group', 'accodes_global_css_on_footer', [ 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', 'default' => false ] ); } /*---------------- functions ---------------*/ function accodes_get_current_page_id() { global $post; $current_page_id = false; if ( class_exists( 'WooCommerce' ) && is_shop() ) { $current_page_id = wc_get_page_id( 'shop' ); } else { // get page id of individual only if ( is_singular() ) { return get_queried_object_id(); } } return $current_page_id; } function accodes_get_current_taxonomy_id() { if (is_tax() || is_category() || is_tag()) { $term = get_queried_object(); if (!empty($term) && !is_wp_error($term)) { return $term->term_id; } } return false; } /*--------------------------------- output Global CSS ----------------*/ function accodes_get_global_custom_css() { $accodes_global_css = ''; $options = get_option( 'custom-css-codes-input' ); if($options!='') { $accodes_global_css = $options; } return $accodes_global_css; } function accodes_css_output_header() { //check if global css to be added before footer $css_on_footer = (bool) get_option('accodes_global_css_on_footer', false); //put css in footer not ticked if(!$css_on_footer) { $accodes_global_css = accodes_get_global_custom_css(); // Intentionally output admin-provided CSS without HTML-encoding so quotes stay intact. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Intentionally outputting admin-provided CSS. echo ' '; } } // Hook global CSS output into the head add_action( 'wp_head', 'accodes_css_output_header' ); add_action( 'wp_footer', 'accodes_css_output_footer' ); function accodes_css_output_footer() { //check if global css to be added before footer $css_on_footer = (bool) get_option('accodes_global_css_on_footer', false); //put css in footer ticked if($css_on_footer) { $accodes_global_css = accodes_get_global_custom_css(); // Intentionally output admin-provided CSS without HTML-encoding so quotes stay intact. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Intentionally outputting admin-provided CSS. echo ' '; } } /*--------------------------------- output Global Header Codes ----------------*/ add_action( 'wp_head', 'accodes_header_output' ); function accodes_header_output() { $hide_global_header = ''; $current_page_id = accodes_get_current_page_id(); $current_taxonomy_id = accodes_get_current_taxonomy_id(); $global_header_codes =''; $get_single_header_codes = ''; $single_header_codes = ''; $get_taxonomy_header_codes = ''; $taxonomy_header_codes = ''; $output = ''; //check if single page if ( $current_page_id ) { //value is 'on' if checked $hide_global_header = get_post_meta( $current_page_id, 'accodes_hide_header', true ); //get Single Header Codes $get_single_header_codes = get_post_meta( $current_page_id , '_accodes_header_metabox', true ); if($get_single_header_codes !='') { $single_header_codes = PHP_EOL.''. PHP_EOL; $single_header_codes .= $get_single_header_codes; $single_header_codes .= PHP_EOL.''. PHP_EOL; } } //check if taxonomy page if ( $current_taxonomy_id ) { //value is 'on' if checked $hide_global_header = get_term_meta( $current_taxonomy_id, 'accodes_hide_header', true ); //get Single Header Codes $get_taxonomy_header_codes = get_term_meta( $current_taxonomy_id , '_accodes_header_metabox', true ); if($get_taxonomy_header_codes !='') { $taxonomy_header_codes = PHP_EOL.''. PHP_EOL; $taxonomy_header_codes .= $get_taxonomy_header_codes; $taxonomy_header_codes .= PHP_EOL.''. PHP_EOL; } } //get global header - if not set to hide if($hide_global_header != 'on') { //get Global Header Codes $get_global_header_codes = get_option( 'custom-header-codes-input' ); if($get_global_header_codes!='') { $global_header_codes = ' '; $global_header_codes .= $get_global_header_codes; $global_header_codes .= ' '; } } $output .= $global_header_codes; $output .= $single_header_codes; $output .= $taxonomy_header_codes; if($output !='') { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Intentionally outputting admin-provided header codes. echo $output; } } /*--------------------------------- output Global Footer Codes ----------------*/ add_action( 'wp_footer', 'accodes_footer_output' ); function accodes_footer_output() { $hide_global_footer = ''; $current_page_id = accodes_get_current_page_id(); $current_taxonomy_id = accodes_get_current_taxonomy_id(); $global_footer_codes =''; $single_footer_codes = ''; $taxonomy_footer_codes = ''; $output = ''; if ( $current_page_id ) { //value is 'on' if checked $hide_global_footer = get_post_meta( $current_page_id, 'accodes_hide_footer', true ); //get Footer Codes for Single $get_single_footer_codes = get_post_meta( $current_page_id , '_accodes_footer_metabox', true ); if($get_single_footer_codes !='') { $single_footer_codes = PHP_EOL.''. PHP_EOL; $single_footer_codes .= $get_single_footer_codes; $single_footer_codes .= PHP_EOL.''. PHP_EOL; } } //check if taxonomy page if ( $current_taxonomy_id ) { //value is 'on' if checked $hide_global_footer = get_term_meta( $current_taxonomy_id, 'accodes_hide_footer', true ); //get Single Header Codes $get_taxonomy_footer_codes = get_term_meta( $current_taxonomy_id , '_accodes_footer_metabox', true ); if($get_taxonomy_footer_codes !='') { $taxonomy_footer_codes = PHP_EOL.''. PHP_EOL; $taxonomy_footer_codes .= $get_taxonomy_footer_codes; $taxonomy_footer_codes .= PHP_EOL.''. PHP_EOL; } } //get global footer - if not set to hide if($hide_global_footer != 'on') { //get Global Footer codes $get_global_footer_codes = get_option( 'custom-footer-codes-input' ); if($get_global_footer_codes!='') { $global_footer_codes = PHP_EOL.''. PHP_EOL; $global_footer_codes .= $get_global_footer_codes; $global_footer_codes .= PHP_EOL.''. PHP_EOL; } } $output .= $global_footer_codes; $output .= $single_footer_codes; $output .= $taxonomy_footer_codes; if($output !='') { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Intentionally outputting admin-provided footer codes. echo $output; } } /*---------------- * Individual pages * Create the meta boxes for Single post, page, product any other custom post type ------------------------------------*/ function accodes_create_metabox_single() { $post_types = get_post_types( '', 'names' ); $post_types = array_merge( $post_types, array( 'post', 'page' ) ); $can_code = current_user_can('unfiltered_html') || current_user_can('manage_options'); foreach ( $post_types as $post_type ) { if ($post_type === 'accodes_snippets') { continue; } if (!$can_code) { continue; } add_meta_box( '_accodes_metabox', 'Add Custom Codes by Mak', 'accodes_render_metabox', $post_type, 'normal', 'default' ); } } add_action( 'add_meta_boxes', 'accodes_create_metabox_single' ); /*------------------------------- * Display Meta Boxes for Single * ----------------------------*/ function accodes_render_metabox() { // Variables global $post; // Get the current post data $header_script = get_post_meta( $post->ID, '_accodes_header_metabox', true ); // Get the saved values $footer_script = get_post_meta( $post->ID, '_accodes_footer_metabox', true ); // Get the saved values $hide_header = get_post_meta( $post->ID, 'accodes_hide_header', true ); $hide_footer = get_post_meta( $post->ID, 'accodes_hide_footer', true ); include plugin_dir_path(__FILE__) . 'single-meta.php'; } /*------------------------- * update data on post save - Single ---------------------------*/ function accodes_save_metabox_single( $post_id, $post ) { // Verify nonce for security $acc_meta_nonce = isset($_POST['accodes_meta_nonce']) ? sanitize_text_field( wp_unslash($_POST['accodes_meta_nonce']) ) : ''; if ( !$acc_meta_nonce || !wp_verify_nonce($acc_meta_nonce, 'accodes_save_meta') ) { return $post_id; } // Verify user has permission to edit post if ( !current_user_can( 'edit_post', $post->ID )) { return $post->ID; } // Check if we're in the WordPress REST API request (used by Gutenberg) if (defined('REST_REQUEST') && REST_REQUEST) { return $post_id; } // Check if it's an autosave or a revision. If so, return early. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } // Skip revisions if (wp_is_post_revision($post_id)) { return $post_id; } $privileged = current_user_can('unfiltered_html') || current_user_can('manage_options'); if ($privileged) { $allowed_html = [ 'script' => ['type' => [], 'src' => [], 'async' => [], 'defer' => []], 'style' => ['type' => []], 'meta' => ['name' => [], 'content' => [], 'charset' => []], 'link' => ['rel' => [], 'href' => [], 'type' => []], 'div' => ['class' => [], 'id' => [], 'style' => []], 'span' => ['class' => [], 'style' => []], 'p' => ['class' => [], 'style' => []], 'a' => ['href' => [], 'title' => [], 'target' => []], 'img' => ['src' => [], 'alt' => [], 'width' => [], 'height' => []], 'iframe' => ['src' => [], 'width' => [], 'height' => [], 'frameborder' => [], 'allowfullscreen' => []], ]; } else { // Restrictive for non-privileged users (no scripts/styles/iframes/meta/link) $allowed_html = [ 'div' => ['class' => [], 'id' => [], 'style' => []], 'span' => ['class' => [], 'style' => []], 'p' => ['class' => [], 'style' => []], 'a' => ['href' => [], 'title' => [], 'target' => []], 'img' => ['src' => [], 'alt' => [], 'width' => [], 'height' => []], 'ul' => ['class' => [], 'style' => []], 'ol' => ['class' => [], 'style' => []], 'li' => ['class' => [], 'style' => []], 'strong' => [], 'em' => [], 'br' => [], ]; } $accodes_header_metabox = isset($_POST['_accodes_header_metabox']) ? wp_kses(wp_unslash($_POST['_accodes_header_metabox']), $allowed_html) : ''; $accodes_footer_metabox = isset($_POST['_accodes_footer_metabox']) ? wp_kses(wp_unslash($_POST['_accodes_footer_metabox']), $allowed_html) : ''; // Get values of checkbox $hide_header = isset( $_POST['accodes_hide_header'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_header']) ): ''; $hide_footer = isset( $_POST['accodes_hide_footer'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_footer']) ): ''; //Update values of meta boxes update_post_meta( $post->ID, '_accodes_header_metabox', $accodes_header_metabox ); update_post_meta( $post->ID, '_accodes_footer_metabox', $accodes_footer_metabox ); //Update values of check boxes update_post_meta( $post->ID, 'accodes_hide_header', $hide_header ); update_post_meta( $post->ID, 'accodes_hide_footer', $hide_footer ); } add_action( 'save_post', 'accodes_save_metabox_single', 10, 2 ); /* * single meta end * -----------------------------------------------*/ function accodes_get_current_term_id() { // Prefer queried term object over direct access to $_GET['tag_ID'] $term = get_queried_object(); return ( $term && isset( $term->term_id ) ) ? absint( $term->term_id ) : 0; } // Register meta box function accodes_render_taxonomy_meta_box() { $taxonomies = get_taxonomies(); foreach ($taxonomies as $taxonomy) { add_action("{$taxonomy}_edit_form", function($tag) use ($taxonomy) { if (!current_user_can('unfiltered_html') && !current_user_can('manage_options')) { return; } $term_id = isset($tag->term_id) ? (int) $tag->term_id : 0; $header_script = get_term_meta( $term_id, '_accodes_header_metabox', true ); $footer_script = get_term_meta( $term_id, '_accodes_footer_metabox', true ); $hide_header = get_term_meta( $term_id, 'accodes_hide_header', true ); $hide_footer = get_term_meta( $term_id, 'accodes_hide_footer', true ); include('taxonomy-meta.php'); }); } } add_action('admin_init', 'accodes_render_taxonomy_meta_box'); // Save meta box data of taxonomies function accodes_save_taxonomy_meta_data($term_id) { // Verify nonce for security $tax_meta_nonce = isset($_POST['accodes_tax_meta_nonce']) ? sanitize_text_field( wp_unslash($_POST['accodes_tax_meta_nonce']) ) : ''; if (!$tax_meta_nonce || !wp_verify_nonce($tax_meta_nonce, 'accodes_save_tax_meta')) { return $term_id; } if (!current_user_can('edit_term', $term_id)) { return $term_id; } $privileged = current_user_can('unfiltered_html') || current_user_can('manage_options'); if ($privileged) { $allowed_html = [ 'script' => ['type' => [], 'src' => [], 'async' => [], 'defer' => []], 'style' => ['type' => []], 'meta' => ['name' => [], 'content' => [], 'charset' => []], 'link' => ['rel' => [], 'href' => [], 'type' => []], 'div' => ['class' => [], 'id' => [], 'style' => []], 'span' => ['class' => [], 'style' => []], 'p' => ['class' => [], 'style' => []], 'a' => ['href' => [], 'title' => [], 'target' => []], 'img' => ['src' => [], 'alt' => [], 'width' => [], 'height' => []], 'iframe' => ['src' => [], 'width' => [], 'height' => [], 'frameborder' => [], 'allowfullscreen' => []], ]; } else { $allowed_html = [ 'div' => ['class' => [], 'id' => [], 'style' => []], 'span' => ['class' => [], 'style' => []], 'p' => ['class' => [], 'style' => []], 'a' => ['href' => [], 'title' => [], 'target' => []], 'img' => ['src' => [], 'alt' => [], 'width' => [], 'height' => []], 'ul' => ['class' => [], 'style' => []], 'ol' => ['class' => [], 'style' => []], 'li' => ['class' => [], 'style' => []], 'strong' => [], 'em' => [], 'br' => [], ]; } //get value of meta boxes $accodes_header_metabox = isset($_POST['_accodes_header_metabox']) ? wp_kses(wp_unslash($_POST['_accodes_header_metabox']), $allowed_html) : ''; $accodes_footer_metabox = isset($_POST['_accodes_footer_metabox']) ? wp_kses(wp_unslash($_POST['_accodes_footer_metabox']), $allowed_html) : ''; // Get values of checkbox $hide_header = isset( $_POST['accodes_hide_header'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_header'])) : ''; $hide_footer = isset( $_POST['accodes_hide_footer'] ) ? sanitize_text_field(wp_unslash($_POST['accodes_hide_footer']) ): ''; //Update values of meta boxes update_term_meta( $term_id, '_accodes_header_metabox', $accodes_header_metabox ); update_term_meta( $term_id, '_accodes_footer_metabox', $accodes_footer_metabox ); //Update values of check boxes update_term_meta( $term_id, 'accodes_hide_header', $hide_header ); update_term_meta( $term_id, 'accodes_hide_footer', $hide_footer ); } add_action('edited_term', 'accodes_save_taxonomy_meta_data'); add_action('create_term', 'accodes_save_taxonomy_meta_data'); // Global form callback function function accodes_global_page_callback() { include plugin_dir_path(__FILE__) . 'global-form.php'; } //import export include plugin_dir_path(__FILE__) . 'includes/import-export.php'; //custom snippets include plugin_dir_path(__FILE__) . 'custom-snippets.php'; //about function accodes_about_page_callback() { include plugin_dir_path(__FILE__) . 'includes/about-accodes.php'; } //deactivation include plugin_dir_path(__FILE__) . 'deactivate/deactivate.php';