Completed
Push — master ( a8ef06...de0a9e )
by
unknown
02:00 queued 10s
created

assets::gutenberg_active()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
/**
3
*	Load the assets used for Editus
4
*
5
*	@since 1.0
6
*/
7
namespace lasso_public_facing;
8
9
use lasso\process\gallery;
10
11
class assets {
12
13
	public function __construct(){
14
15
		add_action('wp_enqueue_scripts', array($this,'scripts'));
16
	}
17
    
18
    function is_multipage()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20
        global $post;
21
        $pos = strpos($post->post_content, "<!--nextpage-->");
22
        if (!$pos) return -1;
23
        
24
        global $wp;
25
        $url =  home_url( $wp->request );
26
        $index = intval(basename($url)) == 0 ? 0 : intval(basename($url))-1;
27
        return $index;        
28
    }
29
30
	public function scripts(){
31
32
	
33
		global $post;
34
		if ( lasso_user_can('edit_posts') 
35
		     /* uncomment this line to disable Editus on Gutenberg posts*/
36
             /* && !( function_exists( 'has_blocks' ) && has_blocks( $post->post_content)  && !is_home()) */              
37
             ) {
38
			
39
			/**    Returns the time offset from UTC
40
			*/
41
			function get_UTC_offset() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
				$timezone_string = get_option( 'timezone_string' );
43
				if (empty( $timezone_string ) ) {
44
					return get_option('gmt_offset')*3600;
45
				}
46
				
47
				$origin_dtz = new \DateTimeZone($timezone_string);				
48
				$origin_dt = new \DateTime("now", $origin_dtz);
49
				$offset = $origin_dtz->getOffset($origin_dt);
50
				return $offset;
51
			}
52
53
			wp_enqueue_style('lasso-style', LASSO_URL.'/public/assets/css/lasso.css', LASSO_VERSION, true);
54
55
            //don't load autocomplete if it's a stockholm theme
56
			$themename  	= wp_get_theme()->get('Name');
57
			if ($themename !='Stockholm' ) {
58
				wp_enqueue_script('jquery-ui-autocomplete');
59
			}
60
			wp_enqueue_script('jquery-ui-draggable');
61
			wp_enqueue_script('jquery-ui-sortable');
62
			wp_enqueue_script('jquery-ui-slider');
63
			
64
			// media uploader
65
			wp_enqueue_media();
66
67
			// url for json api
68
			$home_url = function_exists('json_get_url_prefix') ? json_get_url_prefix() : false;
69
70
			$article_object 	= lasso_editor_get_option('article_class','lasso_editor');
71
72
			$article_object 	= empty( $article_object ) && lasso_get_supported_theme_class() ? lasso_get_supported_theme_class() : $article_object;
73
74
			$featImgClass 		= lasso_editor_get_option('featimg_class','lasso_editor');
75
			if (empty( $featImgClass )) {
76
				$featImgClass = lasso_get_supported_theme_featured_image_class();
77
			}
78
			$titleClass 		= lasso_editor_get_option('title_class','lasso_editor');
79
			if (empty( $titleClass )) {
80
				$titleClass = lasso_get_supported_theme_title_class();
81
			}
82
			$toolbar_headings  	= lasso_editor_get_option('toolbar_headings', 'lasso_editor');
83
			$toolbar_headings_h4  	= lasso_editor_get_option('toolbar_headings_h4', 'lasso_editor');
84
			$objectsNoSave  	= lasso_editor_get_option('dont_save', 'lasso_editor');
85
			$objectsNonEditable  	= lasso_editor_get_option('non_editable', 'lasso_editor');
86
			$disableRESTSave = lasso_editor_get_option('save_using_rest_disabled', 'lasso_editor');
87
			$save_to_post_disabled  = lasso_editor_get_option( 'post_save_disabled', 'lasso_editor' );
88
			$edit_post_disabled  = lasso_editor_get_option( 'post_edit_disabled', 'lasso_editor' );
89
			
90
			$bold_tag = lasso_editor_get_option('bold_tag', 'lasso_editor','b');
91
			$i_tag = lasso_editor_get_option('i_tag', 'lasso_editor','i');
92
93
			
94
			//text alignement
95
			$show_align = lasso_editor_get_option('toolbar_show_alignment', 'lasso_editor');
96
			
97
			//make links editable under the editing mode
98
			$links_editable = lasso_editor_get_option('links_editable', 'lasso_editor');
99
			
100
			//color 
101
			$show_color = lasso_editor_get_option('toolbar_show_color', 'lasso_editor');
102
			
103
			// allow change date for post
104
			$allow_change_date = lasso_editor_get_option('allow_change_date', 'lasso_editor');
105
			
106
			if ($show_color) {
107
				//color picker
108
				wp_enqueue_style( 'wp-color-picker' );
109
				wp_enqueue_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), false, 1 );
