Completed
Push — master ( 9aed0a...23281d )
by
unknown
02:09
created

assets::is_multipage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 12
rs 9.8666
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
        echo $index;
28
        return $index;        
29
    }
30
31
	public function scripts(){
32
33
	
34
		global $post;
35
		if ( lasso_user_can('edit_posts') 
36
		     /* uncomment this line to disable Editus on Gutenberg posts*/
37
             /* && !( function_exists( 'has_blocks' ) && has_blocks( $post->post_content)  && !is_home()) */              
38
             ) {
39
			
40
			/**    Returns the time offset from UTC
41
			*/
42
			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...
43
				$timezone_string = get_option( 'timezone_string' );
44
				if (empty( $timezone_string ) ) {
45
					return get_option('gmt_offset')*3600;
46
				}
47
				
48
				$origin_dtz = new \DateTimeZone($timezone_string);				
49
				$origin_dt = new \DateTime("now", $origin_dtz);
50
				$offset = $origin_dtz->getOffset($origin_dt);
51
				return $offset;
52
			}
53
54
			wp_enqueue_style('lasso-style', LASSO_URL.'/public/assets/css/lasso.css', LASSO_VERSION, true);
55
56
            //don't load autocomplete if it's a stockholm theme
57
			$themename  	= wp_get_theme()->get('Name');
58
			if ($themename !='Stockholm' ) {
59
				wp_enqueue_script('jquery-ui-autocomplete');
60
			}
61
			wp_enqueue_script('jquery-ui-draggable');
62
			wp_enqueue_script('jquery-ui-sortable');
63
			wp_enqueue_script('jquery-ui-slider');
64
			
65
			// media uploader
66
			wp_enqueue_media();
67
68
			// url for json api
69
			$home_url = function_exists('json_get_url_prefix') ? json_get_url_prefix() : false;
70
71
			$article_object 	= lasso_editor_get_option('article_class','lasso_editor');
72
73
			$article_object 	= empty( $article_object ) && lasso_get_supported_theme_class() ? lasso_get_supported_theme_class() : $article_object;
74
75
			$featImgClass 		= lasso_editor_get_option('featimg_class','lasso_editor');
76
			if (empty( $featImgClass )) {
77
				$featImgClass = lasso_get_supported_theme_featured_image_class();
78
			}
79
			$titleClass 		= lasso_editor_get_option('title_class','lasso_editor');
80
			if (empty( $titleClass )) {
81
				$titleClass = lasso_get_supported_theme_title_class();
82
			}
83
			$toolbar_headings  	= lasso_editor_get_option('toolbar_headings', 'lasso_editor');
84
			$toolbar_headings_h4  	= lasso_editor_get_option('toolbar_headings_h4', 'lasso_editor');
85
			$objectsNoSave  	= lasso_editor_get_option('dont_save', 'lasso_editor');
86
			$objectsNonEditable  	= lasso_editor_get_option('non_editable', 'lasso_editor');
87
			$disableRESTSave = lasso_editor_get_option('save_using_rest_disabled', 'lasso_editor');
88
			$save_to_post_disabled  = lasso_editor_get_option( 'post_save_disabled', 'lasso_editor' );
89
			$edit_post_disabled  = lasso_editor_get_option( 'post_edit_disabled', 'lasso_editor' );
90
			
91
			$bold_tag = lasso_editor_get_option('bold_tag', 'lasso_editor','b');
92
			$i_tag = lasso_editor_get_option('i_tag', 'lasso_editor','i');
93
94
			
95
			//text alignement
96
			$show_align = lasso_editor_get_option('toolbar_show_alignment', 'lasso_editor');
97
			
98
			//make links editable under the editing mode
99
			$links_editable = lasso_editor_get_option('links_editable', 'lasso_editor');
100
			
101
			//color 
102
			$show_color = lasso_editor_get_option('toolbar_show_color', 'lasso_editor');
103
			
104
			// allow change date for post
105
			$allow_change_date = lasso_editor_get_option('allow_change_date', 'lasso_editor');
106
			
107
			if ($show_color) {
108
				//color picker
109
				wp_enqueue_style( 'wp-color-picker' );
110
				wp_enqueue_script( 'iris', admin_url( 'js/iris.min.js' ), array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ), false, 1 );
111
			}
