This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hatchbuck-functions.php
180 lines (151 loc) · 5.06 KB
/
hatchbuck-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
if(!function_exists('hatchbuck_trim_deep')) {
function hatchbuck_trim_deep($value) {
if ( is_array($value) ) {
$value = array_map('hatchbuck_trim_deep', $value);
} elseif ( is_object($value) ) {
$vars = get_object_vars( $value );
foreach ($vars as $key=>$data) {
$value->{$key} = hatchbuck_trim_deep( $data );
}
} else {
$value = trim($value);
}
return $value;
}
}
function hatchbuck_side_wide() {
echo "\n".get_option('hatchbuck_sw_code')."\n";
}
add_action('wp_footer', 'hatchbuck_side_wide');
/**
* Adds JS/CSS to correct pages in admin and frontend
* @param type $hook
*/
function hatchbuck_scripts($hook) {
if (is_admin() && ($hook == 'toplevel_page_hatchbuck-manage' || $hook == 'hatchbuck_page_hatchbuck-settings' || $hook == 'hatchbuck_page_hatchbuck-addons' || $hook == 'hatchbuck_page_hatchbuck-help' || $hook == 'admin_page_scroll-box-settings')) {
wp_enqueue_style('hatchbuck_style', plugins_url('/css/hatchbuck_styles.css', __FILE__),'',HATCHBUCK_VERSION);
wp_enqueue_script('ace-editor',plugins_url('js/ace/ace.js', __FILE__),'',HATCHBUCK_VERSION);
wp_enqueue_script('hatchbuckloc',plugins_url('js/hatchbuck.js', __FILE__),'',HATCHBUCK_VERSION, true);
}elseif(!is_admin()){
wp_register_script( 'hbjs', plugins_url( '/js/hbjs.min.js' , __FILE__ ), array('jquery'), HATCHBUCK_VERSION, true );
}
}
add_action( 'admin_enqueue_scripts', 'hatchbuck_scripts' );
add_action( 'wp_enqueue_scripts', 'hatchbuck_scripts' );
/**
* Fix for Hatchbuck icon in the menu
*/
function hatchbuck_icon(){
if(is_admin()){
echo "<style type=\"text/css\">
li#toplevel_page_hatchbuck-manage .wp-menu-image img{
height: auto;
width: 15px;
position: relative;
bottom: 4px;
}
i.hatchbuck-own-icon {
background-image: url('".plugins_url( 'images/logo.png', __FILE__ )."');
}
</style>\n";
};
};
add_action('admin_head', 'hatchbuck_icon');
/**
* Prints the box content.
*/
function hatchbuck_wpsites_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'wpsites_meta_box', 'wpsites_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, 'metabox', true );
?>
<p><label for="metabox" class="screen-reader-text"><b><?php _e( 'Hatchbuck Tracking Code', 'Hatchbuck' ); ?></b></label></p>
<p><textarea class="widefat" rows="4" cols="4" name="metabox" class="inside"><?php echo esc_textarea( get_post_meta($post->ID, 'metabox', true) ); ?></textarea></p>
<p>Copy and paste Hatchbuck's website tracking code for this page. It will be inserted before <code><\body></code> on this page only. This is legacy mode. Switch to new, site-wide mode in settings.</p>
<?php
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function hatchbuck_wpsites_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['wpsites_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['wpsites_meta_box_nonce'], 'wpsites_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, its safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['metabox'] ) ) {
return;
}
// Sanitize user input.
$my_data = $_POST['metabox'];
// Update the meta field in the database.
update_post_meta( $post_id, 'metabox', $my_data );
}
add_action( 'save_post', 'hatchbuck_wpsites_save_meta_box_data' );
// Hook in & Display The Value Conditionally
add_action( 'loop_start', 'hatchbuck_custom_field_before_content', 5 );
function hatchbuck_custom_field_before_content() {
if (get_option('hatchbuck_addons_metabox')) {
add_action('wp_footer','hatchbuckCode');
}
}
function hatchbuckCode() {
$postType = get_post_custom_values('metabox');
if (isset($postType)) {
if (is_array($postType) && isset($postType[0])) {
echo $postType[0];
}else {
echo $postType;
}
}
}
if (get_option('hatchbuck_addons_metabox')) {
add_action( 'add_meta_boxes', 'hatchbuck_wpsites_register_metabox' );
function hatchbuck_wpsites_register_metabox() {
$postTypeTcs = get_option('hatchbuck_postTypeTc');
if ($postTypeTcs) {
foreach($postTypeTcs as $key => $val){
if ($val) {
add_meta_box(
'wpsites_sectionid',
'Hatchbuck Website Tracking Code',
'hatchbuck_wpsites_meta_box_callback',
$key,
'normal',
'high'
);
}
}
}
}
}