Completed
Push — master ( ea40a4...a4569a )
by
unknown
02:51
created

public/includes/helpers.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 356.

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.

Loading history...
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
function lasso_editor_get_option( $option, $section, $default = '' ) {
16
17
	if ( empty( $option ) )
18
		return;
19
20
	if ( function_exists( 'is_multisite' ) && is_multisite() ) {
21
22
		$options = get_site_option( $section );
23
24
	} else {
25
26
		$options = get_option( $section );
27
	}
28
29
	if ( isset( $options[$option] ) ) {
30
		return $options[$option];
31
	}
32
33
	return $default;
34
}
35
36
/**
37
 * Check to see if any Lasso galleries exist
38
 *
39
 * @since 1.0
40
 */
41
function lasso_editor_galleries_exist() {
42
43
	$q = new wp_query( array( 'post_type' => 'ai_galleries', 'post_status' => 'publish' ) );
44
45
	if ( $q->have_posts() )
46
		return true;
47
	else
48
		return false;
49
}
50
51
/**
52
 * Return a CSS class of an automatically supported theme
53
 *
54
 * @since 0.8.6
55
 * @return a css class if the theme is supported, false if nothing
56
 */
57 View Code Duplication
function lasso_get_supported_theme_class() {
58
59
	$name  	= wp_get_theme()->get('Name');
60
	$slug  	= lasso_clean_string( $name );
61
62
	switch ( $slug ) {
63
		case 'aesop-story-theme': // aesop
64
			$out = '.aesop-entry-content';
65
			break;
66
		case 'jorgen': // aesop
67
			$out = '.jorgen-entry-content';
68
			break;
69
		case 'novella': // aesop
70
			$out = '.novella-entry-content';
71
			break;
72
		case 'genji': // aesop
73
			$out = '.genji-entry-content';
74
			break;
75
		case 'kerouac': // aesop
76
			$out = '.kerouac-entry-content';
77
			break;
78
		case 'zealot': // aesop
79
			$out = '.zealot-entry-content';
80
			break;
81
		case 'fable': // aesop
82
			$out = '.fable--entry-content';
83
			break;
84
		case 'canvas': // wootheme..err...Automattic
85
			$out = '.entry';
86
			break;
87
		case 'kleo': // 
88
			$out = '.article-content';
89
			break;
90
		//case 'exposure': // 
91
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
92
		//	break;
93
		//case 'lore': // 
94
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
		//	break;
96
		//case 'worldview': // upthemes
97
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
		//	break;
99
		//case 'genesis': // genesis
100
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
		//	break;
102
		//case 'camera': // array.is
103
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
		//	break;
105
		//case 'longform': // 
106
		//	$out = '.entry-content';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
		//	break;
108
109
	}
110
111
	return !empty( $out ) ? $out : false;
112
}
113
114 View Code Duplication
function lasso_get_supported_theme_title_class() {
115
116
	$name  	= wp_get_theme()->get('Name');
117
	$slug  	= lasso_clean_string( $name );
118
119
	switch ( $slug ) {
120
121
		case 'aesop-story-theme': // aesop
122
			$out = '.aesop-entry-title';
123
			break;
124
		case 'jorgen': // aesop
125
			$out = '.jorgen-entry-title';
126
			break;
127
		case 'novella': // aesop
128
			$out = '.novella-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
	}
146
147
	return !empty( $out ) ? $out : false;
148
}
149
150
/**
151
*	Return a string of classes with items that Lasso will remove when entering the editor
152
*	so that we don't save them as HTML
153
*
154
*	@since 0.8.7
155
*	@return string of comma separated classes
156
*/
157
function lasso_supported_no_save(){
158
159
	return apply_filters('lasso_dont_save', '.lasso--ignore, .sharedaddy, .us_wrapper, .twitter-tweet, .meta');
160
}
161
162
/**
163
 * Generic sanitization, useful for sanitization of arrays.
164
 *
165
 * @since 0.9.2
166
 *
167
 * @param array|object|string $data Data to sanatize.
168
 *
169
 * @return array|mixed|object|string|void
170
 */
171
function lasso_sanitize_data( $data ) {
172
	return \lasso\sanatize::do_sanitize( $data );
173
174
}
175
176
/**
177
 *	Return a comma delimited list of categories for a specific post object
178
 *
179
 *	@since 0.9.3
180
 *	@return string of comma delimited category slugs
181
*/
182
function lasso_get_post_objects( $postid = '', $taxonomy = 'category') {
183
184
	if ( empty( $postid ) )
185
		$postid = get_the_ID();
186
187
	$objects = 'category' == $taxonomy ? get_the_category( $postid ) : get_the_tags( $postid );
188
189
	if ( empty( $objects) )
190
		return;
191
192
	$out = '';
193
	foreach( $objects as $object ) {
194
		$out .= $object->slug.', ';
195
	}
196
197
	return rtrim($out, ', ');
198
199
}
200
201
/**
202
 *	Return an array of categories for autocomplete
203
 *
204
 *	@since 0.9.3
205
 *	@return array all categoiries
206
*/
207
function lasso_get_objects( $taxonomy = 'category' ) {
208
209
	$objects = 'category' == $taxonomy ? get_categories(array('hide_empty' => 0)) : get_tags(array('hide_empty' => 0));
210
211
	if ( empty( $objects) )
212
		return;
213
214
	$out = array();
215
	foreach( $objects as $object ) {
216
		$out[] = $object->slug;
217
	}
218
219
	return $out;
220
}
221
222
223
/**
224
 * Get allowed post types for the post chooser modal.
225
 *
226
 *
227
 * @since 0.9.4
228
 */
