Completed
Push — develop ( 0a0f85...addb8e )
by David
03:18
created

wordlift.php ➔ wordlift_admin_enqueue_scripts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 37
rs 9.328
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-dev4
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
 * @param string|mixed $log The log data.
46
 */
47
function wl_write_log( $log ) {
48
49
	Wordlift_Log_Service::get_instance()->debug( $log );
50
51
}
52
53
/**
54
 * Hide the WordLift Key from the provided text.
55
 *
56
 * @deprecated
57
 *
58
 * @since 3.0.0
59
 *
60
 * @param string $text A text that may potentially contain a WL key.
61
 *
62
 * @return string A text with the key hidden.
63
 */
64
function wl_write_log_hide_key( $text ) {
65
66
	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...
67
}
68
69
/**
70
 * Enable microdata schema.org tagging.
71
 * see http://vip.wordpress.com/documentation/register-additional-html-attributes-for-tinymce-and-wp-kses/
72
 */
73
function wordlift_allowed_post_tags() {
74
	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...
75
76
	$tags           = array( 'span' );
77
	$new_attributes = array(
78
		'itemscope' => array(),
79
		'itemtype'  => array(),
80
		'itemprop'  => array(),
81
		'itemid'    => array(),
82
	);
83
84
	foreach ( $tags as $tag ) {
85
		if ( isset( $allowedposttags[ $tag ] ) && is_array( $allowedposttags[ $tag ] ) ) {
86
			$allowedposttags[ $tag ] = array_merge( $allowedposttags[ $tag ], $new_attributes );
87
		}
88
	}
89
}
90
91
// add allowed post tags.
92
add_action( 'init', 'wordlift_allowed_post_tags' );
93
94
/**
95
 * Register additional scripts for the admin UI.
96
 */
97
function wordlift_admin_enqueue_scripts() {
98
99
	// Added for compatibility with WordPress 3.9 (see http://make.wordpress.org/core/2014/04/16/jquery-ui-and-wpdialogs-in-wordpress-3-9/)
100
	wp_enqueue_script( 'wpdialogs' );
101
	wp_enqueue_style( 'wp-jquery-ui-dialog' );
102
103
	wp_enqueue_style( 'wordlift-reloaded', plugin_dir_url( __FILE__ ) . 'css/wordlift-reloaded.min.css' );
104
105
	wp_enqueue_script( 'jquery-ui-autocomplete' );
106
107
	/*
108
	 * Angular isn't loaded anymore separately, it is embedded in wordlift-reloaded.js.
109
	 *
110
	 * See https://github.com/insideout10/wordlift-plugin/issues/865.
111
	 *
112
	 * @since 3.19.6
113
	 */
114
//	$log = Wordlift_Log_Service::get_logger( 'wordlift_admin_enqueue_scripts' );
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...
115
//	$log->trace( 'Registering admin scripts...' );
116
//
117
//	// We now register angular scripts and have dependent scripts (currently
118
//	// only the edit post page) depend on them, to avoid potential conflicts.
119
//	//
120
//	// See https://github.com/insideout10/wordlift-plugin/issues/691.
121
//	$result = wp_register_script( 'wl-angular', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular.min.js' )
122
//	          && wp_register_script( 'wl-angular-geolocation', plugin_dir_url( __FILE__ ) . 'bower_components/angularjs-geolocation/dist/angularjs-geolocation.min.js' )
123
//	          && wp_register_script( 'wl-angular-touch', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-touch.min.js' )
124
//	          && wp_register_script( 'wl-angular-animate', 'https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-animate.min.js' );
125
//
126
//	$log->debug( 'Registering angular scripts was ' . ( $result ? 'successful.' : 'unsuccessful.' ) );
127
128
	// Disable auto-save for custom entity posts only
129
	if ( Wordlift_Entity_Service::TYPE_NAME === get_post_type() ) {
130
		wp_dequeue_script( 'autosave' );
131
	}
132
133
}
134
135
add_action( 'admin_enqueue_scripts', 'wordlift_admin_enqueue_scripts' );
136
137
// We shouldn't load the wordlift-ui.min.css stylesheet when not needed.
138
//
139
// @see https://github.com/insideout10/wordlift-plugin/issues/699
140
//function wl_enqueue_scripts() {
141
//	wp_enqueue_style( 'wordlift-ui', plugin_dir_url( __FILE__ ) . 'css/wordlift-ui.min.css' );
142
//}
143
//
144
//add_action( 'wp_enqueue_scripts', 'wl_enqueue_scripts' );
145
146
/**
147
 * Hooked to *wp_kses_allowed_html* filter, adds microdata attributes.
148
 *
149
 * @param array  $allowedtags The array with the currently configured elements and attributes.
150
 * @param string $context The context.
151
 *
152
 * @return array An array which contains allowed microdata attributes.
153
 */