110
			}
111
			
112
			// click to insert components, not drag and drop
113
			$insert_comp_ui = lasso_editor_get_option('insert_comp_ui', 'lasso_editor');
114
			
115
			// do we support pending status
116
			$no_pending_status = lasso_editor_get_option('no_pending_status', 'lasso_editor');
117
			
118
			
119
			// custom fields
120
			
121
			$custom_fields = apply_filters( 'editus_custom_fields', null ); 
122
123
124
			// post id reference
125
			$postid 			= get_the_ID();
126
			$tz_offset = get_UTC_offset();
127
			$post_date = get_the_time('U', $postid);
128
			$time = (time()+$tz_offset);
129
            $delta = $time - $post_date;
130
            
131
			$strings = array(
132
				'save' 				=> __('Save','lasso'),
133
				'selectText'	  	=> __('Please Select Text First.','lasso'),
134
				'cancel' 			=> __('Cancel','lasso'),
135
				'exiteditor' 		=> __('Exit Editor','lasso'),
136
				'saving' 			=> __('Saving...','lasso'),
137
				'saved'				=> __('Saved!','lasso'),
138
				'adding' 			=> __('Adding...','lasso'),
139
				'added'				=> __('Added!','lasso'),
140
				'loading' 			=> __('Loading...','lasso'),
141
				'loadMore'			=> __('Load More','lasso'),
142
				'close'			=> __('Close','lasso'),
143
				'noPostsFound'		=> __('No more posts found','lasso'),
144
				'fetchFail'	    	=> __('Fetching failed.','lasso'),
145
				'galleryCreated' 	=> __('Gallery Created!','lasso'),
146
				'galleryUpdated' 	=> __('Gallery Updated!','lasso'),
147
				'justWrite'			=> __('Just write...','lasso'),
148
				'chooseImage'		=> __('Choose an image','lasso'),
149
				'updateImage'		=> __('Update Image','lasso'),
150
				'insertImage'		=> __('Insert Image','lasso'),
151
				'selectImage'		=> __('Select Image','lasso'),
152
				'removeFeatImg'     => __('Remove featured image?','lasso'),
153
				'updateSelectedImg' => __('Update Selected Image','lasso'),
154
				'chooseImages'		=> __('Choose images','lasso'),
155
				'editImage'			=> __('Edit Image','lasso'),
156
				'addImages'			=> __('Add Images','lasso'),
157
				'addNewGallery'		=> __('Add New Gallery','lasso'),
158
				'selectGallery'		=> __('Select Editus Gallery Image','lasso'),
159
				'useSelectedImages' => __('Use Selected Images','lasso'),
160
				'publishPost'		=> __('Publish Post?','lasso'),
161
				'publishYes'		=> __('Yes, publish it!','lasso'),
162
				'deletePost'		=> __('Trash Post?','lasso'),
163
				'deleteYes'			=> __('Yes, trash it!','lasso'),
164
				'warning'			=> __('Oh snap!','laso'),
165
				'cancelText'		=> __('O.K. got it!','lasso'),
166
				'missingClass'		=> __('It looks like we are either missing the Article CSS class, or it is configured incorrectly. Editus will not function correctly without this CSS class.','lasso'),
167
				'missingConfirm'	=> __('Update Settings', 'lasso'),
168
				'helperText'		=> __('one more letter','lasso'),
169
				'editingBackup'  	=> __('You are currently editing a backup copy of this post.'),
170
				
171
				'catsPlaceholder'     => __('add categories...'),
172
				'tagsPlaceholder'     => __('add tags...'),
173
				
174
				
175
			);
176
177
			$api_url = trailingslashit( home_url() ) . 'lasso-internal-api';
178
179
			$gallery_class = new gallery();
180
			$gallery_nonce_action = $gallery_class->nonce_action;
181
			$gallery_nonce = wp_create_nonce( $gallery_nonce_action );
182
			
183
			
184
            if ($allow_change_date) {
185
			    $permalink = get_site_url().'/?p='.$postid;
186
            } else {
187
                $permalink = get_permalink($postid);
188
            }
189
			
190
			// rest api
191
			$rest_nonce = '';
192
			$rest_root = site_url().'/?rest_route=/'; 
193
			  
194
			if (function_exists('rest_url')) {
195
				//$rest_root = esc_url_raw( rest_url());
196
				$rest_nonce = wp_create_nonce( 'wp_rest' );
197
				$settings = array( 'root' => $rest_root, 'nonce' => $rest_nonce );
198
				wp_enqueue_script( 'wp-api', '', array( 'jquery', 'underscore', 'backbone' ), LASSO_VERSION, true );
199
				wp_localize_script( 'wp-api', 'wpApiSettings', $settings );
200
				wp_localize_script( 'wp-api', 'WP_API_Settings', $settings );
201
				
202
				if ( class_exists( 'WP_REST_Controller' )) {
203
					// we are using REST API V2
204
					$using_restapiv2 = true;
205
				}
206
			}
