1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Grab an option from our settings |
5
|
|
|
* |
6
|
|
|
* If we're on multsite we'll grab the site option which is stored in the main blogs site option tables, otherwise |
7
|
|
|
* we'll grab the option which is stored on the single blogs option tables |
8
|
|
|
* |
9
|
|
|
* @param unknown $option string name of the option |
10
|
|
|
* @param unknown $section string name of the section |
11
|
|
|
* @param unknown $default string/int default option value |
12
|
|
|
* @return the option value |
13
|
|
|
* @since 1.0 |
14
|
|
|
*/ |
15
|
|
|
if( !function_exists('lasso_editor_get_option')): |
16
|
|
View Code Duplication |
function lasso_editor_get_option( $option, $section, $default = '' ) { |
|
|
|
|
17
|
|
|
|
18
|
|
|
if ( empty( $option ) ) |
19
|
|
|
return; |
20
|
|
|
|
21
|
|
|
if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
22
|
|
|
|
23
|
|
|
$options = get_site_option( $section ); |
24
|
|
|
|
25
|
|
|
} else { |
26
|
|
|
|
27
|
|
|
$options = get_option( $section ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ( isset( $options[$option] ) ) { |
31
|
|
|
return $options[$option]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $default; |
35
|
|
|
} |
36
|
|
|
endif; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check to see if any Lasso galleries exist |
40
|
|
|
* |
41
|
|
|
* @since 1.0 |
42
|
|
|
*/ |
43
|
|
|
function lasso_editor_galleries_exist() { |
44
|
|
|
|
45
|
|
|
$q = new wp_query( array( 'post_type' => 'ai_galleries', 'post_status' => 'publish' ) ); |
46
|
|
|
|
47
|
|
|
if ( $q->have_posts() ) |
48
|
|
|
return true; |
49
|
|
|
else |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return a CSS class of an automatically supported theme |
55
|
|
|
* |
56
|
|
|
* @since 0.8.6 |
57
|
|
|
* @return a css class if the theme is supported, false if nothing |
58
|
|
|
*/ |
59
|
|
|
function lasso_get_supported_theme_class() { |
60
|
|
|
|
61
|
|
|
$name = wp_get_theme()->get('Name'); |
62
|
|
|
$slug = lasso_clean_string( $name ); |
63
|
|
|
|
64
|
|
|
switch ( $slug ) { |
65
|
|
|
case 'aesop-story-theme': // aesop |
66
|
|
|
$out = '.aesop-entry-content'; |
67
|
|
|
break; |
68
|
|
|
case 'jorgen': // aesop |
69
|
|
|
$out = '.jorgen-entry-content'; |
70
|
|
|
break; |
71
|
|
|
case 'novella': // aesop |
72
|
|
|
$out = '.novella-entry-content'; |
73
|
|
|
break; |
74
|
|
|
case 'genji': // aesop |
75
|
|
|
$out = '.genji-entry-content'; |
76
|
|
|
break; |
77
|
|
|
case 'kerouac': // aesop |
78
|
|
|
$out = '.kerouac-entry-content'; |
79
|
|
|
break; |
80
|
|
|
case 'zealot': // aesop |
81
|
|
|
$out = '.zealot-entry-content'; |
82
|
|
|
break; |
83
|
|
|
case 'fable': // aesop |
84
|
|
|
$out = '.fable--entry-content'; |
85
|
|
|
break; |
86
|
|
|
case 'canvas': // wootheme..err...Automattic |
87
|
|
|
$out = '.entry'; |
88
|
|
|
break; |
89
|
|
|
case 'kleo': // |
90
|
|
|
$out = '.article-content'; |
91
|
|
|
break; |
92
|
|
|
//case 'exposure': // |
93
|
|
|
// $out = '.entry-content'; |
|
|
|
|
94
|
|
|
// break; |
95
|
|
|
//case 'lore': // |
96
|
|
|
// $out = '.entry-content'; |
|
|
|
|
97
|
|
|
// break; |
98
|
|
|
//case 'worldview': // upthemes |
99
|
|
|
// $out = '.entry-content'; |
|
|
|
|
100
|
|
|
// break; |
101
|
|
|
//case 'genesis': // genesis |
102
|
|
|
// $out = '.entry-content'; |
|
|
|
|
103
|
|
|
// break; |
104
|
|
|
//case 'camera': // array.is |
105
|
|
|
// $out = '.entry-content'; |
|
|
|
|
106
|
|
|
// break; |
107
|
|
|
case 'longform': |
108
|
|
|
$out = '.entry-content-wrapper'; |
109
|
|
|
break; |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return apply_filters('lasso_content_class', !empty( $out ) ? $out : false); |
114
|
|
|
//return !empty( $out ) ? $out : false; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
function lasso_get_supported_theme_title_class() { |
118
|
|
|
|
119
|
|
|
$name = wp_get_theme()->get('Name'); |
120
|
|
|
$slug = lasso_clean_string( $name ); |
121
|
|
|
|
122
|
|
|
switch ( $slug ) { |
123
|
|
|
|
124
|
|
|
case 'aesop-story-theme': // aesop |
125
|
|
|
$out = '.aesop-entry-title'; |
126
|
|
|
break; |
127
|
|
|
case 'jorgen': // aesop |
128
|
|
|
$out = '.jorgen-entry-title'; |
129
|
|
|
break; |
130
|
|
|
case 'genji': // aesop |
131
|
|
|
$out = '.genji-entry-title'; |
132
|
|
|
break; |
133
|
|
|
case 'kerouac': // aesop |
134
|
|
|
$out = '.kerouac-entry-title'; |
135
|
|
|
break; |
136
|
|
|
case 'zealot': // aesop |
137
|
|
|
$out = '.zealot-entry-title'; |
138
|
|
|
break; |
139
|
|
|
case 'fable': // aesop |
140
|
|
|
$out = '.fable--entry-title'; |
141
|
|
|
break; |
142
|
|
|
case 'kleo': // |
143
|
|
|
$out = '.page-title'; |
144
|
|
|
break; |
145
|
|
|
case 'longform': // |
146
|
|
|
$out = '.entry-title'; |
147
|
|
|
break; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return apply_filters('lasso_title_class', !empty( $out ) ? $out : false); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
//since 0.9.9.6 |
154
|
|
|
function lasso_get_supported_theme_featured_image_class() { |
155
|
|
|
|
156
|
|
|
$name = wp_get_theme()->get('Name'); |
157
|
|
|
$slug = lasso_clean_string( $name ); |
|
|
|
|
158
|
|
|
|
159
|
|
|
return apply_filters('lasso_featured_image_class', !empty( $out ) ? $out : false); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Return a string of classes with items that Lasso will remove when entering the editor |
165
|
|
|
* so that we don't save them as HTML |
166
|
|
|
* |
167
|
|
|
* @since 0.8.7 |
168
|
|
|
* @return string of comma separated classes |
169
|
|
|
*/ |
170
|
|
|
function lasso_supported_no_save(){ |
171
|
|
|
|
172
|
|
|
return apply_filters('lasso_dont_save', '.lasso--ignore, .sharedaddy, .us_wrapper, .meta, .edit-link, .ssba, .jp-relatedposts, .fb-comments'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Generic sanitization, useful for sanitization of arrays. |
177
|
|
|
* |
178
|
|
|
* @since 0.9.2 |
179
|
|
|
* |
180
|
|
|
* @param array|object|string $data Data to sanatize. |
181
|
|
|
* |
182
|
|
|
* @return array|mixed|object|string|void |
183
|
|
|
*/ |
184
|
|
|
function lasso_sanitize_data( $data ) { |
185
|
|
|
return \lasso\sanatize::do_sanitize( $data ); |
186
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Return a comma delimited list of categories for a specific post object |
191
|
|
|
* |
192
|
|
|
* @since 0.9.3 |
193
|
|
|
* @return string of comma delimited category slugs |
194
|
|
|
*/ |
195
|
|
|
function lasso_get_post_objects( $postid = '', $taxonomy = 'category') { |
196
|
|
|
|
197
|
|
|
if ( empty( $postid ) ) |
198
|
|
|
$postid = get_the_ID(); |
199
|
|
|
|
200
|
|
|
$objects = 'category' == $taxonomy ? get_the_category( $postid ) : get_the_tags( $postid ); |
201
|
|
|
|
202
|
|
|
if ( empty( $objects) ) |
203
|
|
|
return; |
204
|
|
|
|
205
|
|
|
$out = ''; |
206
|
|
|
foreach( $objects as $object ) { |
207
|
|
|
//$out .= $object->slug.', '; |
|
|
|
|
208
|
|
|
$out .= $object->name.','; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return rtrim($out, ', '); |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Return an array of categories for autocomplete |
217
|
|
|
* |
218
|
|
|
* @since 0.9.3 |
219
|
|
|
* @return array all categoiries |
220
|
|
|
*/ |
221
|
|
|
function lasso_get_objects( $taxonomy = 'category' ) { |
222
|
|
|
|
223
|
|
|
$objects = 'category' == $taxonomy ? get_categories(array('hide_empty' => 0)) : get_tags(array('hide_empty' => 0)); |
224
|
|
|
|
225
|
|
|
if ( empty( $objects) ) |
226
|
|
|
return; |
227
|
|
|
|
228
|
|
|
$out = ""; |
229
|
|
|
foreach( $objects as $object ) { |
230
|
|
|
$out .= $object->name.','; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $out; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get allowed post types for the post chooser modal. |
239
|
|
|
* |
240
|
|
|
* |
241
|
|
|
* @since 0.9.4 |
242
|
|
|
*/ |
243
|
|
|
function lasso_post_types_names() { |
244
|
|
|
$post_types = get_post_types( array( |
245
|
|
|
'public' => true, |
246
|
|
|
), 'objects' ); |
247
|
|
|
$post_types = array_combine( array_keys( $post_types ), wp_list_pluck( $post_types, 'label' ) ); |
248
|
|
|
unset( $post_types[ 'attachment' ] ); |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Set which post types are allowed |
252
|
|
|
* |
253
|
|
|
* @since 0.9.4 |
254
|
|
|
* |
255
|
|
|
* @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered. |
256
|
|
|
*/ |
257
|
|
|
$allowed_post_types = lasso_editor_get_option( 'allowed_post_types', 'lasso_editor', array( 'post', 'page') ); |
|
|
|
|
258
|
|
|
$allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types ); |
259
|
|
|
if (!current_user_can('edit_pages')) { |
260
|
|
|
$allowed_post_types = array_diff($allowed_post_types,array('page')); |
261
|
|
|
} |
262
|
|
|
foreach( $post_types as $name => $label ) { |
263
|
|
|
if ( ! in_array( $name, $allowed_post_types ) ) { |
264
|
|
|
unset( $post_types[ $name ] ); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
return $post_types; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
|
271
|
|
|
function lasso_post_types() { |
272
|
|
|
$post_types = get_post_types( array( |
273
|
|
|
'public' => true, |
274
|
|
|
), 'names' ); |
275
|
|
|
unset( $post_types[ 'attachment' ] ); |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Set which post types are allowed |
279
|
|
|
* |
280
|
|
|
* @since 0.9.4 |
281
|
|
|
* |
282
|
|
|
* @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered. |
283
|
|
|
*/ |
284
|
|
|
$allowed_post_types = lasso_editor_get_option( 'allowed_post_types', 'lasso_editor', array( 'post') ); |
|
|
|
|
285
|
|
|
$allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types ); |
286
|
|
|
foreach( $post_types as $name => $label ) { |
287
|
|
|
if ( ! in_array( $name, $allowed_post_types ) ) { |
288
|
|
|
unset( $post_types[ $name ] ); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
return $post_types; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
function lasso_post_types_rest_base() { |
295
|
|
|
global $wp_post_types; |
|
|
|
|
296
|
|
|
$post_types = lasso_post_types(); |
297
|
|
|
$rest_base = array(); |
298
|
|
|
foreach ( $post_types as $post_type) { |
299
|
|
|
$rest_base[$post_type] = $wp_post_types[$post_type]->rest_base; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $rest_base; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
//////////////////// |
306
|
|
|
// INTERNAL |
307
|
|
|
//////////////////// |
308
|
|
|
/** |
309
|
|
|
* Used internally as a callback to build a tab or content area for modal addons |
310
|
|
|
* |
311
|
|
|
* @param $tab object |
312
|
|
|
* @param $type string tab or content |
313
|
|
|
* @uses lasso_modal_addons() |
314
|
|
|
* @since 0.9.4 |
315
|
|
|
*/ |
316
|
|
|
function lasso_modal_addons_content( $tab = '', $type ){ |
317
|
|
|
|
318
|
|
|
$name = lasso_clean_string( $tab['name'] ); |
319
|
|
|
|
320
|
|
|
if ( 'tab' == $type ) { |
321
|
|
|
|
322
|
|
|
$out = sprintf( '<li data-addon-name="%s">%s</li>', $name, $tab['name'] ); |
323
|
|
|
|
324
|
|
|
} else if ( 'content' == $type ){ |
325
|
|
|
|
326
|
|
|
$content = isset( $tab['content'] ) && is_callable( $tab['content'] ) ? call_user_func( $tab['content'] ) : false; |
327
|
|
|
$options = isset( $tab['options'] ) && is_callable( $tab['options'] ) ? call_user_func( $tab['options'] ) : false; |
328
|
|
|
|
329
|
|
|
$out = sprintf( '<div class="lasso--modal__content not-visible" data-addon-content="%s"> |
330
|
|
|
%s%s |
331
|
|
|
</div>', $name, $content, lasso_option_form( $name, $options ) ); |
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $out; |
|
|
|
|
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Helper function to clean a string and replace spaces with dash |
340
|
|
|
* |
341
|
|
|
* @param $string string content |
342
|
|
|
* @since 0.9.4 |
343
|
|
|
* |
344
|
|
|
* @return void|string |
345
|
|
|
*/ |
346
|
|
|
function lasso_clean_string( $string = '' ) { |
347
|
|
|
|
348
|
|
|
if ( empty( $string ) ) |
349
|
|
|
return; |
350
|
|
|
|
351
|
|
|
return sanitize_text_field( strtolower( preg_replace('/[\s_]/', '-', $string ) ) ); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Helper function to switch - to _ after having. |
356
|
|
|
* |
357
|
|
|
* This is the evil twin of lasso_clean_string() and may or may not make your data forever unclean. |
358
|
|
|
* |
359
|
|
|
* @param $string string content |
360
|
|
|
* @since 0.9.5 |
361
|
|
|
* |
362
|
|
|
* @return void|string |
363
|
|
|
*/ |
364
|
|
|
function lasso_unclean_string( $string = '' ) { |
365
|
|
|
|
366
|
|
|
if ( empty( $string ) ) { |
367
|
|
|
return; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return sanitize_text_field( strtolower( str_replace( '-', '_', $string ) ) ); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
|
374
|
|
|
//////////////////// |
375
|
|
|
// PLUGGABLE |
376
|
|
|
//////////////////// |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Check if the user is logged in and has the correctly passed capability |
380
|
|
|
* |
381
|
|
|
* @param unknown $action string a capability such as edit_posts or publish_posts |
382
|
|
|
* @param unknown $postid int the id of the post object to check against |
383
|
|
|
* @since 0.9.9.7 added filter 'lasso_user_can_filter' |
384
|
|
|
*/ |
385
|
|
|
if ( !function_exists( 'lasso_user_can' ) ): |
386
|
|
|
function lasso_user_can( $action = '', $postid = 0 ) { |
387
|
|
|
$result = false; |
388
|
|
|
if ( empty( $action ) ) |
389
|
|
|
$action = 'edit_posts'; |
390
|
|
|
|
391
|
|
|
if ( empty( $postid ) && $action != 'edit_posts' && $action != 'publish_posts' && $action != 'delete_posts') |
392
|
|
|
$postid = get_the_ID(); |
393
|
|
|
|
394
|
|
|
if ( is_user_logged_in() && current_user_can( $action, $postid ) ) { |
395
|
|
|
// check against post types: |
396
|
|
|
$allowed_post_types = lasso_editor_get_option( 'allowed_post_types', 'lasso_editor', array( 'post', 'page') ); |
|
|
|
|
397
|
|
|
|
398
|
|
|
if (!current_user_can('edit_pages')) { |
399
|
|
|
$allowed_post_types = array_diff($allowed_post_types,array('page')); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
if (!empty($allowed_post_types) && !empty($postid)) { |
403
|
|
|
$type = get_post_type( $postid ); |
404
|
|
|
$allowed_post_types = apply_filters( 'lasso_allowed_post_types', $allowed_post_types ); |
405
|
|
|
|
406
|
|
|
if ( in_array( $type, $allowed_post_types ) ) { |
407
|
|
|
$result = true; |
408
|
|
|
} |
409
|
|
|
} else { |
410
|
|
|
//we are not checking against a post, return true |
411
|
|
|
$result = true; |
412
|
|
|
} |
413
|
|
|
} else { |
414
|
|
|
$result = false; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return apply_filters( 'lasso_user_can_filter', $result, $action, $postid); |
418
|
|
|
} |
419
|
|
|
endif; |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Empty state message thats shown when no data available |
423
|
|
|
* |
424
|
|
|
* @since 0.9.5 |
425
|
|
|
*/ |
426
|
|
|
if ( !function_exists('lasso_editor_empty_results') ): |
427
|
|
|
|
428
|
|
|
function lasso_editor_empty_results( $type = 'posts' ){ |
429
|
|
|
|
430
|
|
|
if ( 'posts' == $type ) { |
431
|
|
|
|
432
|
|
|
$string = apply_filters('lasso_empty_state_message', __('No posts to show', 'lasso') ); |
433
|
|
|
$icon = 'lasso-icon-file-text2'; |
434
|
|
|
$button = false; |
435
|
|
|
|
436
|
|
|
} elseif ( 'revision' == $type ) { |
437
|
|
|
|
438
|
|
|
$string = apply_filters('lasso_empty_state_message', __('No revisions found', 'lasso') ); |
439
|
|
|
$icon = 'lasso-icon-history'; |
440
|
|
|
$button = sprintf('<a href="#" class="lasso--btn-secondary" id="lasso--close-modal">%s</a>', __('Close','lasso') ); |
441
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
return sprintf('<div id="lasso--empty-state" class="lasso--empty-state"><i class="lasso--empty-state-icon lasso-icon %s"></i><p>%s</p>%s</div>', $icon, $string, $button ); |
|
|
|
|
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
endif; |
448
|
|
|
|
449
|
|
|
|
450
|
|
|
|
451
|
|
|
|
452
|
|
|
|
453
|
|
|
|
454
|
|
|
|
455
|
|
|
|
456
|
|
|
|
457
|
|
|
|
458
|
|
|
|
459
|
|
|
|
460
|
|
|
|
461
|
|
|
|
462
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.