154
function wordlift_allowed_html( $allowedtags, $context ) {
155
156
	if ( 'post' !== $context ) {
157
		return $allowedtags;
158
	}
159
160
	return array_merge_recursive( $allowedtags, array(
161
		'span' => array(
162
			'itemscope' => true,
163
			'itemtype'  => true,
164
			'itemid'    => true,
165
			'itemprop'  => true,
166
		),
167
	) );
168
}
169
170
add_filter( 'wp_kses_allowed_html', 'wordlift_allowed_html', 10, 2 );
171
172
/**
173
 * Get the coordinates for the specified post ID.
174
 *
175
 * @param int $post_id The post ID.
176
 *
177
 * @return array|null An array of coordinates or null.
178
 */
179
function wl_get_coordinates( $post_id ) {
180
181
	$latitude  = wl_schema_get_value( $post_id, 'latitude' );
182
	$longitude = wl_schema_get_value( $post_id, 'longitude' );
183
184
	// DO NOT set latitude/longitude to 0/0 as default values. It's a specific
185
	// place on the globe:"The zero/zero point of this system is located in the
186
	// Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana."
187
	return array(
188
		'latitude'  => isset( $latitude[0] ) && is_numeric( $latitude[0] ) ? $latitude[0] : '',
189
		'longitude' => isset( $longitude[0] ) && is_numeric( $longitude[0] ) ? $longitude[0] : '',
190
	);
191
}
192
193
/**
194
 * Get all the images bound to a post.
195
 *
196
 * @deprecated use Wordlift_Storage_Factory::get_instance()->post_images()->get( $post_id )
197
 *
198
 * @param int $post_id The post ID.
199
 *
200
 * @return array An array of image URLs.
201
 */
202
function wl_get_image_urls( $post_id ) {
203
204
	return Wordlift_Storage_Factory::get_instance()
205
	                               ->post_images()
206
	                               ->get( $post_id );
207
208
//	// 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...
209
//	$featured_image_id = get_post_thumbnail_id( $post_id );
210
//	if ( is_numeric( $featured_image_id ) ) {
211
//		$image_url = wp_get_attachment_url( $featured_image_id );
212
//
213
//		return array( $image_url );
214
//	}
215
//
216
//	$images = get_children( array(
217
//		'post_parent'    => $post_id,
218
//		'post_type'      => 'attachment',
219
//		'post_mime_type' => 'image',
220
//	) );
221
//
222
//	// Return an empty array if no image is found.
223
//	if ( empty( $images ) ) {
224
//		return array();
225
//	}
226
//
227
//	// Prepare the return array.
228
//	$image_urls = array();
229
//
230
//	// Collect the URLs.
231
//	foreach ( $images as $attachment_id => $attachment ) {
232
//		$image_url = wp_get_attachment_url( $attachment_id );
233
//		// Ensure the URL isn't collected already.
234
//		if ( ! in_array( $image_url, $image_urls ) ) {
235
//			array_push( $image_urls, $image_url );
236
//		}
237
//	}
238
//
239
//	// wl_write_log( "wl_get_image_urls [ post id :: $post_id ][ image urls count :: " . count( $image_urls ) . " ]" );
240
//
241
//	return $image_urls;
242
}
243
244
/**
245
 * Get an attachment with the specified parent post ID and source URL.
246
 *
247
 * @param int    $parent_post_id The parent post ID.
248
 * @param string $source_url The source URL.
249
 *
250
 * @return WP_Post|null A post instance or null if not found.
251
 */
252
function wl_get_attachment_for_source_url( $parent_post_id, $source_url ) {
253
254
	// 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...
255
256
	$posts = get_posts( array(
257
		'post_type'      => 'attachment',
258
		'posts_per_page' => 1,
259
		'post_status'    => 'any',
260
		'post_parent'    => $parent_post_id,
261
		'meta_key'       => 'wl_source_url',
262
		'meta_value'     => $source_url,
263
	) );
264
265
	// Return the found post.
266
	if ( 1 === count( $posts ) ) {
267
		return $posts[0];
268
	}
269
270
	// Return null.
271
	return null;
272
}
273
274
/**
275
 * Set the source URL.
276
 *
277
 * @param int    $post_id The post ID.
278
 * @param string $source_url The source URL.
279
 */