112
			
113
			// click to insert components, not drag and drop
114
			$insert_comp_ui = lasso_editor_get_option('insert_comp_ui', 'lasso_editor');
115
			
116
			// do we support pending status
117
			$no_pending_status = lasso_editor_get_option('no_pending_status', 'lasso_editor');
118
			
119
			
120
			// custom fields
121
			
122
			$custom_fields = apply_filters( 'editus_custom_fields', null ); //array('testFieldName' => '#field1');
123
124
125
126
			// post id reference
127
			$postid 			= get_the_ID();
128
			$tz_offset = get_UTC_offset();
129
			$post_date = get_the_time('U', $postid);
130
			$time = (time()+$tz_offset);
131
            $delta = $time - $post_date;
132
            
133
			$strings = array(
134
				'save' 				=> __('Save','lasso'),
135
				'selectText'	  	=> __('Please Select Text First.','lasso'),
136
				'cancel' 			=> __('Cancel','lasso'),
137
				'exiteditor' 		=> __('Exit Editor','lasso'),
138
				'saving' 			=> __('Saving...','lasso'),
139
				'saved'				=> __('Saved!','lasso'),
140
				'adding' 			=> __('Adding...','lasso'),
141
				'added'				=> __('Added!','lasso'),
142
				'loading' 			=> __('Loading...','lasso'),
143
				'loadMore'			=> __('Load More','lasso'),
144
				'close'			=> __('Close','lasso'),
145
				'noPostsFound'		=> __('No more posts found','lasso'),
146
				'fetchFail'	    	=> __('Fetching failed.','lasso'),
147
				'galleryCreated' 	=> __('Gallery Created!','lasso'),
148
				'galleryUpdated' 	=> __('Gallery Updated!','lasso'),
149
				'justWrite'			=> __('Just write...','lasso'),
150
				'chooseImage'		=> __('Choose an image','lasso'),
151
				'updateImage'		=> __('Update Image','lasso'),
152
				'insertImage'		=> __('Insert Image','lasso'),
153
				'selectImage'		=> __('Select Image','lasso'),
154
				'removeFeatImg'     => __('Remove featured image?','lasso'),
155
				'updateSelectedImg' => __('Update Selected Image','lasso'),
156
				'chooseImages'		=> __('Choose images','lasso'),
157
				'editImage'			=> __('Edit Image','lasso'),
158
				'addImages'			=> __('Add Images','lasso'),
159
				'addNewGallery'		=> __('Add New Gallery','lasso'),
160
				'selectGallery'		=> __('Select Editus Gallery Image','lasso'),
161
				'useSelectedImages' => __('Use Selected Images','lasso'),
162
				'publishPost'		=> __('Publish Post?','lasso'),
163
				'publishYes'		=> __('Yes, publish it!','lasso'),
164
				'deletePost'		=> __('Trash Post?','lasso'),
165
				'deleteYes'			=> __('Yes, trash it!','lasso'),
166
				'warning'			=> __('Oh snap!','laso'),
167
				'cancelText'		=> __('O.K. got it!','lasso'),
168
				'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'),
169
				'missingConfirm'	=> __('Update Settings', 'lasso'),
170
				'helperText'		=> __('one more letter','lasso'),
171
				'editingBackup'  	=> __('You are currently editing a backup copy of this post.'),
172
				
173
				'catsPlaceholder'     => __('add categories...'),
174
				'tagsPlaceholder'     => __('add tags...'),
175
				
176
				
177
			);
178
179
			$api_url = trailingslashit( home_url() ) . 'lasso-internal-api';
180
181
			$gallery_class = new gallery();
182
			$gallery_nonce_action = $gallery_class->nonce_action;
183
			$gallery_nonce = wp_create_nonce( $gallery_nonce_action );
184
			
185
			
186
            if ($allow_change_date) {
187
			    $permalink = get_site_url().'/?p='.$postid;
188
            } else {
189
                $permalink = get_permalink($postid);
190
            }
191
			
192
			// rest api
193
			$rest_nonce = '';
194
			$rest_root = site_url().'/?rest_route=/'; 
195
			  
