Completed
Push — develop ( 5c7568...6a9ba1 )
by David
14:52
created

wordlift.php ➔ wl_enqueue_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The plugin bootstrap file
4
 *
5
 * This file is read by WordPress to generate the plugin information in the plugin
6
 * admin area. This file also includes all of the dependencies used by the plugin,
7
 * registers the activation and deactivation functions, and defines a function
8
 * that starts the plugin.
9
 *
10
 * @link              https://wordlift.io
11
 * @since             1.0.0
12
 * @package           Wordlift
13
 *
14
 * @wordpress-plugin
15
 * Plugin Name:       WordLift
16
 * Plugin URI:        https://wordlift.io
17
 * Description:       WordLift brings the power of AI to organize content, attract new readers and get their attention. To activate the plugin <a href="https://wordlift.io/">visit our website</a>.
18
 * Version:           3.20.0-dev
19
 * Author:            WordLift, Insideout10
20
 * Author URI:        https://wordlift.io
21
 * License:           GPL-2.0+
22
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
23
 * Text Domain:       wordlift
24
 * Domain Path:       /languages
25
 */
26
27
// If this file is called directly, abort.
28
if ( ! defined( 'WPINC' ) ) {
29
	die;
30
}
31
32
// Include WordLift constants.
33
require_once( 'wordlift_constants.php' );
34
35
// Load modules.
36
require_once( 'modules/core/wordlift_core.php' );
37
38
/**
39
 * Log to the debug.log file.
40
 *
41
 * @deprecated use Wordlift_Log_Service::get_instance()->info( $log );
42
 *
43
 * @since      3.0.0
44
 *
45
 * @uses       wl_write_log_handler() to write the log output.
46
 *
47
 * @param string|mixed $log The log data.
48
 */
49
function wl_write_log( $log ) {
50
51
	Wordlift_Log_Service::get_instance()->debug( $log );
52
53
}
54
55
/**
56
 * The default log handler prints out the log.
57
 *
58
 * @deprecated
59
 *
60
 * @since 3.0.0
61
 *
62
 * @param string|array $log The log data.
63
 * @param string       $caller The calling function.
64
 */
65
function wl_write_log_handler( $log, $caller = null ) {
66
67
	global $wl_logger;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
68
69
	if ( true === WP_DEBUG ) {
70
71
		$message = ( isset( $caller ) ? sprintf( '[%-40.40s] ', $caller ) : '' ) .
72
		           ( is_array( $log ) || is_object( $log ) ? print_r( $log, true ) : wl_write_log_hide_key( $log ) );
0 ignored issues
show
Deprecated Code introduced by
The function wl_write_log_hide_key() has been deprecated.

This function has been deprecated.

Loading history...
73
74
		if ( isset( $wl_logger ) ) {
75
			$wl_logger->info( $message );
76
		} else {
77
			error_log( $message );
78
		}
79
80
	}
81
82
}
83
84
/**
85
 * Hide the WordLift Key from the provided text.
86
 *
87
 * @deprecated
88
 *
89
 * @since 3.0.0
90
 *
91
 * @param string $text A text that may potentially contain a WL key.
92
 *
93
 * @return string A text with the key hidden.
94
 */
95
function wl_write_log_hide_key( $text ) {
96
97
	return str_ireplace( wl_configuration_get_key(), '<hidden>', $text );
0 ignored issues
show
Deprecated Code introduced by
The function wl_configuration_get_key() has been deprecated with message: use Wordlift_Configuration_Service::get_instance()->get_key()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
98
}
99
100
/**
101
 * Enable microdata schema.org tagging.
102
 * see http://vip.wordpress.com/documentation/register-additional-html-attributes-for-tinymce-and-wp-kses/
103
 */
104
function wordlift_allowed_post_tags() {
105
	global $allowedposttags;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
106
107
	$tags           = array( 'span' );
108
	$new_attributes = array(
109
		'itemscope' => array(),
110
		'itemtype'  => array(),
111
		'itemprop'  => array(),
112
		'itemid'    => array(),
113
	);
114
115
	foreach ( $tags as $tag ) {
116
		if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) {
117
			$allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes );
118
		}
119
	}
120
}
121
122
// add allowed post tags.
123
add_action( 'init', 'wordlift_allowed_post_tags' );
124
125
/**
126
 * Register additional scripts for the admin UI.
127
 */