280
function wl_set_source_url( $post_id, $source_url ) {
281
282
	delete_post_meta( $post_id, 'wl_source_url' );
283
	add_post_meta( $post_id, 'wl_source_url', $source_url );
284
}
285
286
/**
287
 * Sanitizes an URI path by replacing the non allowed characters with an underscore.
288
 * @uses       sanitize_title() to manage not ASCII chars
289
 * @deprecated use Wordlift_Uri_Service::get_instance()->sanitize_path();
290
 * @see        https://codex.wordpress.org/Function_Reference/sanitize_title
291
 *
292
 * @param string $path The path to sanitize.
293
 * @param string $char The replacement character (by default an underscore).
294
 *
295
 * @return string The sanitized path.
296
 */
297
function wl_sanitize_uri_path( $path, $char = '_' ) {
298
299
	return Wordlift_Uri_Service::get_instance()->sanitize_path( $path, $char );
300
}
301
302
///**
303
// * Schedule the execution of SPARQL Update queries before the WordPress look ends.
304
// */
305
//function wl_shutdown() {
306
//
307
//	// Get the filename to the temporary SPARQL file.
308
//	$filename = WL_TEMP_DIR . WL_REQUEST_ID . '.sparql';
309
//
310
//	// If WordLift is buffering SPARQL queries, we're admins and a buffer exists, then schedule it.
311
//	if ( WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING && is_admin() && file_exists( $filename ) ) {
312
//
313
//		// The request ID.
314
//		$args = array( WL_REQUEST_ID );
315
//
316
//		// Schedule the execution of the SPARQL query with the request ID.
317
//		wp_schedule_single_event( time(), 'wl_execute_saved_sparql_update_query', $args );
318
//
319
//		// Check that the request is scheduled.
320
//		$timestamp = wp_next_scheduled( 'wl_execute_saved_sparql_update_query', $args );
321
//
322
//		// Spawn the cron.
323
//		spawn_cron();
324
//
325
//		wl_write_log( "wl_shutdown [ request id :: " . WL_REQUEST_ID . " ][ timestamp :: $timestamp ]" );
326
//	}
327
//}
328
//
329
//add_action( 'shutdown', 'wl_shutdown' );
330
331
/**
332
 * Replaces the *itemid* attributes URIs with the WordLift URIs.
333
 *
334
 * @param string $content The post content.
335
 *
336
 * @return string The updated post content.
337
 */
338
function wl_replace_item_id_with_uri( $content ) {
339
340
	$log = Wordlift_Log_Service::get_logger( 'wl_replace_item_id_with_uri' );
341
	$log->trace( 'Replacing item IDs with URIs...' );
342
343
	// Strip slashes, see https://core.trac.wordpress.org/ticket/21767
344
	$content = stripslashes( $content );
345
346
	// If any match are found.
347
	$matches = array();
348
	if ( 0 < preg_match_all( '/ itemid="([^"]+)"/i', $content, $matches, PREG_SET_ORDER ) ) {
349
350
		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...
351
352
			// Get the item ID.
353
			$item_id = $match[1];
354
355
			// Get the post bound to that item ID (looking both in the 'official' URI and in the 'same-as' .
356
			$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...
357
			                               ->get_entity_post_by_uri( $item_id );
358
359
			// If no entity is found, continue to the next one.
360
			if ( null === $post ) {
361
				continue;
362
			}
363
364
			// Get the URI for that post.
365
			$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...
366
367
			// 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...
368
369
			// If the item ID and the URI differ, replace the item ID with the URI saved in WordPress.
370
			if ( $item_id !== $uri ) {
371
				$uri_e   = esc_html( $uri );
372
				$content = str_replace( " itemid=\"$item_id\"", " itemid=\"$uri_e\"", $content );
373
			}
374
		}
375
	}
376
377
	// Reapply slashes.
378
	$content = addslashes( $content );
379
380
	return $content;
