| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Add Custom Codes by Mak |
| 4 |
* Description: Light-weight plugin to add Custom CSS, Javascript, Google Analytics, Meta verification tags and any other code snippets to your Wordpress website. Go to <em>Appearance -> Add Custom Codes</em> after installing the plugin. |
| 5 |
* Version: 4.3 |
| 6 |
* Author: Saifudheen Mak |
| 7 |
* Author URI: https://www.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__), 'add_action_links_accodes' ); |
| 26 |
|
| 27 |
function add_action_links_accodes ( $actions ) { |
| 28 |
$mylinks = array( |
| 29 |
'<a href="' . admin_url( 'themes.php?page=add-custom-codes' ) . '">Settings</a>', |
| 30 |
); |
| 31 |
$actions = array_merge( $actions, $mylinks ); |
| 32 |
return $actions; |
| 33 |
} |
| 34 |
|
| 35 |
add_filter( 'plugin_row_meta', 'plugin_row_meta_accodes', 10, 2 ); |
| 36 |
|
| 37 |
function plugin_row_meta_accodes( $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 |
|
| 44 |
); |
| 45 |
|
| 46 |
return array_merge( $links, $row_meta ); |
| 47 |
} |
| 48 |
return (array) $links; |
| 49 |
} |
| 50 |
|
| 51 |
/*--------------------------------- |
| 52 |
styles and scripts for plugin page |
| 53 |
-----------------------------------------*/ |
| 54 |
|
| 55 |
add_action('admin_enqueue_scripts', 'codemirror_enqueue_scripts'); |
| 56 |
|
| 57 |
function codemirror_enqueue_scripts($hook) { |
| 58 |
$cm_settings['codeEditor'] = wp_enqueue_code_editor(array('type' => 'htmlmixed')); |
| 59 |
wp_localize_script('jquery', 'cm_settings', $cm_settings); |
| 60 |
|
| 61 |
wp_enqueue_style('wp-codemirror'); |
| 62 |
|
| 63 |
wp_register_style( 'accodes-css', plugins_url( 'add-custom-codes/css/style41.css' ), '', '4.192' ); |
| 64 |
wp_enqueue_style( 'accodes-css' ); |
| 65 |
|
| 66 |
wp_register_script( 'accodes-js', plugins_url( 'add-custom-codes/js/script.js' ), '', '4.1' ); |
| 67 |
wp_enqueue_script( 'accodes-js' ); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/*------------------------------ |
| 72 |
add menu link for plugin settings page |
| 73 |
------------------------*/ |
| 74 |
add_action('admin_menu', 'accodes_menu'); |
| 75 |
|
| 76 |
function accodes_menu() { |
| 77 |
add_theme_page('Add Custom Codes', 'Add Custom Codes', 'administrator', 'add-custom-codes', 'accodes_settings_page'); |
| 78 |
} |
| 79 |
|
| 80 |
function accodes_settings_page() { |
| 81 |
include('global-form.php'); |
| 82 |
} |
| 83 |
|
| 84 |
/*--------------------------- |
| 85 |
define items in settings page |
| 86 |
-------------------------------*/ |
| 87 |
add_action( 'admin_init', 'accodes_settings' ); |
| 88 |
|
| 89 |
function accodes_settings() { |
| 90 |
register_setting( 'accodes-settings-group', 'custom-css-codes-input' ); |
| 91 |
register_setting( 'accodes-settings-group', 'custom-footer-codes-input' ); |
| 92 |
register_setting( 'accodes-settings-group', 'custom-header-codes-input' ); |
| 93 |
register_setting( 'accodes-settings-group', 'accodes_global_css_on_footer' ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/*---------------- |
| 98 |
functions |
| 99 |
---------------*/ |
| 100 |
function get_current_page_id() |
| 101 |
{ |
| 102 |
global $post; |
| 103 |
$current_page_id = false; |
| 104 |
if ( ! isset( $post ) ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
// if woocommerce product, get id |
| 108 |
if ( class_exists( 'WooCommerce' ) && is_shop() ) { |
| 109 |
$current_page_id = wc_get_page_id( 'shop' ); |
| 110 |
} |
| 111 |
else { |
| 112 |
// get page id of individual only |
| 113 |
if ( is_singular() ) { |
| 114 |
$current_page_id = $post->ID; |
| 115 |
} |
| 116 |
} |
| 117 |
return $current_page_id; |
| 118 |
} |
| 119 |
|
| 120 |
/*--------------------------------- |
| 121 |
output Global CSS |
| 122 |
----------------*/ |
| 123 |
function get_global_custom_css() |
| 124 |
{ |
| 125 |
$options = get_option( 'custom-css-codes-input' ); |
| 126 |
if($options!='') |
| 127 |
{ |
| 128 |
$accodes_global_css = '<!-- Global CSS by Add Custom Codes -->'. PHP_EOL; |
| 129 |
$accodes_global_css .= '<style type="text/css">'; |
| 130 |
$accodes_global_css .= ''. PHP_EOL; |
| 131 |
$accodes_global_css .= $options; |
| 132 |
$accodes_global_css .= ''. PHP_EOL.'</style>'.PHP_EOL; |
| 133 |
$accodes_global_css .= '<!-- End - Global CSS by Add Custom Codes -->'; |
| 134 |
} |
| 135 |
return $accodes_global_css; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
add_action( 'wp_head', 'accodes_css_output_header' ); |
| 140 |
function accodes_css_output_header() { |
| 141 |
//check if global css to be added before footer |
| 142 |
$css_on_footer = esc_attr( get_option('accodes_global_css_on_footer') ); |
| 143 |
//put css in footer not ticked |
| 144 |
if($css_on_footer != 'on') |
| 145 |
{ |
| 146 |
$accodes_global_css = get_global_custom_css(); |
| 147 |
echo $accodes_global_css; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
add_action( 'wp_footer', 'accodes_css_output_footer' ); |
| 152 |
function accodes_css_output_footer() { |
| 153 |
//check if global css to be added before footer |
| 154 |
$css_on_footer = esc_attr( get_option('accodes_global_css_on_footer') ); |
| 155 |
//put css in footer ticked |
| 156 |
if($css_on_footer == 'on') |
| 157 |
{ |
| 158 |
$accodes_global_css = get_global_custom_css(); |
| 159 |
echo $accodes_global_css; |
| 160 |
} |
| 161 |
} |
| 162 |
/*--------------------------------- |
| 163 |
output Global Header Codes |
| 164 |
----------------*/ |
| 165 |
add_action( 'wp_head', 'accodes_header_output' ); |
| 166 |
function accodes_header_output() { |
| 167 |
|
| 168 |
$hide_global_header = ''; |
| 169 |
$current_page_id = get_current_page_id(); |
| 170 |
$global_header_codes =''; |
| 171 |
$single_header_codes = ''; |
| 172 |
$output = ''; |
| 173 |
|
| 174 |
if ( $current_page_id ) { |
| 175 |
//value is 'on' if checked |
| 176 |
$hide_global_header = get_post_meta( $current_page_id, 'accodes_hide_header', true ); |
| 177 |
//get Single Header Codes |
| 178 |
$get_single_header_codes = get_post_meta( $current_page_id , '_accodes_header_metabox', true ); |
| 179 |
if($get_single_header_codes !='') |
| 180 |
{ |
| 181 |
$single_header_codes = PHP_EOL.'<!-- Single header Scripts by Add Custom Codes -->'. PHP_EOL; |
| 182 |
$single_header_codes .= $get_single_header_codes; |
| 183 |
$single_header_codes .= PHP_EOL.'<!-- End of Single header Scripts by Add Custom Codes -->'. PHP_EOL; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
//get global header - if not set to hide |
| 188 |
if($hide_global_header != 'on') |
| 189 |
{ |
| 190 |
//get Global Header Codes |
| 191 |
$get_global_header_codes = get_option( 'custom-header-codes-input' ); |
| 192 |
if($get_global_header_codes!='') |
| 193 |
{ |
| 194 |
$global_header_codes = PHP_EOL.'<!-- Global Header Scripts by Add Custom Codes -->'. PHP_EOL; |
| 195 |
$global_header_codes .= $get_global_header_codes; |
| 196 |
$global_header_codes .= PHP_EOL.'<!-- End - Global Header Scripts by Add Custom Codes -->'. PHP_EOL; |
| 197 |
} |
| 198 |
} |
| 199 |
$output .= $global_header_codes; |
| 200 |
$output .= $single_header_codes; |
| 201 |
|
| 202 |
if($output !='') |
| 203 |
{ |
| 204 |
echo $output; |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
/*--------------------------------- |
| 212 |
output Global Footer Codes |
| 213 |
----------------*/ |
| 214 |
add_action( 'wp_footer', 'accodes_footer_output' ); |
| 215 |
function accodes_footer_output() { |
| 216 |
|
| 217 |
$hide_global_footer = ''; |
| 218 |
$current_page_id = get_current_page_id(); |
| 219 |
$global_footer_codes =''; |
| 220 |
$single_footer_codes = ''; |
| 221 |
$output = ''; |
| 222 |
|
| 223 |
if ( $current_page_id ) { |
| 224 |
//value is 'on' if checked |
| 225 |
$hide_global_footer = get_post_meta( $current_page_id, 'accodes_hide_footer', true ); |
| 226 |
//get Footer Codes for Single |
| 227 |
$get_single_footer_codes = get_post_meta( $current_page_id , '_accodes_footer_metabox', true ); |
| 228 |
if($get_single_footer_codes !='') |
| 229 |
{ |
| 230 |
$single_footer_codes = PHP_EOL.'<!-- Single Footer Scripts by Add Custom Codes -->'. PHP_EOL; |
| 231 |
$single_footer_codes .= $get_single_footer_codes; |
| 232 |
$single_footer_codes .= PHP_EOL.'<!-- End - Single Footer Scripts by Add Custom Codes -->'. PHP_EOL; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
//get global footer - if not set to hide |
| 237 |
if($hide_global_footer != 'on') |
| 238 |
{ |
| 239 |
//get Global Footer codes |
| 240 |
$get_global_footer_codes = get_option( 'custom-footer-codes-input' ); |
| 241 |
if($get_global_footer_codes!='') |
| 242 |
{ |
| 243 |
$global_footer_codes = PHP_EOL.'<!-- Global Footer Scripts by Add Custom Codes -->'. PHP_EOL; |
| 244 |
$global_footer_codes .= $get_global_footer_codes; |
| 245 |
$global_footer_codes .= PHP_EOL.'<!-- End - Global Footer Scripts by Add Custom Codes -->'. PHP_EOL; |
| 246 |
} |
| 247 |
} |
| 248 |
$output .= $global_footer_codes; |
| 249 |
$output .= $single_footer_codes; |
| 250 |
|
| 251 |
if($output !='') |
| 252 |
{ |
| 253 |
echo $output; |
| 254 |
} |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
/*---------------- |
| 261 |
* Individual pages |
| 262 |
* -----------------------*/ |
| 263 |
|
| 264 |
/** |
| 265 |
* Create the meta boxes for Single post, page, product any other custom post type |
| 266 |
*/ |
| 267 |
function _accodes_create_metabox_single() { |
| 268 |
|
| 269 |
$post_types = get_post_types( '', 'names' ); |
| 270 |
$post_types = array_merge( $post_types, array( 'post', 'page' ) ); |
| 271 |
|
| 272 |
foreach ( $post_types as $post_type ) { |
| 273 |
add_meta_box( '_accodes_metabox', 'Add Custom Codes by Mak', '_accodes_render_metabox', $post_type, |
| 274 |
'normal', 'default'); |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
add_action( 'add_meta_boxes', '_accodes_create_metabox_single' ); |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
/*------------------------------- |
| 284 |
* Display Meta Boxes for Single |
| 285 |
* ----------------------------*/ |
| 286 |
function _accodes_render_metabox() { |
| 287 |
// Variables |
| 288 |
global $post; // Get the current post data |
| 289 |
$header_script = get_post_meta( $post->ID, '_accodes_header_metabox', true ); // Get the saved values |
| 290 |
$footer_script = get_post_meta( $post->ID, '_accodes_footer_metabox', true ); // Get the saved values |
| 291 |
|
| 292 |
$hide_header = get_post_meta( $post->ID, 'accodes_hide_header', true ); |
| 293 |
$hide_footer = get_post_meta( $post->ID, 'accodes_hide_footer', true ); |
| 294 |
|
| 295 |
include('single-meta.php'); |
| 296 |
|
| 297 |
/* check if the submission come from actual server dashboard */ |
| 298 |
wp_nonce_field( '_accodes_form_metabox_nonce', '_accodes_form_metabox_process' ); |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
/*------------------------- |
| 304 |
* update data on post save - Single |
| 305 |
---------------------------*/ |
| 306 |
function _accodes_save_metabox_single( $post_id, $post ) { |
| 307 |
|
| 308 |
// Verify data came from edit/dashboard screen |
| 309 |
if ( !wp_verify_nonce( $_POST['_accodes_form_metabox_process'], '_accodes_form_metabox_nonce' ) ) { |
| 310 |
return $post->ID; |
| 311 |
} |
| 312 |
|
| 313 |
// Verify user has permission to edit post |
| 314 |
if ( !current_user_can( 'edit_post', $post->ID )) { |
| 315 |
return $post->ID; |
| 316 |
} |
| 317 |
|
| 318 |
//Get values of meta boxes |
| 319 |
$accodes_header_metabox = isset( $_POST['_accodes_header_metabox'] ) ? $_POST['_accodes_header_metabox'] : ''; |
| 320 |
$accodes_footer_metabox = isset( $_POST['_accodes_footer_metabox'] ) ? $_POST['_accodes_footer_metabox'] : ''; |
| 321 |
|
| 322 |
// Get values of checkbox |
| 323 |
$hide_header = isset( $_POST['accodes_hide_header'] ) ? $_POST['accodes_hide_header'] : ''; |
| 324 |
$hide_footer = isset( $_POST['accodes_hide_footer'] ) ? $_POST['accodes_hide_footer'] : ''; |
| 325 |
|
| 326 |
//Update values of meta boxes |
| 327 |
update_post_meta( $post->ID, '_accodes_header_metabox', $accodes_header_metabox ); |
| 328 |
update_post_meta( $post->ID, '_accodes_footer_metabox', $accodes_footer_metabox ); |
| 329 |
|
| 330 |
//Update values of check boxes |
| 331 |
update_post_meta( $post->ID, 'accodes_hide_header', $hide_header ); |
| 332 |
update_post_meta( $post->ID, 'accodes_hide_footer', $hide_footer ); |
| 333 |
|
| 334 |
} |
| 335 |
add_action( 'save_post', '_accodes_save_metabox_single', 1, 2 ); |
| 336 |
?> |