Completed
Push — master ( e37cc4...f545a8 )
by
unknown
01:48
created

assets::scripts()   F

Complexity

Conditions 26
Paths > 20000

Size

Total Lines 265

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
dl 0
loc 265
rs 0
c 0
b 0
f 0
nc 12582913
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A assets.php ➔ get_UTC_offset() 0 6 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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