196
			if (function_exists('rest_url')) {
197
				//$rest_root = esc_url_raw( rest_url());
198
				$rest_nonce = wp_create_nonce( 'wp_rest' );
199
				$settings = array( 'root' => $rest_root, 'nonce' => $rest_nonce );
200
				wp_enqueue_script( 'wp-api', '', array( 'jquery', 'underscore', 'backbone' ), LASSO_VERSION, true );
201
				wp_localize_script( 'wp-api', 'wpApiSettings', $settings );
202
				wp_localize_script( 'wp-api', 'WP_API_Settings', $settings );
203
				
204
				if ( class_exists( 'WP_REST_Controller' )) {
205
					// we are using REST API V2
206
					$using_restapiv2 = true;
207
				}
208
			}
209
            
210
            //find if this is multi page
211
            $multipage = self::is_multipage();
212
            $post_content = "";
213
            if ($linkpages != -1) {
0 ignored issues
show
Bug introduced by
The variable $linkpages does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
214
                $post_content = $post->post_content;
215
            }
216
217
			// localized objects
218
			$objects = array(
219
				'ajaxurl' 			=> esc_url( $api_url ),
220
				'ajaxurl2' 			=> esc_url( admin_url( 'admin-ajax.php' )),
221
				'siteUrl'           => site_url(),
222
				'rest_root'         => $rest_root,
223
				'rest_nonce'        => $rest_nonce,
224
				'editor' 			=> 'lasso--content', // ID of editable content (without #) DONT CHANGE
225
				'article_object'	=> $article_object,
226
				'featImgClass'		=> $featImgClass,
227
				'titleClass'		=> $titleClass,
228
				'strings'			=> $strings,
229
				'settingsLink'		=> function_exists('is_multisite') && is_multisite() ? network_admin_url( 'settings.php?page=lasso-editor' ) : admin_url( 'admin.php?page=lasso-editor-settings' ),
230
				'post_status'		=> get_post_status( $postid ),
231
				'postid'			=> $postid,
232
				'permalink'			=> $permalink,
233
				'edit_others_pages'	=> current_user_can('edit_others_pages') ? true : false,
234
				'edit_others_posts'	=> current_user_can('edit_others_posts') ? true : false,
235
				'userCanEdit'		=> current_user_can('edit_post', $postid ),
236
				'can_publish'		=> is_page() ? current_user_can('publish_pages') : current_user_can('publish_posts'),
237
				//'can_publish_posts'	=> current_user_can('publish_posts'),
238
				//'can_publish_pages'	=> current_user_can('publish_pages'),
239
				'author'			=> is_user_logged_in() ? get_current_user_ID() : false,
240
				'nonce'				=> wp_create_nonce('lasso_editor'),
241
				'handle'			=> lasso_editor_settings_toolbar(),
242
				'toolbar'			=> lasso_editor_text_toolbar(),
243
				'toolbarHeadings'   => $toolbar_headings,
244
				'toolbarHeadingsH4'   => $toolbar_headings_h4,
245
				'component_modal'	=> lasso_editor_component_modal(),
246
				'component_sidebar'	=> lasso_editor_component_sidebar(),
247
				'components'		=> lasso_editor_components(),
248
				'wpImgEdit'			=> lasso_editor_wpimg_edit(),
249
				'wpVideoEdit'		=> lasso_editor_wpvideo_edit(),
250
				'featImgControls'   => lasso_editor_image_controls(),
251
				'featImgNonce'		=> $gallery_nonce,
252
				'getGallImgNonce'	=> $gallery_nonce,
253
				'createGallNonce'	=> $gallery_nonce,
254
				'swapGallNonce'		=> $gallery_nonce,
255
				'titleNonce'		=> wp_create_nonce('lasso_update_title'),
256
				'wpImgNonce'		=> wp_create_nonce('lasso_update_wpimg'),
257
				'deletePost'		=> wp_create_nonce('lasso_delete_post'),
258
				'searchPosts'		=> wp_create_nonce('lasso_search_posts'),
259
				'component_options' => lasso_editor_options_blob(),
260
				'newPostModal'		=> lasso_editor_newpost_modal(),
261
				'allPostModal'		=> lasso_editor_allpost_modal(),
262
				'mapFormFooter'		=> lasso_map_form_footer(),
263
				'refreshRequired'	=> lasso_editor_refresh_message(),
264
				'objectsNoSave'		=> $objectsNoSave,
265
				'objectsNonEditable' => $objectsNonEditable,
266
				'supportedNoSave'	=> lasso_supported_no_save(),
267
				'postCategories'    => lasso_get_objects('category'),
268
				'postTags'    		=> lasso_get_objects('tag'),
269
				'noResultsDiv'		=> lasso_editor_empty_results(),
270
				'noRevisionsDiv'	=> lasso_editor_empty_results('revision'),
271
				'mapTileProvider'   => function_exists('aesop_map_tile_provider') ? aesop_map_tile_provider( $postid ) : false,
272
				'mapLocations'		=> get_post_meta( $postid, 'ase_map_component_locations' ),
273
				'mapStart'			=> get_post_meta( $postid, 'ase_map_component_start_point', true ),
274
				'mapZoom'			=> get_post_meta( $postid, 'ase_map_component_zoom', true ),
275
				'revisionModal' 	=> lasso_editor_revision_modal(),
276
				'isMobile'          => wp_is_mobile(),
277
				'enableAutoSave'    => lasso_editor_get_option( 'enable_autosave', 'lasso_editor' ),
278
				'showColor'         => $show_color,
279
				'showAlignment'     => $show_align,
280
				'showIgnoredItems'  => lasso_editor_get_option('show_ignored_items', 'lasso_editor'),
281
				'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...
282
				'saveusingrest'     => $using_restapiv2 && !$disableRESTSave,
283
				'newObjectContent'  => '<p>'.apply_filters( 'lasso_new_object_content', __( 'Once upon a time...','lasso') ).'</p>',
284
				'disableSavePost'   => $save_to_post_disabled,
285
				'disableEditPost'   => $edit_post_disabled,
286
				'boldTag'           => $bold_tag,
287
				'iTag'           	=> $i_tag,
288
				'customFields'      => $custom_fields,
289
				'clickToInsert'     => ($insert_comp_ui =='click'),
290
				'buttonOnEmptyP'     => ($insert_comp_ui =='mediumcom'),      // auto show a button to insert components on an empty paragraph      
291
                'rtl'               => is_rtl(),				
292
				'skipToEdit'        =>( $delta < 10 && $delta >=0 ), // if it's a new post, skip to edit mode
293
				'linksEditable'    => $links_editable,
294
				'supportPendingStatus' => !$no_pending_status,
295
				'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>'),
