Completed
Push — develop ( 6a9ba1...12b36e )
by David
09:36
created

wordlift.php ➔ wl_write_log_handler()   A

Complexity

Conditions 6
Paths 17

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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