229
function lasso_post_types() {
230
231
	$post_types = get_post_types( array(
232
		'public' => true,
233
	), 'objects' );
234
	$post_types = array_combine( array_keys( $post_types ), wp_list_pluck( $post_types, 'label' ) );
235
    unset( $post_types[ 'attachment' ] );
236
237
	/**
238
	 * Set which post types are allowed
239
	 *
240
	 * @since 0.9.4
241
	 *
242
	 * @param array $allowed_post_types Array of names (not labels) of allowed post types. Must be registered.
243
	 */
244
	$allowed_post_types = apply_filters( 'lasso_allowed_post_types', array( 'post', 'page' ) );
245
	foreach( $post_types as $name => $label ) {
246
		if ( ! in_array( $name, $allowed_post_types ) ) {
247
			unset( $post_types[ $name ] );
248
		}
249
250
	}
251
252
	return $post_types;
253
254
}
255
256
////////////////////
257
// INTERNAL
258
////////////////////
259
/**
260
*	Used internally as a callback to build a tab or content area for modal addons
261
*
262
*	@param $tab object
263
*	@param $type string tab or content
264
*	@uses lasso_modal_addons()
265
*	@since 0.9.4
266
*/
267
function lasso_modal_addons_content( $tab = '', $type ){
268
269
	$name = lasso_clean_string( $tab['name'] );
270
271
	if ( 'tab' == $type ) {
272
273
		$out = sprintf( '<li data-addon-name="%s">%s</li>', $name, $tab['name'] );
274
275
	} else if ( 'content' == $type ){
276
277
		$content = isset( $tab['content'] ) && is_callable( $tab['content'] ) ? call_user_func( $tab['content'] ) : false;
278
		$options = isset( $tab['options'] ) && is_callable( $tab['options'] ) ? call_user_func( $tab['options'] ) : false;
279
280
		$out = sprintf( '<div class="lasso--modal__content not-visible" data-addon-content="%s">
281
			%s%s
282
			</div>', $name, $content, lasso_option_form( $name, $options ) );
283
284
	}
285
286
	return $out;
287
}
288
289
/**
290
*	Helper function to clean a string and replace spaces with dash
291
*
292
*	@param $string string content
293
*	@since 0.9.4
294
*
295
* @return void|string
296
*/
297
function lasso_clean_string( $string = '' ) {
298
299
	if ( empty( $string ) )
300
		return;
301
302
	return sanitize_text_field( strtolower( preg_replace('/[\s_]/', '-', $string ) ) );
303
}
304
305
/**
306
 *	Helper function to switch - to _ after having.
307
 *
308
 * This is the evil twin of lasso_clean_string() and may or may not make your data forever unclean.
309
 *
310
 *	@param $string string content
311
 *	@since 0.9.5
312
 *
313
 * @return void|string
314
 */
315
function lasso_unclean_string( $string = '' ) {
316
317
	if ( empty( $string ) ) {
318
		return;
319
	}
320
321
	return sanitize_text_field( strtolower( str_replace( '-', '_', $string ) ) );
322
}
323
324
325
////////////////////
326
// PLUGGABLE
327
////////////////////
328
329
/**
330
 * Check if the user is logged in and has the correctly passed capability
331
 *
332
 * @param unknown $action string a capability such as edit_posts or publish_posts
333
 * @param unknown $postid int the id of the post object to check against
334
 * @since 1.0
335
 */
336
if ( !function_exists( 'lasso_user_can' ) ):
337
	function lasso_user_can( $action = '', $postid = 0 ) {
338
339
		if ( empty( $action ) )
340
			$action = 'edit_posts';
341
342
		if ( empty( $postid ) )
343
			$postid = get_the_ID();
344
345
		if ( is_user_logged_in() && current_user_can( $action, $postid ) ) {
346
347
			return true;
348
349
		} else {
350
351
			return false;
352
353
		}
354
355
	}
356
endif;
357
358
/**
359
*	Empty state message thats shown when no data available
360
*
361
*	@since 0.9.5
362
*/
363
if ( !function_exists('lasso_editor_empty_results') ):
364
365
	function lasso_editor_empty_results( $type = 'posts' ){
366
367
		if ( 'posts' == $type ) {
368
369
			$string = apply_filters('lasso_empty_state_message', __('No posts to show', 'lasso') );
370
			$icon = 'lasso-icon-file-text2';
371
			$button = false;
372
373
		} elseif ( 'revision' == $type ) {
374
375
			$string = apply_filters('lasso_empty_state_message', __('No revisions found', 'lasso') );
376
			$icon = 'lasso-icon-history';
377
			$button = sprintf('<a href="#" class="lasso--btn-secondary" id="lasso--close-modal">%s</a>', __('Close','lasso') );
378
379
		}
380
381
		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 );
382
	}
383
384
endif;
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399