-
Notifications
You must be signed in to change notification settings - Fork 1
/
extra-functions.php~
344 lines (292 loc) · 11.1 KB
/
extra-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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* @package WordPress
* @subpackage WP Paintbrush
* @since WP Paintbrush 0.1
*
* Extra functions used in WP Paintbrush designer tool
*/
/**
* Do not continue processing since file was called directly
* @since 0.1
*/
if ( !defined( 'ABSPATH' ) )
die( 'Eh! What you doin in here?' );
/**
* Add link to admin bar
* @since 1.0
*/
function wppb_admin_bar_link() {
global $wp_admin_bar;
if ( !is_admin_bar_showing() || !current_user_can( 'manage_options' ) )
return;
if ( 'on' == get_option( 'wppb_designer_pane' ) )
$opt = 'off';
else
$opt = 'on';
// Creating link
$link = home_url() . '/?wppb_designer_pane=' . $opt;
$link = wp_nonce_url( $link, 'wppb_editor' );
// Adding menu item
$wp_admin_bar->add_menu(
array(
'parent' => 'appearance',
'id' => 'wppb_designer_pane',
'title' => __( 'Editor ' . $opt ),
'href' => $link
)
);
}
add_action( 'wp_before_admin_bar_render', 'wppb_admin_bar_link' );
/* Load content via AJAX
* @since 0.8
*/
function ptc_ajax_content() {
die( ptc_create_template() );
}
/* Create template
* @since 0.1
*/
function ptc_create_template() {
// Grab and sanitize inputs
if ( isset( $_POST['positions'] ) )
$content_layout = ptc_sanitize_inputs( $_POST ); // Grab submitted data
else
$content_layout = ptc_sanitize_inputs( get_option( WPPB_DESIGNER_SETTINGS ) ); // Grab from database
// Setting processed template variable
$processed_template = '';
// Work out block positions
$positions = explode( ',', $content_layout['positions'] ); // Splitting positions into an array
$sidebar_positions = explode( ',', $content_layout['sidebar_positions'] ); // Splitting positions into an array
// Pass through each block position and add appropriate block to template string
foreach( $positions as $bit ) {
// Process template chunks
foreach( ptc_page_chunks() as $chunk=>$shortcode ) {
$id = strtolower( $chunk ); // Convert to lowercase
$id = str_replace( ' ', '', $id ); // Strip spaces
$id = 'layout-' . $id; // Prepend ID prefix
if ( !isset( $content_layout['changehome_homelayout_display'] ) )
$content_layout['changehome_homelayout_display'] = '';
// Replacing [ptc_content] with appropriate page specific template
if ( is_front_page() && 'Magazine' == $content_layout['changehome_homelayout_display'] )
$shortcode = str_replace( 'ptc_content', 'ptc_content_front', $shortcode );
elseif ( is_home() || is_archive() || is_search() )
$shortcode = str_replace( 'ptc_content', 'ptc_content_home', $shortcode );
elseif ( is_single() )
$shortcode = str_replace( 'ptc_content', 'ptc_content_single', $shortcode );
elseif ( is_page() )
$shortcode = str_replace( 'ptc_content', 'ptc_content_page', $shortcode );
// Shortcodifying template
if ( $bit == $id )
$template = do_shortcode( $shortcode );
}
// Execute template
$processed_template .= do_shortcode( $template );
}
return $processed_template;
}
/* Process editor submits
* Handles saves, publishes, exports etc.
* @since 0.1
*/
function ptc_load_stuff() {
global $options, $css;
// Grab and sanitize data
$options = ptc_sanitize_inputs();
// Processing CSS on an external server
$css = ptc_load_css();
// Grabbing raw CSS before addition of URLs (urls are needed for internal CSS file, but cause problems when exporting themes etc. as they need to be removed)
$options['css'] = $css;
// Add URLs (needed since CSS won't reference images correctly otherwise - not needed for storage though as can dynamically add it later)
$css = wppb_convert_urls_in_css( $css );
// Serve CSS
echo $css;
// Hook for extra functionality at this point
do_action( 'ptc_load_stuff_hook' );
exit;
}
/* Publish, save or export
* Used in ptc_load_stuff()
* @since 1.0
*/
function ptc_publish_save_export() {
global $options, $css;
// Save options
if ( 'save' == $_GET['generator-css'] || 'export' == $_GET['generator-css'] || 'publish' == $_GET['generator-css'] )
update_option( WPPB_DESIGNER_SETTINGS, $options );
// Publishing and Export options
if ( 'publish' == $_GET['generator-css'] || 'export' == $_GET['generator-css'] )
ptc_publish_options( $options, $css );
// Export zip file (creates redirect to serve zip file - required for loading zip via AJAX)
if ( 'export' == $_GET['generator-css'] ) {
echo '</style><meta http-equiv="refresh" content="0;url=' . home_url() . '/?generator-export=' . esc_attr( $_POST['ptc_nonce'] ) . '"><style type="text/css">';
die;
}
}
add_action( 'ptc_load_stuff_hook', 'ptc_publish_save_export' );
/* Get options ready for storing
* This processes the designer settings into a format suitable for publishing
* Returns unfiltered/sanitized options (no need to sanitize since done during publishing process)
* @since 0.1
*/
function ptc_get_options_for_storing( $content_layout, $css='' ) {
// Setting CSS
$options['css'] = $css;
// Add support for sidebars
$sidebar_positions = str_replace( 'layout-', '', $content_layout['sidebar_positions'] ) ;
$sidebar_positions = explode( ',', $sidebar_positions );
foreach( $sidebar_positions as $block ) {
$count = 0;
while( $count < 2 ) {
$count++;
$options['show_widget' . $count] = '';
$options['name_widget' . $count] = '';
$options['before_widget' . $count] = '';
$options['after_widget' . $count] = '';
$options['before_title' . $count] = '';
$options['after_title' . $count] = '';
if ( 'sidebar' . $count == $block ) {
$options['show_widget' . $count] = 'on';
$options['name_widget' . $count] = 'Sidebar ' . $count;
$options['before_widget' . $count] = '<div class="widget">';
$options['after_widget' . $count] = '</div>';
$options['before_title' . $count] = '<h3>';
$options['after_title' . $count] = '</h3>';
}
}
}
// Set header and footer templates
$positions = explode( ',', $content_layout['positions'] );
$section = 'header'; // Load header first
$options['header'] = ''; // Resetting header
$options['footer'] = ''; // Resetting footer
foreach( $positions as $pos ) {
foreach( ptc_page_chunks() as $chunk=>$test ) {
$chunk = strtolower( $chunk ); // Convert to lowercase
$chunk = str_replace( ' ', '', $chunk ); // Strip spaces
if ( ( 'layout-' . $chunk ) == $pos ) {
$chunk = '[ptc_' . strtolower( $chunk ) . ']'; // Convert to lowercase
if ( 'footer' == $section )
$options['footer'] .= do_shortcode( $chunk );
if ( '[ptc_content]' == $chunk )
$section = 'footer';
if ( 'header' == $section )
$options['header'] .= do_shortcode( $chunk );
}
}
}
// Add support for menus
$positions = explode( ',', $content_layout['positions'] );
foreach( $positions as $pos ) {
foreach( ptc_page_chunks() as $chunk=>$test ) {
$chunk = strtolower( $chunk ); // Convert to lowercase
$chunk = str_replace( ' ', '', $chunk ); // Strip spaces
if ( ( 'layout-' . $chunk ) == $pos ) {
if ( 'menu' == $chunk )
$options['support_primarymenu'] = 'on';
}
}
}
// Set main templates
$options['front_page'] = '';
$options['home'] = "[get_header]\n\n" . do_shortcode( '[ptc_content_home]' ) . "\n\n[get_footer]";
$options['archive'] = "[get_header]\n\n" . do_shortcode( '[ptc_content_home]' ) . "\n\n[get_footer]";
$options['index'] = "[get_header]\n\n" . do_shortcode( '[ptc_content_home]' ) . "\n\n[get_footer]";
$options['page'] = "[get_header]\n\n" . do_shortcode( '[ptc_content_page]' ) . "\n\n[get_footer]";
$options['single'] = "[get_header]\n\n" . do_shortcode( '[ptc_content_single]' ) . "\n\n[get_footer]";
// Correct URLs
$options['css'] = str_replace( 'http: //', 'http://', $options['css'] );
return $options;
}
/* Array of page chunks
* @since 0.1
*/
function ptc_page_chunks() {
global $chunks; // Need global for passing data via action hook
do_action( 'wppb_add_chunk' ); // Creating action hook for additional chunks to be added via plugins
return $chunks;
}
/* Array of sidebar chunks
* @since 0.1
*/
function ptc_sidebar_chunks() {
$chunks = array(
'S1' => 'layout-sidebar1',
'S2' => 'layout-sidebar2',
'Content' => 'layout-maincontent',
);
return $chunks;
}
/* On button for editor
* @since 1.0
*/
function ptc_open_editor() {
?>
<a id="ptc-openme" <?php
if ( 'on' == get_option( 'wppb_designer_pane' ) )
echo 'style="display:none;"';
else {
// Creating link
$link = home_url() . '/?wppb_designer_pane=on';
$link = wp_nonce_url( $link, 'wppb_editor' );
echo 'href="' . $link . '"';
}
?>>Open Editor</a><?php
}
add_action( 'wp_footer', 'ptc_open_editor' );
/* Init
*
* Handling image uploading
* Setting options to ensure that widgets are shown
*
* @since 0.1
*/
function ptc_load_files() {
// Bail out if in admin panel
if ( is_admin() )
return;
// Make sure both sidebars are set - UGLY IF STATEMENT IS A HACK TO PREVENT MUCK UPS WHEN NO SETTINGS ARE PRESENT YET - IE: WHEN INSTALLING THE THEME
if ( get_option( WPPB_SETTINGS ) ) {
$wppb_options = get_option( WPPB_SETTINGS );
$wppb_options['show_widget1'] = 'on';
$wppb_options['show_widget2'] = 'on';
$wppb_options['name_widget1'] = 'Sidebar 1';
$wppb_options['before_widget1'] = '<div class="widget">';
$wppb_options['after_widget1'] = '</div>';
$wppb_options['before_title1'] = '<h3>';
$wppb_options['after_title1'] = '</h3>';
$wppb_options['name_widget2'] = 'Sidebar 2';
$wppb_options['before_widget2'] = '<div class="widget">';
$wppb_options['after_widget2'] = '</div>';
$wppb_options['before_title2'] = '<h3>';
$wppb_options['after_title2'] = '</h3>';
update_option( WPPB_SETTINGS, $wppb_options );
}
// Security check for image uploads
if ( $_FILES ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' ); // Load file necessary for processing image file on front-end
require_once( ABSPATH . 'wp-includes/pluggable.php' ); // Load file necessary for processing image file on front-end
wppb_image_upload_form_check();
}
}
add_action( 'init', 'ptc_load_files', 9 );
/* Initialize theme
* Preferentially loads either designer template, stored template, or AJAX request for content
* @since 0.9
*/
function wppb_designer_init() {
if ( !isset( $_GET['generator-css'] ) )
$_GET['generator-css'] = '';
// Load editor, if set to "on" and user is logged in and not attempting to load AJAX content
if ( !isset( $_GET['generator-content'] ) )
$_GET['generator-content'] = '';
if ( 'on' == get_option( 'wppb_designer_pane' ) && current_user_can( 'manage_options' ) && 'load' != $_GET['generator-content'] && '' == $_GET['generator-css'] ) {
remove_action( 'wppb_pre_theme', 'wppb_template_load' ); // Unhooks existing template editor template
remove_action( 'wp_print_styles', 'wppb_settings_css' ); // Disabling current themes stylesheet
add_action( 'wp_footer', 'ptc_load_template', 9 );
}
// Else load AJAX content
else
add_action( 'wppb_pre_header', 'ptc_ajax_content', 1 ); // Need at high priority to make sure theme is loaded before random junk is loaded
}
add_action( 'init', 'wppb_designer_init' );