Completed
Push — master ( 0c53d4...debbec )
by David
09:08 queued 10s
created

wordlift_admin.php ➔ wl_remove_text_annotations()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 8

Duplication

Lines 6
Ratio 25 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 6
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains miscellaneous admin-functions.
4
 *
5
 * @package Wordlift
6
 */
7
8
// Add the Admin menu.
9
require_once( 'wordlift_admin_menu.php' );
10
11
/**
12
 * Serialize an entity post.
13
 *
14
 * @param array $entity The entity post or the entity post id.
15
 *
16
 * @return array mixed The entity data array.
17
 */
18
function wl_serialize_entity( $entity ) {
19
20
	$entity = ( is_numeric( $entity ) ) ? get_post( $entity ) : $entity;
21
22
	// Bail if the entity doesn't exists.
23
	// In some cases we have `wl_topic` meta
24
	// pointing to an entity that has been deleted.
25
	if ( empty( $entity ) ) {
26
		return;
27
	}
28
29
	$type   = Wordlift_Entity_Type_Service::get_instance()->get( $entity->ID );
30
	$images = wl_get_image_urls( $entity->ID );
0 ignored issues
show
Deprecated Code introduced by
The function wl_get_image_urls() has been deprecated with message: use Wordlift_Storage_Factory::get_instance()->post_images()->get( $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...
31
32
	return array(
33
		'id'          => wl_get_entity_uri( $entity->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...
34
		'label'       => $entity->post_title,
35
		'description' => $entity->post_content,
36
		'sameAs'      => wl_schema_get_value( $entity->ID, 'sameAs' ),
37
		'mainType'    => str_replace( 'wl-', '', $type['css_class'] ),
38
		'types'       => wl_get_entity_rdf_types( $entity->ID ),
39
		'images'      => $images,
40
41
	);
42
}
43
44
/**
45
 * Removes empty text annotations from the post content.
46
 *
47
 * @since 1.0.0
48
 *
49
 * @param array $data The post data.
50
 *
51
 * @return array mixed The post data array.
52
 */
53
function wl_remove_text_annotations( $data ) {
54
55
	// Remove blank elements that can interfere with annotations removing
56
	// See https://github.com/insideout10/wordlift-plugin/issues/234
57
	// Just blank attributes without any attributes are cleaned up.
58
	$pattern = '/<(\w+)><\/\1>/im';
59
	// Remove the pattern while it is found (match nested annotations).
60 View Code Duplication
	while ( 1 === preg_match( $pattern, $data['post_content'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
		$data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count );
62
	}
63
	// Remove text annotations
64
	// <span class="textannotation" id="urn:enhancement-777cbed4-b131-00fb-54a4-ed9b26ae57ea">.
65
66
	// @see https://github.com/insideout10/wordlift-plugin/issues/771
67
	// Changing this:
68
	//	$pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?![^\\"]*\sdisambiguated)[^\\"]*\\\"[^>]*>([^<]+)<\/\1>/im';
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...
69
	// to:
70
	$pattern = '/<(\w+)[^>]*\sclass=\\\"textannotation(?![^\\"]*\sdisambiguated)[^\\"]*\\\"[^>]*>(.*?)<\/\1>/im';
71
	// Remove the pattern while it is found (match nested annotations).
72 View Code Duplication
	while ( 1 === preg_match( $pattern, $data['post_content'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
		$data['post_content'] = preg_replace( $pattern, '$2', $data['post_content'], -1, $count );
74
	}
75
	return $data;
76
}
77
78
add_filter( 'wp_insert_post_data', 'wl_remove_text_annotations', '98', 1 );
79
80
/**
81
 * Adds wl-metabox CSS class to a metabox.
82
 *
83
 * @since 3.2.0
84
 *
85
 * @param array $classes List of CSS classes already assigned to the metabox.
86
 *
87
 * @return array The updated list of CSS classes.
88
 */
89
function wl_admin_metaboxes_add_css_class( $classes = array() ) {
90
91
	return array_merge( $classes, array( 'wl-metabox' ) );
92
}
93