128
function wordlift_admin_enqueue_scripts() {
129
130
	// Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/)
131
	wp_enqueue_script( 'wpdialogs' );
132
	wp_enqueue_style( 'wp-jquery-ui-dialog' );
133
134
	wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' );
135
136
	wp_enqueue_script( 'jquery-ui-autocomplete' );
137
138
	$log = Wordlift_Log_Service::get_logger( 'wordlift_admin_enqueue_scripts' );
139
	$log->trace( 'Registering admin scripts...' );
140
141
	// We now register angular scripts and have dependent scripts (currently
142
	// only the edit post page) depend on them, to avoid potential conflicts.
143
	//
144
	// See https://github.com/insideout10/wordlift-plugin/issues/691.
145
	$result = wp_register_script( 'wl-angular', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js' )
146
	          && wp_register_script( 'wl-angular-geolocation', plugin_dir_url( __FILE__ ) . 'bower_components/angularjs-geolocation/dist/angularjs-geolocation.min.js' )
147
	          && wp_register_script( 'wl-angular-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js' )
148
	          && wp_register_script( 'wl-angular-animate', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-animate.min.js' );
149
150
	$log->debug( 'Registering angular scripts was ' . ( $result ? 'successful.' : 'unsuccessful.' ) );
151
152
	// Disable auto-save for custom entity posts only
153
	if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) {
154
		wp_dequeue_script( 'autosave' );
155
	}
156
157
}
158
159
add_action( 'admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts' );
160
161
// We shouldn't load the wordlift-ui.min.css stylesheet when not needed.
162
//
163
// @see https://github.com/insideout10/wordlift-plugin/issues/699
164
//function wl_enqueue_scripts() {
165
//	wp_enqueue_style( 'wordlift-ui', plugin_dir_url( __FILE__ ) . 'css/wordlift-ui.min.css' );
166
//}
167
//
168
//add_action( 'wp_enqueue_scripts', 'wl_enqueue_scripts' );
169
170
/**
171
 * Hooked to *wp_kses_allowed_html* filter, adds microdata attributes.
172
 *
173
 * @param array  $allowedtags The array with the currently configured elements and attributes.
174
 * @param string $context The context.
175
 *
176
 * @return array An array which contains allowed microdata attributes.
177
 */
178
function wordlift_allowed_html( $allowedtags, $context ) {
179
180
	if ( 'post' !== $context ) {
181
		return $allowedtags;
182
	}
183
184
	return array_merge_recursive( $allowedtags, array(
185
		'span' => array(
186
			'itemscope' => true,
187
			'itemtype'  => true,
188
			'itemid'    => true,
189
			'itemprop'  => true,
190
		),
191
	) );
192
}
193
194
add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 );
195
196
/**
197
 * Get the coordinates for the specified post ID.
198
 *
199
 * @param int $post_id The post ID.
200
 *
201
 * @return array|null An array of coordinates or null.
202
 */
203
function wl_get_coordinates( $post_id ) {
204
205
	$latitude  = wl_schema_get_value( $post_id, 'latitude' );
206
	$longitude = wl_schema_get_value( $post_id, 'longitude' );
207
208
	// DO NOT set latitude/longitude to 0/0 as default values. It's a specific
209
	// place on the globe:"The zero/zero point of this system is located in the
210
	// Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana."
211
	return array(
212
		'latitude'  => isset( $latitude[0] ) && is_numeric( $latitude[0] ) ? $latitude[0] : '',
213
		'longitude' => isset( $longitude[0] ) && is_numeric( $longitude[0] ) ? $longitude[0] : '',
214
	);
215
}
216
217
/**
218
 * Get all the images bound to a post.
219
 *
220
 * @deprecated use Wordlift_Storage_Factory::get_instance()->post_images()->get( $post_id )
221
 *
222
 * @param int $post_id The post ID.
223
 *
224
 * @return array An array of image URLs.
225
 */
226
function wl_get_image_urls( $post_id ) {
227
228
	return Wordlift_Storage_Factory::get_instance()
229
	                               ->post_images()
230
	                               ->get( $post_id );
231
232
//	// If there is a featured image it has the priority.
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...
233
//	$featured_image_id = get_post_thumbnail_id( $post_id );
234
//	if ( is_numeric( $featured_image_id ) ) {
235
//		$image_url = wp_get_attachment_url( $featured_image_id );
236
//
237
//		return array( $image_url );
238
//	}
239
//
240
//	$images = get_children( array(
241
//		'post_parent'    => $post_id,
242
//		'post_type'      => 'attachment',
243
//		'post_mime_type' => 'image',
244
//	) );
245
//
246
//	// Return an empty array if no image is found.
247
//	if ( empty( $images ) ) {
248
//		return array();
249
//	}
250
//
251
//	// Prepare the return array.
252
//	$image_urls = array();
253
//
254
//	// Collect the URLs.
255
//	foreach ( $images as $attachment_id => $attachment ) {
256
//		$image_url = wp_get_attachment_url( $attachment_id );
257
//		// Ensure the URL isn't collected already.
258
//		if ( ! in_array( $image_url, $image_urls ) ) {
259
//			array_push( $image_urls, $image_url );
260
//		}
261
//	}
262
//
263
//	// wl_write_log( "wl_get_image_urls [ post id :: $post_id ][ image urls count :: " . count( $image_urls ) . " ]" );
264
//
265
//	return $image_urls;
266
}
267
268
/**
269
 * Get an attachment with the specified parent post ID and source URL.
270
 *
271
 * @param int    $parent_post_id The parent post ID.
272
 * @param string $source_url The source URL.
273
 *
274
 * @return WP_Post|null A post instance or null if not found.
275
 */