296
                'hasGutenberg' => (function_exists( 'has_blocks' ) && has_blocks( $post->post_content)),
297
                'multipages'=> $multipage,
298
                'post_content'=>$post_content
299
			);
300
301
302
			// wp api 
303
			
304
			
305
			if (!$using_restapiv2) {
306
               // enqueue REST API V1
307
			   wp_enqueue_script( 'wp-api-js', LASSO_URL.'/public/assets/js/source/util--wp-api.js', array( 'jquery', 'underscore', 'backbone' ), LASSO_VERSION, true );
308
			   $settings = array( 'root' => home_url( $home_url ), 'nonce' => wp_create_nonce( 'wp_json' ) );
309
			   wp_localize_script( 'wp-api-js', 'WP_API_Settings', $settings );
310
			}
311
			
312
			if ($allow_change_date) {
313
				wp_enqueue_script('jquery-ui-datepicker');
314
				wp_register_style('jquery-ui', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css');
315
				wp_enqueue_style('jquery-ui');
316
			}
317
318
			$postfix = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
319
			if ($show_color) {
320
				wp_enqueue_script('lasso', LASSO_URL. "/public/assets/js/lasso{$postfix}.js", array('jquery', 'wp-api','iris'), LASSO_VERSION, true);
321
			} else {
322
			    wp_enqueue_script('lasso', LASSO_URL. "/public/assets/js/lasso{$postfix}.js", array('jquery', 'wp-api'), LASSO_VERSION, true);
323
			}
324
			wp_localize_script('lasso', 'lasso_editor', apply_filters('lasso_localized_objects', $objects ) );
325
326
327
		}
328
329
	}
330
331
}
332
333