207
            
208
            //find if this is multi page. -1 if not
209
            $multipage = self::is_multipage();
210
            $post_content = "";
211
            //pass post_content if we need to process multipage. In future we may need to pass this for other purposes
212
            if ($multipage != -1) {
213
               $post_content = $post->post_content;
214
            }
215
216
			// localized objects
217
			$objects = array(
218
				'ajaxurl' 			=> esc_url( $api_url ),
219
				'ajaxurl2' 			=> esc_url( admin_url( 'admin-ajax.php' )),
220
				'siteUrl'           => site_url(),
221
				'rest_root'         => $rest_root,
222
				'rest_nonce'        => $rest_nonce,
223
				'editor' 			=> 'lasso--content', // ID of editable content (without #) DONT CHANGE
224
				'article_object'	=> $article_object,
225
				'featImgClass'		=> $featImgClass,
226
				'titleClass'		=> $titleClass,
227
				'strings'			=> $strings,
228
				'settingsLink'		=> function_exists('is_multisite') && is_multisite() ? network_admin_url( 'settings.php?page=lasso-editor' ) : admin_url( 'admin.php?page=lasso-editor-settings' ),
229
				'post_status'		=> get_post_status( $postid ),
230
				'postid'			=> $postid,
231
				'permalink'			=> $permalink,
232
				'edit_others_pages'	=> current_user_can('edit_others_pages') ? true : false,
233
				'edit_others_posts'	=> current_user_can('edit_others_posts') ? true : false,
234
				'userCanEdit'		=> current_user_can('edit_post', $postid ),
235
				'can_publish'		=> is_page() ? current_user_can('publish_pages') : current_user_can('publish_posts'),
236
				//'can_publish_posts'	=> current_user_can('publish_posts'),
237
				//'can_publish_pages'	=> current_user_can('publish_pages'),
238
				'author'			=> is_user_logged_in() ? get_current_user_ID() : false,
239
				'nonce'				=> wp_create_nonce('lasso_editor'),
240
				'handle'			=> lasso_editor_settings_toolbar(),
241
				'toolbar'			=> lasso_editor_text_toolbar(),
242
				'toolbarHeadings'   => $toolbar_headings,
243
				'toolbarHeadingsH4'   => $toolbar_headings_h4,
244
				'component_modal'	=> lasso_editor_component_modal(),
245
				'component_sidebar'	=> lasso_editor_component_sidebar(),
246
				'components'		=> lasso_editor_components(),
247
				'wpImgEdit'			=> lasso_editor_wpimg_edit(),
248
				'wpVideoEdit'		=> lasso_editor_wpvideo_edit(),
249
				'featImgControls'   => lasso_editor_image_controls(),
250
				'featImgNonce'		=> $gallery_nonce,
251
				'getGallImgNonce'	=> $gallery_nonce,
252
				'createGallNonce'	=> $gallery_nonce,
253
				'swapGallNonce'		=> $gallery_nonce,
254
				'titleNonce'		=> wp_create_nonce('lasso_update_title'),
255
				'wpImgNonce'		=> wp_create_nonce('lasso_update_wpimg'),
256
				'deletePost'		=> wp_create_nonce('lasso_delete_post'),
257
				'searchPosts'		=> wp_create_nonce('lasso_search_posts'),
258
				'component_options' => lasso_editor_options_blob(),
259
				'newPostModal'		=> lasso_editor_newpost_modal(),
260
				'allPostModal'		=> lasso_editor_allpost_modal(),
261
				'mapFormFooter'		=> lasso_map_form_footer(),
262
				'refreshRequired'	=> lasso_editor_refresh_message(),
263
				'objectsNoSave'		=> $objectsNoSave,
264
				'objectsNonEditable' => $objectsNonEditable,
265
				'supportedNoSave'	=> lasso_supported_no_save(),
266
				'postCategories'    => lasso_get_objects('category'),
267
				'postTags'    		=> lasso_get_objects('tag'),
268
				'noResultsDiv'		=> lasso_editor_empty_results(),
269
				'noRevisionsDiv'	=> lasso_editor_empty_results('revision'),
270
				'mapTileProvider'   => function_exists('aesop_map_tile_provider') ? aesop_map_tile_provider( $postid ) : false,
271
				'mapLocations'		=> get_post_meta( $postid, 'ase_map_component_locations' ),
272
				'mapStart'			=> get_post_meta( $postid, 'ase_map_component_start_point', true ),
273
				'mapZoom'			=> get_post_meta( $postid, 'ase_map_component_zoom', true ),
274
				'revisionModal' 	=> lasso_editor_revision_modal(),
275
				'isMobile'          => wp_is_mobile(),
276
				'enableAutoSave'    => lasso_editor_get_option( 'enable_autosave', 'lasso_editor' ),
277
				'showColor'         => $show_color,
278
				'showAlignment'     => $show_align,
279
				'showIgnoredItems'  => lasso_editor_get_option('show_ignored_items', 'lasso_editor'),
280
				'restapi2'          => $using_restapiv2,
0 ignored issues
show
Bug introduced by
The variable $using_restapiv2 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
281
				'saveusingrest'     => $using_restapiv2 && !$disableRESTSave,
282
				'newObjectContent'  => '<p>'.apply_filters( 'lasso_new_object_content', __( 'Once upon a time...','lasso') ).'</p>',
283
				'disableSavePost'   => $save_to_post_disabled,
284
				'disableEditPost'   => $edit_post_disabled,
285
				'boldTag'           => $bold_tag,
286
				'iTag'           	=> $i_tag,
287
				'customFields'      => $custom_fields,
288
				'clickToInsert'     => ($insert_comp_ui =='click'),
289
				'buttonOnEmptyP'     => ($insert_comp_ui =='mediumcom'),      // auto show a button to insert components on an empty paragraph      
290
                'rtl'               => is_rtl(),				
291
				'skipToEdit'        =>( $delta < 10 && $delta >=0 ), // if it's a new post, skip to edit mode
292
				'linksEditable'    => $links_editable,
293
				'supportPendingStatus' => !$no_pending_status,
294
				'tableCode' => apply_filters( 'lasso_table_html_code','<table><tr><th>Cell 1</th><th>Cell 2</th></tr><tr><td>Cell 3</td><td>Cell 4</td></tr></table>'),
295
                'hasGutenberg' => (function_exists( 'has_blocks' ) && has_blocks( $post->post_content)) || self::gutenberg_active(),//,
296
                'multipages'=> $multipage,
297
                'post_content'=>$post_content
298
			);