381
}
382
383
add_filter( 'content_save_pre', 'wl_replace_item_id_with_uri', 1, 1 );
384
385
require_once( 'wordlift_entity_functions.php' );
386
387
// add editor related methods.
388
require_once( 'wordlift_editor.php' );
389
390
// add the WordLift entity custom type.
391
require_once( 'wordlift_entity_type.php' );
392
393
// add callbacks on post save to notify data changes from wp to redlink triple store
394
require_once( 'wordlift_to_redlink_data_push_callbacks.php' );
395
396
require_once( 'modules/configuration/wordlift_configuration_settings.php' );
397
398
// Load modules
399
require_once( 'modules/analyzer/wordlift_analyzer.php' );
400
require_once( 'modules/linked_data/wordlift_linked_data.php' );
401
require_once( 'modules/prefixes/wordlift_prefixes.php' );
402
403
// Shortcodes
404
405
require_once( 'modules/geo_widget/wordlift_geo_widget.php' );
406
require_once( 'shortcodes/wordlift_shortcode_chord.php' );
407
require_once( 'shortcodes/wordlift_shortcode_geomap.php' );
408
require_once( 'shortcodes/wordlift_shortcode_field.php' );
409
require_once( 'shortcodes/wordlift_shortcode_faceted_search.php' );
410
require_once( 'shortcodes/wordlift_shortcode_navigator.php' );
411
412
require_once( 'widgets/wordlift_widget_geo.php' );
413
require_once( 'widgets/class-wordlift-chord-widget.php' );
414
require_once( 'widgets/wordlift_widget_timeline.php' );
415
416
require_once( 'wordlift_redlink.php' );
417
418
// Add admin functions.
419
// TODO: find a way to make 'admin' UI tests work.
420
//if ( is_admin() ) {
421
422
require_once( 'admin/wordlift_admin.php' );
423
require_once( 'admin/wordlift_admin_edit_post.php' );
424
require_once( 'admin/wordlift_admin_save_post.php' );
425
426
// add the entities meta box.
427
require_once( 'admin/wordlift_admin_meta_box_entities.php' );
428
429
// add the entity creation AJAX.
430
require_once( 'admin/wordlift_admin_ajax_related_posts.php' );
431
432
// Load the wl_chord TinyMCE button and configuration dialog.
433
require_once( 'admin/wordlift_admin_shortcodes.php' );
434
435
/**
436
 * The code that runs during plugin activation.
437
 * This action is documented in includes/class-wordlift-activator.php
438
 */
439
function activate_wordlift() {
440
441
	$log = Wordlift_Log_Service::get_logger( 'activate_wordlift' );
442
443
	$log->info( 'Activating WordLift...' );
444
445
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-activator.php';
446
	Wordlift_Activator::activate();
447
448
	/**
449
	 * Tell the {@link Wordlift_Http_Api} class that we're activating, to let it run activation tasks.
450
	 *
451
	 * @see https://github.com/insideout10/wordlift-plugin/issues/820 related issue.
452
	 * @since 3.19.2
453
	 */
454
	Wordlift_Http_Api::activate();
455
456
	// Ensure the post type is registered before flushing the rewrite rules.
457
	Wordlift_Entity_Post_Type_Service::get_instance()->register();
458
	flush_rewrite_rules();
459
460
}
461
462
/**
463
 * The code that runs during plugin deactivation.
464
 * This action is documented in includes/class-wordlift-deactivator.php
465
 */
466
function deactivate_wordlift() {
467
468
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-wordlift-deactivator.php';
469
	Wordlift_Deactivator::deactivate();
470
	Wordlift_Http_Api::deactivate();
471
	flush_rewrite_rules();
472
473
}
474
475
register_activation_hook( __FILE__, 'activate_wordlift' );
476
register_deactivation_hook( __FILE__, 'deactivate_wordlift' );
477
478
/**
479
 * The core plugin class that is used to define internationalization,
480
 * admin-specific hooks, and public-facing site hooks.
481
 */
482
require plugin_dir_path( __FILE__ ) . 'includes/class-wordlift.php';
483
484
/**
485
 * Begins execution of the plugin.
486
 *
487
 * Since everything within the plugin is registered via hooks,
488
 * then kicking off the plugin from this point in the file does
489
 * not affect the page life cycle.
490
 *
491
 * @since    1.0.0
492
 */
493
function run_wordlift() {
494
495
	$plugin = new Wordlift();
496
	$plugin->run();
497
498
}
499
500
run_wordlift();
501