Head & Footer Code - Version 1.0.9

Version Description

  • Add: Descriptive post types and descriptions for article specific sections
  • Add: Option to process shortcodes in FOOTER section (global setting for site-wide and article specific)
  • Change: Separate priority for HEAD and FOOT so admin can choose different priorities for header and footer
  • Add: CodeMirror code editor for HEAD and FOOTER code in plugin settings
  • Add: Plugin activation hook to prevent fatal errors in case of legacy WP and/or PHP
  • Improve: Loading security
Download this release

Release Info

Developer urkekg
Plugin Icon Head & Footer Code
Version 1.0.9
Comparing to
See all releases

Code changes from version 1.0.8 to 1.0.9

assets/css/admin.css CHANGED
@@ -1,2 +1,2 @@
1
- #head_footer_code_settings .head_footer_code_wrapper{display:table}#head_footer_code_settings .head_footer_code_wrapper .content_cell{display:table-cell}#head_footer_code_settings .head_footer_code_wrapper .sidebar_container{display:table-cell;padding:0 0 0 20px;width:25%}#head_footer_code_settings .head_footer_code_wrapper .sidebar_container .auhfc-button{display:block;background:#fff;border-radius:7px;border:1px solid rgba(0,0,0,0.1);box-shadow:0 0 0 5px rgba(0,0,0,0.1);padding:11px 15px;white-space:nowrap;line-height:18px;color:#bdbdbd;text-shadow:1px 1px #fff;text-decoration:none;position:relative;min-width:120px}#head_footer_code_settings .head_footer_code_wrapper .sidebar_container .auhfc-button:hover{color:#6796a9;background-color:#c2edff}#head_footer_code_settings .head_footer_code_wrapper .sidebar_container .auhfc-button.paypal_donate:after{content:' ';background-image:url(../img/paypal.png);background-repeat:no-repeat;background-position:center right;width:62px;height:100%;display:inline-block;position:absolute;right:15px;top:0;bottom:0}
2
/*# sourceMappingURL=admin.css.map */
1
+ #head_footer_code_settings .head_footer_code_wrapper{display:table;width:100%}#head_footer_code_settings .head_footer_code_wrapper .content_cell{display:table-cell}#head_footer_code_settings .head_footer_code_wrapper .sidebar_container{display:table-cell;padding:0 0 0 20px;width:25%}#head_footer_code_settings .head_footer_code_wrapper .sidebar_container .auhfc-button{display:block;background:#fff;border-radius:7px;border:1px solid rgba(0,0,0,0.1);box-shadow:0 0 0 5px rgba(0,0,0,0.1);padding:11px 15px;white-space:nowrap;line-height:18px;color:#bdbdbd;text-shadow:1px 1px #fff;text-decoration:none;position:relative;min-width:120px}#head_footer_code_settings .head_footer_code_wrapper .sidebar_container .auhfc-button:hover{color:#6796a9;background-color:#c2edff}#head_footer_code_settings .head_footer_code_wrapper .sidebar_container .auhfc-button.paypal_donate:after{content:' ';background-image:url(../img/paypal.png);background-repeat:no-repeat;background-position:center right;width:62px;height:100%;display:inline-block;position:absolute;right:15px;top:0;bottom:0}
2
/*# sourceMappingURL=admin.css.map */
assets/css/admin.css.map CHANGED
@@ -1,6 +1,6 @@
1
{
2
"version": 3,
3
- "mappings": "AACI,oDAA0B,CACtB,OAAO,CAAE,KAAK,CAEd,kEAAc,CACV,OAAO,CAAE,UAAU,CAGvB,uEAAmB,CACf,OAAO,CAAE,UAAU,CACnB,OAAO,CAAE,UAAU,CACnB,KAAK,CAAE,GAAG,CAEV,qFAAc,CACV,OAAO,CAAE,KAAK,CACd,UAAU,CAAE,IAAI,CAChB,aAAa,CAAE,GAAG,CAClB,MAAM,CAAE,yBAAyB,CACjC,UAAU,CAAE,yBAAyB,CACrC,OAAO,CAAE,SAAS,CAClB,WAAW,CAAE,MAAM,CACnB,WAAW,CAAE,IAAI,CACjB,KAAK,CAAE,OAAO,CACd,WAAW,CAAE,YAAY,CACzB,eAAe,CAAE,IAAI,CACrB,QAAQ,CAAE,QAAQ,CAClB,SAAS,CAAE,KAAK,CAEhB,2FAAQ,CACJ,KAAK,CAAE,OAAO,CACd,gBAAgB,CAAE,OAAO,CAG7B,yGAAsB,CAClB,OAAO,CAAE,GAAG,CACZ,gBAAgB,CAAE,sBAAsB,CACxC,iBAAiB,CAAE,SAAS,CAC5B,mBAAmB,CAAE,YAAY,CACjC,KAAK,CAAE,IAAI,CACX,MAAM,CAAE,IAAI,CACZ,OAAO,CAAE,YAAY,CACrB,QAAQ,CAAE,QAAQ,CAClB,KAAK,CAAE,IAAI,CACX,GAAG,CAAE,CAAC,CACN,MAAM,CAAE,CAAC",
4
"sources": ["admin.scss"],
5
"names": [],
6
"file": "admin.css"
1
{
2
"version": 3,
3
+ "mappings": "AACI,oDAA0B,CACtB,OAAO,CAAE,KAAK,CACd,KAAK,CAAE,IAAI,CAEX,kEAAc,CACV,OAAO,CAAE,UAAU,CAGvB,uEAAmB,CACf,OAAO,CAAE,UAAU,CACnB,OAAO,CAAE,UAAU,CACnB,KAAK,CAAE,GAAG,CAEV,qFAAc,CACV,OAAO,CAAE,KAAK,CACd,UAAU,CAAE,IAAI,CAChB,aAAa,CAAE,GAAG,CAClB,MAAM,CAAE,yBAAyB,CACjC,UAAU,CAAE,yBAAyB,CACrC,OAAO,CAAE,SAAS,CAClB,WAAW,CAAE,MAAM,CACnB,WAAW,CAAE,IAAI,CACjB,KAAK,CAAE,OAAO,CACd,WAAW,CAAE,YAAY,CACzB,eAAe,CAAE,IAAI,CACrB,QAAQ,CAAE,QAAQ,CAClB,SAAS,CAAE,KAAK,CAEhB,2FAAQ,CACJ,KAAK,CAAE,OAAO,CACd,gBAAgB,CAAE,OAAO,CAG7B,yGAAsB,CAClB,OAAO,CAAE,GAAG,CACZ,gBAAgB,CAAE,sBAAsB,CACxC,iBAAiB,CAAE,SAAS,CAC5B,mBAAmB,CAAE,YAAY,CACjC,KAAK,CAAE,IAAI,CACX,MAAM,CAAE,IAAI,CACZ,OAAO,CAAE,YAAY,CACrB,QAAQ,CAAE,QAAQ,CAClB,KAAK,CAAE,IAAI,CACX,GAAG,CAAE,CAAC,CACN,MAAM,CAAE,CAAC",
4
"sources": ["admin.scss"],
5
"names": [],
6
"file": "admin.css"
assets/css/admin.scss CHANGED
@@ -1,6 +1,7 @@
1
#head_footer_code_settings {
2
.head_footer_code_wrapper {
3
display: table;
4
5
.content_cell {
6
display: table-cell;
1
#head_footer_code_settings {
2
.head_footer_code_wrapper {
3
display: table;
4
+ width: 100%;
5
6
.content_cell {
7
display: table-cell;
head-footer-code.php CHANGED
@@ -7,8 +7,8 @@
7
* @wordpress-plugin
8
* Plugin Name: Head & Footer Code
9
* Plugin URI: https://urosevic.net/wordpress/plugins/head-footer-code/
10
- * Description: Easy add site-wide and/or article specific custom code to head and/or footer sections (before the &lt;/head&gt; or &lt;/body&gt;) by hooking to <code>wp_head</code> and <code>wp_footer</code>. Support Multisite environment.
11
- * Version: 1.0.8
12
* Author: Aleksandar Urosevic
13
* Author URI: https://urosevic.net
14
* License: GPL-3.0+
@@ -22,13 +22,9 @@ if ( ! defined( 'WPINC' ) ) {
22
die;
23
}
24
25
- define( 'WPAU_HEAD_FOOTER_CODE_VER', '1.0.8' );
26
27
// Load files.
28
require_once 'inc/helpers.php';
29
- if ( is_admin() ) {
30
- require_once 'inc/settings.php';
31
- require_once 'inc/class-auhfc-meta-box.php';
32
- } else {
33
- require_once 'inc/front.php';
34
- }
7
* @wordpress-plugin
8
* Plugin Name: Head & Footer Code
9
* Plugin URI: https://urosevic.net/wordpress/plugins/head-footer-code/
10
+ * Description: Easy add site-wide and/or article specific custom code to head and/or footer sections (before the &lt;/head&gt; or &lt;/body&gt;) by hooking to <code>wp_head</code> and <code>wp_footer</code>.
11
+ * Version: 1.0.9
12
* Author: Aleksandar Urosevic
13
* Author URI: https://urosevic.net
14
* License: GPL-3.0+
22
die;
23
}
24
25
+ define( 'WPAU_HEAD_FOOTER_CODE_VER', '1.0.9' );
26
+ define( 'WPAU_HEAD_FOOTER_CODE_DB_VER', '2' );
27
+ define( 'WPAU_HEAD_FOOTER_CODE_FILE', basename( __FILE__ ) );
28
29
// Load files.
30
require_once 'inc/helpers.php';
inc/class-auhfc-meta-box.php CHANGED
@@ -1,4 +1,9 @@
1
<?php
2
abstract class AUHfc_Meta_Box {
3
4
/**
1
<?php
2
+ // If this file is called directly, abort.
3
+ if ( ! defined( 'WPINC' ) ) {
4
+ die;
5
+ }
6
+
7
abstract class AUHfc_Meta_Box {
8
9
/**
inc/front.php CHANGED
@@ -1,15 +1,23 @@
1
<?php
2
/**
3
* Inject site-wide code to head and footer with custom priorty.
4
*/
5
$auhfc_defaults = auhfc_defaults();
6
- if ( empty( $auhfc_defaults['priority'] ) ) {
7
- $auhfc_defaults['priority'] = 10;
8
- }
9
10
// Define actions for HEAD and FOOTER
11
- add_action( 'wp_head', 'auhfc_wp_head', $auhfc_defaults['priority'] );
12
- add_action( 'wp_footer', 'auhfc_wp_footer', $auhfc_defaults['priority'] );
13
14
/**
15
* Inject site-wide and Article specific head code before </head>
@@ -32,7 +40,7 @@ function auhfc_wp_head() {
32
$auhfc_meta = auhfc_get_meta( 'head' );
33
$behavior = auhfc_get_meta( 'behavior' );
34
if ( WP_DEBUG ) {
35
- $dbg_set = "(type: {$auhfc_post_type}; bahavior: {$behavior})";
36
}
37
} else {
38
$auhfc_meta = '';
@@ -59,20 +67,21 @@ function auhfc_wp_head() {
59
( 'replace' == $behavior && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) && empty( $auhfc_meta ) )
60
)
61
) {
62
- if ( WP_DEBUG ) { $out .= "<!-- Head & Footer Code: Site-wide head section start {$dbg_set} -->\n"; }
63
$out .= $auhfc_settings['head'];
64
- if ( WP_DEBUG ) { $out .= "<!-- Head & Footer Code: Site-wide head section end {$dbg_set} -->\n"; }
65
}
66
67
// Inject article specific head code if post_type is allowed
68
if ( ! empty( $auhfc_meta ) && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) {
69
- if ( WP_DEBUG ) { $out .= "<!-- Head & Footer Code: Article specific head section start {$dbg_set} -->\n"; }
70
$out .= $auhfc_meta;
71
- if ( WP_DEBUG ) { $out .= "<!-- Head & Footer Code: Article specific head section end {$dbg_set} -->\n"; }
72
}
73
74
// Print prepared code.
75
echo $out;
76
77
// Free some memory.
78
unset( $auhfc_post_type, $auhfc_settings, $auhfc_meta, $behavior, $out );
@@ -100,7 +109,7 @@ function auhfc_wp_footer() {
100
$auhfc_meta = auhfc_get_meta( 'footer' );
101
$behavior = auhfc_get_meta( 'behavior' );
102
if ( WP_DEBUG ) {
103
- $dbg_set = "(type: {$auhfc_post_type}; bahavior: {$behavior})";
104
}
105
} else {
106
$auhfc_meta = '';
@@ -127,20 +136,20 @@ function auhfc_wp_footer() {
127
( 'replace' == $behavior && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) && empty( $auhfc_meta ) )
128
)
129
) {
130
- if ( WP_DEBUG ) { $out .= "<!-- Head & Footer Code: Site-wide footer section start {$dbg_set} -->\n"; }
131
$out .= $auhfc_settings['footer'];
132
- if ( WP_DEBUG ) { $out .= "<!-- Head & Footer Code: Site-wide footer section end {$dbg_set} -->\n"; }
133
}
134
135
// Inject article specific head code if post_type is allowed
136
if ( ! empty( $auhfc_meta ) && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) {
137
- if ( WP_DEBUG ) { $out .= "<!-- Head & Footer Code: Article specific footer section start {$dbg_set} -->\n"; }
138
$out .= trim( $auhfc_meta );
139
- if ( WP_DEBUG ) { $out .= "<!-- Head & Footer Code: Article specific footer section end {$dbg_set} -->\n"; }
140
}
141
142
// Print prepared code.
143
- echo $out;
144
145
// Free some memory.
146
unset( $auhfc_post_type, $auhfc_settings, $auhfc_meta, $behavior, $out );
1
<?php
2
+ // If this file is called directly, abort.
3
+ if ( ! defined( 'WPINC' ) ) {
4
+ die;
5
+ }
6
+
7
/**
8
* Inject site-wide code to head and footer with custom priorty.
9
*/
10
$auhfc_defaults = auhfc_defaults();
11
12
+ if ( empty( $auhfc_defaults['priority_h'] ) ) {
13
+ $auhfc_defaults['priority_h'] = 10;
14
+ }
15
+ if ( empty( $auhfc_defaults['priority_f'] ) ) {
16
+ $auhfc_defaults['priority_f'] = 10;
17
+ }
18
// Define actions for HEAD and FOOTER
19
+ add_action( 'wp_head', 'auhfc_wp_head', $auhfc_defaults['priority_h'] );
20
+ add_action( 'wp_footer', 'auhfc_wp_footer', $auhfc_defaults['priority_f'] );
21
22
/**
23
* Inject site-wide and Article specific head code before </head>
40
$auhfc_meta = auhfc_get_meta( 'head' );
41
$behavior = auhfc_get_meta( 'behavior' );
42
if ( WP_DEBUG ) {
43
+ $dbg_set = "(type: {$auhfc_post_type}; bahavior: {$behavior}; priority: {$auhfc_settings['priority_h']}; do_shortcode: {$auhfc_settings['do_shortcode']})";
44
}
45
} else {
46
$auhfc_meta = '';
67
( 'replace' == $behavior && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) && empty( $auhfc_meta ) )
68
)
69
) {
70
+ $out .= WP_DEBUG ? "<!-- Head & Footer Code: Site-wide head section start {$dbg_set} -->\n" : '';
71
$out .= $auhfc_settings['head'];
72
+ $out .= WP_DEBUG ? $out .= "<!-- Head & Footer Code: Site-wide head section end {$dbg_set} -->\n" : '';
73
}
74
75
// Inject article specific head code if post_type is allowed
76
if ( ! empty( $auhfc_meta ) && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) {
77
+ $out .= WP_DEBUG ? "<!-- Head & Footer Code: Article specific head section start {$dbg_set} -->\n" : '';
78
$out .= $auhfc_meta;
79
+ $out .= WP_DEBUG ? "<!-- Head & Footer Code: Article specific head section end {$dbg_set} -->\n" : '';
80
}
81
82
// Print prepared code.
83
echo $out;
84
+ // echo ( 'y' === $auhfc_settings['do_shortcode'] ) ? do_shortcode( $out ) : $out;
85
86
// Free some memory.
87
unset( $auhfc_post_type, $auhfc_settings, $auhfc_meta, $behavior, $out );
109
$auhfc_meta = auhfc_get_meta( 'footer' );
110
$behavior = auhfc_get_meta( 'behavior' );
111
if ( WP_DEBUG ) {
112
+ $dbg_set = "(type: {$auhfc_post_type}; bahavior: {$behavior}; priority: {$auhfc_settings['priority_f']}; do_shortcode: {$auhfc_settings['do_shortcode']})";
113
}
114
} else {
115
$auhfc_meta = '';
136
( 'replace' == $behavior && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) && empty( $auhfc_meta ) )
137
)
138
) {
139
+ $out .= WP_DEBUG ? "<!-- Head & Footer Code: Site-wide footer section start {$dbg_set} -->\n" : '';
140
$out .= $auhfc_settings['footer'];
141
+ $out .= WP_DEBUG ? "<!-- Head & Footer Code: Site-wide footer section end {$dbg_set} -->\n" : '';
142
}
143
144
// Inject article specific head code if post_type is allowed
145
if ( ! empty( $auhfc_meta ) && in_array( $auhfc_post_type, $auhfc_settings['post_types'] ) ) {
146
+ $out .= WP_DEBUG ? "<!-- Head & Footer Code: Article specific footer section start {$dbg_set} -->\n" : '';
147
$out .= trim( $auhfc_meta );
148
+ $out .= WP_DEBUG ? "<!-- Head & Footer Code: Article specific footer section end {$dbg_set} -->\n" : '';
149
}
150
151
// Print prepared code.
152
+ echo ( 'y' === $auhfc_settings['do_shortcode'] ) ? do_shortcode( $out ) : $out;
153
154
// Free some memory.
155
unset( $auhfc_post_type, $auhfc_settings, $auhfc_meta, $behavior, $out );
inc/helpers.php CHANGED
@@ -1,15 +1,89 @@
1
<?php
2
/**
3
* Provide global defaults
4
* @return array Arary of defined global values
5
*/
6
function auhfc_defaults() {
7
- $defaults = array(
8
- 'head' => '',
9
- 'footer' => '',
10
- 'priority' => 10,
11
- 'post_types' => array(),
12
- );
13
$auhfc_settings = get_option( 'auhfc_settings', $defaults );
14
$auhfc_settings = wp_parse_args( $auhfc_settings, $defaults );
15
return $auhfc_settings;
1
<?php
2
+ // If this file is called directly, abort.
3
+ if ( ! defined( 'WPINC' ) ) {
4
+ die;
5
+ }
6
+
7
+ // Include back-end/front-end resources.
8
+ if ( is_admin() ) {
9
+ require_once 'settings.php';
10
+ require_once 'class-auhfc-meta-box.php';
11
+ } else {
12
+ require_once 'front.php';
13
+ }
14
+
15
+ register_activation_hook( WPAU_HEAD_FOOTER_CODE_FILE, 'auhfc_activate' );
16
+ /**
17
+ * Plugin Activation hook function to check for Minimum PHP and WordPress versions
18
+ */
19
+ function auhfc_activate() {
20
+ global $wp_version;
21
+ $php_req = '5.6'; // Minimum version of PHP required for this plugin
22
+ $wp_req = '4.9'; // Minimum version of WordPress required for this plugin
23
+
24
+ if ( version_compare( PHP_VERSION, $php_req, '<' ) ) {
25
+ $flag = 'PHP';
26
+ } elseif ( version_compare( $wp_version, $wp_req, '<' ) ) {
27
+ $flag = 'WordPress';
28
+ } else {
29
+ return;
30
+ }
31
+ $version = 'PHP' == $flag ? $php_req : $wp_req;
32
+ deactivate_plugins( WPAU_HEAD_FOOTER_CODE_FILE );
33
+ wp_die(
34
+ '<p>The <strong>Head & Footer Code</strong> plugin requires' . $flag . ' version ' . $version . ' or greater.</p>',
35
+ 'Plugin Activation Error',
36
+ [
37
+ 'response' => 200,
38
+ 'back_link' => true,
39
+ ]
40
+ );
41
+
42
+ // Trigger updater function.
43
+ auhfc_maybe_update();
44
+ } // END function auhfc_activate()
45
+
46
+ // Regular update trigger.
47
+ add_action( 'plugins_loaded', 'auhfc_maybe_update' );
48
+ function auhfc_maybe_update() {
49
+ // Bail if this plugin data doesn't need updating.
50
+ if ( get_option( 'auhfc_db_ver' ) >= WPAU_HEAD_FOOTER_CODE_DB_VER ) {
51
+ return;
52
+ }
53
+ // Require update script.
54
+ require_once( dirname( __FILE__ ) . '/update.php' );
55
+ // Trigger update function.
56
+ auhfc_update();
57
+ } // END function auhfc_maybe_update()
58
+
59
+ add_action( 'admin_enqueue_scripts', 'auhfc_codemirror_enqueue_scripts' );
60
+ /**
61
+ * CodeMirror enqueue hoot function to enable code editor in plugin settings
62
+ * @param string $hook Current page hook
63
+ */
64
+ function auhfc_codemirror_enqueue_scripts( $hook ) {
65
+ if ( 'tools_page_head_footer_code' !== $hook ) {
66
+ return;
67
+ }
68
+ $cm_settings['codeEditor'] = wp_enqueue_code_editor( [ 'type' => 'text/html' ] );
69
+ wp_localize_script( 'jquery', 'cm_settings', $cm_settings );
70
+ wp_enqueue_script( 'wp-codemirror' );
71
+ wp_enqueue_style( 'wp-codemirror' );
72
+ } // END function auhfc_codemirror_enqueue_scripts( $hook )
73
+
74
/**
75
* Provide global defaults
76
* @return array Arary of defined global values
77
*/
78
function auhfc_defaults() {
79
+ $defaults = [
80
+ 'head' => '',
81
+ 'footer' => '',
82
+ 'priority_h' => 10,
83
+ 'priority_f' => 10,
84
+ 'post_types' => [],
85
+ 'do_shortcode' => 'n',
86
+ ];
87
$auhfc_settings = get_option( 'auhfc_settings', $defaults );
88
$auhfc_settings = wp_parse_args( $auhfc_settings, $defaults );
89
return $auhfc_settings;
inc/settings.php CHANGED
@@ -1,4 +1,8 @@
1
<?php
2
3
// Initiate settings section and fields.
4
add_action( 'admin_init', 'auhfc_settings_init' );
@@ -23,7 +27,7 @@ function auhfc_admin_enqueue_scripts( $hook ) {
23
wp_enqueue_style(
24
'head-footer-code-admin',
25
plugin_dir_url( __FILE__ ) . '../assets/css/admin.css',
26
- array(),
27
WPAU_HEAD_FOOTER_CODE_VER
28
);
29
}
@@ -88,47 +92,80 @@ function auhfc_settings_init() {
88
'auhfc_textarea_field_render',
89
'head_footer_code',
90
'head_footer_code_sitewide_settings',
91
- array(
92
'field' => 'auhfc_settings[head]',
93
'value' => $auhfc_settings['head'],
94
'description' => __( 'Code to enqueue in HEAD section', 'head-footer-code' ),
95
'field_class' => 'widefat code',
96
'rows' => 7,
97
- )
98
);
99
100
add_settings_field(
101
'auhfc_footer_code',
102
__( 'FOOTER Code', 'head-footer-code' ),
103
'auhfc_textarea_field_render',
104
'head_footer_code',
105
'head_footer_code_sitewide_settings',
106
- array(
107
'field' => 'auhfc_settings[footer]',
108
'value' => $auhfc_settings['footer'],
109
'description' => esc_html__( 'Code to enqueue in footer section (before the </body>)', 'head-footer-code' ),
110
'field_class' => 'widefat code',
111
'rows' => 7,
112
- )
113
);
114
115
add_settings_field(
116
- 'auhfc_priority',
117
- __( 'Priority', 'head-footer-code' ),
118
'auhfc_number_field_render',
119
'head_footer_code',
120
'head_footer_code_sitewide_settings',
121
- array(
122
- 'field' => 'auhfc_settings[priority]',
123
- 'value' => $auhfc_settings['priority'],
124
- 'description' => esc_html__( 'Priority of inserted head and footer code. Default is 10. Larger number inject code closer to </head> and </body>.', 'head-footer-code' ),
125
'class' => 'num',
126
'min' => 1,
127
'max' => 1000,
128
'step' => 1,
129
- )
130
);
131
132
/**
133
* Settings Sections are the groups of settings you see on WordPress settings pages
134
* with a shared heading. In your plugin you can add new sections to existing
@@ -144,25 +181,32 @@ function auhfc_settings_init() {
144
);
145
146
// Prepare clean list of post types w/o attachment
147
- $clean_post_types = get_post_types( array( 'public' => true ) );
148
- unset( $clean_post_types['attachment'] );
149
150
add_settings_field(
151
'auhfc_post_types',
152
- __( 'Post types', 'head-footer-code' ),
153
'auhfc_checkbox_group_field_render',
154
'head_footer_code',
155
'head_footer_code_article_settings',
156
- array(
157
'field' => 'auhfc_settings[post_types]',
158
'items' => $clean_post_types,
159
'value' => $auhfc_settings['post_types'],
160
'description' => esc_html__( 'Select which post types will have Article specific section. Default is post and page. Please note, even if you have Head/Footer Code set per article and then you disable that post type, article specific code will not be printed but only site-wide code.', 'head-footer-code' ),
161
'class' => 'checkbox',
162
- )
163
);
164
165
- } // END function auhfc_settings_init( )
166
167
/**
168
* This function provides textarea for settings fields
@@ -172,12 +216,13 @@ function auhfc_textarea_field_render( $args ) {
172
$rows = 7;
173
}
174
printf(
175
- '<textarea name="%1$s" id="%1$s" rows="%2$s" class="%3$s">%4$s</textarea><p class="description">%5$s</p>',
176
$args['field'],
177
$args['rows'],
178
$args['field_class'],
179
$args['value'],
180
- $args['description']
181
);
182
} // END function auhfc_textarea_field_render( $args )
183
@@ -223,6 +268,13 @@ function auhfc_checkbox_group_field_render( $args ) {
223
);
224
}
225
226
$out .= '</fieldset>';
227
$out .= sprintf( '<p class="description">%s</p>', $description );
228
@@ -230,6 +282,47 @@ function auhfc_checkbox_group_field_render( $args ) {
230
231
} // eom settings_field_checkbox()
232
233
function auhfc_sitewide_settings_section_description() {
234
?>
235
<p>Define site-wide code and behavior. You can Add custom content like JavaScript, CSS, HTML meta and link tags, Google Analytics, site verification, etc.</p>
@@ -271,7 +364,7 @@ function auhfc_add_plugin_meta_links( $links, $file ) {
271
if ( 'head-footer-code/head-footer-code.php' === $file ) {
272
return array_merge(
273
$links,
274
- array(
275
sprintf(
276
'<a href="https://wordpress.org/support/plugin/head-footer-code" target="_blank">%s</a>',
277
__( 'Support' )
@@ -280,7 +373,7 @@ function auhfc_add_plugin_meta_links( $links, $file ) {
280
'<a href="https://urosevic.net/wordpress/donate/?donate_for=head-footer-code" target="_blank">%s</a>',
281
__( 'Donate' )
282
),
283
- )
284
);
285
}
286
return $links;
1
<?php
2
+ // If this file is called directly, abort.
3
+ if ( ! defined( 'WPINC' ) ) {
4
+ die;
5
+ }
6
7
// Initiate settings section and fields.
8
add_action( 'admin_init', 'auhfc_settings_init' );
27
wp_enqueue_style(
28
'head-footer-code-admin',
29
plugin_dir_url( __FILE__ ) . '../assets/css/admin.css',
30
+ [],
31
WPAU_HEAD_FOOTER_CODE_VER
32
);
33
}
92
'auhfc_textarea_field_render',
93
'head_footer_code',
94
'head_footer_code_sitewide_settings',
95
+ [
96
'field' => 'auhfc_settings[head]',
97
'value' => $auhfc_settings['head'],
98
'description' => __( 'Code to enqueue in HEAD section', 'head-footer-code' ),
99
'field_class' => 'widefat code',
100
'rows' => 7,
101
+ ]
102
);
103
104
+ add_settings_field(
105
+ 'auhfc_priority_h',
106
+ __( 'HEAD Priority', 'head-footer-code' ),
107
+ 'auhfc_number_field_render',
108
+ 'head_footer_code',
109
+ 'head_footer_code_sitewide_settings',
110
+ [
111
+ 'field' => 'auhfc_settings[priority_h]',
112
+ 'value' => $auhfc_settings['priority_h'],
113
+ 'description' => esc_html__( 'Priority for enqueued HEAD code. Default is 10. Larger number inject code closer to </head>.', 'head-footer-code' ),
114
+ 'class' => 'num',
115
+ 'min' => 1,
116
+ 'max' => 1000,
117
+ 'step' => 1,
118
+ ]
119
+ );
120
add_settings_field(
121
'auhfc_footer_code',
122
__( 'FOOTER Code', 'head-footer-code' ),
123
'auhfc_textarea_field_render',
124
'head_footer_code',
125
'head_footer_code_sitewide_settings',
126
+ [
127
'field' => 'auhfc_settings[footer]',
128
'value' => $auhfc_settings['footer'],
129
'description' => esc_html__( 'Code to enqueue in footer section (before the </body>)', 'head-footer-code' ),
130
'field_class' => 'widefat code',
131
'rows' => 7,
132
+ ]
133
);
134
135
add_settings_field(
136
+ 'auhfc_priority_f',
137
+ __( 'FOOTER Priority', 'head-footer-code' ),
138
'auhfc_number_field_render',
139
'head_footer_code',
140
'head_footer_code_sitewide_settings',
141
+ [
142
+ 'field' => 'auhfc_settings[priority_f]',
143
+ 'value' => $auhfc_settings['priority_f'],
144
+ 'description' => esc_html__( 'Priority for enqueued FOOTER code. Default is 10. Larger number inject code closer to </body>.', 'head-footer-code' ),
145
'class' => 'num',
146
'min' => 1,
147
'max' => 1000,
148
'step' => 1,
149
+ ]
150
);
151
152
+ add_settings_field(
153
+ 'auhfc_do_shortcode',
154
+ __( 'Process Shortcodes', 'head-footer-code' ),
155
+ 'auhfc_select_field_render',
156
+ 'head_footer_code',
157
+ 'head_footer_code_sitewide_settings',
158
+ [
159
+ 'field' => 'auhfc_settings[do_shortcode]',
160
+ 'items' => [
161
+ 'y' => __( 'Enable' ),
162
+ 'n' => __( 'Disable' ),
163
+ ],
164
+ 'value' => $auhfc_settings['do_shortcode'],
165
+ 'description' => esc_html__( 'If you wish to process shortcodes in FOOTER section, enable this option.', 'head-footer-code' ),
166
+ 'class' => 'regilar-text',
167
+ ]
168
+ );
169
/**
170
* Settings Sections are the groups of settings you see on WordPress settings pages
171
* with a shared heading. In your plugin you can add new sections to existing
181
);
182
183
// Prepare clean list of post types w/o attachment
184
+ $public_post_types = get_post_types( [ 'public' => true ], 'objects' );
185
+ $clean_post_types = [];
186
+ foreach ( $public_post_types as $public_post_type => $public_post_object ) {
187
+ if ( 'attachment' === $public_post_type ) {
188
+ continue;
189
+ }
190
+ $clean_post_types[ $public_post_type ] = "{$public_post_object->label} ({$public_post_type})";
191
+ }
192
+ // unset( $clean_post_types['attachment'] );
193
194
add_settings_field(
195
'auhfc_post_types',
196
+ __( 'Post Types', 'head-footer-code' ),
197
'auhfc_checkbox_group_field_render',
198
'head_footer_code',
199
'head_footer_code_article_settings',
200
+ [
201
'field' => 'auhfc_settings[post_types]',
202
'items' => $clean_post_types,
203
'value' => $auhfc_settings['post_types'],
204
'description' => esc_html__( 'Select which post types will have Article specific section. Default is post and page. Please note, even if you have Head/Footer Code set per article and then you disable that post type, article specific code will not be printed but only site-wide code.', 'head-footer-code' ),
205
'class' => 'checkbox',
206
+ ]
207
);
208
209
+ } // END function auhfc_settings_init()
210
211
/**
212
* This function provides textarea for settings fields
216
$rows = 7;
217
}
218
printf(
219
+ '<textarea name="%1$s" id="%6$s" rows="%2$s" class="%3$s">%4$s</textarea><p class="description">%5$s</p>',
220
$args['field'],
221
$args['rows'],
222
$args['field_class'],
223
$args['value'],
224
+ $args['description'],
225
+ str_replace( ']', '', str_replace( '[', '_', $args['field'] ) )
226
);
227
} // END function auhfc_textarea_field_render( $args )
228
268
);
269
}
270
271
+ if ( ! empty( $args['description'] ) ) {
272
+ $out .= sprintf(
273
+ '<p class="description">%s</p>',
274
+ trim( $args['description'] )
275
+ );
276
+ }
277
+
278
$out .= '</fieldset>';
279
$out .= sprintf( '<p class="description">%s</p>', $description );
280
282
283
} // eom settings_field_checkbox()
284
285
+
286
+ /**
287
+ * This function provides select for settings fields
288
+ * @param array $args Array of field arguments.
289
+ */
290
+ function auhfc_select_field_render( $args ) {
291
+ if ( empty( $args['class'] ) ) {
292
+ $args['class'] = 'regular-text';
293
+ }
294
+ printf(
295
+ '<select id="%1$s" name="%1$s" class="%2$s">',
296
+ esc_attr( $args['field'] ),
297
+ sanitize_html_class( $args['class'] )
298
+ );
299
+ foreach ( $args['items'] as $key => $val ) {
300
+ $selected = ( $args['value'] == $key ) ? 'selected=selected' : '';
301
+ printf(
302
+ '<option %1$s value="%2$s">%3$s</option>',
303
+ esc_attr( $selected ), // 1
304
+ sanitize_key( $key ), // 2
305
+ sanitize_text_field( $val ) // 3
306
+ );
307
+ }
308
+ printf(
309
+ '</select><p class="description">%s</p>',
310
+ wp_kses(
311
+ $args['description'],
312
+ [
313
+ 'a' => [
314
+ 'href' => [],
315
+ 'target' => [ '_blank' ],
316
+ ],
317
+ 'strong',
318
+ 'em',
319
+ 'pre',
320
+ 'code',
321
+ ]
322
+ )
323
+ );
324
+ } // END public function settings_field_select($args)
325
+
326
function auhfc_sitewide_settings_section_description() {
327
?>
328
<p>Define site-wide code and behavior. You can Add custom content like JavaScript, CSS, HTML meta and link tags, Google Analytics, site verification, etc.</p>
364
if ( 'head-footer-code/head-footer-code.php' === $file ) {
365
return array_merge(
366
$links,
367
+ [
368
sprintf(
369
'<a href="https://wordpress.org/support/plugin/head-footer-code" target="_blank">%s</a>',
370
__( 'Support' )
373
'<a href="https://urosevic.net/wordpress/donate/?donate_for=head-footer-code" target="_blank">%s</a>',
374
__( 'Donate' )
375
),
376
+ ]
377
);
378
}
379
return $links;
inc/update.php CHANGED
@@ -1,39 +1,44 @@
1
<?php
2
/**
3
* Run the incremental updates one by one.
4
*
5
* For example, if the current DB version is 3, and the target DB version is 6,
6
* this function will execute update routines if they exist:
7
- * - auhfc_update_routine_4()
8
- * - auhfc_update_routine_5()
9
- * - auhfc_update_routine_6()
10
*/
11
12
function auhfc_update() {
13
- // no PHP timeout for running updates
14
set_time_limit( 0 );
15
16
- // this is the current database schema version number
17
$current_db_ver = get_option( 'auhfc_db_ver', 0 );
18
19
- // this is the target version that we need to reach
20
$target_db_ver = WPAU_HEAD_FOOTER_CODE_DB_VER;
21
22
- // run update routines one by one until the current version number
23
- // reaches the target version number
24
while ( $current_db_ver < $target_db_ver ) {
25
- // increment the current db_ver by one
26
++$current_db_ver;
27
28
- // each db version will require a separate update function
29
- // for example, for db_ver 3, the function name should be solis_update_routine_3
30
- $func = "auhfc_update_routine_{$current_db_ver}";
31
if ( function_exists( $func ) ) {
32
call_user_func( $func );
33
}
34
35
- // update the option in the database, so that this process can always
36
- // pick up where it left off
37
update_option( 'auhfc_db_ver', $current_db_ver );
38
}
39
@@ -42,40 +47,41 @@ function auhfc_update() {
42
/**
43
* Initialize updater
44
*/
45
- function auhfc_update_routine_1() {
46
47
- // get options from DB
48
$defaults = get_option( 'auhfc_settings' );
49
50
- // split pre-1.0.8 priority to priority_h and priority_f
51
if ( isset( $defaults['priority'] ) ) {
52
- // Split single to separate option values
53
if ( ! isset( $defaults['priority_h'] ) ) {
54
$defaults['priority_h'] = $defaults['priority'];
55
}
56
if ( ! isset( $defaults['priority_f'] ) ) {
57
$defaults['priority_f'] = $defaults['priority'];
58
}
59
- // Unset old key value
60
unset( $defaults['priority'] );
61
- // Update option
62
update_option( 'auhfc_settings', $defaults );
63
}
64
65
- } // END function auhfc_update_routine_1()
66
67
/**
68
- * Add network wide head and footer defaults to main site
69
*/
70
- function auhfc_update_routine_2() {
71
- if ( is_multisite() && is_main_site() ) {
72
- $defaults = get_option( 'auhfc_settings' );
73
- if ( ! isset( $defaults['network_head'] ) ) {
74
- $defaults['network_head'] = '';
75
- }
76
- if ( ! isset( $defaults['network_footer'] ) ) {
77
- $defaults['network_footer'] = '';
78
- }
79
- update_option( 'auhfc_settings', $defaults );
80
}
81
- } // END function auhfc_update_routine_2()
1
<?php
2
+ // If this file is called directly, abort.
3
+ if ( ! defined( 'WPINC' ) ) {
4
+ die;
5
+ }
6
+
7
/**
8
* Run the incremental updates one by one.
9
*
10
* For example, if the current DB version is 3, and the target DB version is 6,
11
* this function will execute update routines if they exist:
12
+ * - auhfc_update_4()
13
+ * - auhfc_update_5()
14
+ * - auhfc_update_6()
15
*/
16
17
function auhfc_update() {
18
+ // Disable PHP timeout for running updates.
19
set_time_limit( 0 );
20
21
+ // Get the current database schema version number.
22
$current_db_ver = get_option( 'auhfc_db_ver', 0 );
23
24
+ // Get the target version that we need to reach.
25
$target_db_ver = WPAU_HEAD_FOOTER_CODE_DB_VER;
26
27
+ // Run update routines one by one until the current version number
28
+ // reaches the target version number.
29
while ( $current_db_ver < $target_db_ver ) {
30
+ // Increment the current_db_ver by one.
31
++$current_db_ver;
32
33
+ // Each DB version will require a separate update function
34
+ // for example, for db_ver 3, the function name should be auhfc_update_3.
35
+ $func = "auhfc_update_{$current_db_ver}";
36
if ( function_exists( $func ) ) {
37
call_user_func( $func );
38
}
39
40
+ // Update the option in the database,
41
+ // so that this process can always pick up where it left off.
42
update_option( 'auhfc_db_ver', $current_db_ver );
43
}
44
47
/**
48
* Initialize updater
49
*/
50
+ function auhfc_update_1() {
51
52
+ // Get options from DB.
53
$defaults = get_option( 'auhfc_settings' );
54
55
+ // Split priority to priority_h and priority_f.
56
if ( isset( $defaults['priority'] ) ) {
57
+ // Split single to specific option values.
58
if ( ! isset( $defaults['priority_h'] ) ) {
59
$defaults['priority_h'] = $defaults['priority'];
60
}
61
if ( ! isset( $defaults['priority_f'] ) ) {
62
$defaults['priority_f'] = $defaults['priority'];
63
}
64
+ // Unset old key value.
65
unset( $defaults['priority'] );
66
+ // Save settings to DB.
67
update_option( 'auhfc_settings', $defaults );
68
}
69
70
+ } // END function auhfc_update_1()
71
72
/**
73
+ * Add shortcode processor option
74
*/
75
+ function auhfc_update_2() {
76
+
77
+ // Get options from DB.
78
+ $defaults = get_option( 'auhfc_settings' );
79
+
80
+ // Add new plugin option.
81
+ if ( ! isset( $defaults['do_shortcode'] ) ) {
82
+ $defaults['do_shortcode'] = 'n';
83
}
84
+ // Save settings to DB.
85
+ update_option( 'auhfc_settings', $defaults );
86
+
87
+ } // END function auhfc_update_2()
readme.txt CHANGED
@@ -2,10 +2,10 @@
2
Contributors: urkekg
3
Donate link: https://urosevic.net/wordpress/donate/?donate_for=head-footer-code
4
Tags: wp_head, wp_footer, head footer code, custom head script, custom footer script, google analytics, pixel tracking, tracking code, javascript, scripts, site verification, css
5
- Requires at least: 4.8
6
Tested up to: 5.1.1
7
- Stable tag: 1.0.8
8
- Requires PHP: 5.5
9
License: GPLv3 or later
10
License URI: http://www.gnu.org/licenses/gpl-3.0.html
11
@@ -85,6 +85,15 @@ Because all other similar plugins could not satisfy my requirements. In general,
85
Initial release of new plugin developed by Aleksandar Urosevic.
86
87
== Changelog ==
88
= 1.0.8 =
89
* Test compatibility with WordPress 5.1.1 and PHP 7.2.15
90
* Change: Meta boxes layout and type of behavior selector
2
Contributors: urkekg
3
Donate link: https://urosevic.net/wordpress/donate/?donate_for=head-footer-code
4
Tags: wp_head, wp_footer, head footer code, custom head script, custom footer script, google analytics, pixel tracking, tracking code, javascript, scripts, site verification, css
5
+ Requires at least: 4.9
6
Tested up to: 5.1.1
7
+ Stable tag: 1.0.9
8
+ Requires PHP: 5.6
9
License: GPLv3 or later
10
License URI: http://www.gnu.org/licenses/gpl-3.0.html
11
85
Initial release of new plugin developed by Aleksandar Urosevic.
86
87
== Changelog ==
88
+
89
+ = 1.0.9 =
90
+ * Add: Descriptive post types and descriptions for article specific sections
91
+ * Add: Option to process shortcodes in FOOTER section (global setting for site-wide and article specific)
92
+ * Change: Separate priority for HEAD and FOOT so admin can choose different priorities for header and footer
93
+ * Add: CodeMirror code editor for HEAD and FOOTER code in plugin settings
94
+ * Add: Plugin activation hook to prevent fatal errors in case of legacy WP and/or PHP
95
+ * Improve: Loading security
96
+
97
= 1.0.8 =
98
* Test compatibility with WordPress 5.1.1 and PHP 7.2.15
99
* Change: Meta boxes layout and type of behavior selector
templates/settings.php CHANGED
@@ -1,4 +1,9 @@
1
<?php
2
/**
3
* Head & Footer Code General Settings page template
4
*
@@ -36,3 +41,9 @@
36
</div><!-- .sidebar_container -->
37
</div><!-- .head_footer_code_wrapper -->
38
</div>
1
<?php
2
+ // If this file is called directly, abort.
3
+ if ( ! defined( 'WPINC' ) ) {
4
+ die;
5
+ }
6
+
7
/**
8
* Head & Footer Code General Settings page template
9
*
41
</div><!-- .sidebar_container -->
42
</div><!-- .head_footer_code_wrapper -->
43
</div>
44
+ <script type="text/javascript">
45
+ jQuery(document).ready(function($) {
46
+ wp.codeEditor.initialize($('#auhfc_settings_head'), cm_settings);
47
+ wp.codeEditor.initialize($('#auhfc_settings_footer'), cm_settings);
48
+ });
49
+ </script>
uninstall.php CHANGED
@@ -1,4 +1,9 @@
1
<?php
2
/**
3
* @link https://urosevic.net
4
* @since 1.0.5
@@ -10,11 +15,11 @@ if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
10
exit();
11
}
12
13
- $option_name = 'auhfc_settings';
14
- delete_option( $option_name );
15
-
16
- // For site options in Multisite
17
- delete_site_option( $option_name );
18
19
// Delete post meta values
20
$post_meta_key = '_auhfc';
1
<?php
2
+ // If this file is called directly, abort.
3
+ if ( ! defined( 'WPINC' ) ) {
4
+ die;
5
+ }
6
+
7
/**
8
* @link https://urosevic.net
9
* @since 1.0.5
15
exit();
16
}
17
18
+ $auhfc_options = array( 'auhfc_settings', 'auhfc_db_ver' );
19
+ foreach ( $auhfc_options as $option_name ) {
20
+ // Delete option on single site
21
+ delete_option( $option_name );
22
+ }
23
24
// Delete post meta values
25
$post_meta_key = '_auhfc';