276
function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) {
277
278
	// wl_write_log( "wl_get_attachment_for_source_url [ parent post id :: $parent_post_id ][ source url :: $source_url ]" );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
279
280
	$posts = get_posts( array(
281
		'post_type'      => 'attachment',
282
		'posts_per_page' => 1,
283
		'post_status'    => 'any',
284
		'post_parent'    => $parent_post_id,
285
		'meta_key'       => 'wl_source_url',
286
		'meta_value'     => $source_url,
287
	) );
288
289
	// Return the found post.
290
	if ( 1 === count( $posts ) ) {
291
		return $posts[0];
292
	}
293
294
	// Return null.
295
	return null;
296
}
297
298
/**
299
 * Set the source URL.
300
 *
301
 * @param int    $post_id The post ID.
302
 * @param string $source_url The source URL.
303
 */
304
function wl_set_source_url( $post_id, $source_url ) {
305
306
	delete_post_meta( $post_id, 'wl_source_url' );
307
	add_post_meta( $post_id, 'wl_source_url', $source_url );
308
}
309
310
/**
311
 * Sanitizes an URI path by replacing the non allowed characters with an underscore.
312
 * @uses       sanitize_title() to manage not ASCII chars
313
 * @deprecated use Wordlift_Uri_Service::get_instance()->sanitize_path();
314
 * @see        https://codex.wordpress.org/Function_Reference/sanitize_title
315
 *
316
 * @param string $path The path to sanitize.
317
 * @param string $char The replacement character (by default an underscore).
318
 *
319
 * @return string The sanitized path.
320
 */
321
function wl_sanitize_uri_path( $path, $char = '_' ) {
322
323
	return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char );
324
}
325
326
///**
327
// * Schedule the execution of SPARQL Update queries before the WordPress look ends.
328
// */
329
//function wl_shutdown() {
330
//
331
//	// Get the filename to the temporary SPARQL file.
332
//	$filename = WL_TEMP_DIR . WL_REQUEST_ID . '.sparql';
333
//
334
//	// If WordLift is buffering SPARQL queries, we're admins and a buffer exists, then schedule it.
335
//	if ( WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING && is_admin() && file_exists( $filename ) ) {
336
//
337
//		// The request ID.
338
//		$args = array( WL_REQUEST_ID );
339
//
340
//		// Schedule the execution of the SPARQL query with the request ID.
341
//		wp_schedule_single_event( time(), 'wl_execute_saved_sparql_update_query', $args );
342
//
343
//		// Check that the request is scheduled.
344
//		$timestamp = wp_next_scheduled( 'wl_execute_saved_sparql_update_query', $args );
345
//
346
//		// Spawn the cron.
347
//		spawn_cron();
348
//
349
//		wl_write_log( "wl_shutdown [ request id :: " . WL_REQUEST_ID . " ][ timestamp :: $timestamp ]" );
350
//	}
351
//}
352
//
353
//add_action( 'shutdown', 'wl_shutdown' );
354
355
/**
356
 * Replaces the *itemid* attributes URIs with the WordLift URIs.
357
 *
358
 * @param string $content The post content.
359
 *
360
 * @return string The updated post content.
361
 */
362
function wl_replace_item_id_with_uri( $content ) {
363
364
	$log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' );
365
	$log->trace( 'Replacing item IDs with URIs...' );
366
367
	// Strip slashes, see https://core.trac.wordpress.org/ticket/21767
368
	$content = stripslashes( $content );
369
370
	// If any match are found.
371
	$matches = array();
372
	if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) {
373
374
		foreach ( $matches as $match ) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
375
376
			// Get the item ID.
377
			$item_id = $match[1];
378
379
			// Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' .
380
			$post = Wordlift_Entity_Service::get_instance()
0 ignored issues
show
Deprecated Code introduced by
The method Wordlift_Entity_Service::get_entity_post_by_uri() has been deprecated with message: in favor of Wordlift_Entity_Uri_Service->get_entity( $uri );

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
381
			                               ->get_entity_post_by_uri( $item_id );
382
383
			// If no entity is found, continue to the next one.
384
			if ( null === $post ) {
385
				continue;
386
			}
387
388
			// Get the URI for that post.
389
			$uri = wl_get_entity_uri( $post->ID );
0 ignored issues
show
Deprecated Code introduced by
The function wl_get_entity_uri() has been deprecated with message: use Wordlift_Entity_Service::get_instance()->get_uri( $post_id )

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
390
391
			// wl_write_log( "wl_replace_item_id_with_uri [ item id :: $item_id ][ uri :: $uri ]" );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
392
393
			// If the item ID and the URI differ, replace the item ID with the URI saved in WordPress.
394
			if ( $item_id !== $uri ) {
395
				$uri_e   = esc_html( $uri );
396
				$content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content );
397
			}