299
300
301
			// wp api 
302
			
303
			
304
			if (!$using_restapiv2) {
305
               // enqueue REST API V1
306
			   wp_enqueue_script( 'wp-api-js', LASSO_URL.'/public/assets/js/source/util--wp-api.js', array( 'jquery', 'underscore', 'backbone' ), LASSO_VERSION, true );
307
			   $settings = array( 'root' => home_url( $home_url ), 'nonce' => wp_create_nonce( 'wp_json' ) );
308
			   wp_localize_script( 'wp-api-js', 'WP_API_Settings', $settings );
309
			}
310
			
311
			if ($allow_change_date) {
312
				wp_enqueue_script('jquery-ui-datepicker');
313
				wp_register_style('jquery-ui', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css');
314
				wp_enqueue_style('jquery-ui');
315
			}
316
317
			$postfix = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
318
			if ($show_color) {
319
				wp_enqueue_script('lasso', LASSO_URL. "/public/assets/js/lasso{$postfix}.js", array('jquery', 'wp-api','iris'), LASSO_VERSION, true);
320
			} else {
321
			    wp_enqueue_script('lasso', LASSO_URL. "/public/assets/js/lasso{$postfix}.js", array('jquery', 'wp-api'), LASSO_VERSION, true);
322
			}
323
			wp_localize_script('lasso', 'lasso_editor', apply_filters('lasso_localized_objects', $objects ) );
324
325
326
		}
327
328
	}
329
    
330
    function gutenberg_active() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
331
        
332
        // Gutenberg plugin is installed and activated.
333
        $gutenberg = ! ( false === has_filter( 'replace_editor', 'gutenberg_init' ) );
334
335
        // Block editor since 5.0.
336
        $block_editor = version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' );
337
338
        if ( ! $gutenberg && ! $block_editor ) {
339
            return false;
340
        }
341
342
        if ( self::is_classic_editor_plugin_active() ) {
343
            $editor_option       = get_option( 'classic-editor-replace' );
344
            $block_editor_active = array( 'no-replace', 'block' );
345
346
            return in_array( $editor_option, $block_editor_active, true );
347
        }
348
349
        return true;
350
    }
351
352
    /**
353
     * Check if Classic Editor plugin is active.
354
     *
355
     * @return bool
356
     */
357
    function is_classic_editor_plugin_active() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
358
        if ( ! function_exists( 'is_plugin_active' ) ) {
359
            include_once ABSPATH . 'wp-admin/includes/plugin.php';
360
        }
361
362
        if ( is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
363
            return true;
364
        }
365
366
        return false;
367
    }
368
369
}
370
371