398
		}
399
	}
400
401
	// Reapply slashes.
402
	$content = addslashes( $content );
403
404
	return $content;
405
}
406
407
add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 );
408
409
require_once( 'wordlift_entity_functions.php' );
410
411
// add editor related methods.
412
require_once( 'wordlift_editor.php' );
413
414
// add the WordLift entity custom type.
415
require_once( 'wordlift_entity_type.php' );
416
417
// add callbacks on post save to notify data changes from wp to redlink triple store
418
require_once( 'wordlift_to_redlink_data_push_callbacks.php' );
419
420
require_once( 'modules/configuration/wordlift_configuration_settings.php' );
421
422
// Load modules
423
require_once( 'modules/analyzer/wordlift_analyzer.php' );
424
require_once( 'modules/linked_data/wordlift_linked_data.php' );
425
require_once( 'modules/prefixes/wordlift_prefixes.php' );
426
427
// Shortcodes
428
429
require_once( 'modules/geo_widget/wordlift_geo_widget.php' );
430
require_once( 'shortcodes/wordlift_shortcode_chord.php' );
431
require_once( 'shortcodes/wordlift_shortcode_geomap.php' );
432
require_once( 'shortcodes/wordlift_shortcode_field.php' );
433
require_once( 'shortcodes/wordlift_shortcode_faceted_search.php' );
434
require_once( 'shortcodes/wordlift_shortcode_navigator.php' );
435
436
require_once( 'widgets/wordlift_widget_geo.php' );
437
require_once( 'widgets/class-wordlift-chord-widget.php' );
438
require_once( 'widgets/wordlift_widget_timeline.php' );
439
440
require_once( 'wordlift_redlink.php' );
441
442
// Add admin functions.
443
// TODO: find a way to make 'admin' UI tests work.
444
//if ( is_admin() ) {
445
446
require_once( 'admin/wordlift_admin.php' );
447
require_once( 'admin/wordlift_admin_edit_post.php' );
448
require_once( 'admin/wordlift_admin_save_post.php' );
449
450
// add the entities meta box.
451
require_once( 'admin/wordlift_admin_meta_box_entities.php' );
452
453
// add the entity creation AJAX.
454
require_once( 'admin/wordlift_admin_ajax_related_posts.php' );
455
456
// Load the wl_chord TinyMCE button and configuration dialog.
457
require_once( 'admin/wordlift_admin_shortcodes.php' );
458
459
/**
460
 * The code that runs during plugin activation.
461
 * This action is documented in includes/class-wordlift-activator.php
462
 */
463
function activate_wordlift() {
464
465
	$log = Wordlift_Log_Service::get_logger( 'activate_wordlift' );
466
467
	$log->info( 'Activating WordLift...' );
468
469
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php';
470
	Wordlift_Activator::activate();
471
472
	/**
473
	 * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks.
474
	 *
475
	 * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue.
476
	 * @since 3.19.2
477
	 */
478
	Wordlift_Http_Api::activate();
479
480
	// Ensure the post type is registered before flushing the rewrite rules.
481
	Wordlift_Entity_Post_Type_Service::get_instance()->register();
482
	flush_rewrite_rules();
483
484
}
485
486
/**
487
 * The code that runs during plugin deactivation.
488
 * This action is documented in includes/class-wordlift-deactivator.php
489
 */
490
function deactivate_wordlift() {
491
492
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php';
493
	Wordlift_Deactivator::deactivate();
494
	flush_rewrite_rules();
495
496
}
497
498
register_activation_hook( __FILE__, 'activate_wordlift' );
499
register_deactivation_hook( __FILE__, 'deactivate_wordlift' );
500
501
/**
502
 * The core plugin class that is used to define internationalization,
503
 * admin-specific hooks, and public-facing site hooks.
504
 */
505
require plugin_dir_path( __FILE__ ) . 'includes/class-wordlift.php';
506
507
/**
508
 * Begins execution of the plugin.
509
 *
510
 * Since everything within the plugin is registered via hooks,
511
 * then kicking off the plugin from this point in the file does
512
 * not affect the page life cycle.
513
 *
514
 * @since    1.0.0
515
 */
516
function run_wordlift() {
517
518
	$plugin = new Wordlift();
519
	$plugin->run();
520
521
}
522
523
run_wordlift();
524