Completed
Push — master ( 9f3eac...f0c1b5 )
by David
02:49 queued 13s
created
src/admin/WL_Metabox/WL_Metabox_Field_coordinates.php 2 patches
Indentation   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -3,51 +3,51 @@  discard block
 block discarded – undo
3 3
 
4 4
 class WL_Metabox_Field_coordinates extends WL_Metabox_Field {
5 5
 
6
-	public function __construct( $args ) {
6
+    public function __construct( $args ) {
7 7
 
8
-		// Just set up the necessary info without calling the parent constructor.
9
-		// TODO: write a parent class for grouped properties
8
+        // Just set up the necessary info without calling the parent constructor.
9
+        // TODO: write a parent class for grouped properties
10 10
 
11
-		// we use 'coordinates' to namespace the Field in $_POST data.
12
-		// In  the DB the correct meta names will be used.
13
-		$this->meta_name = 'coordinates';
14
-	}
11
+        // we use 'coordinates' to namespace the Field in $_POST data.
12
+        // In  the DB the correct meta names will be used.
13
+        $this->meta_name = 'coordinates';
14
+    }
15 15
 
16
-	public function get_data() {
17
-		$entity_id  = get_the_ID();
18
-		$this->data = wl_get_coordinates( $entity_id );
19
-	}
16
+    public function get_data() {
17
+        $entity_id  = get_the_ID();
18
+        $this->data = wl_get_coordinates( $entity_id );
19
+    }
20 20
 
21
-	public function html() {
21
+    public function html() {
22 22
 
23
-		// Open main <div> for the Field
24
-		$html = $this->html_wrapper_open();
23
+        // Open main <div> for the Field
24
+        $html = $this->html_wrapper_open();
25 25
 
26
-		// Label
27
-		$html .= '<h3>coordinates</h3>';
26
+        // Label
27
+        $html .= '<h3>coordinates</h3>';
28 28
 
29
-		// print nonce
30
-		$html .= $this->html_nonce();
29
+        // print nonce
30
+        $html .= $this->html_nonce();
31 31
 
32
-		// Get coordinates
33
-		$data        = $this->data;
34
-		// TODO: We temporary use here 0,0 as default coordinates for the marker, but if no coordinates are given we
35
-		// want to use the current user location for the marker.
36
-		$coordinates = ( ! empty( $data['latitude'] ) && ! empty( $data['longitude'] ) ? sprintf( '[%f,%f]', $data['latitude'], $data['longitude'] ) : '[0,0]' );
37
-		$map_init    = '[0,0]' === $coordinates
38
-			? 'locate( {setView: true, maxZoom: 16} )'
39
-			: sprintf( "setView( [%f,%f], 9 )", $data['latitude'], $data['longitude'] );
32
+        // Get coordinates
33
+        $data        = $this->data;
34
+        // TODO: We temporary use here 0,0 as default coordinates for the marker, but if no coordinates are given we
35
+        // want to use the current user location for the marker.
36
+        $coordinates = ( ! empty( $data['latitude'] ) && ! empty( $data['longitude'] ) ? sprintf( '[%f,%f]', $data['latitude'], $data['longitude'] ) : '[0,0]' );
37
+        $map_init    = '[0,0]' === $coordinates
38
+            ? 'locate( {setView: true, maxZoom: 16} )'
39
+            : sprintf( "setView( [%f,%f], 9 )", $data['latitude'], $data['longitude'] );
40 40
 
41
-		// Print input fields
42
-		$html .= '<label for="wl_place_lat">' . __( 'Latitude', 'wordlift' ) . '</label>';
43
-		$html .= '<input type="text" id="wl_place_lat" name="wl_metaboxes[coordinates][]" value="' . $data['latitude'] . '" style="width:100%" />';
41
+        // Print input fields
42
+        $html .= '<label for="wl_place_lat">' . __( 'Latitude', 'wordlift' ) . '</label>';
43
+        $html .= '<input type="text" id="wl_place_lat" name="wl_metaboxes[coordinates][]" value="' . $data['latitude'] . '" style="width:100%" />';
44 44
 
45
-		$html .= '<label for="wl_place_lon">' . __( 'Longitude', 'wordlift' ) . '</label>';
46
-		$html .= '<input type="text" id="wl_place_lon" name="wl_metaboxes[coordinates][]" value="' . $data['longitude'] . '" style="width:100%" />';
45
+        $html .= '<label for="wl_place_lon">' . __( 'Longitude', 'wordlift' ) . '</label>';
46
+        $html .= '<input type="text" id="wl_place_lon" name="wl_metaboxes[coordinates][]" value="' . $data['longitude'] . '" style="width:100%" />';
47 47
 
48
-		// Show Leaflet map to pick coordinates
49
-		$element_id = uniqid( 'wl-geo-map-' );
50
-		$html .= <<<EOF
48
+        // Show Leaflet map to pick coordinates
49
+        $element_id = uniqid( 'wl-geo-map-' );
50
+        $html .= <<<EOF
51 51
 
52 52
 <div id="$element_id"></div>
53 53
 
@@ -79,44 +79,44 @@  discard block
 block discarded – undo
79 79
 EOF;
80 80
 
81 81
 
82
-		$html .= $this->html_wrapper_close();
82
+        $html .= $this->html_wrapper_close();
83 83
 
84
-		return $html;
85
-	}
84
+        return $html;
85
+    }
86 86
 
87
-	public function save_data( $coords ) {
87
+    public function save_data( $coords ) {
88 88
 
89
-		$this->sanitize_data( $coords );
89
+        $this->sanitize_data( $coords );
90 90
 
91
-		$entity_id = get_the_ID();
91
+        $entity_id = get_the_ID();
92 92
 
93
-		// Take away old values
94
-		delete_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LATITUDE );
95
-		delete_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LONGITUDE );
93
+        // Take away old values
94
+        delete_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LATITUDE );
95
+        delete_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LONGITUDE );
96 96
 
97
-		$latitude  = $this->data[0];
98
-		$longitude = $this->data[1];
97
+        $latitude  = $this->data[0];
98
+        $longitude = $this->data[1];
99 99
 
100
-		// insert new coordinate values
101
-		if ( ! empty( $latitude ) && ! empty( $longitude ) ) {
102
-			add_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $latitude, true );
103
-			add_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $longitude, true );
104
-		}
100
+        // insert new coordinate values
101
+        if ( ! empty( $latitude ) && ! empty( $longitude ) ) {
102
+            add_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $latitude, true );
103
+            add_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $longitude, true );
104
+        }
105 105
 
106
-	}
106
+    }
107 107
 
108
-	/**
109
-	 * Only accept float numbers
110
-	 */
111
-	public function sanitize_data_filter( $value ) {
108
+    /**
109
+     * Only accept float numbers
110
+     */
111
+    public function sanitize_data_filter( $value ) {
112 112
 
113
-		// DO NOT set latitude/longitude to 0/0 as default values. It's a specific place on the globe:
114
-		// "The zero/zero point of this system is located in the Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana."
115
-		if ( ! is_numeric( $value ) ) {
116
-			return '';
117
-		}
113
+        // DO NOT set latitude/longitude to 0/0 as default values. It's a specific place on the globe:
114
+        // "The zero/zero point of this system is located in the Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana."
115
+        if ( ! is_numeric( $value ) ) {
116
+            return '';
117
+        }
118 118
 
119
-		return $value;
120
-	}
119
+        return $value;
120
+    }
121 121
 }
122 122
 
Please login to merge, or discard this patch.
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -3,7 +3,7 @@  discard block
 block discarded – undo
3 3
 
4 4
 class WL_Metabox_Field_coordinates extends WL_Metabox_Field {
5 5
 
6
-	public function __construct( $args ) {
6
+	public function __construct($args) {
7 7
 
8 8
 		// Just set up the necessary info without calling the parent constructor.
9 9
 		// TODO: write a parent class for grouped properties
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
 
16 16
 	public function get_data() {
17 17
 		$entity_id  = get_the_ID();
18
-		$this->data = wl_get_coordinates( $entity_id );
18
+		$this->data = wl_get_coordinates($entity_id);
19 19
 	}
20 20
 
21 21
 	public function html() {
@@ -33,20 +33,20 @@  discard block
 block discarded – undo
33 33
 		$data        = $this->data;
34 34
 		// TODO: We temporary use here 0,0 as default coordinates for the marker, but if no coordinates are given we
35 35
 		// want to use the current user location for the marker.
36
-		$coordinates = ( ! empty( $data['latitude'] ) && ! empty( $data['longitude'] ) ? sprintf( '[%f,%f]', $data['latitude'], $data['longitude'] ) : '[0,0]' );
36
+		$coordinates = ( ! empty($data['latitude']) && ! empty($data['longitude']) ? sprintf('[%f,%f]', $data['latitude'], $data['longitude']) : '[0,0]');
37 37
 		$map_init    = '[0,0]' === $coordinates
38 38
 			? 'locate( {setView: true, maxZoom: 16} )'
39
-			: sprintf( "setView( [%f,%f], 9 )", $data['latitude'], $data['longitude'] );
39
+			: sprintf("setView( [%f,%f], 9 )", $data['latitude'], $data['longitude']);
40 40
 
41 41
 		// Print input fields
42
-		$html .= '<label for="wl_place_lat">' . __( 'Latitude', 'wordlift' ) . '</label>';
43
-		$html .= '<input type="text" id="wl_place_lat" name="wl_metaboxes[coordinates][]" value="' . $data['latitude'] . '" style="width:100%" />';
42
+		$html .= '<label for="wl_place_lat">'.__('Latitude', 'wordlift').'</label>';
43
+		$html .= '<input type="text" id="wl_place_lat" name="wl_metaboxes[coordinates][]" value="'.$data['latitude'].'" style="width:100%" />';
44 44
 
45
-		$html .= '<label for="wl_place_lon">' . __( 'Longitude', 'wordlift' ) . '</label>';
46
-		$html .= '<input type="text" id="wl_place_lon" name="wl_metaboxes[coordinates][]" value="' . $data['longitude'] . '" style="width:100%" />';
45
+		$html .= '<label for="wl_place_lon">'.__('Longitude', 'wordlift').'</label>';
46
+		$html .= '<input type="text" id="wl_place_lon" name="wl_metaboxes[coordinates][]" value="'.$data['longitude'].'" style="width:100%" />';
47 47
 
48 48
 		// Show Leaflet map to pick coordinates
49
-		$element_id = uniqid( 'wl-geo-map-' );
49
+		$element_id = uniqid('wl-geo-map-');
50 50
 		$html .= <<<EOF
51 51
 
52 52
 <div id="$element_id"></div>
@@ -84,23 +84,23 @@  discard block
 block discarded – undo
84 84
 		return $html;
85 85
 	}
86 86
 
87
-	public function save_data( $coords ) {
87
+	public function save_data($coords) {
88 88
 
89
-		$this->sanitize_data( $coords );
89
+		$this->sanitize_data($coords);
90 90
 
91 91
 		$entity_id = get_the_ID();
92 92
 
93 93
 		// Take away old values
94
-		delete_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LATITUDE );
95
-		delete_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LONGITUDE );
94
+		delete_post_meta($entity_id, Wordlift_Schema_Service::FIELD_GEO_LATITUDE);
95
+		delete_post_meta($entity_id, Wordlift_Schema_Service::FIELD_GEO_LONGITUDE);
96 96
 
97 97
 		$latitude  = $this->data[0];
98 98
 		$longitude = $this->data[1];
99 99
 
100 100
 		// insert new coordinate values
101
-		if ( ! empty( $latitude ) && ! empty( $longitude ) ) {
102
-			add_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $latitude, true );
103
-			add_post_meta( $entity_id, Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $longitude, true );
101
+		if ( ! empty($latitude) && ! empty($longitude)) {
102
+			add_post_meta($entity_id, Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $latitude, true);
103
+			add_post_meta($entity_id, Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $longitude, true);
104 104
 		}
105 105
 
106 106
 	}
@@ -108,11 +108,11 @@  discard block
 block discarded – undo
108 108
 	/**
109 109
 	 * Only accept float numbers
110 110
 	 */
111
-	public function sanitize_data_filter( $value ) {
111
+	public function sanitize_data_filter($value) {
112 112
 
113 113
 		// DO NOT set latitude/longitude to 0/0 as default values. It's a specific place on the globe:
114 114
 		// "The zero/zero point of this system is located in the Gulf of Guinea about 625 km (390 mi) south of Tema, Ghana."
115
-		if ( ! is_numeric( $value ) ) {
115
+		if ( ! is_numeric($value)) {
116 116
 			return '';
117 117
 		}
118 118
 
Please login to merge, or discard this patch.
src/public/class-wordlift-geomap-shortcode.php 2 patches
Indentation   +87 added lines, -87 removed lines patch added patch discarded remove patch
@@ -18,98 +18,98 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Geomap_Shortcode extends Wordlift_Shortcode {
20 20
 
21
-	const SHORTCODE = 'wl_geomap';
22
-
23
-	/**
24
-	 * Create a {@link Wordlift_Geomap_Shortcode} instance.
25
-	 *
26
-	 * @since 3.5.4
27
-	 */
28
-	public function __construct() {
29
-		parent::__construct();
30
-
31
-		// Hook to the `amp_post_template_css` to hide ourselves when in AMP
32
-		// rendering.
33
-		add_action( 'amp_post_template_css', array( $this, 'amp_post_template_css', ) );
34
-
35
-	}
36
-
37
-	/**
38
-	 * Render the shortcode.
39
-	 *
40
-	 * @param array $atts An array of shortcode attributes as set by the editor.
41
-	 *
42
-	 * @return string The output html code.
43
-	 * @since 3.5.4
44
-	 *
45
-	 */
46
-	public function render( $atts ) {
47
-
48
-		// Extract attributes and set default values.
49
-		$geomap_atts = shortcode_atts( array(
50
-			'width'  => '100%',
51
-			'height' => '300px',
52
-			'global' => false,
53
-		), $atts );
54
-
55
-		// Get id of the post
56
-		$post_id = get_the_ID();
57
-
58
-		if ( $geomap_atts['global'] || is_null( $post_id ) ) {
59
-			// Global geomap
60
-			$geomap_id = 'wl_geomap_global';
61
-			$post_id   = null;
62
-		} else {
63
-			// Post-specific geomap
64
-			$geomap_id = 'wl_geomap_' . $post_id;
65
-		}
66
-
67
-		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
68
-		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true );
69
-
70
-		// Use the registered style which define an optional dependency to font-awesome.
71
-		//
72
-		// @see https://github.com/insideout10/wordlift-plugin/issues/699
73
-		//		wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
74
-		wp_enqueue_style( 'wordlift-ui' );
75
-
76
-		$this->enqueue_scripts();
77
-
78
-		wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array(
79
-			'ajax_url' => admin_url( 'admin-ajax.php' ),    // Global param
80
-			'action'   => 'wl_geomap'            // Global param
81
-		) );
82
-
83
-		// Escaping atts.
84
-		$esc_id      = esc_attr( $geomap_id );
85
-		$esc_width   = esc_attr( $geomap_atts['width'] );
86
-		$esc_height  = esc_attr( $geomap_atts['height'] );
87
-		$esc_post_id = esc_attr( $post_id );
88
-
89
-		// Return HTML template.
90
-		return <<<EOF
21
+    const SHORTCODE = 'wl_geomap';
22
+
23
+    /**
24
+     * Create a {@link Wordlift_Geomap_Shortcode} instance.
25
+     *
26
+     * @since 3.5.4
27
+     */
28
+    public function __construct() {
29
+        parent::__construct();
30
+
31
+        // Hook to the `amp_post_template_css` to hide ourselves when in AMP
32
+        // rendering.
33
+        add_action( 'amp_post_template_css', array( $this, 'amp_post_template_css', ) );
34
+
35
+    }
36
+
37
+    /**
38
+     * Render the shortcode.
39
+     *
40
+     * @param array $atts An array of shortcode attributes as set by the editor.
41
+     *
42
+     * @return string The output html code.
43
+     * @since 3.5.4
44
+     *
45
+     */
46
+    public function render( $atts ) {
47
+
48
+        // Extract attributes and set default values.
49
+        $geomap_atts = shortcode_atts( array(
50
+            'width'  => '100%',
51
+            'height' => '300px',
52
+            'global' => false,
53
+        ), $atts );
54
+
55
+        // Get id of the post
56
+        $post_id = get_the_ID();
57
+
58
+        if ( $geomap_atts['global'] || is_null( $post_id ) ) {
59
+            // Global geomap
60
+            $geomap_id = 'wl_geomap_global';
61
+            $post_id   = null;
62
+        } else {
63
+            // Post-specific geomap
64
+            $geomap_id = 'wl_geomap_' . $post_id;
65
+        }
66
+
67
+        wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
68
+        wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true );
69
+
70
+        // Use the registered style which define an optional dependency to font-awesome.
71
+        //
72
+        // @see https://github.com/insideout10/wordlift-plugin/issues/699
73
+        //		wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
74
+        wp_enqueue_style( 'wordlift-ui' );
75
+
76
+        $this->enqueue_scripts();
77
+
78
+        wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array(
79
+            'ajax_url' => admin_url( 'admin-ajax.php' ),    // Global param
80
+            'action'   => 'wl_geomap'            // Global param
81
+        ) );
82
+
83
+        // Escaping atts.
84
+        $esc_id      = esc_attr( $geomap_id );
85
+        $esc_width   = esc_attr( $geomap_atts['width'] );
86
+        $esc_height  = esc_attr( $geomap_atts['height'] );
87
+        $esc_post_id = esc_attr( $post_id );
88
+
89
+        // Return HTML template.
90
+        return <<<EOF
91 91
 <div class="wl-geomap"  id="$esc_id" data-post-id="$esc_post_id"
92 92
 	style="width:$esc_width; height:$esc_height; background-color: gray;">
93 93
 </div>
94 94
 EOF;
95
-	}
96
-
97
-	/**
98
-	 * Customize the CSS when in AMP.
99
-	 *
100
-	 * See https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-css
101
-	 *
102
-	 * @param object $amp_template The template.
103
-	 *
104
-	 * @since 3.13.0
105
-	 *
106
-	 */
107
-	public function amp_post_template_css( $amp_template ) {
108
-
109
-		// Hide the `wl-geomap` when in AMP.
110
-		?>
95
+    }
96
+
97
+    /**
98
+     * Customize the CSS when in AMP.
99
+     *
100
+     * See https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-css
101
+     *
102
+     * @param object $amp_template The template.
103
+     *
104
+     * @since 3.13.0
105
+     *
106
+     */
107
+    public function amp_post_template_css( $amp_template ) {
108
+
109
+        // Hide the `wl-geomap` when in AMP.
110
+        ?>
111 111
         .wl-geomap { display: none; }
112 112
 		<?php
113
-	}
113
+    }
114 114
 
115 115
 }
Please login to merge, or discard this patch.
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -30,7 +30,7 @@  discard block
 block discarded – undo
30 30
 
31 31
 		// Hook to the `amp_post_template_css` to hide ourselves when in AMP
32 32
 		// rendering.
33
-		add_action( 'amp_post_template_css', array( $this, 'amp_post_template_css', ) );
33
+		add_action('amp_post_template_css', array($this, 'amp_post_template_css',));
34 34
 
35 35
 	}
36 36
 
@@ -43,48 +43,48 @@  discard block
 block discarded – undo
43 43
 	 * @since 3.5.4
44 44
 	 *
45 45
 	 */
46
-	public function render( $atts ) {
46
+	public function render($atts) {
47 47
 
48 48
 		// Extract attributes and set default values.
49
-		$geomap_atts = shortcode_atts( array(
49
+		$geomap_atts = shortcode_atts(array(
50 50
 			'width'  => '100%',
51 51
 			'height' => '300px',
52 52
 			'global' => false,
53
-		), $atts );
53
+		), $atts);
54 54
 
55 55
 		// Get id of the post
56 56
 		$post_id = get_the_ID();
57 57
 
58
-		if ( $geomap_atts['global'] || is_null( $post_id ) ) {
58
+		if ($geomap_atts['global'] || is_null($post_id)) {
59 59
 			// Global geomap
60 60
 			$geomap_id = 'wl_geomap_global';
61 61
 			$post_id   = null;
62 62
 		} else {
63 63
 			// Post-specific geomap
64
-			$geomap_id = 'wl_geomap_' . $post_id;
64
+			$geomap_id = 'wl_geomap_'.$post_id;
65 65
 		}
66 66
 
67
-		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
68
-		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true );
67
+		wp_enqueue_style('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0');
68
+		wp_enqueue_script('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true);
69 69
 
70 70
 		// Use the registered style which define an optional dependency to font-awesome.
71 71
 		//
72 72
 		// @see https://github.com/insideout10/wordlift-plugin/issues/699
73 73
 		//		wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
74
-		wp_enqueue_style( 'wordlift-ui' );
74
+		wp_enqueue_style('wordlift-ui');
75 75
 
76 76
 		$this->enqueue_scripts();
77 77
 
78
-		wp_localize_script( 'wordlift-ui', 'wl_geomap_params', array(
79
-			'ajax_url' => admin_url( 'admin-ajax.php' ),    // Global param
78
+		wp_localize_script('wordlift-ui', 'wl_geomap_params', array(
79
+			'ajax_url' => admin_url('admin-ajax.php'), // Global param
80 80
 			'action'   => 'wl_geomap'            // Global param
81
-		) );
81
+		));
82 82
 
83 83
 		// Escaping atts.
84
-		$esc_id      = esc_attr( $geomap_id );
85
-		$esc_width   = esc_attr( $geomap_atts['width'] );
86
-		$esc_height  = esc_attr( $geomap_atts['height'] );
87
-		$esc_post_id = esc_attr( $post_id );
84
+		$esc_id      = esc_attr($geomap_id);
85
+		$esc_width   = esc_attr($geomap_atts['width']);
86
+		$esc_height  = esc_attr($geomap_atts['height']);
87
+		$esc_post_id = esc_attr($post_id);
88 88
 
89 89
 		// Return HTML template.
90 90
 		return <<<EOF
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
 	 * @since 3.13.0
105 105
 	 *
106 106
 	 */
107
-	public function amp_post_template_css( $amp_template ) {
107
+	public function amp_post_template_css($amp_template) {
108 108
 
109 109
 		// Hide the `wl-geomap` when in AMP.
110 110
 		?>
Please login to merge, or discard this patch.
src/shortcodes/wordlift_shortcode_geomap.php 2 patches
Indentation   +103 added lines, -103 removed lines patch added patch discarded remove patch
@@ -11,42 +11,42 @@  discard block
 block discarded – undo
11 11
  * Print both global or post related places in json. It's executed via Ajax
12 12
  */
13 13
 function wl_shortcode_geomap_ajax() {
14
-	// Get the post Id.
15
-	$post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
14
+    // Get the post Id.
15
+    $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
16 16
 
17
-	$map_data = ( is_numeric( $post_id )
18
-		? wl_shortcode_geomap_ajax_single_post( $post_id )
19
-		: wl_shortcode_geomap_ajax_all_posts() );
17
+    $map_data = ( is_numeric( $post_id )
18
+        ? wl_shortcode_geomap_ajax_single_post( $post_id )
19
+        : wl_shortcode_geomap_ajax_all_posts() );
20 20
 
21
-	wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
21
+    wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
22 22
 
23 23
 }
24 24
 
25 25
 function wl_shortcode_geomap_ajax_all_posts() {
26
-	global $wpdb;
27
-
28
-	return $wpdb->get_results( $wpdb->prepare(
29
-		"SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude"
30
-		. " FROM {$wpdb->posts} p1 "
31
-		. " INNER JOIN {$wpdb->prefix}wl_relation_instances ri"
32
-		. "  ON ri.object_id = p1.ID AND ri.predicate = %s"
33
-		. " INNER JOIN {$wpdb->postmeta} pm1"
34
-		. "  ON pm1.post_id = p1.ID AND pm1.meta_key = %s AND '0' != pm1.meta_value"
35
-		. " INNER JOIN {$wpdb->postmeta} pm2"
36
-		. "  ON pm2.post_id = p1.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value"
37
-		. " WHERE p1.post_status = %s",
38
-		'where',
39
-		'wl_geo_latitude',
40
-		'wl_geo_longitude',
41
-		'publish'
42
-	) );
26
+    global $wpdb;
27
+
28
+    return $wpdb->get_results( $wpdb->prepare(
29
+        "SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude"
30
+        . " FROM {$wpdb->posts} p1 "
31
+        . " INNER JOIN {$wpdb->prefix}wl_relation_instances ri"
32
+        . "  ON ri.object_id = p1.ID AND ri.predicate = %s"
33
+        . " INNER JOIN {$wpdb->postmeta} pm1"
34
+        . "  ON pm1.post_id = p1.ID AND pm1.meta_key = %s AND '0' != pm1.meta_value"
35
+        . " INNER JOIN {$wpdb->postmeta} pm2"
36
+        . "  ON pm2.post_id = p1.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value"
37
+        . " WHERE p1.post_status = %s",
38
+        'where',
39
+        'wl_geo_latitude',
40
+        'wl_geo_longitude',
41
+        'publish'
42
+    ) );
43 43
 }
44 44
 
45 45
 function wl_shortcode_geomap_ajax_single_post( $post_id ) {
46
-	global $wpdb;
46
+    global $wpdb;
47 47
 
48
-	return $wpdb->get_results( $wpdb->prepare(
49
-		"
48
+    return $wpdb->get_results( $wpdb->prepare(
49
+        "
50 50
 		SELECT p2.ID, p2.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude
51 51
 		 FROM {$wpdb->prefix}wl_relation_instances ri
52 52
 		 INNER JOIN {$wpdb->posts} p2
@@ -65,69 +65,69 @@  discard block
 block discarded – undo
65 65
 		  ON pm2.post_id = p.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value
66 66
 		 WHERE p.ID = %s
67 67
 		",
68
-		'publish',
69
-		'wl_geo_latitude',
70
-		'wl_geo_longitude',
71
-		$post_id,
72
-		'where',
73
-		// UNION
74
-		'wl_geo_latitude',
75
-		'wl_geo_longitude',
76
-		$post_id
77
-	) );
68
+        'publish',
69
+        'wl_geo_latitude',
70
+        'wl_geo_longitude',
71
+        $post_id,
72
+        'where',
73
+        // UNION
74
+        'wl_geo_latitude',
75
+        'wl_geo_longitude',
76
+        $post_id
77
+    ) );
78 78
 }
79 79
 
80 80
 function wl_shortcode_geomap_get_subjects( $post_id, $exclude_post_id ) {
81 81
 
82
-	global $wpdb;
83
-
84
-	return $wpdb->get_results( $wpdb->prepare(
85
-		"SELECT p.ID, p.post_title"
86
-		. " FROM {$wpdb->prefix}wl_relation_instances ri" .
87
-		" INNER JOIN {$wpdb->posts} p" .
88
-		"  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" .
89
-		" WHERE ri.object_id = %d AND ri.predicate = %s",
90
-		'publish',
91
-		$exclude_post_id,
92
-		$post_id,
93
-		'where'
94
-	) );
82
+    global $wpdb;
83
+
84
+    return $wpdb->get_results( $wpdb->prepare(
85
+        "SELECT p.ID, p.post_title"
86
+        . " FROM {$wpdb->prefix}wl_relation_instances ri" .
87
+        " INNER JOIN {$wpdb->posts} p" .
88
+        "  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" .
89
+        " WHERE ri.object_id = %d AND ri.predicate = %s",
90
+        'publish',
91
+        $exclude_post_id,
92
+        $post_id,
93
+        'where'
94
+    ) );
95 95
 }
96 96
 
97 97
 function wl_shortcode_geomap_format_results( $results, $post_id = null ) {
98 98
 
99
-	$boundaries = array();
100
-	$features   = array_map( function ( $item ) use ( &$boundaries, $post_id ) {
99
+    $boundaries = array();
100
+    $features   = array_map( function ( $item ) use ( &$boundaries, $post_id ) {
101 101
 
102
-		$thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
103
-		$thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
102
+        $thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
103
+        $thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
104 104
 
105
-		// Related posts.
106
-		$subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
107
-		$subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) {
105
+        // Related posts.
106
+        $subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
107
+        $subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) {
108 108
 
109
-			$permalink = get_permalink( $subject->ID );
109
+            $permalink = get_permalink( $subject->ID );
110 110
 
111
-			return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
112
-		}, '' );
113
-		$subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
111
+            return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
112
+        }, '' );
113
+        $subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
114 114
 
115
-		$popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html );
116
-		$latitude      = floatval( $item->latitude );
117
-		$longitude     = floatval( $item->longitude );
118
-		$coordinates   = array( $latitude, $longitude, );
119
-		$geometry      = array( 'type' => 'Point', 'coordinates' => $coordinates, );
115
+        $popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html );
116
+        $latitude      = floatval( $item->latitude );
117
+        $longitude     = floatval( $item->longitude );
118
+        $coordinates   = array( $latitude, $longitude, );
119
+        $geometry      = array( 'type' => 'Point', 'coordinates' => $coordinates, );
120 120
 
121
-		$boundaries[] = array( $longitude, $latitude, );
121
+        $boundaries[] = array( $longitude, $latitude, );
122 122
 
123
-		return array(
124
-			'type'       => 'Feature',
125
-			'properties' => array( 'popupContent' => $popup_content ),
126
-			'geometry'   => $geometry,
127
-		);
128
-	}, $results );
123
+        return array(
124
+            'type'       => 'Feature',
125
+            'properties' => array( 'popupContent' => $popup_content ),
126
+            'geometry'   => $geometry,
127
+        );
128
+    }, $results );
129 129
 
130
-	return array( 'features' => $features, 'boundaries' => $boundaries, );
130
+    return array( 'features' => $features, 'boundaries' => $boundaries, );
131 131
 }
132 132
 
133 133
 add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' );
@@ -138,34 +138,34 @@  discard block
 block discarded – undo
138 138
  */
139 139
 add_action( 'init', function () {
140 140
 
141
-	// Bail out if the `register_block_type` function isn't available.
142
-	if ( ! function_exists( 'register_block_type' ) ) {
143
-		return;
144
-	}
145
-
146
-	register_block_type( 'wordlift/geomap', array(
147
-		'editor_script'   => 'wl-block-editor',
148
-		'render_callback' => function ( $attributes ) {
149
-			$attr_code = '';
150
-			foreach ( $attributes as $key => $value ) {
151
-				$attr_code .= $key . '="' . $value . '" ';
152
-			}
153
-
154
-			return '[wl_geomap ' . $attr_code . ']';
155
-		},
156
-		'attributes'      => array(
157
-			'width'  => array(
158
-				'type'    => 'string',
159
-				'default' => '100%'
160
-			),
161
-			'height' => array(
162
-				'type'    => 'string',
163
-				'default' => '300px'
164
-			),
165
-			'global' => array(
166
-				'type'    => 'bool',
167
-				'default' => false
168
-			),
169
-		)
170
-	) );
141
+    // Bail out if the `register_block_type` function isn't available.
142
+    if ( ! function_exists( 'register_block_type' ) ) {
143
+        return;
144
+    }
145
+
146
+    register_block_type( 'wordlift/geomap', array(
147
+        'editor_script'   => 'wl-block-editor',
148
+        'render_callback' => function ( $attributes ) {
149
+            $attr_code = '';
150
+            foreach ( $attributes as $key => $value ) {
151
+                $attr_code .= $key . '="' . $value . '" ';
152
+            }
153
+
154
+            return '[wl_geomap ' . $attr_code . ']';
155
+        },
156
+        'attributes'      => array(
157
+            'width'  => array(
158
+                'type'    => 'string',
159
+                'default' => '100%'
160
+            ),
161
+            'height' => array(
162
+                'type'    => 'string',
163
+                'default' => '300px'
164
+            ),
165
+            'global' => array(
166
+                'type'    => 'bool',
167
+                'default' => false
168
+            ),
169
+        )
170
+    ) );
171 171
 } );
Please login to merge, or discard this patch.
Spacing   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -12,20 +12,20 @@  discard block
 block discarded – undo
12 12
  */
13 13
 function wl_shortcode_geomap_ajax() {
14 14
 	// Get the post Id.
15
-	$post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null );
15
+	$post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : null);
16 16
 
17
-	$map_data = ( is_numeric( $post_id )
18
-		? wl_shortcode_geomap_ajax_single_post( $post_id )
19
-		: wl_shortcode_geomap_ajax_all_posts() );
17
+	$map_data = (is_numeric($post_id)
18
+		? wl_shortcode_geomap_ajax_single_post($post_id)
19
+		: wl_shortcode_geomap_ajax_all_posts());
20 20
 
21
-	wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
21
+	wl_core_send_json(wl_shortcode_geomap_format_results($map_data, $post_id));
22 22
 
23 23
 }
24 24
 
25 25
 function wl_shortcode_geomap_ajax_all_posts() {
26 26
 	global $wpdb;
27 27
 
28
-	return $wpdb->get_results( $wpdb->prepare(
28
+	return $wpdb->get_results($wpdb->prepare(
29 29
 		"SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude"
30 30
 		. " FROM {$wpdb->posts} p1 "
31 31
 		. " INNER JOIN {$wpdb->prefix}wl_relation_instances ri"
@@ -39,13 +39,13 @@  discard block
 block discarded – undo
39 39
 		'wl_geo_latitude',
40 40
 		'wl_geo_longitude',
41 41
 		'publish'
42
-	) );
42
+	));
43 43
 }
44 44
 
45
-function wl_shortcode_geomap_ajax_single_post( $post_id ) {
45
+function wl_shortcode_geomap_ajax_single_post($post_id) {
46 46
 	global $wpdb;
47 47
 
48
-	return $wpdb->get_results( $wpdb->prepare(
48
+	return $wpdb->get_results($wpdb->prepare(
49 49
 		"
50 50
 		SELECT p2.ID, p2.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude
51 51
 		 FROM {$wpdb->prefix}wl_relation_instances ri
@@ -74,84 +74,84 @@  discard block
 block discarded – undo
74 74
 		'wl_geo_latitude',
75 75
 		'wl_geo_longitude',
76 76
 		$post_id
77
-	) );
77
+	));
78 78
 }
79 79
 
80
-function wl_shortcode_geomap_get_subjects( $post_id, $exclude_post_id ) {
80
+function wl_shortcode_geomap_get_subjects($post_id, $exclude_post_id) {
81 81
 
82 82
 	global $wpdb;
83 83
 
84
-	return $wpdb->get_results( $wpdb->prepare(
84
+	return $wpdb->get_results($wpdb->prepare(
85 85
 		"SELECT p.ID, p.post_title"
86
-		. " FROM {$wpdb->prefix}wl_relation_instances ri" .
87
-		" INNER JOIN {$wpdb->posts} p" .
88
-		"  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d" .
86
+		. " FROM {$wpdb->prefix}wl_relation_instances ri".
87
+		" INNER JOIN {$wpdb->posts} p".
88
+		"  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d".
89 89
 		" WHERE ri.object_id = %d AND ri.predicate = %s",
90 90
 		'publish',
91 91
 		$exclude_post_id,
92 92
 		$post_id,
93 93
 		'where'
94
-	) );
94
+	));
95 95
 }
96 96
 
97
-function wl_shortcode_geomap_format_results( $results, $post_id = null ) {
97
+function wl_shortcode_geomap_format_results($results, $post_id = null) {
98 98
 
99 99
 	$boundaries = array();
100
-	$features   = array_map( function ( $item ) use ( &$boundaries, $post_id ) {
100
+	$features   = array_map(function($item) use (&$boundaries, $post_id) {
101 101
 
102
-		$thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
103
-		$thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
102
+		$thumbnail_url  = get_the_post_thumbnail_url($item->ID);
103
+		$thumbnail_html = ($thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '');
104 104
 
105 105
 		// Related posts.
106
-		$subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
107
-		$subjects_inner_html = array_reduce( $subjects, function ( $carry, $subject ) {
106
+		$subjects            = wl_shortcode_geomap_get_subjects($item->ID, $post_id);
107
+		$subjects_inner_html = array_reduce($subjects, function($carry, $subject) {
108 108
 
109
-			$permalink = get_permalink( $subject->ID );
109
+			$permalink = get_permalink($subject->ID);
110 110
 
111
-			return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
112
-		}, '' );
113
-		$subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
111
+			return $carry.sprintf('<li><a href="%s">%s</a></li>', $permalink, esc_html($subject->post_title));
112
+		}, '');
113
+		$subjects_html = ( ! empty($subjects_inner_html) ? '<ul>'.$subjects_inner_html.'</ul>' : '');
114 114
 
115
-		$popup_content = sprintf( '<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink( $item->ID ), $thumbnail_html, esc_html( $item->post_title ), $subjects_html );
116
-		$latitude      = floatval( $item->latitude );
117
-		$longitude     = floatval( $item->longitude );
118
-		$coordinates   = array( $latitude, $longitude, );
119
-		$geometry      = array( 'type' => 'Point', 'coordinates' => $coordinates, );
115
+		$popup_content = sprintf('<a href="%s"><h6>%s</h6>%s</a>%s', get_permalink($item->ID), $thumbnail_html, esc_html($item->post_title), $subjects_html);
116
+		$latitude      = floatval($item->latitude);
117
+		$longitude     = floatval($item->longitude);
118
+		$coordinates   = array($latitude, $longitude,);
119
+		$geometry      = array('type' => 'Point', 'coordinates' => $coordinates,);
120 120
 
121
-		$boundaries[] = array( $longitude, $latitude, );
121
+		$boundaries[] = array($longitude, $latitude,);
122 122
 
123 123
 		return array(
124 124
 			'type'       => 'Feature',
125
-			'properties' => array( 'popupContent' => $popup_content ),
125
+			'properties' => array('popupContent' => $popup_content),
126 126
 			'geometry'   => $geometry,
127 127
 		);
128
-	}, $results );
128
+	}, $results);
129 129
 
130
-	return array( 'features' => $features, 'boundaries' => $boundaries, );
130
+	return array('features' => $features, 'boundaries' => $boundaries,);
131 131
 }
132 132
 
133
-add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' );
134
-add_action( 'wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax' );
133
+add_action('wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax');
134
+add_action('wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax');
135 135
 
136 136
 /**
137 137
  * register_block_type for Gutenberg blocks
138 138
  */
139
-add_action( 'init', function () {
139
+add_action('init', function() {
140 140
 
141 141
 	// Bail out if the `register_block_type` function isn't available.
142
-	if ( ! function_exists( 'register_block_type' ) ) {
142
+	if ( ! function_exists('register_block_type')) {
143 143
 		return;
144 144
 	}
145 145
 
146
-	register_block_type( 'wordlift/geomap', array(
146
+	register_block_type('wordlift/geomap', array(
147 147
 		'editor_script'   => 'wl-block-editor',
148
-		'render_callback' => function ( $attributes ) {
148
+		'render_callback' => function($attributes) {
149 149
 			$attr_code = '';
150
-			foreach ( $attributes as $key => $value ) {
151
-				$attr_code .= $key . '="' . $value . '" ';
150
+			foreach ($attributes as $key => $value) {
151
+				$attr_code .= $key.'="'.$value.'" ';
152 152
 			}
153 153
 
154
-			return '[wl_geomap ' . $attr_code . ']';
154
+			return '[wl_geomap '.$attr_code.']';
155 155
 		},
156 156
 		'attributes'      => array(
157 157
 			'width'  => array(
@@ -167,5 +167,5 @@  discard block
 block discarded – undo
167 167
 				'default' => false
168 168
 			),
169 169
 		)
170
-	) );
170
+	));
171 171
 } );
Please login to merge, or discard this patch.
src/modules/geo_widget/wordlift_geo_widget.php 2 patches
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -9,32 +9,32 @@  discard block
 block discarded – undo
9 9
 
10 10
 function wordlift_geo_widget_shortcode( $atts, $content = null ) {
11 11
 
12
-	// Extract attributes and set default values.
13
-	$params = shortcode_atts( array(
14
-		'width'     => '100%',
15
-		'height'    => '300px',
16
-		'latitude'  => 0.0,
17
-		'longitude' => 0.0,
18
-		'zoom'      => 5,
19
-
20
-	), $atts );
21
-
22
-	// Add leaflet css and library.
23
-	wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
24
-	wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true );
25
-
26
-	ob_start(); // Collect the buffer.
27
-	wordlift_geo_widget_html(
28
-		$params['width'],
29
-		$params['height'],
30
-		$params['latitude'],
31
-		$params['longitude'],
32
-		$params['zoom'],
33
-		$content
34
-	);
35
-
36
-	// Return the accumulated buffer.
37
-	return ob_get_clean();
12
+    // Extract attributes and set default values.
13
+    $params = shortcode_atts( array(
14
+        'width'     => '100%',
15
+        'height'    => '300px',
16
+        'latitude'  => 0.0,
17
+        'longitude' => 0.0,
18
+        'zoom'      => 5,
19
+
20
+    ), $atts );
21
+
22
+    // Add leaflet css and library.
23
+    wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
24
+    wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true );
25
+
26
+    ob_start(); // Collect the buffer.
27
+    wordlift_geo_widget_html(
28
+        $params['width'],
29
+        $params['height'],
30
+        $params['latitude'],
31
+        $params['longitude'],
32
+        $params['zoom'],
33
+        $content
34
+    );
35
+
36
+    // Return the accumulated buffer.
37
+    return ob_get_clean();
38 38
 
39 39
 }
40 40
 
@@ -42,24 +42,24 @@  discard block
 block discarded – undo
42 42
 
43 43
 function wl_geo_widget_layer_shortcode( $atts ) {
44 44
 
45
-	// Extract attributes and set default values.
46
-	$params = shortcode_atts( array(
47
-		'name'  => '',
48
-		'label' => '',
49
-	), $atts );
45
+    // Extract attributes and set default values.
46
+    $params = shortcode_atts( array(
47
+        'name'  => '',
48
+        'label' => '',
49
+    ), $atts );
50 50
 
51
-	// Return if a SPARQL Query name hasn't been provided.
52
-	if ( empty( $params['name'] ) ) {
53
-		return;
54
-	}
51
+    // Return if a SPARQL Query name hasn't been provided.
52
+    if ( empty( $params['name'] ) ) {
53
+        return;
54
+    }
55 55
 
56
-	// Set the layer label.
57
-	$label_j = json_encode( empty( $params['label'] ) ? $params['name'] : $params['label'] );
56
+    // Set the layer label.
57
+    $label_j = json_encode( empty( $params['label'] ) ? $params['name'] : $params['label'] );
58 58
 
59
-	// Define the AJAX Url.
60
-	$ajax_url = admin_url( 'admin-ajax.php?action=wl_sparql&format=geojson&slug=' . urlencode( $params['name'] ) );
59
+    // Define the AJAX Url.
60
+    $ajax_url = admin_url( 'admin-ajax.php?action=wl_sparql&format=geojson&slug=' . urlencode( $params['name'] ) );
61 61
 
62
-	echo <<<EOF
62
+    echo <<<EOF
63 63
 
64 64
         $.ajax( '$ajax_url', {
65 65
             success: function( data ) {
@@ -90,21 +90,21 @@  discard block
 block discarded – undo
90 90
 
91 91
 function wl_geo_widget_marker_shortcode( $atts ) {
92 92
 
93
-	// Extract attributes and set default values.
94
-	$params = shortcode_atts( array(
95
-		'latitude'  => null,
96
-		'longitude' => null,
97
-	), $atts );
93
+    // Extract attributes and set default values.
94
+    $params = shortcode_atts( array(
95
+        'latitude'  => null,
96
+        'longitude' => null,
97
+    ), $atts );
98 98
 
99
-	// Return if either latitude or longitude haven't been provided.
100
-	if ( empty( $params['latitude'] ) || empty( $params['longitude'] ) ) {
101
-		return;
102
-	}
99
+    // Return if either latitude or longitude haven't been provided.
100
+    if ( empty( $params['latitude'] ) || empty( $params['longitude'] ) ) {
101
+        return;
102
+    }
103 103
 
104
-	$latitude_j  = json_encode( $params['latitude'] );
105
-	$longitude_j = json_encode( $params['longitude'] );
104
+    $latitude_j  = json_encode( $params['latitude'] );
105
+    $longitude_j = json_encode( $params['longitude'] );
106 106
 
107
-	echo <<<EOF
107
+    echo <<<EOF
108 108
 
109 109
 		L.marker([$latitude_j, $longitude_j]).addTo(map);
110 110
 EOF;
@@ -116,10 +116,10 @@  discard block
 block discarded – undo
116 116
 
117 117
 function wordlift_geo_widget_html( $width, $height, $latitude, $longitude, $zoom, $content ) {
118 118
 
119
-	// Create a unique Id for this widget.
120
-	$div_id = uniqid( 'wl-geo-' );
119
+    // Create a unique Id for this widget.
120
+    $div_id = uniqid( 'wl-geo-' );
121 121
 
122
-	echo <<<EOF
122
+    echo <<<EOF
123 123
 <div id="$div_id" style="width: $width; height: $height;"></div>
124 124
 
125 125
 <script type="text/javascript">
@@ -144,10 +144,10 @@  discard block
 block discarded – undo
144 144
 
145 145
 EOF;
146 146
 
147
-	// Run inner shortcodes.
148
-	do_shortcode( $content );
147
+    // Run inner shortcodes.
148
+    do_shortcode( $content );
149 149
 
150
-	echo <<<EOF
150
+    echo <<<EOF
151 151
 
152 152
     } );
153 153
 </script>
Please login to merge, or discard this patch.
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -7,21 +7,21 @@  discard block
 block discarded – undo
7 7
  * @subpackage Wordlift/modules/geo_widget
8 8
  */
9 9
 
10
-function wordlift_geo_widget_shortcode( $atts, $content = null ) {
10
+function wordlift_geo_widget_shortcode($atts, $content = null) {
11 11
 
12 12
 	// Extract attributes and set default values.
13
-	$params = shortcode_atts( array(
13
+	$params = shortcode_atts(array(
14 14
 		'width'     => '100%',
15 15
 		'height'    => '300px',
16 16
 		'latitude'  => 0.0,
17 17
 		'longitude' => 0.0,
18 18
 		'zoom'      => 5,
19 19
 
20
-	), $atts );
20
+	), $atts);
21 21
 
22 22
 	// Add leaflet css and library.
23
-	wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
24
-	wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true );
23
+	wp_enqueue_style('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0');
24
+	wp_enqueue_script('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0', true);
25 25
 
26 26
 	ob_start(); // Collect the buffer.
27 27
 	wordlift_geo_widget_html(
@@ -38,26 +38,26 @@  discard block
 block discarded – undo
38 38
 
39 39
 }
40 40
 
41
-add_shortcode( 'wl_geo', 'wordlift_geo_widget_shortcode' );
41
+add_shortcode('wl_geo', 'wordlift_geo_widget_shortcode');
42 42
 
43
-function wl_geo_widget_layer_shortcode( $atts ) {
43
+function wl_geo_widget_layer_shortcode($atts) {
44 44
 
45 45
 	// Extract attributes and set default values.
46
-	$params = shortcode_atts( array(
46
+	$params = shortcode_atts(array(
47 47
 		'name'  => '',
48 48
 		'label' => '',
49
-	), $atts );
49
+	), $atts);
50 50
 
51 51
 	// Return if a SPARQL Query name hasn't been provided.
52
-	if ( empty( $params['name'] ) ) {
52
+	if (empty($params['name'])) {
53 53
 		return;
54 54
 	}
55 55
 
56 56
 	// Set the layer label.
57
-	$label_j = json_encode( empty( $params['label'] ) ? $params['name'] : $params['label'] );
57
+	$label_j = json_encode(empty($params['label']) ? $params['name'] : $params['label']);
58 58
 
59 59
 	// Define the AJAX Url.
60
-	$ajax_url = admin_url( 'admin-ajax.php?action=wl_sparql&format=geojson&slug=' . urlencode( $params['name'] ) );
60
+	$ajax_url = admin_url('admin-ajax.php?action=wl_sparql&format=geojson&slug='.urlencode($params['name']));
61 61
 
62 62
 	echo <<<EOF
63 63
 
@@ -85,24 +85,24 @@  discard block
 block discarded – undo
85 85
 
86 86
 }
87 87
 
88
-add_shortcode( 'wl_geo_layer', 'wl_geo_widget_layer_shortcode' );
88
+add_shortcode('wl_geo_layer', 'wl_geo_widget_layer_shortcode');
89 89
 
90 90
 
91
-function wl_geo_widget_marker_shortcode( $atts ) {
91
+function wl_geo_widget_marker_shortcode($atts) {
92 92
 
93 93
 	// Extract attributes and set default values.
94
-	$params = shortcode_atts( array(
94
+	$params = shortcode_atts(array(
95 95
 		'latitude'  => null,
96 96
 		'longitude' => null,
97
-	), $atts );
97
+	), $atts);
98 98
 
99 99
 	// Return if either latitude or longitude haven't been provided.
100
-	if ( empty( $params['latitude'] ) || empty( $params['longitude'] ) ) {
100
+	if (empty($params['latitude']) || empty($params['longitude'])) {
101 101
 		return;
102 102
 	}
103 103
 
104
-	$latitude_j  = json_encode( $params['latitude'] );
105
-	$longitude_j = json_encode( $params['longitude'] );
104
+	$latitude_j  = json_encode($params['latitude']);
105
+	$longitude_j = json_encode($params['longitude']);
106 106
 
107 107
 	echo <<<EOF
108 108
 
@@ -111,13 +111,13 @@  discard block
 block discarded – undo
111 111
 
112 112
 }
113 113
 
114
-add_shortcode( 'wl_geo_marker', 'wl_geo_widget_marker_shortcode' );
114
+add_shortcode('wl_geo_marker', 'wl_geo_widget_marker_shortcode');
115 115
 
116 116
 
117
-function wordlift_geo_widget_html( $width, $height, $latitude, $longitude, $zoom, $content ) {
117
+function wordlift_geo_widget_html($width, $height, $latitude, $longitude, $zoom, $content) {
118 118
 
119 119
 	// Create a unique Id for this widget.
120
-	$div_id = uniqid( 'wl-geo-' );
120
+	$div_id = uniqid('wl-geo-');
121 121
 
122 122
 	echo <<<EOF
123 123
 <div id="$div_id" style="width: $width; height: $height;"></div>
@@ -145,7 +145,7 @@  discard block
 block discarded – undo
145 145
 EOF;
146 146
 
147 147
 	// Run inner shortcodes.
148
-	do_shortcode( $content );
148
+	do_shortcode($content);
149 149
 
150 150
 	echo <<<EOF
151 151
 
Please login to merge, or discard this patch.
src/wordlift_entity_functions.php 2 patches
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -12,35 +12,35 @@  discard block
 block discarded – undo
12 12
  */
13 13
 function wl_get_entity_post_ids_by_uris( $uris ) {
14 14
 
15
-	if ( empty( $uris ) ) {
16
-		return array();
17
-	}
18
-
19
-	$query = new WP_Query( array(
20
-			'fields'      => 'ids',
21
-			'post_status' => 'any',
22
-			'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
23
-			'meta_query'  => array(
24
-				'relation' => 'OR',
25
-				array(
26
-					'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
27
-					'value'   => $uris,
28
-					'compare' => 'IN',
29
-				),
30
-				array(
31
-					'key'     => 'entity_url',
32
-					'value'   => $uris,
33
-					'compare' => 'IN',
34
-				),
35
-			),
36
-		)
37
-	);
38
-
39
-	// Get the matching entity posts.
40
-	$posts = $query->get_posts();
41
-
42
-	// Return the array
43
-	return $posts;
15
+    if ( empty( $uris ) ) {
16
+        return array();
17
+    }
18
+
19
+    $query = new WP_Query( array(
20
+            'fields'      => 'ids',
21
+            'post_status' => 'any',
22
+            'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
23
+            'meta_query'  => array(
24
+                'relation' => 'OR',
25
+                array(
26
+                    'key'     => Wordlift_Schema_Service::FIELD_SAME_AS,
27
+                    'value'   => $uris,
28
+                    'compare' => 'IN',
29
+                ),
30
+                array(
31
+                    'key'     => 'entity_url',
32
+                    'value'   => $uris,
33
+                    'compare' => 'IN',
34
+                ),
35
+            ),
36
+        )
37
+    );
38
+
39
+    // Get the matching entity posts.
40
+    $posts = $query->get_posts();
41
+
42
+    // Return the array
43
+    return $posts;
44 44
 }
45 45
 
46 46
 /**
@@ -55,29 +55,29 @@  discard block
 block discarded – undo
55 55
  */
56 56
 function wl_build_entity_uri( $post_id ) {
57 57
 
58
-	// Get the post.
59
-	$post = get_post( $post_id );
58
+    // Get the post.
59
+    $post = get_post( $post_id );
60 60
 
61
-	if ( null === $post ) {
62
-		wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" );
61
+    if ( null === $post ) {
62
+        wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" );
63 63
 
64
-		return null;
65
-	}
64
+        return null;
65
+    }
66 66
 
67
-	// Create an ID given the title.
68
-	$entity_slug = wl_sanitize_uri_path( $post->post_title );
69
-	// If the entity slug is empty, i.e. there's no title, use the post ID as path.
70
-	if ( empty( $entity_slug ) ) {
71
-		return sprintf( '%s/%s/%s',
72
-			wl_configuration_get_redlink_dataset_uri(),
73
-			$post->post_type,
74
-			"id/$post->ID"
75
-		);
76
-	}
67
+    // Create an ID given the title.
68
+    $entity_slug = wl_sanitize_uri_path( $post->post_title );
69
+    // If the entity slug is empty, i.e. there's no title, use the post ID as path.
70
+    if ( empty( $entity_slug ) ) {
71
+        return sprintf( '%s/%s/%s',
72
+            wl_configuration_get_redlink_dataset_uri(),
73
+            $post->post_type,
74
+            "id/$post->ID"
75
+        );
76
+    }
77 77
 
78
-	return Wordlift_Uri_Service::get_instance()->build_uri(
79
-		$entity_slug,
80
-		$post->post_type );
78
+    return Wordlift_Uri_Service::get_instance()->build_uri(
79
+        $entity_slug,
80
+        $post->post_type );
81 81
 
82 82
 }
83 83
 
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
  */
96 96
 function wl_get_entity_uri( $post_id ) {
97 97
 
98
-	return Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
98
+    return Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
99 99
 }
100 100
 
101 101
 /**
@@ -108,9 +108,9 @@  discard block
 block discarded – undo
108 108
  */
109 109
 function wl_set_entity_uri( $post_id, $uri ) {
110 110
 
111
-	// wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" );
111
+    // wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" );
112 112
 
113
-	return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri );
113
+    return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri );
114 114
 }
115 115
 
116 116
 
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
  */
126 126
 function wl_get_entity_rdf_types( $post_id ) {
127 127
 
128
-	return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
128
+    return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
129 129
 }
130 130
 
131 131
 /**
@@ -136,21 +136,21 @@  discard block
 block discarded – undo
136 136
  */
137 137
 function wl_set_entity_rdf_types( $post_id, $type_uris = array() ) {
138 138
 
139
-	// Avoid errors because of null values.
140
-	if ( is_null( $type_uris ) ) {
141
-		$type_uris = array();
142
-	}
143
-
144
-	// Ensure there are no duplicates.
145
-	$type_uris = array_unique( $type_uris );
146
-
147
-	delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
148
-	foreach ( $type_uris as $type_uri ) {
149
-		if ( empty( $type_uri ) ) {
150
-			continue;
151
-		}
152
-		add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri );
153
-	}
139
+    // Avoid errors because of null values.
140
+    if ( is_null( $type_uris ) ) {
141
+        $type_uris = array();
142
+    }
143
+
144
+    // Ensure there are no duplicates.
145
+    $type_uris = array_unique( $type_uris );
146
+
147
+    delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
148
+    foreach ( $type_uris as $type_uri ) {
149
+        if ( empty( $type_uri ) ) {
150
+            continue;
151
+        }
152
+        add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri );
153
+    }
154 154
 }
155 155
 
156 156
 ///**
@@ -199,59 +199,59 @@  discard block
 block discarded – undo
199 199
  */
200 200
 function wl_entity_taxonomy_get_custom_fields( $entity_id = null ) {
201 201
 
202
-	if ( is_null( $entity_id ) ) {
202
+    if ( is_null( $entity_id ) ) {
203 203
 
204
-		// Return all custom fields.
205
-		// Get taxonomy terms
206
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) );
204
+        // Return all custom fields.
205
+        // Get taxonomy terms
206
+        $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) );
207 207
 
208
-		if ( is_wp_error( $terms ) ) {
209
-			return null;
210
-		}
208
+        if ( is_wp_error( $terms ) ) {
209
+            return null;
210
+        }
211 211
 
212
-		$custom_fields = array();
213
-		foreach ( $terms as $term ) {
214
-			// Get custom_fields
215
-			$term_options = Wordlift_Schema_Service::get_instance()
216
-			                                       ->get_schema( $term->slug );
212
+        $custom_fields = array();
213
+        foreach ( $terms as $term ) {
214
+            // Get custom_fields
215
+            $term_options = Wordlift_Schema_Service::get_instance()
216
+                                                    ->get_schema( $term->slug );
217 217
 
218
-			if ( ! isset( $term_options['uri'] ) || ! isset( $term_options['custom_fields'] ) ) {
219
-				continue;
220
-			}
218
+            if ( ! isset( $term_options['uri'] ) || ! isset( $term_options['custom_fields'] ) ) {
219
+                continue;
220
+            }
221 221
 
222
-			$custom_fields[ $term_options['uri'] ] = $term_options['custom_fields'];
223
-		}
222
+            $custom_fields[ $term_options['uri'] ] = $term_options['custom_fields'];
223
+        }
224 224
 
225
-		return $custom_fields;
225
+        return $custom_fields;
226 226
 
227
-	}
227
+    }
228 228
 
229
-	// Return custom fields for this specific entity's type.
230
-	$types = Wordlift_Entity_Type_Service::get_instance()->get_ids( $entity_id );
229
+    // Return custom fields for this specific entity's type.
230
+    $types = Wordlift_Entity_Type_Service::get_instance()->get_ids( $entity_id );
231 231
 
232
-	/** @var WP_Term[] $terms */
233
-	$terms = array_filter( array_map( function ( $item ) {
234
-		return get_term( $item );
235
-	}, $types ), function ( $item ) {
236
-		return isset( $item ) && is_a( $item, 'WP_Term' );
237
-	} );
232
+    /** @var WP_Term[] $terms */
233
+    $terms = array_filter( array_map( function ( $item ) {
234
+        return get_term( $item );
235
+    }, $types ), function ( $item ) {
236
+        return isset( $item ) && is_a( $item, 'WP_Term' );
237
+    } );
238 238
 
239
-	$term_slugs = array_map( function ( $item ) {
240
-		return $item->slug;
241
-	}, $terms );
239
+    $term_slugs = array_map( function ( $item ) {
240
+        return $item->slug;
241
+    }, $terms );
242 242
 
243
-	$term_slugs[] = 'thing';
243
+    $term_slugs[] = 'thing';
244 244
 
245
-	$schema_service = Wordlift_Schema_Service::get_instance();
245
+    $schema_service = Wordlift_Schema_Service::get_instance();
246 246
 
247
-	return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
247
+    return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
248 248
 
249
-		$schema = $schema_service->get_schema( $item );
249
+        $schema = $schema_service->get_schema( $item );
250 250
 
251
-		if ( ! isset( $schema['custom_fields'] ) ) {
252
-			return $carry;
253
-		}
251
+        if ( ! isset( $schema['custom_fields'] ) ) {
252
+            return $carry;
253
+        }
254 254
 
255
-		return $carry + $schema['custom_fields'];
256
-	}, array() );
255
+        return $carry + $schema['custom_fields'];
256
+    }, array() );
257 257
 }
Please login to merge, or discard this patch.
Spacing   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -10,13 +10,13 @@  discard block
 block discarded – undo
10 10
  *
11 11
  * @return array A WP_Post instance or null if not found.
12 12
  */
13
-function wl_get_entity_post_ids_by_uris( $uris ) {
13
+function wl_get_entity_post_ids_by_uris($uris) {
14 14
 
15
-	if ( empty( $uris ) ) {
15
+	if (empty($uris)) {
16 16
 		return array();
17 17
 	}
18 18
 
19
-	$query = new WP_Query( array(
19
+	$query = new WP_Query(array(
20 20
 			'fields'      => 'ids',
21 21
 			'post_status' => 'any',
22 22
 			'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
@@ -53,22 +53,22 @@  discard block
 block discarded – undo
53 53
  * @uses wl_configuration_get_redlink_dataset_uri() to get the dataset base URI.
54 54
  *
55 55
  */
56
-function wl_build_entity_uri( $post_id ) {
56
+function wl_build_entity_uri($post_id) {
57 57
 
58 58
 	// Get the post.
59
-	$post = get_post( $post_id );
59
+	$post = get_post($post_id);
60 60
 
61
-	if ( null === $post ) {
62
-		wl_write_log( "wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]" );
61
+	if (null === $post) {
62
+		wl_write_log("wl_build_entity_uri : error [ post ID :: $post_id ][ post :: null ]");
63 63
 
64 64
 		return null;
65 65
 	}
66 66
 
67 67
 	// Create an ID given the title.
68
-	$entity_slug = wl_sanitize_uri_path( $post->post_title );
68
+	$entity_slug = wl_sanitize_uri_path($post->post_title);
69 69
 	// If the entity slug is empty, i.e. there's no title, use the post ID as path.
70
-	if ( empty( $entity_slug ) ) {
71
-		return sprintf( '%s/%s/%s',
70
+	if (empty($entity_slug)) {
71
+		return sprintf('%s/%s/%s',
72 72
 			wl_configuration_get_redlink_dataset_uri(),
73 73
 			$post->post_type,
74 74
 			"id/$post->ID"
@@ -93,9 +93,9 @@  discard block
 block discarded – undo
93 93
  *
94 94
  * @uses       wl_build_entity_uri() to create a new URI if the entity doesn't have an URI yet.
95 95
  */
96
-function wl_get_entity_uri( $post_id ) {
96
+function wl_get_entity_uri($post_id) {
97 97
 
98
-	return Wordlift_Entity_Service::get_instance()->get_uri( $post_id );
98
+	return Wordlift_Entity_Service::get_instance()->get_uri($post_id);
99 99
 }
100 100
 
101 101
 /**
@@ -106,11 +106,11 @@  discard block
 block discarded – undo
106 106
  *
107 107
  * @return bool True if successful, otherwise false.
108 108
  */
109
-function wl_set_entity_uri( $post_id, $uri ) {
109
+function wl_set_entity_uri($post_id, $uri) {
110 110
 
111 111
 	// wl_write_log( "wl_set_entity_uri [ post id :: $post_id ][ uri :: $uri ]" );
112 112
 
113
-	return update_post_meta( $post_id, WL_ENTITY_URL_META_NAME, $uri );
113
+	return update_post_meta($post_id, WL_ENTITY_URL_META_NAME, $uri);
114 114
 }
115 115
 
116 116
 
@@ -123,9 +123,9 @@  discard block
 block discarded – undo
123 123
  * @since 3.0.0
124 124
  *
125 125
  */
126
-function wl_get_entity_rdf_types( $post_id ) {
126
+function wl_get_entity_rdf_types($post_id) {
127 127
 
128
-	return get_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
128
+	return get_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE);
129 129
 }
130 130
 
131 131
 /**
@@ -134,22 +134,22 @@  discard block
 block discarded – undo
134 134
  * @param int $post_id The entity post ID.
135 135
  * @param array $type_uris An array of type URIs.
136 136
  */
137
-function wl_set_entity_rdf_types( $post_id, $type_uris = array() ) {
137
+function wl_set_entity_rdf_types($post_id, $type_uris = array()) {
138 138
 
139 139
 	// Avoid errors because of null values.
140
-	if ( is_null( $type_uris ) ) {
140
+	if (is_null($type_uris)) {
141 141
 		$type_uris = array();
142 142
 	}
143 143
 
144 144
 	// Ensure there are no duplicates.
145
-	$type_uris = array_unique( $type_uris );
145
+	$type_uris = array_unique($type_uris);
146 146
 
147
-	delete_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE );
148
-	foreach ( $type_uris as $type_uri ) {
149
-		if ( empty( $type_uri ) ) {
147
+	delete_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE);
148
+	foreach ($type_uris as $type_uri) {
149
+		if (empty($type_uri)) {
150 150
 			continue;
151 151
 		}
152
-		add_post_meta( $post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri );
152
+		add_post_meta($post_id, Wordlift_Schema_Service::FIELD_ENTITY_TYPE, $type_uri);
153 153
 	}
154 154
 }
155 155
 
@@ -197,29 +197,29 @@  discard block
 block discarded – undo
197 197
  *
198 198
  * @return array|null if $entity_id was specified, return custom_fields for that entity's type. Otherwise returns all custom_fields
199 199
  */
200
-function wl_entity_taxonomy_get_custom_fields( $entity_id = null ) {
200
+function wl_entity_taxonomy_get_custom_fields($entity_id = null) {
201 201
 
202
-	if ( is_null( $entity_id ) ) {
202
+	if (is_null($entity_id)) {
203 203
 
204 204
 		// Return all custom fields.
205 205
 		// Get taxonomy terms
206
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'get' => 'all' ) );
206
+		$terms = get_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('get' => 'all'));
207 207
 
208
-		if ( is_wp_error( $terms ) ) {
208
+		if (is_wp_error($terms)) {
209 209
 			return null;
210 210
 		}
211 211
 
212 212
 		$custom_fields = array();
213
-		foreach ( $terms as $term ) {
213
+		foreach ($terms as $term) {
214 214
 			// Get custom_fields
215 215
 			$term_options = Wordlift_Schema_Service::get_instance()
216
-			                                       ->get_schema( $term->slug );
216
+			                                       ->get_schema($term->slug);
217 217
 
218
-			if ( ! isset( $term_options['uri'] ) || ! isset( $term_options['custom_fields'] ) ) {
218
+			if ( ! isset($term_options['uri']) || ! isset($term_options['custom_fields'])) {
219 219
 				continue;
220 220
 			}
221 221
 
222
-			$custom_fields[ $term_options['uri'] ] = $term_options['custom_fields'];
222
+			$custom_fields[$term_options['uri']] = $term_options['custom_fields'];
223 223
 		}
224 224
 
225 225
 		return $custom_fields;
@@ -227,31 +227,31 @@  discard block
 block discarded – undo
227 227
 	}
228 228
 
229 229
 	// Return custom fields for this specific entity's type.
230
-	$types = Wordlift_Entity_Type_Service::get_instance()->get_ids( $entity_id );
230
+	$types = Wordlift_Entity_Type_Service::get_instance()->get_ids($entity_id);
231 231
 
232 232
 	/** @var WP_Term[] $terms */
233
-	$terms = array_filter( array_map( function ( $item ) {
234
-		return get_term( $item );
235
-	}, $types ), function ( $item ) {
236
-		return isset( $item ) && is_a( $item, 'WP_Term' );
233
+	$terms = array_filter(array_map(function($item) {
234
+		return get_term($item);
235
+	}, $types), function($item) {
236
+		return isset($item) && is_a($item, 'WP_Term');
237 237
 	} );
238 238
 
239
-	$term_slugs = array_map( function ( $item ) {
239
+	$term_slugs = array_map(function($item) {
240 240
 		return $item->slug;
241
-	}, $terms );
241
+	}, $terms);
242 242
 
243 243
 	$term_slugs[] = 'thing';
244 244
 
245 245
 	$schema_service = Wordlift_Schema_Service::get_instance();
246 246
 
247
-	return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
247
+	return array_reduce($term_slugs, function($carry, $item) use ($schema_service) {
248 248
 
249
-		$schema = $schema_service->get_schema( $item );
249
+		$schema = $schema_service->get_schema($item);
250 250
 
251
-		if ( ! isset( $schema['custom_fields'] ) ) {
251
+		if ( ! isset($schema['custom_fields'])) {
252 252
 			return $carry;
253 253
 		}
254 254
 
255 255
 		return $carry + $schema['custom_fields'];
256
-	}, array() );
256
+	}, array());
257 257
 }
Please login to merge, or discard this patch.
src/admin/WL_Metabox/class-wl-metabox.php 2 patches
Indentation   +298 added lines, -298 removed lines patch added patch discarded remove patch
@@ -27,346 +27,346 @@
 block discarded – undo
27 27
  */
28 28
 class WL_Metabox {
29 29
 
30
-	/**
31
-	 * The metabox custom fields for the current {@link WP_Post}.
32
-	 *
33
-	 * @since  3.1.0
34
-	 * @access public
35
-	 * @var array $fields The metabox custom fields.
36
-	 */
37
-	public $fields;
38
-
39
-	/**
40
-	 * A {@link Wordlift_Log_Service} instance.
41
-	 *
42
-	 * @since 3.15.4
43
-	 *
44
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
45
-	 */
46
-	private $log;
47
-
48
-	/**
49
-	 * WL_Metabox constructor.
50
-	 *
51
-	 * @since 3.1.0
52
-	 */
53
-	public function __construct() {
54
-
55
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
56
-
57
-		// Add hooks to print metaboxes and save submitted data.
58
-		add_action( 'add_meta_boxes', array( $this, 'add_main_metabox' ) );
59
-		add_action( 'wl_linked_data_save_post', array( $this, 'save_form_data', ) );
60
-
61
-		// Enqueue js and css.
62
-		$this->enqueue_scripts_and_styles();
63
-
64
-	}
65
-
66
-	/**
67
-	 * Add a callback to print the metabox in page.
68
-	 * Wordpress will fire the $this->html() callback at the right time.
69
-	 */
70
-	public function add_main_metabox() {
71
-
72
-		// Build the fields we need to print.
73
-		$this->instantiate_fields( get_the_ID() );
74
-
75
-		// Bailout if there are no actual fields, we do not need a metabox in that case.
76
-		if ( empty( $this->fields ) ) {
77
-			return;
78
-		}
79
-
80
-		// Add main metabox (will print also the inner fields).
81
-		$id    = uniqid( 'wl-metabox-' );
82
-		$title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
83
-
84
-		// WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
85
-		foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
86
-			add_meta_box( $id, $title, array(
87
-				$this,
88
-				'html',
89
-			), $screen, 'normal', 'high' );
90
-		}
91
-
92
-		// Add filter to change the metabox CSS class.
93
-		//
94
-		// @since 3.20.0 Since we support post types other than `entity` for entities, we need to set the `screen`
95
-		//  dynamically according to the `get_current_screen()` function.
96
-		$current_screen = get_current_screen();
97
-		$screen         = $current_screen ? $current_screen->post_type : 'entity';
98
-		add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
99
-
100
-	}
101
-
102
-	/**
103
-	 * Called from WP to print the metabox content in page.
104
-	 *
105
-	 * @param WP_Post $post The post.
106
-	 *
107
-	 * @since 3.1.0
108
-	 *
109
-	 */
110
-	public function html( $post ) {
111
-
112
-		// Loop over the fields.
113
-		foreach ( $this->fields as $field ) {
114
-
115
-			// load data from DB (values will be available in $field->data).
116
-			$field->get_data();
117
-
118
-			// print field HTML (nonce included).
119
-			echo $field->html();
120
-		}
121
-
122
-	}
123
-
124
-	/**
125
-	 * Read the WL <-> Schema mapping and build the Fields for the entity being edited.
126
-	 *
127
-	 * Note: the first function that calls this method will instantiate the fields.
128
-	 * Why it isn't called from the constructor? Because we need to hook this process as late as possible.
129
-	 *
130
-	 * @param int $post_id The post id.
131
-	 *
132
-	 * @since 3.1.0
133
-	 *
134
-	 */
135
-	public function instantiate_fields( $post_id ) {
136
-
137
-		$this->log->trace( "Instantiating fields for entity post $post_id..." );
138
-
139
-		// This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
140
-		if ( isset( $this->fields ) ) {
141
-			return;
142
-		}
143
-
144
-		$entity_type = wl_entity_taxonomy_get_custom_fields( $post_id );
145
-
146
-		if ( isset( $entity_type ) ) {
147
-
148
-			/*
30
+    /**
31
+     * The metabox custom fields for the current {@link WP_Post}.
32
+     *
33
+     * @since  3.1.0
34
+     * @access public
35
+     * @var array $fields The metabox custom fields.
36
+     */
37
+    public $fields;
38
+
39
+    /**
40
+     * A {@link Wordlift_Log_Service} instance.
41
+     *
42
+     * @since 3.15.4
43
+     *
44
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
45
+     */
46
+    private $log;
47
+
48
+    /**
49
+     * WL_Metabox constructor.
50
+     *
51
+     * @since 3.1.0
52
+     */
53
+    public function __construct() {
54
+
55
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
56
+
57
+        // Add hooks to print metaboxes and save submitted data.
58
+        add_action( 'add_meta_boxes', array( $this, 'add_main_metabox' ) );
59
+        add_action( 'wl_linked_data_save_post', array( $this, 'save_form_data', ) );
60
+
61
+        // Enqueue js and css.
62
+        $this->enqueue_scripts_and_styles();
63
+
64
+    }
65
+
66
+    /**
67
+     * Add a callback to print the metabox in page.
68
+     * Wordpress will fire the $this->html() callback at the right time.
69
+     */
70
+    public function add_main_metabox() {
71
+
72
+        // Build the fields we need to print.
73
+        $this->instantiate_fields( get_the_ID() );
74
+
75
+        // Bailout if there are no actual fields, we do not need a metabox in that case.
76
+        if ( empty( $this->fields ) ) {
77
+            return;
78
+        }
79
+
80
+        // Add main metabox (will print also the inner fields).
81
+        $id    = uniqid( 'wl-metabox-' );
82
+        $title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
83
+
84
+        // WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
85
+        foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
86
+            add_meta_box( $id, $title, array(
87
+                $this,
88
+                'html',
89
+            ), $screen, 'normal', 'high' );
90
+        }
91
+
92
+        // Add filter to change the metabox CSS class.
93
+        //
94
+        // @since 3.20.0 Since we support post types other than `entity` for entities, we need to set the `screen`
95
+        //  dynamically according to the `get_current_screen()` function.
96
+        $current_screen = get_current_screen();
97
+        $screen         = $current_screen ? $current_screen->post_type : 'entity';
98
+        add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
99
+
100
+    }
101
+
102
+    /**
103
+     * Called from WP to print the metabox content in page.
104
+     *
105
+     * @param WP_Post $post The post.
106
+     *
107
+     * @since 3.1.0
108
+     *
109
+     */
110
+    public function html( $post ) {
111
+
112
+        // Loop over the fields.
113
+        foreach ( $this->fields as $field ) {
114
+
115
+            // load data from DB (values will be available in $field->data).
116
+            $field->get_data();
117
+
118
+            // print field HTML (nonce included).
119
+            echo $field->html();
120
+        }
121
+
122
+    }
123
+
124
+    /**
125
+     * Read the WL <-> Schema mapping and build the Fields for the entity being edited.
126
+     *
127
+     * Note: the first function that calls this method will instantiate the fields.
128
+     * Why it isn't called from the constructor? Because we need to hook this process as late as possible.
129
+     *
130
+     * @param int $post_id The post id.
131
+     *
132
+     * @since 3.1.0
133
+     *
134
+     */
135
+    public function instantiate_fields( $post_id ) {
136
+
137
+        $this->log->trace( "Instantiating fields for entity post $post_id..." );
138
+
139
+        // This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
140
+        if ( isset( $this->fields ) ) {
141
+            return;
142
+        }
143
+
144
+        $entity_type = wl_entity_taxonomy_get_custom_fields( $post_id );
145
+
146
+        if ( isset( $entity_type ) ) {
147
+
148
+            /*
149 149
 			 * Might not have any relevant meta box field, for example for articles,
150 150
 			 * therefor make sure fields are at least an empty array to help the considered
151 151
 			 * in other functions using it.
152 152
 			 */
153
-			$this->fields = array();
153
+            $this->fields = array();
154 154
 
155
-			/**
156
-			 * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods.
157
-			 * We must divide fields in two groups:
158
-			 * - simple: accept values for one property
159
-			 * - grouped: accept values for more properties, or for one property that needs a specific metabox.
160
-			 */
161
-			$metaboxes         = $this->group_properties_by_input_field( $entity_type );
162
-			$simple_metaboxes  = $metaboxes[0];
163
-			$grouped_metaboxes = $metaboxes[1];
155
+            /**
156
+             * In some special case, properties must be grouped in one field (e.g. coordinates) or dealed with custom methods.
157
+             * We must divide fields in two groups:
158
+             * - simple: accept values for one property
159
+             * - grouped: accept values for more properties, or for one property that needs a specific metabox.
160
+             */
161
+            $metaboxes         = $this->group_properties_by_input_field( $entity_type );
162
+            $simple_metaboxes  = $metaboxes[0];
163
+            $grouped_metaboxes = $metaboxes[1];
164 164
 
165
-			// Loop over simple entity properties.
166
-			foreach ( $simple_metaboxes as $key => $property ) {
165
+            // Loop over simple entity properties.
166
+            foreach ( $simple_metaboxes as $key => $property ) {
167 167
 
168
-				// Info passed to the metabox.
169
-				$info         = array();
170
-				$info[ $key ] = $property;
168
+                // Info passed to the metabox.
169
+                $info         = array();
170
+                $info[ $key ] = $property;
171 171
 
172
-				// Build the requested field as WL_Metabox_Field_ object.
173
-				$this->add_field( $info );
172
+                // Build the requested field as WL_Metabox_Field_ object.
173
+                $this->add_field( $info );
174 174
 
175
-			}
175
+            }
176 176
 
177
-			// Loop over grouped properties.
178
-			foreach ( $grouped_metaboxes as $key => $property ) {
177
+            // Loop over grouped properties.
178
+            foreach ( $grouped_metaboxes as $key => $property ) {
179 179
 
180
-				// Info passed to the metabox.
181
-				$info         = array();
182
-				$info[ $key ] = $property;
180
+                // Info passed to the metabox.
181
+                $info         = array();
182
+                $info[ $key ] = $property;
183 183
 
184
-				// Build the requested field group as WL_Metabox_Field_ object.
185
-				$this->add_field( $info, true );
184
+                // Build the requested field group as WL_Metabox_Field_ object.
185
+                $this->add_field( $info, true );
186 186
 
187
-			}
188
-		}
187
+            }
188
+        }
189 189
 
190
-	}
190
+    }
191 191
 
192
-	/**
193
-	 * Separates metaboxes in simple and grouped.
194
-	 *
195
-	 * @param array $custom_fields Information on the entity type.
196
-	 *
197
-	 * @return array
198
-	 */
199
-	public function group_properties_by_input_field( $custom_fields ) {
192
+    /**
193
+     * Separates metaboxes in simple and grouped.
194
+     *
195
+     * @param array $custom_fields Information on the entity type.
196
+     *
197
+     * @return array
198
+     */
199
+    public function group_properties_by_input_field( $custom_fields ) {
200 200
 
201
-		$simple_properties  = array();
202
-		$grouped_properties = array();
201
+        $simple_properties  = array();
202
+        $grouped_properties = array();
203 203
 
204
-		// Loop over possible entity properties.
205
-		foreach ( $custom_fields as $key => $property ) {
204
+        // Loop over possible entity properties.
205
+        foreach ( $custom_fields as $key => $property ) {
206 206
 
207
-			// Check presence of predicate and type.
208
-			if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
207
+            // Check presence of predicate and type.
208
+            if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
209 209
 
210
-				// Check if input_field is defined.
211
-				if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
210
+                // Check if input_field is defined.
211
+                if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
212 212
 
213
-					$grouped_key = $property['input_field'];
213
+                    $grouped_key = $property['input_field'];
214 214
 
215
-					// Update list of grouped properties.
216
-					$grouped_properties[ $grouped_key ][ $key ] = $property;
215
+                    // Update list of grouped properties.
216
+                    $grouped_properties[ $grouped_key ][ $key ] = $property;
217 217
 
218
-				} else {
218
+                } else {
219 219
 
220
-					// input_field not defined, add simple metabox.
221
-					$simple_properties[ $key ] = $property;
222
-				}
223
-			}
224
-		}
220
+                    // input_field not defined, add simple metabox.
221
+                    $simple_properties[ $key ] = $property;
222
+                }
223
+            }
224
+        }
225 225
 
226
-		return array( $simple_properties, $grouped_properties );
227
-	}
226
+        return array( $simple_properties, $grouped_properties );
227
+    }
228 228
 
229
-	/**
230
-	 * Add a Field to the current Metabox, based on the description of the Field.
231
-	 * This method is a rude factory for Field objects.
232
-	 *
233
-	 * @param array $args The field's information.
234
-	 * @param bool $grouped Flag to distinguish between simple and grouped fields.
235
-	 */
236
-	public function add_field( $args, $grouped = false ) {
229
+    /**
230
+     * Add a Field to the current Metabox, based on the description of the Field.
231
+     * This method is a rude factory for Field objects.
232
+     *
233
+     * @param array $args The field's information.
234
+     * @param bool $grouped Flag to distinguish between simple and grouped fields.
235
+     */
236
+    public function add_field( $args, $grouped = false ) {
237 237
 
238
-		if ( $grouped ) {
238
+        if ( $grouped ) {
239 239
 
240
-			// Special fields (sameas, coordinates, etc.).
241
-			//
242
-			// Build Field with a custom class (e.g. WL_Metabox_Field_date).
243
-			$field_class = 'WL_Metabox_Field_' . key( $args );
240
+            // Special fields (sameas, coordinates, etc.).
241
+            //
242
+            // Build Field with a custom class (e.g. WL_Metabox_Field_date).
243
+            $field_class = 'WL_Metabox_Field_' . key( $args );
244 244
 
245
-		} else {
245
+        } else {
246 246
 
247
-			// Simple fields (string, uri, boolean, etc.).
248
-			//
249
-			// Which field? We want to use the class that is specific for the field.
250
-			$meta      = key( $args );
251
-			$this_meta = $args[ $meta ];
247
+            // Simple fields (string, uri, boolean, etc.).
248
+            //
249
+            // Which field? We want to use the class that is specific for the field.
250
+            $meta      = key( $args );
251
+            $this_meta = $args[ $meta ];
252 252
 
253
-			// If the field declares what metabox it wants, use that one.
254
-			if ( isset( $this_meta['metabox']['class'] ) ) {
253
+            // If the field declares what metabox it wants, use that one.
254
+            if ( isset( $this_meta['metabox']['class'] ) ) {
255 255
 
256
-				$field_class = $this_meta['metabox']['class'];
256
+                $field_class = $this_meta['metabox']['class'];
257 257
 
258
-			} elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
258
+            } elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
259 259
 
260
-				// TODO: all fields should explicitly declare the required WL_Metabox.
261
-				// When they will remove this.
262
-				//
263
-				// Use default WL_Metabox_Field (manages strings).
264
-				$field_class = 'WL_Metabox_Field';
260
+                // TODO: all fields should explicitly declare the required WL_Metabox.
261
+                // When they will remove this.
262
+                //
263
+                // Use default WL_Metabox_Field (manages strings).
264
+                $field_class = 'WL_Metabox_Field';
265 265
 
266
-			} else {
266
+            } else {
267 267
 
268
-				// TODO: all fields should explicitly declare the required WL_Metabox.
269
-				// When they will remove this.
270
-				//
271
-				// Build Field with a custom class (e.g. WL_Metabox_Field_date).
272
-				$field_class = 'WL_Metabox_Field_' . $this_meta['type'];
268
+                // TODO: all fields should explicitly declare the required WL_Metabox.
269
+                // When they will remove this.
270
+                //
271
+                // Build Field with a custom class (e.g. WL_Metabox_Field_date).
272
+                $field_class = 'WL_Metabox_Field_' . $this_meta['type'];
273 273
 
274
-			}
274
+            }
275 275
 
276
-		}
277
-		// End if().
276
+        }
277
+        // End if().
278 278
 
279
-		// Call apropriate constructor (e.g. WL_Metabox_Field_... ).
280
-		$this->fields[] = new $field_class( $args );
281
-	}
279
+        // Call apropriate constructor (e.g. WL_Metabox_Field_... ).
280
+        $this->fields[] = new $field_class( $args );
281
+    }
282 282
 
283
-	/**
284
-	 * Save the form data for the specified entity {@link WP_Post}'s id.
285
-	 *
286
-	 * @param int $entity_id The entity's {@link WP_Post}'s id.
287
-	 *
288
-	 * @since 3.5.4
289
-	 *
290
-	 */
291
-	public function save_form_data( $entity_id ) {
292
-
293
-		$this->log->trace( "Saving form data for entity post $entity_id..." );
294
-
295
-		// Build Field objects.
296
-		$this->instantiate_fields( $entity_id );
283
+    /**
284
+     * Save the form data for the specified entity {@link WP_Post}'s id.
285
+     *
286
+     * @param int $entity_id The entity's {@link WP_Post}'s id.
287
+     *
288
+     * @since 3.5.4
289
+     *
290
+     */
291
+    public function save_form_data( $entity_id ) {
292
+
293
+        $this->log->trace( "Saving form data for entity post $entity_id..." );
294
+
295
+        // Build Field objects.
296
+        $this->instantiate_fields( $entity_id );
297 297
 
298
-		// Check if WL metabox form was posted.
299
-		if ( ! isset( $_POST['wl_metaboxes'] ) ) {
300
-			$this->log->debug( "`wl_metaboxes`, skipping..." );
298
+        // Check if WL metabox form was posted.
299
+        if ( ! isset( $_POST['wl_metaboxes'] ) ) {
300
+            $this->log->debug( "`wl_metaboxes`, skipping..." );
301 301
 
302
-			return;
303
-		}
302
+            return;
303
+        }
304 304
 
305
-		foreach ( $this->fields as $field ) {
305
+        foreach ( $this->fields as $field ) {
306 306
 
307
-			// Verify nonce.
308
-			$valid_nonce = $field->verify_nonce();
309
-			if ( $valid_nonce ) {
310
-
311
-				$posted_data = $_POST['wl_metaboxes'];
312
-				$field_name  = $field->meta_name;
313
-
314
-				// Each Filed only deals with its values.
315
-				if ( isset( $posted_data[ $field_name ] ) ) {
316
-
317
-					$values = $posted_data[ $field_name ];
318
-					if ( ! is_array( $values ) ) {
319
-						$values = array( $values );
320
-					}
321
-
322
-					// Save data permanently
323
-					$field->save_data( $values );
324
-				}
325
-			}
326
-		}
327
-
328
-		/**
329
-		 * Filter: 'wl_save_form_pre_push_entity' - Allow to hook right
330
-		 * before the triples are pushed to the linked dataset.
331
-		 *
332
-		 * @param int $entity_id The entity id.
333
-		 * @param int $id The post data.
334
-		 *
335
-		 * @since  3.18.2
336
-		 *
337
-		 */
338
-		do_action( 'wl_save_form_pre_push_entity', $entity_id, $_POST );
339
-
340
-		Wordlift_Linked_Data_Service::get_instance()->push( $entity_id );
341
-
342
-	}
343
-
344
-	/**
345
-	 * Enqueue scripts and styles.
346
-	 *
347
-	 * @since 3.0.0
348
-	 */
349
-	public function enqueue_scripts_and_styles() {
350
-
351
-		// Use the minified version if PW_DEBUG isn't set.
352
-		$min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
353
-
354
-		// Load the jquery-ui-timepicker-addon library.
355
-		wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
356
-		wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
357
-
358
-		// Leaflet.
359
-		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
360
-		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
361
-
362
-		// Add AJAX autocomplete to facilitate metabox editing.
363
-		wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' );
364
-		wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
365
-				'ajax_url' => admin_url( 'admin-ajax.php' ),
366
-				'action'   => 'entity_by_title',
367
-			)
368
-		);
369
-
370
-	}
307
+            // Verify nonce.
308
+            $valid_nonce = $field->verify_nonce();
309
+            if ( $valid_nonce ) {
310
+
311
+                $posted_data = $_POST['wl_metaboxes'];
312
+                $field_name  = $field->meta_name;
313
+
314
+                // Each Filed only deals with its values.
315
+                if ( isset( $posted_data[ $field_name ] ) ) {
316
+
317
+                    $values = $posted_data[ $field_name ];
318
+                    if ( ! is_array( $values ) ) {
319
+                        $values = array( $values );
320
+                    }
321
+
322
+                    // Save data permanently
323
+                    $field->save_data( $values );
324
+                }
325
+            }
326
+        }
327
+
328
+        /**
329
+         * Filter: 'wl_save_form_pre_push_entity' - Allow to hook right
330
+         * before the triples are pushed to the linked dataset.
331
+         *
332
+         * @param int $entity_id The entity id.
333
+         * @param int $id The post data.
334
+         *
335
+         * @since  3.18.2
336
+         *
337
+         */
338
+        do_action( 'wl_save_form_pre_push_entity', $entity_id, $_POST );
339
+
340
+        Wordlift_Linked_Data_Service::get_instance()->push( $entity_id );
341
+
342
+    }
343
+
344
+    /**
345
+     * Enqueue scripts and styles.
346
+     *
347
+     * @since 3.0.0
348
+     */
349
+    public function enqueue_scripts_and_styles() {
350
+
351
+        // Use the minified version if PW_DEBUG isn't set.
352
+        $min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
353
+
354
+        // Load the jquery-ui-timepicker-addon library.
355
+        wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
356
+        wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
357
+
358
+        // Leaflet.
359
+        wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
360
+        wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
361
+
362
+        // Add AJAX autocomplete to facilitate metabox editing.
363
+        wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' );
364
+        wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
365
+                'ajax_url' => admin_url( 'admin-ajax.php' ),
366
+                'action'   => 'entity_by_title',
367
+            )
368
+        );
369
+
370
+    }
371 371
 
372 372
 }
Please login to merge, or discard this patch.
Spacing   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -7,16 +7,16 @@  discard block
 block discarded – undo
7 7
  * @subpackage Wordlift/admin/WL_Metabox
8 8
  */
9 9
 
10
-require_once( 'class-wl-metabox-field.php' );
11
-require_once( 'class-wl-metabox-field-uri.php' );
12
-require_once( 'class-wl-metabox-field-sameas.php' );
13
-require_once( 'WL_Metabox_Field_date.php' );
14
-require_once( 'WL_Metabox_Field_coordinates.php' );
15
-require_once( 'WL_Metabox_Field_address.php' );
16
-require_once( 'class-wordlift-metabox-field-duration.php' );
17
-require_once( 'class-wordlift-metabox-field-multiline.php' );
18
-require_once( 'class-wordlift-metabox-field-integer.php' );
19
-require_once( 'class-wordlift-metabox-field-select.php' );
10
+require_once('class-wl-metabox-field.php');
11
+require_once('class-wl-metabox-field-uri.php');
12
+require_once('class-wl-metabox-field-sameas.php');
13
+require_once('WL_Metabox_Field_date.php');
14
+require_once('WL_Metabox_Field_coordinates.php');
15
+require_once('WL_Metabox_Field_address.php');
16
+require_once('class-wordlift-metabox-field-duration.php');
17
+require_once('class-wordlift-metabox-field-multiline.php');
18
+require_once('class-wordlift-metabox-field-integer.php');
19
+require_once('class-wordlift-metabox-field-select.php');
20 20
 
21 21
 /**
22 22
  * Define the {@link WL_Metabox} class.
@@ -52,11 +52,11 @@  discard block
 block discarded – undo
52 52
 	 */
53 53
 	public function __construct() {
54 54
 
55
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
55
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
56 56
 
57 57
 		// Add hooks to print metaboxes and save submitted data.
58
-		add_action( 'add_meta_boxes', array( $this, 'add_main_metabox' ) );
59
-		add_action( 'wl_linked_data_save_post', array( $this, 'save_form_data', ) );
58
+		add_action('add_meta_boxes', array($this, 'add_main_metabox'));
59
+		add_action('wl_linked_data_save_post', array($this, 'save_form_data',));
60 60
 
61 61
 		// Enqueue js and css.
62 62
 		$this->enqueue_scripts_and_styles();
@@ -70,23 +70,23 @@  discard block
 block discarded – undo
70 70
 	public function add_main_metabox() {
71 71
 
72 72
 		// Build the fields we need to print.
73
-		$this->instantiate_fields( get_the_ID() );
73
+		$this->instantiate_fields(get_the_ID());
74 74
 
75 75
 		// Bailout if there are no actual fields, we do not need a metabox in that case.
76
-		if ( empty( $this->fields ) ) {
76
+		if (empty($this->fields)) {
77 77
 			return;
78 78
 		}
79 79
 
80 80
 		// Add main metabox (will print also the inner fields).
81
-		$id    = uniqid( 'wl-metabox-' );
82
-		$title = get_the_title() . ' ' . __( 'properties', 'wordlift' );
81
+		$id    = uniqid('wl-metabox-');
82
+		$title = get_the_title().' '.__('properties', 'wordlift');
83 83
 
84 84
 		// WordPress 4.2 do not accept an array of screens as parameter, have to do be explicit.
85
-		foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $screen ) {
86
-			add_meta_box( $id, $title, array(
85
+		foreach (Wordlift_Entity_Service::valid_entity_post_types() as $screen) {
86
+			add_meta_box($id, $title, array(
87 87
 				$this,
88 88
 				'html',
89
-			), $screen, 'normal', 'high' );
89
+			), $screen, 'normal', 'high');
90 90
 		}
91 91
 
92 92
 		// Add filter to change the metabox CSS class.
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 		//  dynamically according to the `get_current_screen()` function.
96 96
 		$current_screen = get_current_screen();
97 97
 		$screen         = $current_screen ? $current_screen->post_type : 'entity';
98
-		add_filter( "postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class' );
98
+		add_filter("postbox_classes_{$screen}_$id", 'wl_admin_metaboxes_add_css_class');
99 99
 
100 100
 	}
101 101
 
@@ -107,10 +107,10 @@  discard block
 block discarded – undo
107 107
 	 * @since 3.1.0
108 108
 	 *
109 109
 	 */
110
-	public function html( $post ) {
110
+	public function html($post) {
111 111
 
112 112
 		// Loop over the fields.
113
-		foreach ( $this->fields as $field ) {
113
+		foreach ($this->fields as $field) {
114 114
 
115 115
 			// load data from DB (values will be available in $field->data).
116 116
 			$field->get_data();
@@ -132,18 +132,18 @@  discard block
 block discarded – undo
132 132
 	 * @since 3.1.0
133 133
 	 *
134 134
 	 */
135
-	public function instantiate_fields( $post_id ) {
135
+	public function instantiate_fields($post_id) {
136 136
 
137
-		$this->log->trace( "Instantiating fields for entity post $post_id..." );
137
+		$this->log->trace("Instantiating fields for entity post $post_id...");
138 138
 
139 139
 		// This function must be called only once. Not called from the constructor because WP hooks have a rococo ordering.
140
-		if ( isset( $this->fields ) ) {
140
+		if (isset($this->fields)) {
141 141
 			return;
142 142
 		}
143 143
 
144
-		$entity_type = wl_entity_taxonomy_get_custom_fields( $post_id );
144
+		$entity_type = wl_entity_taxonomy_get_custom_fields($post_id);
145 145
 
146
-		if ( isset( $entity_type ) ) {
146
+		if (isset($entity_type)) {
147 147
 
148 148
 			/*
149 149
 			 * Might not have any relevant meta box field, for example for articles,
@@ -158,31 +158,31 @@  discard block
 block discarded – undo
158 158
 			 * - simple: accept values for one property
159 159
 			 * - grouped: accept values for more properties, or for one property that needs a specific metabox.
160 160
 			 */
161
-			$metaboxes         = $this->group_properties_by_input_field( $entity_type );
161
+			$metaboxes         = $this->group_properties_by_input_field($entity_type);
162 162
 			$simple_metaboxes  = $metaboxes[0];
163 163
 			$grouped_metaboxes = $metaboxes[1];
164 164
 
165 165
 			// Loop over simple entity properties.
166
-			foreach ( $simple_metaboxes as $key => $property ) {
166
+			foreach ($simple_metaboxes as $key => $property) {
167 167
 
168 168
 				// Info passed to the metabox.
169 169
 				$info         = array();
170
-				$info[ $key ] = $property;
170
+				$info[$key] = $property;
171 171
 
172 172
 				// Build the requested field as WL_Metabox_Field_ object.
173
-				$this->add_field( $info );
173
+				$this->add_field($info);
174 174
 
175 175
 			}
176 176
 
177 177
 			// Loop over grouped properties.
178
-			foreach ( $grouped_metaboxes as $key => $property ) {
178
+			foreach ($grouped_metaboxes as $key => $property) {
179 179
 
180 180
 				// Info passed to the metabox.
181 181
 				$info         = array();
182
-				$info[ $key ] = $property;
182
+				$info[$key] = $property;
183 183
 
184 184
 				// Build the requested field group as WL_Metabox_Field_ object.
185
-				$this->add_field( $info, true );
185
+				$this->add_field($info, true);
186 186
 
187 187
 			}
188 188
 		}
@@ -196,34 +196,34 @@  discard block
 block discarded – undo
196 196
 	 *
197 197
 	 * @return array
198 198
 	 */
199
-	public function group_properties_by_input_field( $custom_fields ) {
199
+	public function group_properties_by_input_field($custom_fields) {
200 200
 
201 201
 		$simple_properties  = array();
202 202
 		$grouped_properties = array();
203 203
 
204 204
 		// Loop over possible entity properties.
205
-		foreach ( $custom_fields as $key => $property ) {
205
+		foreach ($custom_fields as $key => $property) {
206 206
 
207 207
 			// Check presence of predicate and type.
208
-			if ( isset( $property['predicate'] ) && isset( $property['type'] ) ) {
208
+			if (isset($property['predicate']) && isset($property['type'])) {
209 209
 
210 210
 				// Check if input_field is defined.
211
-				if ( isset( $property['input_field'] ) && '' !== $property['input_field'] ) {
211
+				if (isset($property['input_field']) && '' !== $property['input_field']) {
212 212
 
213 213
 					$grouped_key = $property['input_field'];
214 214
 
215 215
 					// Update list of grouped properties.
216
-					$grouped_properties[ $grouped_key ][ $key ] = $property;
216
+					$grouped_properties[$grouped_key][$key] = $property;
217 217
 
218 218
 				} else {
219 219
 
220 220
 					// input_field not defined, add simple metabox.
221
-					$simple_properties[ $key ] = $property;
221
+					$simple_properties[$key] = $property;
222 222
 				}
223 223
 			}
224 224
 		}
225 225
 
226
-		return array( $simple_properties, $grouped_properties );
226
+		return array($simple_properties, $grouped_properties);
227 227
 	}
228 228
 
229 229
 	/**
@@ -233,29 +233,29 @@  discard block
 block discarded – undo
233 233
 	 * @param array $args The field's information.
234 234
 	 * @param bool $grouped Flag to distinguish between simple and grouped fields.
235 235
 	 */
236
-	public function add_field( $args, $grouped = false ) {
236
+	public function add_field($args, $grouped = false) {
237 237
 
238
-		if ( $grouped ) {
238
+		if ($grouped) {
239 239
 
240 240
 			// Special fields (sameas, coordinates, etc.).
241 241
 			//
242 242
 			// Build Field with a custom class (e.g. WL_Metabox_Field_date).
243
-			$field_class = 'WL_Metabox_Field_' . key( $args );
243
+			$field_class = 'WL_Metabox_Field_'.key($args);
244 244
 
245 245
 		} else {
246 246
 
247 247
 			// Simple fields (string, uri, boolean, etc.).
248 248
 			//
249 249
 			// Which field? We want to use the class that is specific for the field.
250
-			$meta      = key( $args );
251
-			$this_meta = $args[ $meta ];
250
+			$meta      = key($args);
251
+			$this_meta = $args[$meta];
252 252
 
253 253
 			// If the field declares what metabox it wants, use that one.
254
-			if ( isset( $this_meta['metabox']['class'] ) ) {
254
+			if (isset($this_meta['metabox']['class'])) {
255 255
 
256 256
 				$field_class = $this_meta['metabox']['class'];
257 257
 
258
-			} elseif ( ! isset( $this_meta['type'] ) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type'] ) {
258
+			} elseif ( ! isset($this_meta['type']) || Wordlift_Schema_Service::DATA_TYPE_STRING === $this_meta['type']) {
259 259
 
260 260
 				// TODO: all fields should explicitly declare the required WL_Metabox.
261 261
 				// When they will remove this.
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
 				// When they will remove this.
270 270
 				//
271 271
 				// Build Field with a custom class (e.g. WL_Metabox_Field_date).
272
-				$field_class = 'WL_Metabox_Field_' . $this_meta['type'];
272
+				$field_class = 'WL_Metabox_Field_'.$this_meta['type'];
273 273
 
274 274
 			}
275 275
 
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
 		// End if().
278 278
 
279 279
 		// Call apropriate constructor (e.g. WL_Metabox_Field_... ).
280
-		$this->fields[] = new $field_class( $args );
280
+		$this->fields[] = new $field_class($args);
281 281
 	}
282 282
 
283 283
 	/**
@@ -288,39 +288,39 @@  discard block
 block discarded – undo
288 288
 	 * @since 3.5.4
289 289
 	 *
290 290
 	 */
291
-	public function save_form_data( $entity_id ) {
291
+	public function save_form_data($entity_id) {
292 292
 
293
-		$this->log->trace( "Saving form data for entity post $entity_id..." );
293
+		$this->log->trace("Saving form data for entity post $entity_id...");
294 294
 
295 295
 		// Build Field objects.
296
-		$this->instantiate_fields( $entity_id );
296
+		$this->instantiate_fields($entity_id);
297 297
 
298 298
 		// Check if WL metabox form was posted.
299
-		if ( ! isset( $_POST['wl_metaboxes'] ) ) {
300
-			$this->log->debug( "`wl_metaboxes`, skipping..." );
299
+		if ( ! isset($_POST['wl_metaboxes'])) {
300
+			$this->log->debug("`wl_metaboxes`, skipping...");
301 301
 
302 302
 			return;
303 303
 		}
304 304
 
305
-		foreach ( $this->fields as $field ) {
305
+		foreach ($this->fields as $field) {
306 306
 
307 307
 			// Verify nonce.
308 308
 			$valid_nonce = $field->verify_nonce();
309
-			if ( $valid_nonce ) {
309
+			if ($valid_nonce) {
310 310
 
311 311
 				$posted_data = $_POST['wl_metaboxes'];
312 312
 				$field_name  = $field->meta_name;
313 313
 
314 314
 				// Each Filed only deals with its values.
315
-				if ( isset( $posted_data[ $field_name ] ) ) {
315
+				if (isset($posted_data[$field_name])) {
316 316
 
317
-					$values = $posted_data[ $field_name ];
318
-					if ( ! is_array( $values ) ) {
319
-						$values = array( $values );
317
+					$values = $posted_data[$field_name];
318
+					if ( ! is_array($values)) {
319
+						$values = array($values);
320 320
 					}
321 321
 
322 322
 					// Save data permanently
323
-					$field->save_data( $values );
323
+					$field->save_data($values);
324 324
 				}
325 325
 			}
326 326
 		}
@@ -335,9 +335,9 @@  discard block
 block discarded – undo
335 335
 		 * @since  3.18.2
336 336
 		 *
337 337
 		 */
338
-		do_action( 'wl_save_form_pre_push_entity', $entity_id, $_POST );
338
+		do_action('wl_save_form_pre_push_entity', $entity_id, $_POST);
339 339
 
340
-		Wordlift_Linked_Data_Service::get_instance()->push( $entity_id );
340
+		Wordlift_Linked_Data_Service::get_instance()->push($entity_id);
341 341
 
342 342
 	}
343 343
 
@@ -349,20 +349,20 @@  discard block
 block discarded – undo
349 349
 	public function enqueue_scripts_and_styles() {
350 350
 
351 351
 		// Use the minified version if PW_DEBUG isn't set.
352
-		$min = ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ? '.min' : '';
352
+		$min = ! defined('WP_DEBUG') || ! WP_DEBUG ? '.min' : '';
353 353
 
354 354
 		// Load the jquery-ui-timepicker-addon library.
355
-		wp_enqueue_style( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.css", array(), '3.0.6' );
356
-		wp_enqueue_script( 'wl-flatpickr', dirname( plugin_dir_url( __FILE__ ) ) . "/js/flatpickr/flatpickr$min.js", array( 'jquery' ), '3.0.6', true );
355
+		wp_enqueue_style('wl-flatpickr', dirname(plugin_dir_url(__FILE__))."/js/flatpickr/flatpickr$min.css", array(), '3.0.6');
356
+		wp_enqueue_script('wl-flatpickr', dirname(plugin_dir_url(__FILE__))."/js/flatpickr/flatpickr$min.js", array('jquery'), '3.0.6', true);
357 357
 
358 358
 		// Leaflet.
359
-		wp_enqueue_style( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0' );
360
-		wp_enqueue_script( 'wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0' );
359
+		wp_enqueue_style('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', array(), '1.6.0');
360
+		wp_enqueue_script('wl-leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', array(), '1.6.0');
361 361
 
362 362
 		// Add AJAX autocomplete to facilitate metabox editing.
363
-		wp_enqueue_script( 'wl-entity-metabox-utility', dirname( plugin_dir_url( __FILE__ ) ) . '/js/wl_entity_metabox_utilities.js' );
364
-		wp_localize_script( 'wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
365
-				'ajax_url' => admin_url( 'admin-ajax.php' ),
363
+		wp_enqueue_script('wl-entity-metabox-utility', dirname(plugin_dir_url(__FILE__)).'/js/wl_entity_metabox_utilities.js');
364
+		wp_localize_script('wl-entity-metabox-utility', 'wlEntityMetaboxParams', array(
365
+				'ajax_url' => admin_url('admin-ajax.php'),
366 366
 				'action'   => 'entity_by_title',
367 367
 			)
368 368
 		);
Please login to merge, or discard this patch.
src/admin/wordlift_admin_meta_box_entities.php 2 patches
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -10,16 +10,16 @@  discard block
 block discarded – undo
10 10
  */
11 11
 function wl_register_metaboxes() {
12 12
 
13
-	// Load metabox classes.
14
-	require_once( 'WL_Metabox/class-wl-metabox.php' );
13
+    // Load metabox classes.
14
+    require_once( 'WL_Metabox/class-wl-metabox.php' );
15 15
 
16
-	new WL_Metabox();     // Everything is done inside here with the correct timing.
16
+    new WL_Metabox();     // Everything is done inside here with the correct timing.
17 17
 
18 18
 }
19 19
 
20 20
 if ( is_admin() ) {
21
-	add_action( 'load-post.php', 'wl_register_metaboxes' );
22
-	add_action( 'load-post-new.php', 'wl_register_metaboxes' );
21
+    add_action( 'load-post.php', 'wl_register_metaboxes' );
22
+    add_action( 'load-post-new.php', 'wl_register_metaboxes' );
23 23
 }
24 24
 
25 25
 /**
@@ -29,28 +29,28 @@  discard block
 block discarded – undo
29 29
  */
30 30
 function wl_admin_add_entities_meta_box( $post_type ) {
31 31
 
32
-	/*
32
+    /*
33 33
 	 * Call the `wl_can_see_classification_box` filter to determine whether we can display the classification box.
34 34
 	 *
35 35
 	 * @since 3.20.3
36 36
 	 *
37 37
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/914
38 38
 	 */
39
-	if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
40
-		return;
41
-	}
42
-
43
-	// Bail out if the post type doesn't support a TinyMCE editor.
44
-	if ( ! wl_post_type_supports_editor( $post_type ) ) {
45
-		return;
46
-	}
47
-
48
-	if ( ! Wordlift_Admin::is_gutenberg() ) {
49
-		// Add main meta box for related entities and 4W only if not Gutenberg
50
-		add_meta_box(
51
-			'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
52
-		);
53
-	}
39
+    if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
40
+        return;
41
+    }
42
+
43
+    // Bail out if the post type doesn't support a TinyMCE editor.
44
+    if ( ! wl_post_type_supports_editor( $post_type ) ) {
45
+        return;
46
+    }
47
+
48
+    if ( ! Wordlift_Admin::is_gutenberg() ) {
49
+        // Add main meta box for related entities and 4W only if not Gutenberg
50
+        add_meta_box(
51
+            'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
52
+        );
53
+    }
54 54
 
55 55
 }
56 56
 
@@ -67,19 +67,19 @@  discard block
 block discarded – undo
67 67
  */
68 68
 function wl_post_type_supports_editor( $post_type ) {
69 69
 
70
-	$default = post_type_supports( $post_type, 'editor' );
71
-
72
-	/**
73
-	 * Allow 3rd parties to force the classification to load.
74
-	 *
75
-	 * @param bool $default The preset value as gathered by the `post_type_supports` call.
76
-	 *
77
-	 * @see https://github.com/insideout10/wordlift-plugin/issues/847.
78
-	 *
79
-	 * @since 3.19.4
80
-	 *
81
-	 */
82
-	return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
70
+    $default = post_type_supports( $post_type, 'editor' );
71
+
72
+    /**
73
+     * Allow 3rd parties to force the classification to load.
74
+     *
75
+     * @param bool $default The preset value as gathered by the `post_type_supports` call.
76
+     *
77
+     * @see https://github.com/insideout10/wordlift-plugin/issues/847.
78
+     *
79
+     * @since 3.19.4
80
+     *
81
+     */
82
+    return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
83 83
 }
84 84
 
85 85
 /**
@@ -89,135 +89,135 @@  discard block
 block discarded – undo
89 89
  */
90 90
 function wl_entities_box_content( $post, $wrapper = true ) {
91 91
 
92
-	// Angularjs edit-post widget wrapper.
93
-	if ( $wrapper ) {
94
-		echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
95
-	}
92
+    // Angularjs edit-post widget wrapper.
93
+    if ( $wrapper ) {
94
+        echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
95
+    }
96 96
 }
97 97
 
98 98
 function wl_entities_box_content_scripts() {
99
-	$post = get_post();
99
+    $post = get_post();
100 100
 
101
-	// Angularjs edit-post widget classification boxes configuration.
102
-	$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
101
+    // Angularjs edit-post widget classification boxes configuration.
102
+    $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
103 103
 
104
-	// Array to store all related entities ids.
105
-	$all_referenced_entities_ids = array();
104
+    // Array to store all related entities ids.
105
+    $all_referenced_entities_ids = array();
106 106
 
107
-	// Add selected entities to classification_boxes.
108
-	foreach ( $classification_boxes as $i => $box ) {
109
-		// Build the proper relation name.
110
-		$relation_name = $box['id'];
107
+    // Add selected entities to classification_boxes.
108
+    foreach ( $classification_boxes as $i => $box ) {
109
+        // Build the proper relation name.
110
+        $relation_name = $box['id'];
111 111
 
112
-		// Get the entity referenced from the post content.
113
-		/*
112
+        // Get the entity referenced from the post content.
113
+        /*
114 114
 		 * Allow 3rd parties to provide another post content.
115 115
 		 *
116 116
 		 * @since 3.20.0
117 117
 		 */
118
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
119
-		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
120
-
121
-		// Enhance current box selected entities.
122
-		$classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
123
-
124
-		// Maps the URIs to entity posts.
125
-		$entity_service = Wordlift_Entity_Service::get_instance();
126
-
127
-		// Replace all entity URI's with post ID's if found or null if there is no related post.
128
-		$entity_ids = array_map( function ( $item ) use ( $entity_service ) {
129
-			// Return entity post by the entity URI or null.
130
-			$post = $entity_service->get_entity_post_by_uri( $item );
131
-
132
-			// Check that the post object is not null.
133
-			if ( ! empty( $post ) ) {
134
-				return $post->ID;
135
-			}
136
-		}, $entity_uris );
137
-		// Store the entity ids for all the 4W.
138
-		$all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
139
-
140
-	}
141
-
142
-	// Json encoding for classification boxes structure.
143
-	$classification_boxes = wp_json_encode( $classification_boxes );
144
-
145
-	// Ensure there are no repetitions of the referenced entities.
146
-	$all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
147
-
148
-	// Remove all null, false and empty strings.
149
-	// NULL is being returned in some cases, when there is not related post, so we need to remove it.
150
-	$all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
151
-
152
-	// Build the entity storage object.
153
-	$referenced_entities_obj = array();
154
-	foreach ( $all_referenced_entities_ids as $referenced_entity ) {
155
-		$entity = wl_serialize_entity( $referenced_entity );
156
-		// Set a default confidence of `PHP_INT_MAX` for already annotated entities.
157
-		$referenced_entities_obj[ $entity['id'] ] = $entity
158
-		                                            + array( 'confidence' => PHP_INT_MAX );
159
-	}
160
-
161
-	$referenced_entities_obj = empty( $referenced_entities_obj ) ?
162
-		'{}' : wp_json_encode( $referenced_entities_obj );
163
-
164
-	$published_place_id  = get_post_meta(
165
-		$post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
166
-	);
167
-	$published_place_obj = ( $published_place_id ) ?
168
-		wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
169
-		null;
170
-
171
-	$topic_id  = get_post_meta(
172
-		$post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
173
-	);
174
-	$topic_obj = ( $topic_id ) ?
175
-		wp_json_encode( wl_serialize_entity( $topic_id ) ) :
176
-		null;
177
-
178
-	$configuration_service = Wordlift_Configuration_Service::get_instance();
179
-
180
-	$default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
181
-	$default_path           = WL_DEFAULT_PATH;
182
-	$dataset_uri            = $configuration_service->get_dataset_uri();
183
-	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
184
-	$is_entity              = Wordlift_Entity_Service::get_instance()->is_entity( $post->ID );
185
-
186
-	// Retrieve the current post author.
187
-	$post_author = get_userdata( $post->post_author )->display_name;
188
-	// Retrive the published date.
189
-	$published_date = get_the_time( 'Y-m-d', $post->ID );
190
-	// Current language.
191
-	$current_language            = $configuration_service->get_language_code();
192
-	$wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
193
-	$timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
194
-	$addslashes_post_author      = addslashes( $post_author );
195
-
196
-	$metabox_settings = array(
197
-		"classificationBoxes"      => json_decode( $classification_boxes ),
198
-		"entities"                 => json_decode( $referenced_entities_obj ),
199
-		"currentPostId"            => intval( $post->ID ),
200
-		"currentPostUri"           => $current_post_uri,
201
-		"currentPostType"          => $post->post_type,
202
-		"isEntity"                 => ! empty( $is_entity ),
203
-		"defaultThumbnailPath"     => $default_thumbnail_path,
204
-		"defaultWordLiftPath"      => $default_path,
205
-		"datasetUri"               => $dataset_uri,
206
-		"currentUser"              => $addslashes_post_author,
207
-		"publishedDate"            => $published_date,
208
-		"publishedPlace"           => $published_place_obj,
209
-		"topic"                    => json_decode( $topic_obj ),
210
-		"currentLanguage"          => $current_language,
211
-		"timelinejsDefaultOptions" => json_decode( $timelinejs_default_options ),
212
-		"ajax_url"                 => admin_url( 'admin-ajax.php' ),
213
-	);
214
-
215
-	// Allow Classic and Block Editor scripts to register first.
216
-	// Hook to the Block Editor script.
217
-	wp_localize_script( 'wl-block-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
218
-
219
-	// Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page.
220
-	wp_localize_script( 'wl-classic-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
118
+        $post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
119
+        $entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
120
+
121
+        // Enhance current box selected entities.
122
+        $classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
123
+
124
+        // Maps the URIs to entity posts.
125
+        $entity_service = Wordlift_Entity_Service::get_instance();
126
+
127
+        // Replace all entity URI's with post ID's if found or null if there is no related post.
128
+        $entity_ids = array_map( function ( $item ) use ( $entity_service ) {
129
+            // Return entity post by the entity URI or null.
130
+            $post = $entity_service->get_entity_post_by_uri( $item );
131
+
132
+            // Check that the post object is not null.
133
+            if ( ! empty( $post ) ) {
134
+                return $post->ID;
135
+            }
136
+        }, $entity_uris );
137
+        // Store the entity ids for all the 4W.
138
+        $all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
139
+
140
+    }
141
+
142
+    // Json encoding for classification boxes structure.
143
+    $classification_boxes = wp_json_encode( $classification_boxes );
144
+
145
+    // Ensure there are no repetitions of the referenced entities.
146
+    $all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
147
+
148
+    // Remove all null, false and empty strings.
149
+    // NULL is being returned in some cases, when there is not related post, so we need to remove it.
150
+    $all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
151
+
152
+    // Build the entity storage object.
153
+    $referenced_entities_obj = array();
154
+    foreach ( $all_referenced_entities_ids as $referenced_entity ) {
155
+        $entity = wl_serialize_entity( $referenced_entity );
156
+        // Set a default confidence of `PHP_INT_MAX` for already annotated entities.
157
+        $referenced_entities_obj[ $entity['id'] ] = $entity
158
+                                                    + array( 'confidence' => PHP_INT_MAX );
159
+    }
160
+
161
+    $referenced_entities_obj = empty( $referenced_entities_obj ) ?
162
+        '{}' : wp_json_encode( $referenced_entities_obj );
163
+
164
+    $published_place_id  = get_post_meta(
165
+        $post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
166
+    );
167
+    $published_place_obj = ( $published_place_id ) ?
168
+        wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
169
+        null;
170
+
171
+    $topic_id  = get_post_meta(
172
+        $post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
173
+    );
174
+    $topic_obj = ( $topic_id ) ?
175
+        wp_json_encode( wl_serialize_entity( $topic_id ) ) :
176
+        null;
177
+
178
+    $configuration_service = Wordlift_Configuration_Service::get_instance();
179
+
180
+    $default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
181
+    $default_path           = WL_DEFAULT_PATH;
182
+    $dataset_uri            = $configuration_service->get_dataset_uri();
183
+    $current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
184
+    $is_entity              = Wordlift_Entity_Service::get_instance()->is_entity( $post->ID );
185
+
186
+    // Retrieve the current post author.
187
+    $post_author = get_userdata( $post->post_author )->display_name;
188
+    // Retrive the published date.
189
+    $published_date = get_the_time( 'Y-m-d', $post->ID );
190
+    // Current language.
191
+    $current_language            = $configuration_service->get_language_code();
192
+    $wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
193
+    $timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
194
+    $addslashes_post_author      = addslashes( $post_author );
195
+
196
+    $metabox_settings = array(
197
+        "classificationBoxes"      => json_decode( $classification_boxes ),
198
+        "entities"                 => json_decode( $referenced_entities_obj ),
199
+        "currentPostId"            => intval( $post->ID ),
200
+        "currentPostUri"           => $current_post_uri,
201
+        "currentPostType"          => $post->post_type,
202
+        "isEntity"                 => ! empty( $is_entity ),
203
+        "defaultThumbnailPath"     => $default_thumbnail_path,
204
+        "defaultWordLiftPath"      => $default_path,
205
+        "datasetUri"               => $dataset_uri,
206
+        "currentUser"              => $addslashes_post_author,
207
+        "publishedDate"            => $published_date,
208
+        "publishedPlace"           => $published_place_obj,
209
+        "topic"                    => json_decode( $topic_obj ),
210
+        "currentLanguage"          => $current_language,
211
+        "timelinejsDefaultOptions" => json_decode( $timelinejs_default_options ),
212
+        "ajax_url"                 => admin_url( 'admin-ajax.php' ),
213
+    );
214
+
215
+    // Allow Classic and Block Editor scripts to register first.
216
+    // Hook to the Block Editor script.
217
+    wp_localize_script( 'wl-block-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
218
+
219
+    // Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page.
220
+    wp_localize_script( 'wl-classic-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
221 221
 
222 222
 }
223 223
 
Please login to merge, or discard this patch.
Spacing   +56 added lines, -58 removed lines patch added patch discarded remove patch
@@ -11,15 +11,15 @@  discard block
 block discarded – undo
11 11
 function wl_register_metaboxes() {
12 12
 
13 13
 	// Load metabox classes.
14
-	require_once( 'WL_Metabox/class-wl-metabox.php' );
14
+	require_once('WL_Metabox/class-wl-metabox.php');
15 15
 
16
-	new WL_Metabox();     // Everything is done inside here with the correct timing.
16
+	new WL_Metabox(); // Everything is done inside here with the correct timing.
17 17
 
18 18
 }
19 19
 
20
-if ( is_admin() ) {
21
-	add_action( 'load-post.php', 'wl_register_metaboxes' );
22
-	add_action( 'load-post-new.php', 'wl_register_metaboxes' );
20
+if (is_admin()) {
21
+	add_action('load-post.php', 'wl_register_metaboxes');
22
+	add_action('load-post-new.php', 'wl_register_metaboxes');
23 23
 }
24 24
 
25 25
 /**
@@ -27,7 +27,7 @@  discard block
 block discarded – undo
27 27
  *
28 28
  * @param string $post_type The type of the current open post.
29 29
  */
30
-function wl_admin_add_entities_meta_box( $post_type ) {
30
+function wl_admin_add_entities_meta_box($post_type) {
31 31
 
32 32
 	/*
33 33
 	 * Call the `wl_can_see_classification_box` filter to determine whether we can display the classification box.
@@ -36,25 +36,25 @@  discard block
 block discarded – undo
36 36
 	 *
37 37
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/914
38 38
 	 */
39
-	if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
39
+	if ( ! apply_filters('wl_can_see_classification_box', true)) {
40 40
 		return;
41 41
 	}
42 42
 
43 43
 	// Bail out if the post type doesn't support a TinyMCE editor.
44
-	if ( ! wl_post_type_supports_editor( $post_type ) ) {
44
+	if ( ! wl_post_type_supports_editor($post_type)) {
45 45
 		return;
46 46
 	}
47 47
 
48
-	if ( ! Wordlift_Admin::is_gutenberg() ) {
48
+	if ( ! Wordlift_Admin::is_gutenberg()) {
49 49
 		// Add main meta box for related entities and 4W only if not Gutenberg
50 50
 		add_meta_box(
51
-			'wordlift_entities_box', __( 'WordLift', 'wordlift' ), 'wl_entities_box_content', $post_type, 'side', 'high'
51
+			'wordlift_entities_box', __('WordLift', 'wordlift'), 'wl_entities_box_content', $post_type, 'side', 'high'
52 52
 		);
53 53
 	}
54 54
 
55 55
 }
56 56
 
57
-add_action( 'add_meta_boxes', 'wl_admin_add_entities_meta_box' );
57
+add_action('add_meta_boxes', 'wl_admin_add_entities_meta_box');
58 58
 
59 59
 /**
60 60
  * Whether the post type supports the editor UI.
@@ -65,9 +65,9 @@  discard block
 block discarded – undo
65 65
  *
66 66
  * @return bool True if the editor UI is supported otherwise false.
67 67
  */
68
-function wl_post_type_supports_editor( $post_type ) {
68
+function wl_post_type_supports_editor($post_type) {
69 69
 
70
-	$default = post_type_supports( $post_type, 'editor' );
70
+	$default = post_type_supports($post_type, 'editor');
71 71
 
72 72
 	/**
73 73
 	 * Allow 3rd parties to force the classification to load.
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 	 * @since 3.19.4
80 80
 	 *
81 81
 	 */
82
-	return apply_filters( 'wl_post_type_supports_editor', $default, $post_type );
82
+	return apply_filters('wl_post_type_supports_editor', $default, $post_type);
83 83
 }
84 84
 
85 85
 /**
@@ -87,10 +87,10 @@  discard block
 block discarded – undo
87 87
  *
88 88
  * @param WP_Post $post The current post.
89 89
  */
90
-function wl_entities_box_content( $post, $wrapper = true ) {
90
+function wl_entities_box_content($post, $wrapper = true) {
91 91
 
92 92
 	// Angularjs edit-post widget wrapper.
93
-	if ( $wrapper ) {
93
+	if ($wrapper) {
94 94
 		echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
95 95
 	}
96 96
 }
@@ -99,13 +99,13 @@  discard block
 block discarded – undo
99 99
 	$post = get_post();
100 100
 
101 101
 	// Angularjs edit-post widget classification boxes configuration.
102
-	$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
102
+	$classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES);
103 103
 
104 104
 	// Array to store all related entities ids.
105 105
 	$all_referenced_entities_ids = array();
106 106
 
107 107
 	// Add selected entities to classification_boxes.
108
-	foreach ( $classification_boxes as $i => $box ) {
108
+	foreach ($classification_boxes as $i => $box) {
109 109
 		// Build the proper relation name.
110 110
 		$relation_name = $box['id'];
111 111
 
@@ -115,111 +115,109 @@  discard block
 block discarded – undo
115 115
 		 *
116 116
 		 * @since 3.20.0
117 117
 		 */
118
-		$post_content = apply_filters( 'wl_post_content', $post->post_content, $post );
119
-		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris( $post_content );
118
+		$post_content = apply_filters('wl_post_content', $post->post_content, $post);
119
+		$entity_uris  = Wordlift_Content_Filter_Service::get_instance()->get_entity_uris($post_content);
120 120
 
121 121
 		// Enhance current box selected entities.
122
-		$classification_boxes[ $i ]['selectedEntities'] = $entity_uris;
122
+		$classification_boxes[$i]['selectedEntities'] = $entity_uris;
123 123
 
124 124
 		// Maps the URIs to entity posts.
125 125
 		$entity_service = Wordlift_Entity_Service::get_instance();
126 126
 
127 127
 		// Replace all entity URI's with post ID's if found or null if there is no related post.
128
-		$entity_ids = array_map( function ( $item ) use ( $entity_service ) {
128
+		$entity_ids = array_map(function($item) use ($entity_service) {
129 129
 			// Return entity post by the entity URI or null.
130
-			$post = $entity_service->get_entity_post_by_uri( $item );
130
+			$post = $entity_service->get_entity_post_by_uri($item);
131 131
 
132 132
 			// Check that the post object is not null.
133
-			if ( ! empty( $post ) ) {
133
+			if ( ! empty($post)) {
134 134
 				return $post->ID;
135 135
 			}
136
-		}, $entity_uris );
136
+		}, $entity_uris);
137 137
 		// Store the entity ids for all the 4W.
138
-		$all_referenced_entities_ids = array_merge( $all_referenced_entities_ids, $entity_ids );
138
+		$all_referenced_entities_ids = array_merge($all_referenced_entities_ids, $entity_ids);
139 139
 
140 140
 	}
141 141
 
142 142
 	// Json encoding for classification boxes structure.
143
-	$classification_boxes = wp_json_encode( $classification_boxes );
143
+	$classification_boxes = wp_json_encode($classification_boxes);
144 144
 
145 145
 	// Ensure there are no repetitions of the referenced entities.
146
-	$all_referenced_entities_ids = array_unique( $all_referenced_entities_ids );
146
+	$all_referenced_entities_ids = array_unique($all_referenced_entities_ids);
147 147
 
148 148
 	// Remove all null, false and empty strings.
149 149
 	// NULL is being returned in some cases, when there is not related post, so we need to remove it.
150
-	$all_referenced_entities_ids = array_filter( $all_referenced_entities_ids );
150
+	$all_referenced_entities_ids = array_filter($all_referenced_entities_ids);
151 151
 
152 152
 	// Build the entity storage object.
153 153
 	$referenced_entities_obj = array();
154
-	foreach ( $all_referenced_entities_ids as $referenced_entity ) {
155
-		$entity = wl_serialize_entity( $referenced_entity );
154
+	foreach ($all_referenced_entities_ids as $referenced_entity) {
155
+		$entity = wl_serialize_entity($referenced_entity);
156 156
 		// Set a default confidence of `PHP_INT_MAX` for already annotated entities.
157
-		$referenced_entities_obj[ $entity['id'] ] = $entity
158
-		                                            + array( 'confidence' => PHP_INT_MAX );
157
+		$referenced_entities_obj[$entity['id']] = $entity
158
+		                                            + array('confidence' => PHP_INT_MAX);
159 159
 	}
160 160
 
161
-	$referenced_entities_obj = empty( $referenced_entities_obj ) ?
162
-		'{}' : wp_json_encode( $referenced_entities_obj );
161
+	$referenced_entities_obj = empty($referenced_entities_obj) ?
162
+		'{}' : wp_json_encode($referenced_entities_obj);
163 163
 
164 164
 	$published_place_id  = get_post_meta(
165 165
 		$post->ID, Wordlift_Schema_Service::FIELD_LOCATION_CREATED, true
166 166
 	);
167
-	$published_place_obj = ( $published_place_id ) ?
168
-		wp_json_encode( wl_serialize_entity( $published_place_id ) ) :
169
-		null;
167
+	$published_place_obj = ($published_place_id) ?
168
+		wp_json_encode(wl_serialize_entity($published_place_id)) : null;
170 169
 
171 170
 	$topic_id  = get_post_meta(
172 171
 		$post->ID, Wordlift_Schema_Service::FIELD_TOPIC, true
173 172
 	);
174
-	$topic_obj = ( $topic_id ) ?
175
-		wp_json_encode( wl_serialize_entity( $topic_id ) ) :
176
-		null;
173
+	$topic_obj = ($topic_id) ?
174
+		wp_json_encode(wl_serialize_entity($topic_id)) : null;
177 175
 
178 176
 	$configuration_service = Wordlift_Configuration_Service::get_instance();
179 177
 
180 178
 	$default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
181 179
 	$default_path           = WL_DEFAULT_PATH;
182 180
 	$dataset_uri            = $configuration_service->get_dataset_uri();
183
-	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri( $post->ID );
184
-	$is_entity              = Wordlift_Entity_Service::get_instance()->is_entity( $post->ID );
181
+	$current_post_uri       = Wordlift_Entity_Service::get_instance()->get_uri($post->ID);
182
+	$is_entity              = Wordlift_Entity_Service::get_instance()->is_entity($post->ID);
185 183
 
186 184
 	// Retrieve the current post author.
187
-	$post_author = get_userdata( $post->post_author )->display_name;
185
+	$post_author = get_userdata($post->post_author)->display_name;
188 186
 	// Retrive the published date.
189
-	$published_date = get_the_time( 'Y-m-d', $post->ID );
187
+	$published_date = get_the_time('Y-m-d', $post->ID);
190 188
 	// Current language.
191 189
 	$current_language            = $configuration_service->get_language_code();
192 190
 	$wordlift_timeline_shortcode = new Wordlift_Timeline_Shortcode();
193
-	$timelinejs_default_options  = json_encode( $wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT );
194
-	$addslashes_post_author      = addslashes( $post_author );
191
+	$timelinejs_default_options  = json_encode($wordlift_timeline_shortcode->get_timelinejs_default_options(), JSON_PRETTY_PRINT);
192
+	$addslashes_post_author      = addslashes($post_author);
195 193
 
196 194
 	$metabox_settings = array(
197
-		"classificationBoxes"      => json_decode( $classification_boxes ),
198
-		"entities"                 => json_decode( $referenced_entities_obj ),
199
-		"currentPostId"            => intval( $post->ID ),
195
+		"classificationBoxes"      => json_decode($classification_boxes),
196
+		"entities"                 => json_decode($referenced_entities_obj),
197
+		"currentPostId"            => intval($post->ID),
200 198
 		"currentPostUri"           => $current_post_uri,
201 199
 		"currentPostType"          => $post->post_type,
202
-		"isEntity"                 => ! empty( $is_entity ),
200
+		"isEntity"                 => ! empty($is_entity),
203 201
 		"defaultThumbnailPath"     => $default_thumbnail_path,
204 202
 		"defaultWordLiftPath"      => $default_path,
205 203
 		"datasetUri"               => $dataset_uri,
206 204
 		"currentUser"              => $addslashes_post_author,
207 205
 		"publishedDate"            => $published_date,
208 206
 		"publishedPlace"           => $published_place_obj,
209
-		"topic"                    => json_decode( $topic_obj ),
207
+		"topic"                    => json_decode($topic_obj),
210 208
 		"currentLanguage"          => $current_language,
211
-		"timelinejsDefaultOptions" => json_decode( $timelinejs_default_options ),
212
-		"ajax_url"                 => admin_url( 'admin-ajax.php' ),
209
+		"timelinejsDefaultOptions" => json_decode($timelinejs_default_options),
210
+		"ajax_url"                 => admin_url('admin-ajax.php'),
213 211
 	);
214 212
 
215 213
 	// Allow Classic and Block Editor scripts to register first.
216 214
 	// Hook to the Block Editor script.
217
-	wp_localize_script( 'wl-block-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
215
+	wp_localize_script('wl-block-editor', '_wlMetaBoxSettings', array('settings' => $metabox_settings));
218 216
 
219 217
 	// Hook to the Classic Editor script, see Wordlift_Admin_Post_Edit_Page.
220
-	wp_localize_script( 'wl-classic-editor', '_wlMetaBoxSettings', array( 'settings' => $metabox_settings ) );
218
+	wp_localize_script('wl-classic-editor', '_wlMetaBoxSettings', array('settings' => $metabox_settings));
221 219
 
222 220
 }
223 221
 
224
-add_action( 'admin_print_scripts-post.php', 'wl_entities_box_content_scripts', 11 );
225
-add_action( 'admin_print_scripts-post-new.php', 'wl_entities_box_content_scripts', 11 );
222
+add_action('admin_print_scripts-post.php', 'wl_entities_box_content_scripts', 11);
223
+add_action('admin_print_scripts-post-new.php', 'wl_entities_box_content_scripts', 11);
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-type-service.php 2 patches
Indentation   +415 added lines, -415 removed lines patch added patch discarded remove patch
@@ -19,129 +19,129 @@  discard block
 block discarded – undo
19 19
  */
20 20
 class Wordlift_Entity_Type_Service {
21 21
 
22
-	/**
23
-	 * The {@link Wordlift_Schema_Service} instance.
24
-	 *
25
-	 * @since  3.7.0
26
-	 * @access private
27
-	 * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
28
-	 */
29
-	private $schema_service;
30
-
31
-	/**
32
-	 * A {@link Wordlift_Log_Service} instance.
33
-	 *
34
-	 * @since  3.8.0
35
-	 * @access private
36
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
37
-	 */
38
-	private $log;
39
-
40
-	/**
41
-	 * The {@link Wordlift_Entity_Type_Service} singleton instance.
42
-	 *
43
-	 * @since  3.7.0
44
-	 * @access private
45
-	 * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance.
46
-	 */
47
-	private static $instance;
48
-
49
-	/**
50
-	 * Wordlift_Entity_Type_Service constructor.
51
-	 *
52
-	 * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
53
-	 *
54
-	 * @since 3.7.0
55
-	 *
56
-	 */
57
-	public function __construct( $schema_service ) {
58
-
59
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
60
-
61
-		$this->schema_service = $schema_service;
62
-
63
-		self::$instance = $this;
64
-
65
-	}
66
-
67
-	/**
68
-	 * Get the {@link Wordlift_Entity_Type_Service} singleton instance.
69
-	 *
70
-	 * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance.
71
-	 * @since 3.7.0
72
-	 */
73
-	public static function get_instance() {
74
-
75
-		return self::$instance;
76
-	}
77
-
78
-	/**
79
-	 * Get the types associated with the specified entity post id.
80
-	 *
81
-	 * We have a strategy to define the entity type, given that everything is
82
-	 * an entity, i.e. also posts/pages and custom post types.
83
-	 *
84
-	 * @param int $post_id The post id.
85
-	 *
86
-	 * @return array|null {
87
-	 * An array of type properties or null if no term is associated
88
-	 *
89
-	 * @type string css_class     The css class, e.g. `wl-thing`.
90
-	 * @type string uri           The schema.org class URI, e.g. `http://schema.org/Thing`.
91
-	 * @type array  same_as       An array of same as attributes.
92
-	 * @type array  custom_fields An array of custom fields.
93
-	 * @type array  linked_data   An array of {@link Wordlift_Sparql_Tuple_Rendition}.
94
-	 * }
95
-	 * @since 3.7.0
96
-	 *
97
-	 * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0.
98
-	 *
99
-	 * @since 3.18.0 The cases are the following:
100
-	 *  1. the post has a term from the Entity Types Taxonomy: the term defines
101
-	 *     the entity type, e.g. Organization, Person, ...
102
-	 *  2. the post doesn't have a term from the Entity Types Taxonomy:
103
-	 *      a) the post is a `wl_entity` custom post type, then the post is
104
-	 *           assigned the `Thing` entity type by default.
105
-	 *      b) the post is a `post` post type, then the post is
106
-	 *           assigned the `Article` entity type by default.
107
-	 *      c) the post is a custom post type then it is
108
-	 *          assigned the `WebPage` entity type by default.
109
-	 *
110
-	 */
111
-	public function get( $post_id ) {
112
-
113
-		$this->log->trace( "Getting the post type for post $post_id..." );
114
-
115
-		// Get the post type.
116
-		$post_type = get_post_type( $post_id );
117
-
118
-		// Return `web-page` for non entities.
119
-		if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
-			$this->log->info( "Returning `web-page` for post $post_id." );
121
-
122
-			return $this->schema_service->get_schema( 'web-page' );
123
-		}
124
-
125
-		// Get the type from the associated classification.
126
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
127
-
128
-		// Return the schema type if there is a term found.
129
-		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
130
-			// Cycle through the terms and return the first one with a valid schema.
131
-			foreach ( $terms as $term ) {
132
-				$this->log->debug( "Found `{$term->slug}` term for post $post_id." );
133
-
134
-				// Try to get the schema for the term.
135
-				$schema = $this->schema_service->get_schema( $term->slug );
136
-
137
-				// If found, return it, ignoring the other types.
138
-				if ( null !== $schema ) {
139
-					// Return the entity type with the specified id.
140
-					return $schema;
141
-				}
142
-			}
143
-
144
-			/*
22
+    /**
23
+     * The {@link Wordlift_Schema_Service} instance.
24
+     *
25
+     * @since  3.7.0
26
+     * @access private
27
+     * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
28
+     */
29
+    private $schema_service;
30
+
31
+    /**
32
+     * A {@link Wordlift_Log_Service} instance.
33
+     *
34
+     * @since  3.8.0
35
+     * @access private
36
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
37
+     */
38
+    private $log;
39
+
40
+    /**
41
+     * The {@link Wordlift_Entity_Type_Service} singleton instance.
42
+     *
43
+     * @since  3.7.0
44
+     * @access private
45
+     * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance.
46
+     */
47
+    private static $instance;
48
+
49
+    /**
50
+     * Wordlift_Entity_Type_Service constructor.
51
+     *
52
+     * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance.
53
+     *
54
+     * @since 3.7.0
55
+     *
56
+     */
57
+    public function __construct( $schema_service ) {
58
+
59
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
60
+
61
+        $this->schema_service = $schema_service;
62
+
63
+        self::$instance = $this;
64
+
65
+    }
66
+
67
+    /**
68
+     * Get the {@link Wordlift_Entity_Type_Service} singleton instance.
69
+     *
70
+     * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance.
71
+     * @since 3.7.0
72
+     */
73
+    public static function get_instance() {
74
+
75
+        return self::$instance;
76
+    }
77
+
78
+    /**
79
+     * Get the types associated with the specified entity post id.
80
+     *
81
+     * We have a strategy to define the entity type, given that everything is
82
+     * an entity, i.e. also posts/pages and custom post types.
83
+     *
84
+     * @param int $post_id The post id.
85
+     *
86
+     * @return array|null {
87
+     * An array of type properties or null if no term is associated
88
+     *
89
+     * @type string css_class     The css class, e.g. `wl-thing`.
90
+     * @type string uri           The schema.org class URI, e.g. `http://schema.org/Thing`.
91
+     * @type array  same_as       An array of same as attributes.
92
+     * @type array  custom_fields An array of custom fields.
93
+     * @type array  linked_data   An array of {@link Wordlift_Sparql_Tuple_Rendition}.
94
+     * }
95
+     * @since 3.7.0
96
+     *
97
+     * @since 3.20.0 This function will **not** return entity types introduced with 3.20.0.
98
+     *
99
+     * @since 3.18.0 The cases are the following:
100
+     *  1. the post has a term from the Entity Types Taxonomy: the term defines
101
+     *     the entity type, e.g. Organization, Person, ...
102
+     *  2. the post doesn't have a term from the Entity Types Taxonomy:
103
+     *      a) the post is a `wl_entity` custom post type, then the post is
104
+     *           assigned the `Thing` entity type by default.
105
+     *      b) the post is a `post` post type, then the post is
106
+     *           assigned the `Article` entity type by default.
107
+     *      c) the post is a custom post type then it is
108
+     *          assigned the `WebPage` entity type by default.
109
+     *
110
+     */
111
+    public function get( $post_id ) {
112
+
113
+        $this->log->trace( "Getting the post type for post $post_id..." );
114
+
115
+        // Get the post type.
116
+        $post_type = get_post_type( $post_id );
117
+
118
+        // Return `web-page` for non entities.
119
+        if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
+            $this->log->info( "Returning `web-page` for post $post_id." );
121
+
122
+            return $this->schema_service->get_schema( 'web-page' );
123
+        }
124
+
125
+        // Get the type from the associated classification.
126
+        $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
127
+
128
+        // Return the schema type if there is a term found.
129
+        if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
130
+            // Cycle through the terms and return the first one with a valid schema.
131
+            foreach ( $terms as $term ) {
132
+                $this->log->debug( "Found `{$term->slug}` term for post $post_id." );
133
+
134
+                // Try to get the schema for the term.
135
+                $schema = $this->schema_service->get_schema( $term->slug );
136
+
137
+                // If found, return it, ignoring the other types.
138
+                if ( null !== $schema ) {
139
+                    // Return the entity type with the specified id.
140
+                    return $schema;
141
+                }
142
+            }
143
+
144
+            /*
145 145
 			 * When a schema isn't found, we return `thing`. Schema may not be found because
146 146
 			 * the new schema classes that we support since #852 aren't configured in the schema
147 147
 			 * service.
@@ -151,88 +151,88 @@  discard block
 block discarded – undo
151 151
 			 * @since 3.20.0
152 152
 			 */
153 153
 
154
-			return $this->schema_service->get_schema( 'thing' );
155
-		}
156
-
157
-		// If it's a page or post return `Article`.
158
-		if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
-			$this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
160
-
161
-			// Return "Article" schema type for posts.
162
-			return $this->schema_service->get_schema( 'article' );
163
-		}
164
-
165
-		// Return "Thing" schema type for entities.
166
-		$this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
167
-
168
-		// Return the entity type with the specified id.
169
-		return $this->schema_service->get_schema( 'thing' );
170
-
171
-	}
172
-
173
-	/**
174
-	 * Get the term ids of the entity types associated to the specified post.
175
-	 *
176
-	 * @param int $post_id The post id.
177
-	 *
178
-	 * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
179
-	 * @since 3.20.0
180
-	 *
181
-	 */
182
-	public function get_ids( $post_id ) {
183
-
184
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
185
-	}
186
-
187
-	/**
188
-	 * Get the camel case names of the entity types associated to the specified post.
189
-	 *
190
-	 * @param int $post_id The post id.
191
-	 *
192
-	 * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
193
-	 * @since 3.20.0
194
-	 *
195
-	 */
196
-	public function get_names( $post_id ) {
197
-
198
-		$ids = $this->get_ids( $post_id );
199
-
200
-		return array_map( function ( $id ) {
201
-			return get_term_meta( $id, '_wl_name', true );
202
-		}, $ids );
203
-	}
204
-
205
-	/**
206
-	 * Set the main type for the specified entity post, given the type URI.
207
-	 *
208
-	 * @param int $post_id The post id.
209
-	 * @param string $type_uri The type URI.
210
-	 * @param bool $replace Whether the provided type must replace the existing types, by default `true`.
211
-	 *
212
-	 * @since 3.8.0
213
-	 *
214
-	 */
215
-	public function set( $post_id, $type_uri, $replace = true ) {
216
-
217
-		// If the type URI is empty we remove the type.
218
-		if ( empty( $type_uri ) ) {
219
-			$this->log->debug( "Removing entity type for post $post_id..." );
220
-
221
-			wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
222
-
223
-			return;
224
-		}
225
-
226
-		$this->log->debug( "Setting entity type for post $post_id..." );
227
-
228
-		// if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
229
-		$term = ( 0 === strpos( $type_uri, 'wl-' ) )
230
-			// Get term by slug.
231
-			? $this->get_term_by_slug( substr( $type_uri, 3 ) )
232
-			// Get term by URI.
233
-			: $this->get_term_by_uri( $type_uri );
234
-
235
-		/*
154
+            return $this->schema_service->get_schema( 'thing' );
155
+        }
156
+
157
+        // If it's a page or post return `Article`.
158
+        if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
+            $this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
160
+
161
+            // Return "Article" schema type for posts.
162
+            return $this->schema_service->get_schema( 'article' );
163
+        }
164
+
165
+        // Return "Thing" schema type for entities.
166
+        $this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
167
+
168
+        // Return the entity type with the specified id.
169
+        return $this->schema_service->get_schema( 'thing' );
170
+
171
+    }
172
+
173
+    /**
174
+     * Get the term ids of the entity types associated to the specified post.
175
+     *
176
+     * @param int $post_id The post id.
177
+     *
178
+     * @return array|WP_Error An array of entity types ids or a {@link WP_Error}.
179
+     * @since 3.20.0
180
+     *
181
+     */
182
+    public function get_ids( $post_id ) {
183
+
184
+        return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
185
+    }
186
+
187
+    /**
188
+     * Get the camel case names of the entity types associated to the specified post.
189
+     *
190
+     * @param int $post_id The post id.
191
+     *
192
+     * @return array|WP_Error An array of entity types camel case names or a {@link WP_Error}.
193
+     * @since 3.20.0
194
+     *
195
+     */
196
+    public function get_names( $post_id ) {
197
+
198
+        $ids = $this->get_ids( $post_id );
199
+
200
+        return array_map( function ( $id ) {
201
+            return get_term_meta( $id, '_wl_name', true );
202
+        }, $ids );
203
+    }
204
+
205
+    /**
206
+     * Set the main type for the specified entity post, given the type URI.
207
+     *
208
+     * @param int $post_id The post id.
209
+     * @param string $type_uri The type URI.
210
+     * @param bool $replace Whether the provided type must replace the existing types, by default `true`.
211
+     *
212
+     * @since 3.8.0
213
+     *
214
+     */
215
+    public function set( $post_id, $type_uri, $replace = true ) {
216
+
217
+        // If the type URI is empty we remove the type.
218
+        if ( empty( $type_uri ) ) {
219
+            $this->log->debug( "Removing entity type for post $post_id..." );
220
+
221
+            wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
222
+
223
+            return;
224
+        }
225
+
226
+        $this->log->debug( "Setting entity type for post $post_id..." );
227
+
228
+        // if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
229
+        $term = ( 0 === strpos( $type_uri, 'wl-' ) )
230
+            // Get term by slug.
231
+            ? $this->get_term_by_slug( substr( $type_uri, 3 ) )
232
+            // Get term by URI.
233
+            : $this->get_term_by_uri( $type_uri );
234
+
235
+        /*
236 236
 		 * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be
237 237
 		 * connected to Articles via mentions. We realized that the client JS code is passing `wl-other` when the
238 238
 		 * entity type isn't "notable". In which case we couldn't find an entity type.
@@ -243,215 +243,215 @@  discard block
 block discarded – undo
243 243
 		 *
244 244
 		 * @since 3.23.4
245 245
 		 */
246
-		if ( false === $term ) {
247
-			$this->log->warn( "No term found for URI $type_uri, will use Thing." );
248
-
249
-			$term = $this->get_term_by_slug( 'thing' );
250
-
251
-			// We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
252
-			// is installed.
253
-			if ( false === $term ) {
254
-				return;
255
-			}
256
-		}
257
-
258
-		$this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
259
-
260
-		// `$replace` is passed to decide whether to replace or append the term.
261
-		wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
262
-
263
-	}
264
-
265
-	/**
266
-	 * Get an entity type term given its slug.
267
-	 *
268
-	 * @param string $slug The slug.
269
-	 *
270
-	 * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
271
-	 *                             or `$term` was not found.
272
-	 * @since 3.20.0
273
-	 *
274
-	 */
275
-	private function get_term_by_slug( $slug ) {
276
-
277
-		return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
278
-	}
279
-
280
-	/**
281
-	 * Get an entity type term given its URI.
282
-	 *
283
-	 * @param string $uri The uri.
284
-	 *
285
-	 * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
286
-	 *                             or `$term` was not found.
287
-	 * @since 3.20.0
288
-	 *
289
-	 */
290
-	public function get_term_by_uri( $uri ) {
291
-
292
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
293
-			'fields'     => 'all',
294
-			'get'        => 'all',
295
-			'number'     => 1,
296
-			'meta_query' => array(
297
-				array(
298
-					// Don't use a reference to Wordlift_Schemaorg_Class_Service, unless
299
-					// `WL_ALL_ENTITY_TYPES` is set to true.
300
-					'key'   => '_wl_uri',
301
-					'value' => $uri,
302
-				),
303
-			),
304
-			'orderby'    => 'term_id',
305
-			'order'      => 'ASC',
306
-		) );
307
-
308
-		return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
309
-	}
310
-
311
-	/**
312
-	 * Check whether an entity type is set for the {@link WP_Post} with the
313
-	 * specified id.
314
-	 *
315
-	 * @param int $post_id The {@link WP_Post}'s `id`.
316
-	 * @param string $uri The entity type URI.
317
-	 *
318
-	 * @return bool True if an entity type is set otherwise false.
319
-	 * @since 3.15.0
320
-	 *
321
-	 */
322
-	public function has_entity_type( $post_id, $uri = null ) {
323
-
324
-		$this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
325
-
326
-		// If an URI hasn't been specified just check whether we have at least
327
-		// one entity type.
328
-		if ( null === $uri ) {
329
-
330
-			// Get the post terms for the specified post ID.
331
-			$terms = $this->get_post_terms( $post_id );
332
-
333
-			$this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
334
-
335
-			// True if there's at least one term bound to the post.
336
-			return ( 0 < count( $terms ) );
337
-		}
338
-
339
-		$has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
340
-
341
-		$this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
342
-
343
-		// Check whether the post has an entity type with that URI.
344
-		return $has_entity_type;
345
-	}
346
-
347
-	/**
348
-	 * Get the list of entity types' terms for the specified {@link WP_Post}.
349
-	 *
350
-	 * @param int $post_id The {@link WP_Post} id.
351
-	 *
352
-	 * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
353
-	 * @since 3.15.0
354
-	 *
355
-	 */
356
-	private function get_post_terms( $post_id ) {
357
-
358
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
359
-			'hide_empty' => false,
360
-			// Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
361
-			// An issue has been opened with the AAM plugin author as well.
362
-			//
363
-			// see https://github.com/insideout10/wordlift-plugin/issues/334
364
-			// see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
365
-			'fields'     => 'all',
366
-		) );
367
-	}
368
-
369
-	/**
370
-	 * Get an entity type term given its URI.
371
-	 *
372
-	 * @param int $post_id The {@link WP_Post} id.
373
-	 * @param string $uri The entity type URI.
374
-	 *
375
-	 * @return bool True if the post has that type URI bound to it otherwise false.
376
-	 * @since 3.15.0
377
-	 *
378
-	 * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
379
-	 */
380
-	private function has_post_term_by_uri( $post_id, $uri ) {
381
-
382
-		// Get the post terms bound to the specified post.
383
-		$terms = $this->get_post_terms( $post_id );
384
-
385
-		// Look for a term if the specified URI.
386
-		foreach ( $terms as $term ) {
387
-			$term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
388
-
389
-			if ( $uri === $term_uri ) {
390
-				return true;
391
-			}
392
-		}
393
-
394
-		// Return null.
395
-		return false;
396
-	}
397
-
398
-	/**
399
-	 * Get the custom fields for a specific post.
400
-	 *
401
-	 * @param int $post_id The post ID.
402
-	 *
403
-	 * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service).
404
-	 * @since 3.25.2
405
-	 *
406
-	 */
407
-	public function get_custom_fields( $post_id ) {
408
-
409
-		// Return custom fields for this specific entity's type.
410
-		$types = $this->get_ids( $post_id );
411
-
412
-		/** @var WP_Term[] $terms */
413
-		$terms = array_filter( array_map( function ( $item ) {
414
-			return get_term( $item );
415
-		}, $types ), function ( $item ) {
416
-			return isset( $item ) && is_a( $item, 'WP_Term' );
417
-		} );
418
-
419
-		$term_slugs = array_map( function ( $item ) {
420
-			return $item->slug;
421
-		}, $terms );
422
-
423
-		$term_slugs[] = 'thing';
424
-
425
-		$schema_service = Wordlift_Schema_Service::get_instance();
426
-
427
-		return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
428
-
429
-			$schema = $schema_service->get_schema( $item );
430
-
431
-			if ( ! isset( $schema['custom_fields'] ) ) {
432
-				return $carry;
433
-			}
434
-
435
-			return $carry + $schema['custom_fields'];
436
-		}, array() );
437
-	}
438
-
439
-
440
-	/**
441
-	 * Determines whether a post type can be used for entities.
442
-	 *
443
-	 * Criteria is that the post type is public. The list of valid post types
444
-	 * can be overridden with a filter.
445
-	 *
446
-	 * @param string $post_type A post type name.
447
-	 *
448
-	 * @return bool Return true if the post type can be used for entities, otherwise false.
449
-	 * @since 3.15.0
450
-	 *
451
-	 */
452
-	public static function is_valid_entity_post_type( $post_type ) {
453
-
454
-		return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
455
-	}
246
+        if ( false === $term ) {
247
+            $this->log->warn( "No term found for URI $type_uri, will use Thing." );
248
+
249
+            $term = $this->get_term_by_slug( 'thing' );
250
+
251
+            // We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
252
+            // is installed.
253
+            if ( false === $term ) {
254
+                return;
255
+            }
256
+        }
257
+
258
+        $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
259
+
260
+        // `$replace` is passed to decide whether to replace or append the term.
261
+        wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
262
+
263
+    }
264
+
265
+    /**
266
+     * Get an entity type term given its slug.
267
+     *
268
+     * @param string $slug The slug.
269
+     *
270
+     * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
271
+     *                             or `$term` was not found.
272
+     * @since 3.20.0
273
+     *
274
+     */
275
+    private function get_term_by_slug( $slug ) {
276
+
277
+        return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
278
+    }
279
+
280
+    /**
281
+     * Get an entity type term given its URI.
282
+     *
283
+     * @param string $uri The uri.
284
+     *
285
+     * @return false|WP_Term WP_Term instance on success. Will return false if `$taxonomy` does not exist
286
+     *                             or `$term` was not found.
287
+     * @since 3.20.0
288
+     *
289
+     */
290
+    public function get_term_by_uri( $uri ) {
291
+
292
+        $terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
293
+            'fields'     => 'all',
294
+            'get'        => 'all',
295
+            'number'     => 1,
296
+            'meta_query' => array(
297
+                array(
298
+                    // Don't use a reference to Wordlift_Schemaorg_Class_Service, unless
299
+                    // `WL_ALL_ENTITY_TYPES` is set to true.
300
+                    'key'   => '_wl_uri',
301
+                    'value' => $uri,
302
+                ),
303
+            ),
304
+            'orderby'    => 'term_id',
305
+            'order'      => 'ASC',
306
+        ) );
307
+
308
+        return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
309
+    }
310
+
311
+    /**
312
+     * Check whether an entity type is set for the {@link WP_Post} with the
313
+     * specified id.
314
+     *
315
+     * @param int $post_id The {@link WP_Post}'s `id`.
316
+     * @param string $uri The entity type URI.
317
+     *
318
+     * @return bool True if an entity type is set otherwise false.
319
+     * @since 3.15.0
320
+     *
321
+     */
322
+    public function has_entity_type( $post_id, $uri = null ) {
323
+
324
+        $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
325
+
326
+        // If an URI hasn't been specified just check whether we have at least
327
+        // one entity type.
328
+        if ( null === $uri ) {
329
+
330
+            // Get the post terms for the specified post ID.
331
+            $terms = $this->get_post_terms( $post_id );
332
+
333
+            $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
334
+
335
+            // True if there's at least one term bound to the post.
336
+            return ( 0 < count( $terms ) );
337
+        }
338
+
339
+        $has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
340
+
341
+        $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
342
+
343
+        // Check whether the post has an entity type with that URI.
344
+        return $has_entity_type;
345
+    }
346
+
347
+    /**
348
+     * Get the list of entity types' terms for the specified {@link WP_Post}.
349
+     *
350
+     * @param int $post_id The {@link WP_Post} id.
351
+     *
352
+     * @return array|WP_Error An array of entity types' terms or {@link WP_Error}.
353
+     * @since 3.15.0
354
+     *
355
+     */
356
+    private function get_post_terms( $post_id ) {
357
+
358
+        return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
359
+            'hide_empty' => false,
360
+            // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
361
+            // An issue has been opened with the AAM plugin author as well.
362
+            //
363
+            // see https://github.com/insideout10/wordlift-plugin/issues/334
364
+            // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
365
+            'fields'     => 'all',
366
+        ) );
367
+    }
368
+
369
+    /**
370
+     * Get an entity type term given its URI.
371
+     *
372
+     * @param int $post_id The {@link WP_Post} id.
373
+     * @param string $uri The entity type URI.
374
+     *
375
+     * @return bool True if the post has that type URI bound to it otherwise false.
376
+     * @since 3.15.0
377
+     *
378
+     * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
379
+     */
380
+    private function has_post_term_by_uri( $post_id, $uri ) {
381
+
382
+        // Get the post terms bound to the specified post.
383
+        $terms = $this->get_post_terms( $post_id );
384
+
385
+        // Look for a term if the specified URI.
386
+        foreach ( $terms as $term ) {
387
+            $term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
388
+
389
+            if ( $uri === $term_uri ) {
390
+                return true;
391
+            }
392
+        }
393
+
394
+        // Return null.
395
+        return false;
396
+    }
397
+
398
+    /**
399
+     * Get the custom fields for a specific post.
400
+     *
401
+     * @param int $post_id The post ID.
402
+     *
403
+     * @return array An array of custom fields (see `custom_fields` in Wordlift_Schema_Service).
404
+     * @since 3.25.2
405
+     *
406
+     */
407
+    public function get_custom_fields( $post_id ) {
408
+
409
+        // Return custom fields for this specific entity's type.
410
+        $types = $this->get_ids( $post_id );
411
+
412
+        /** @var WP_Term[] $terms */
413
+        $terms = array_filter( array_map( function ( $item ) {
414
+            return get_term( $item );
415
+        }, $types ), function ( $item ) {
416
+            return isset( $item ) && is_a( $item, 'WP_Term' );
417
+        } );
418
+
419
+        $term_slugs = array_map( function ( $item ) {
420
+            return $item->slug;
421
+        }, $terms );
422
+
423
+        $term_slugs[] = 'thing';
424
+
425
+        $schema_service = Wordlift_Schema_Service::get_instance();
426
+
427
+        return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
428
+
429
+            $schema = $schema_service->get_schema( $item );
430
+
431
+            if ( ! isset( $schema['custom_fields'] ) ) {
432
+                return $carry;
433
+            }
434
+
435
+            return $carry + $schema['custom_fields'];
436
+        }, array() );
437
+    }
438
+
439
+
440
+    /**
441
+     * Determines whether a post type can be used for entities.
442
+     *
443
+     * Criteria is that the post type is public. The list of valid post types
444
+     * can be overridden with a filter.
445
+     *
446
+     * @param string $post_type A post type name.
447
+     *
448
+     * @return bool Return true if the post type can be used for entities, otherwise false.
449
+     * @since 3.15.0
450
+     *
451
+     */
452
+    public static function is_valid_entity_post_type( $post_type ) {
453
+
454
+        return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
455
+    }
456 456
 
457 457
 }
Please login to merge, or discard this patch.
Spacing   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -54,9 +54,9 @@  discard block
 block discarded – undo
54 54
 	 * @since 3.7.0
55 55
 	 *
56 56
 	 */
57
-	public function __construct( $schema_service ) {
57
+	public function __construct($schema_service) {
58 58
 
59
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' );
59
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Service');
60 60
 
61 61
 		$this->schema_service = $schema_service;
62 62
 
@@ -108,34 +108,34 @@  discard block
 block discarded – undo
108 108
 	 *          assigned the `WebPage` entity type by default.
109 109
 	 *
110 110
 	 */
111
-	public function get( $post_id ) {
111
+	public function get($post_id) {
112 112
 
113
-		$this->log->trace( "Getting the post type for post $post_id..." );
113
+		$this->log->trace("Getting the post type for post $post_id...");
114 114
 
115 115
 		// Get the post type.
116
-		$post_type = get_post_type( $post_id );
116
+		$post_type = get_post_type($post_id);
117 117
 
118 118
 		// Return `web-page` for non entities.
119
-		if ( ! self::is_valid_entity_post_type( $post_type ) ) {
120
-			$this->log->info( "Returning `web-page` for post $post_id." );
119
+		if ( ! self::is_valid_entity_post_type($post_type)) {
120
+			$this->log->info("Returning `web-page` for post $post_id.");
121 121
 
122
-			return $this->schema_service->get_schema( 'web-page' );
122
+			return $this->schema_service->get_schema('web-page');
123 123
 		}
124 124
 
125 125
 		// Get the type from the associated classification.
126
-		$terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
126
+		$terms = wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
127 127
 
128 128
 		// Return the schema type if there is a term found.
129
-		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
129
+		if ( ! is_wp_error($terms) && ! empty($terms)) {
130 130
 			// Cycle through the terms and return the first one with a valid schema.
131
-			foreach ( $terms as $term ) {
132
-				$this->log->debug( "Found `{$term->slug}` term for post $post_id." );
131
+			foreach ($terms as $term) {
132
+				$this->log->debug("Found `{$term->slug}` term for post $post_id.");
133 133
 
134 134
 				// Try to get the schema for the term.
135
-				$schema = $this->schema_service->get_schema( $term->slug );
135
+				$schema = $this->schema_service->get_schema($term->slug);
136 136
 
137 137
 				// If found, return it, ignoring the other types.
138
-				if ( null !== $schema ) {
138
+				if (null !== $schema) {
139 139
 					// Return the entity type with the specified id.
140 140
 					return $schema;
141 141
 				}
@@ -151,22 +151,22 @@  discard block
 block discarded – undo
151 151
 			 * @since 3.20.0
152 152
 			 */
153 153
 
154
-			return $this->schema_service->get_schema( 'thing' );
154
+			return $this->schema_service->get_schema('thing');
155 155
 		}
156 156
 
157 157
 		// If it's a page or post return `Article`.
158
-		if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
159
-			$this->log->debug( "Post $post_id has no terms, and it's a `post` type, returning `Article`." );
158
+		if (in_array($post_type, array('post', 'page'))) {
159
+			$this->log->debug("Post $post_id has no terms, and it's a `post` type, returning `Article`.");
160 160
 
161 161
 			// Return "Article" schema type for posts.
162
-			return $this->schema_service->get_schema( 'article' );
162
+			return $this->schema_service->get_schema('article');
163 163
 		}
164 164
 
165 165
 		// Return "Thing" schema type for entities.
166
-		$this->log->debug( "Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`." );
166
+		$this->log->debug("Post $post_id has no terms, but it's a `wl_entity` type, returning `Thing`.");
167 167
 
168 168
 		// Return the entity type with the specified id.
169
-		return $this->schema_service->get_schema( 'thing' );
169
+		return $this->schema_service->get_schema('thing');
170 170
 
171 171
 	}
172 172
 
@@ -179,9 +179,9 @@  discard block
 block discarded – undo
179 179
 	 * @since 3.20.0
180 180
 	 *
181 181
 	 */
182
-	public function get_ids( $post_id ) {
182
+	public function get_ids($post_id) {
183 183
 
184
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array( 'fields' => 'ids', ) );
184
+		return wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array('fields' => 'ids',));
185 185
 	}
186 186
 
187 187
 	/**
@@ -193,13 +193,13 @@  discard block
 block discarded – undo
193 193
 	 * @since 3.20.0
194 194
 	 *
195 195
 	 */
196
-	public function get_names( $post_id ) {
196
+	public function get_names($post_id) {
197 197
 
198
-		$ids = $this->get_ids( $post_id );
198
+		$ids = $this->get_ids($post_id);
199 199
 
200
-		return array_map( function ( $id ) {
201
-			return get_term_meta( $id, '_wl_name', true );
202
-		}, $ids );
200
+		return array_map(function($id) {
201
+			return get_term_meta($id, '_wl_name', true);
202
+		}, $ids);
203 203
 	}
204 204
 
205 205
 	/**
@@ -212,25 +212,25 @@  discard block
 block discarded – undo
212 212
 	 * @since 3.8.0
213 213
 	 *
214 214
 	 */
215
-	public function set( $post_id, $type_uri, $replace = true ) {
215
+	public function set($post_id, $type_uri, $replace = true) {
216 216
 
217 217
 		// If the type URI is empty we remove the type.
218
-		if ( empty( $type_uri ) ) {
219
-			$this->log->debug( "Removing entity type for post $post_id..." );
218
+		if (empty($type_uri)) {
219
+			$this->log->debug("Removing entity type for post $post_id...");
220 220
 
221
-			wp_set_object_terms( $post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
221
+			wp_set_object_terms($post_id, null, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
222 222
 
223 223
 			return;
224 224
 		}
225 225
 
226
-		$this->log->debug( "Setting entity type for post $post_id..." );
226
+		$this->log->debug("Setting entity type for post $post_id...");
227 227
 
228 228
 		// if the `$type_uri` starts with `wl-`, we're looking at the class name, which is `wl-` + slug.
229
-		$term = ( 0 === strpos( $type_uri, 'wl-' ) )
229
+		$term = (0 === strpos($type_uri, 'wl-'))
230 230
 			// Get term by slug.
231
-			? $this->get_term_by_slug( substr( $type_uri, 3 ) )
231
+			? $this->get_term_by_slug(substr($type_uri, 3))
232 232
 			// Get term by URI.
233
-			: $this->get_term_by_uri( $type_uri );
233
+			: $this->get_term_by_uri($type_uri);
234 234
 
235 235
 		/*
236 236
 		 * We always want to assign a type to an entity otherwise it won't show in the Vocabulary and it won't be
@@ -243,22 +243,22 @@  discard block
 block discarded – undo
243 243
 		 *
244 244
 		 * @since 3.23.4
245 245
 		 */
246
-		if ( false === $term ) {
247
-			$this->log->warn( "No term found for URI $type_uri, will use Thing." );
246
+		if (false === $term) {
247
+			$this->log->warn("No term found for URI $type_uri, will use Thing.");
248 248
 
249
-			$term = $this->get_term_by_slug( 'thing' );
249
+			$term = $this->get_term_by_slug('thing');
250 250
 
251 251
 			// We still need to be able to bali out here, for example WordPress 5.1 tests create posts before our taxonomy
252 252
 			// is installed.
253
-			if ( false === $term ) {
253
+			if (false === $term) {
254 254
 				return;
255 255
 			}
256 256
 		}
257 257
 
258
-		$this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]..." );
258
+		$this->log->debug("Setting entity type [ post id :: $post_id ][ term id :: $term->term_id ][ term slug :: $term->slug ][ type uri :: $type_uri ]...");
259 259
 
260 260
 		// `$replace` is passed to decide whether to replace or append the term.
261
-		wp_set_object_terms( $post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace );
261
+		wp_set_object_terms($post_id, $term->term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, ! $replace);
262 262
 
263 263
 	}
264 264
 
@@ -272,9 +272,9 @@  discard block
 block discarded – undo
272 272
 	 * @since 3.20.0
273 273
 	 *
274 274
 	 */
275
-	private function get_term_by_slug( $slug ) {
275
+	private function get_term_by_slug($slug) {
276 276
 
277
-		return get_term_by( 'slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
277
+		return get_term_by('slug', $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
278 278
 	}
279 279
 
280 280
 	/**
@@ -287,9 +287,9 @@  discard block
 block discarded – undo
287 287
 	 * @since 3.20.0
288 288
 	 *
289 289
 	 */
290
-	public function get_term_by_uri( $uri ) {
290
+	public function get_term_by_uri($uri) {
291 291
 
292
-		$terms = get_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
292
+		$terms = get_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
293 293
 			'fields'     => 'all',
294 294
 			'get'        => 'all',
295 295
 			'number'     => 1,
@@ -303,9 +303,9 @@  discard block
 block discarded – undo
303 303
 			),
304 304
 			'orderby'    => 'term_id',
305 305
 			'order'      => 'ASC',
306
-		) );
306
+		));
307 307
 
308
-		return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : false;
308
+		return is_array($terms) && ! empty($terms) ? $terms[0] : false;
309 309
 	}
310 310
 
311 311
 	/**
@@ -319,26 +319,26 @@  discard block
 block discarded – undo
319 319
 	 * @since 3.15.0
320 320
 	 *
321 321
 	 */
322
-	public function has_entity_type( $post_id, $uri = null ) {
322
+	public function has_entity_type($post_id, $uri = null) {
323 323
 
324
-		$this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." );
324
+		$this->log->debug("Checking if post $post_id has an entity type [ $uri ]...");
325 325
 
326 326
 		// If an URI hasn't been specified just check whether we have at least
327 327
 		// one entity type.
328
-		if ( null === $uri ) {
328
+		if (null === $uri) {
329 329
 
330 330
 			// Get the post terms for the specified post ID.
331
-			$terms = $this->get_post_terms( $post_id );
331
+			$terms = $this->get_post_terms($post_id);
332 332
 
333
-			$this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' );
333
+			$this->log->debug("Post $post_id has ".count($terms).' type(s).');
334 334
 
335 335
 			// True if there's at least one term bound to the post.
336
-			return ( 0 < count( $terms ) );
336
+			return (0 < count($terms));
337 337
 		}
338 338
 
339
-		$has_entity_type = ( null !== $this->has_post_term_by_uri( $post_id, $uri ) );
339
+		$has_entity_type = (null !== $this->has_post_term_by_uri($post_id, $uri));
340 340
 
341
-		$this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) );
341
+		$this->log->debug("Post $post_id has $uri type: ".($has_entity_type ? 'yes' : 'no'));
342 342
 
343 343
 		// Check whether the post has an entity type with that URI.
344 344
 		return $has_entity_type;
@@ -353,9 +353,9 @@  discard block
 block discarded – undo
353 353
 	 * @since 3.15.0
354 354
 	 *
355 355
 	 */
356
-	private function get_post_terms( $post_id ) {
356
+	private function get_post_terms($post_id) {
357 357
 
358
-		return wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
358
+		return wp_get_object_terms($post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, array(
359 359
 			'hide_empty' => false,
360 360
 			// Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'.
361 361
 			// An issue has been opened with the AAM plugin author as well.
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
 			// see https://github.com/insideout10/wordlift-plugin/issues/334
364 364
 			// see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863
365 365
 			'fields'     => 'all',
366
-		) );
366
+		));
367 367
 	}
368 368
 
369 369
 	/**
@@ -377,16 +377,16 @@  discard block
 block discarded – undo
377 377
 	 *
378 378
 	 * @since 3.20.0 function renamed to `has_post_term_by_uri` and return type changed to `bool`.
379 379
 	 */
380
-	private function has_post_term_by_uri( $post_id, $uri ) {
380
+	private function has_post_term_by_uri($post_id, $uri) {
381 381
 
382 382
 		// Get the post terms bound to the specified post.
383
-		$terms = $this->get_post_terms( $post_id );
383
+		$terms = $this->get_post_terms($post_id);
384 384
 
385 385
 		// Look for a term if the specified URI.
386
-		foreach ( $terms as $term ) {
387
-			$term_uri = get_term_meta( $term->term_id, '_wl_uri', true );
386
+		foreach ($terms as $term) {
387
+			$term_uri = get_term_meta($term->term_id, '_wl_uri', true);
388 388
 
389
-			if ( $uri === $term_uri ) {
389
+			if ($uri === $term_uri) {
390 390
 				return true;
391 391
 			}
392 392
 		}
@@ -404,36 +404,36 @@  discard block
 block discarded – undo
404 404
 	 * @since 3.25.2
405 405
 	 *
406 406
 	 */
407
-	public function get_custom_fields( $post_id ) {
407
+	public function get_custom_fields($post_id) {
408 408
 
409 409
 		// Return custom fields for this specific entity's type.
410
-		$types = $this->get_ids( $post_id );
410
+		$types = $this->get_ids($post_id);
411 411
 
412 412
 		/** @var WP_Term[] $terms */
413
-		$terms = array_filter( array_map( function ( $item ) {
414
-			return get_term( $item );
415
-		}, $types ), function ( $item ) {
416
-			return isset( $item ) && is_a( $item, 'WP_Term' );
413
+		$terms = array_filter(array_map(function($item) {
414
+			return get_term($item);
415
+		}, $types), function($item) {
416
+			return isset($item) && is_a($item, 'WP_Term');
417 417
 		} );
418 418
 
419
-		$term_slugs = array_map( function ( $item ) {
419
+		$term_slugs = array_map(function($item) {
420 420
 			return $item->slug;
421
-		}, $terms );
421
+		}, $terms);
422 422
 
423 423
 		$term_slugs[] = 'thing';
424 424
 
425 425
 		$schema_service = Wordlift_Schema_Service::get_instance();
426 426
 
427
-		return array_reduce( $term_slugs, function ( $carry, $item ) use ( $schema_service ) {
427
+		return array_reduce($term_slugs, function($carry, $item) use ($schema_service) {
428 428
 
429
-			$schema = $schema_service->get_schema( $item );
429
+			$schema = $schema_service->get_schema($item);
430 430
 
431
-			if ( ! isset( $schema['custom_fields'] ) ) {
431
+			if ( ! isset($schema['custom_fields'])) {
432 432
 				return $carry;
433 433
 			}
434 434
 
435 435
 			return $carry + $schema['custom_fields'];
436
-		}, array() );
436
+		}, array());
437 437
 	}
438 438
 
439 439
 
@@ -449,9 +449,9 @@  discard block
 block discarded – undo
449 449
 	 * @since 3.15.0
450 450
 	 *
451 451
 	 */
452
-	public static function is_valid_entity_post_type( $post_type ) {
452
+	public static function is_valid_entity_post_type($post_type) {
453 453
 
454
-		return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true );
454
+		return in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true);
455 455
 	}
456 456
 
457 457
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-post-to-jsonld-converter.php 2 patches
Indentation   +264 added lines, -264 removed lines patch added patch discarded remove patch
@@ -13,275 +13,275 @@
 block discarded – undo
13 13
  */
14 14
 class Wordlift_Entity_Post_To_Jsonld_Converter extends Wordlift_Abstract_Post_To_Jsonld_Converter {
15 15
 
16
-	/**
17
-	 * A {@link Wordlift_Property_Getter} instance.
18
-	 *
19
-	 * @since  3.8.0
20
-	 * @access private
21
-	 * @var \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance.
22
-	 */
23
-	private $property_getter;
24
-
25
-	/**
26
-	 * The {@link Wordlift_Schemaorg_Property_Service} or null if not provided.
27
-	 *
28
-	 * @since 3.20.0
29
-	 * @access private
30
-	 * @var null|Wordlift_Schemaorg_Property_Service The {@link Wordlift_Schemaorg_Property_Service} or null if not provided.
31
-	 */
32
-	private $schemaorg_property_service;
33
-
34
-	/**
35
-	 * The {@link Wordlift_Post_To_Jsonld_Converter} is used to convert entities that also have the `article` term.
36
-	 *
37
-	 * @since 3.25.2
38
-	 * @var \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance.
39
-	 */
40
-	private $post_to_jsonld_converter;
41
-
42
-	/**
43
-	 * Wordlift_Entity_To_Jsonld_Converter constructor.
44
-	 *
45
-	 * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
46
-	 * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
47
-	 * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
48
-	 * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
49
-	 * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance.
50
-	 * @param \Wordlift_Schemaorg_Property_Service $schemaorg_property_service A {@link Wordlift_Schemaorg_Property_Service} instance.
51
-	 * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance.
52
-	 *
53
-	 * @since 3.8.0
54
-	 */
55
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) {
56
-		parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
57
-
58
-		$this->property_getter            = $property_getter;
59
-		$this->schemaorg_property_service = $schemaorg_property_service;
60
-		$this->post_to_jsonld_converter   = $post_to_jsonld_converter;
61
-
62
-	}
63
-
64
-	/**
65
-	 * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
66
-	 * found while processing the post is set in the $references array.
67
-	 *
68
-	 * @param int $post_id The {@link WP_Post} id.
69
-	 *
70
-	 * @param array $references An array of entity references.
71
-	 *
72
-	 * @return array A JSON-LD array.
73
-	 * @since 3.8.0
74
-	 *
75
-	 */
76
-	public function convert( $post_id, &$references = array() ) {
77
-
78
-		// Get the post instance.
79
-		$post = get_post( $post_id );
80
-		if ( null === $post ) {
81
-			// Post not found.
82
-			return null;
83
-		}
84
-
85
-		// Get the base JSON-LD and the list of entities referenced by this entity.
86
-		if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) {
87
-			$jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references );
88
-		} else {
89
-			$jsonld = parent::convert( $post_id, $references );
90
-		}
91
-
92
-		// Get the entity name.
93
-		$jsonld['name'] = $post->post_title;
94
-
95
-		// 3.13.0, add alternate names.
96
-		$alternative_labels = $this->entity_service->get_alternative_labels( $post_id );
97
-		if ( 0 < count( $alternative_labels ) ) {
98
-			$jsonld['alternateName'] = $alternative_labels;
99
-		}
100
-
101
-		// Get the entity `@type` with custom fields set by the Wordlift_Schema_Service.
102
-		//
103
-		// This allows us to gather the basic properties as defined by the `Thing` entity type.
104
-		// Get the configured type custom fields.
105
-		$custom_fields = $this->entity_type_service->get_custom_fields( $post_id );
106
-
107
-		if ( isset( $custom_fields ) ) {
108
-			$this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references );
109
-		}
110
-
111
-		/*
16
+    /**
17
+     * A {@link Wordlift_Property_Getter} instance.
18
+     *
19
+     * @since  3.8.0
20
+     * @access private
21
+     * @var \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance.
22
+     */
23
+    private $property_getter;
24
+
25
+    /**
26
+     * The {@link Wordlift_Schemaorg_Property_Service} or null if not provided.
27
+     *
28
+     * @since 3.20.0
29
+     * @access private
30
+     * @var null|Wordlift_Schemaorg_Property_Service The {@link Wordlift_Schemaorg_Property_Service} or null if not provided.
31
+     */
32
+    private $schemaorg_property_service;
33
+
34
+    /**
35
+     * The {@link Wordlift_Post_To_Jsonld_Converter} is used to convert entities that also have the `article` term.
36
+     *
37
+     * @since 3.25.2
38
+     * @var \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance.
39
+     */
40
+    private $post_to_jsonld_converter;
41
+
42
+    /**
43
+     * Wordlift_Entity_To_Jsonld_Converter constructor.
44
+     *
45
+     * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance.
46
+     * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
47
+     * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance.
48
+     * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance.
49
+     * @param \Wordlift_Property_Getter $property_getter A {@link Wordlift_Property_Getter} instance.
50
+     * @param \Wordlift_Schemaorg_Property_Service $schemaorg_property_service A {@link Wordlift_Schemaorg_Property_Service} instance.
51
+     * @param \Wordlift_Post_To_Jsonld_Converter $post_to_jsonld_converter The {@link Wordlift_Post_To_Jsonld_Converter} instance.
52
+     *
53
+     * @since 3.8.0
54
+     */
55
+    public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) {
56
+        parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
57
+
58
+        $this->property_getter            = $property_getter;
59
+        $this->schemaorg_property_service = $schemaorg_property_service;
60
+        $this->post_to_jsonld_converter   = $post_to_jsonld_converter;
61
+
62
+    }
63
+
64
+    /**
65
+     * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference
66
+     * found while processing the post is set in the $references array.
67
+     *
68
+     * @param int $post_id The {@link WP_Post} id.
69
+     *
70
+     * @param array $references An array of entity references.
71
+     *
72
+     * @return array A JSON-LD array.
73
+     * @since 3.8.0
74
+     *
75
+     */
76
+    public function convert( $post_id, &$references = array() ) {
77
+
78
+        // Get the post instance.
79
+        $post = get_post( $post_id );
80
+        if ( null === $post ) {
81
+            // Post not found.
82
+            return null;
83
+        }
84
+
85
+        // Get the base JSON-LD and the list of entities referenced by this entity.
86
+        if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) {
87
+            $jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references );
88
+        } else {
89
+            $jsonld = parent::convert( $post_id, $references );
90
+        }
91
+
92
+        // Get the entity name.
93
+        $jsonld['name'] = $post->post_title;
94
+
95
+        // 3.13.0, add alternate names.
96
+        $alternative_labels = $this->entity_service->get_alternative_labels( $post_id );
97
+        if ( 0 < count( $alternative_labels ) ) {
98
+            $jsonld['alternateName'] = $alternative_labels;
99
+        }
100
+
101
+        // Get the entity `@type` with custom fields set by the Wordlift_Schema_Service.
102
+        //
103
+        // This allows us to gather the basic properties as defined by the `Thing` entity type.
104
+        // Get the configured type custom fields.
105
+        $custom_fields = $this->entity_type_service->get_custom_fields( $post_id );
106
+
107
+        if ( isset( $custom_fields ) ) {
108
+            $this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references );
109
+        }
110
+
111
+        /*
112 112
 		 * Get the properties attached to the post.
113 113
 		 *
114 114
 		 * @since 3.20.0 We attach properties directly to the posts.
115 115
 		 *
116 116
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
117 117
 		 */
118
-		if ( WL_ALL_ENTITY_TYPES ) {
119
-			$this->process_post_properties( $jsonld, $post_id );
120
-		}
121
-
122
-		/**
123
-		 * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
124
-		 *
125
-		 * @param array $value {
126
-		 *
127
-		 * @type array $jsonld The JSON-LD structure.
128
-		 * @type int[] $references An array of post IDs.
129
-		 * }
130
-		 * @since 3.25.0
131
-		 *
132
-		 * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
133
-		 *
134
-		 * @api
135
-		 */
136
-		$ret_val    = apply_filters( 'wl_entity_jsonld_array', array(
137
-			'jsonld'     => $jsonld,
138
-			'references' => $references,
139
-		), $post_id );
140
-		$jsonld     = $ret_val['jsonld'];
141
-		$references = $ret_val['references'];
142
-
143
-		/**
144
-		 * Call the `wl_entity_jsonld` filter.
145
-		 *
146
-		 * @param array $jsonld The JSON-LD structure.
147
-		 * @param int $post_id The {@link WP_Post} `id`.
148
-		 * @param array $references The array of referenced entities.
149
-		 *
150
-		 * @since 3.20.0
151
-		 *
152
-		 * @api
153
-		 */
154
-		return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references );
155
-	}
156
-
157
-	/**
158
-	 * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property
159
-	 * for the post entity type.
160
-	 *
161
-	 * @param array $jsonld The JSON-LD array.
162
-	 * @param array $fields The entity types field array.
163
-	 * @param WP_Post $post The target {@link WP_Post} instance.
164
-	 * @param array $references The references array.
165
-	 *
166
-	 * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in
167
-	 *  the {@link Wordlift_Schema_Service} class.
168
-	 *
169
-	 */
170
-	private function process_type_custom_fields( &$jsonld, $fields, $post, &$references ) {
171
-
172
-		// Set a reference to use in closures.
173
-		$converter = $this;
174
-
175
-		// Try each field on the entity.
176
-		foreach ( $fields as $key => $value ) {
177
-
178
-			// Get the predicate.
179
-			$name = $this->relative_to_context( $value['predicate'] );
180
-
181
-			// Get the value, the property service will get the right extractor
182
-			// for that property.
183
-			$value = $this->property_getter->get( $post->ID, $key );
184
-
185
-			if ( empty( $value ) ) {
186
-				continue;
187
-			}
188
-
189
-			// Map the value to the property name.
190
-			// If we got an array with just one value, we return that one value.
191
-			// If we got a Wordlift_Property_Entity_Reference we get the URL.
192
-			$jsonld[ $name ] = self::make_one( array_map( function ( $item ) use ( $converter, &$references ) {
193
-
194
-				if ( $item instanceof Wordlift_Property_Entity_Reference ) {
195
-
196
-					$url = $item->getURL();
197
-
198
-					// The refactored converters require the entity id.
199
-					$references[] = $item->getID();
200
-
201
-					return array(
202
-						'@id' => $url,
203
-					);
204
-				}
205
-
206
-				return $converter->relative_to_context( $item );
207
-			}, $value ) );
208
-
209
-		}
210
-
211
-	}
212
-
213
-	/**
214
-	 * Process the properties attached to the {@link WP_Post}.
215
-	 *
216
-	 * @param array $jsonld The JSON-LD array.
217
-	 * @param int $post_id The target {@link WP_Post} id.
218
-	 *
219
-	 * @since 3.20.0
220
-	 *
221
-	 */
222
-	private function process_post_properties( &$jsonld, $post_id ) {
223
-
224
-		// Get all the props.
225
-		$props = $this->schemaorg_property_service->get_all( $post_id );
226
-
227
-		// Process all the props.
228
-		foreach ( $props as $name => $instances ) {
229
-
230
-			// Get the values.
231
-			$values = array_map( function ( $instance ) {
232
-				return $instance['value'];
233
-			}, $instances );
234
-
235
-			// We might receive empty values, remove them.
236
-			$non_empty_values = array_filter( $values, function ( $value ) {
237
-				return ! empty( $value );
238
-			} );
239
-
240
-			// Skip empty properties.
241
-			if ( empty( $non_empty_values ) ) {
242
-				continue;
243
-			}
244
-
245
-			// @@todo: need to handle maybe Numbers and URLs differently.
246
-			// Make an array a single value when possible.
247
-			$jsonld[ $name ] = self::make_one( $non_empty_values );
248
-		}
249
-
250
-	}
251
-
252
-	/**
253
-	 * Post process the generated JSON to reorganize values which are stored as 1st
254
-	 * level in WP but are really 2nd level.
255
-	 *
256
-	 * @param array $jsonld An array of JSON-LD properties and values.
257
-	 *
258
-	 * @return array The array remapped.
259
-	 * @since 3.8.0
260
-	 *
261
-	 */
262
-	private function post_process( $jsonld ) {
263
-
264
-		foreach ( $jsonld as $key => $value ) {
265
-			if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) {
266
-				$jsonld['address']['@type'] = 'PostalAddress';
267
-				$jsonld['address'][ $key ]  = $value;
268
-				unset( $jsonld[ $key ] );
269
-			}
270
-
271
-			if ( 'latitude' === $key || 'longitude' === $key ) {
272
-				$jsonld['geo']['@type'] = 'GeoCoordinates';
273
-				$jsonld['geo'][ $key ]  = $value;
274
-				unset( $jsonld[ $key ] );
275
-			}
276
-
277
-			if ( 'calories' === $key ) {
278
-				$jsonld['nutrition']['@type'] = 'NutritionInformation';
279
-				$jsonld['nutrition'][ $key ]  = $value;
280
-				unset( $jsonld[ $key ] );
281
-			}
282
-		}
283
-
284
-		return $jsonld;
285
-	}
118
+        if ( WL_ALL_ENTITY_TYPES ) {
119
+            $this->process_post_properties( $jsonld, $post_id );
120
+        }
121
+
122
+        /**
123
+         * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
124
+         *
125
+         * @param array $value {
126
+         *
127
+         * @type array $jsonld The JSON-LD structure.
128
+         * @type int[] $references An array of post IDs.
129
+         * }
130
+         * @since 3.25.0
131
+         *
132
+         * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
133
+         *
134
+         * @api
135
+         */
136
+        $ret_val    = apply_filters( 'wl_entity_jsonld_array', array(
137
+            'jsonld'     => $jsonld,
138
+            'references' => $references,
139
+        ), $post_id );
140
+        $jsonld     = $ret_val['jsonld'];
141
+        $references = $ret_val['references'];
142
+
143
+        /**
144
+         * Call the `wl_entity_jsonld` filter.
145
+         *
146
+         * @param array $jsonld The JSON-LD structure.
147
+         * @param int $post_id The {@link WP_Post} `id`.
148
+         * @param array $references The array of referenced entities.
149
+         *
150
+         * @since 3.20.0
151
+         *
152
+         * @api
153
+         */
154
+        return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references );
155
+    }
156
+
157
+    /**
158
+     * Add data to the JSON-LD using the `custom_fields` array which contains the definitions of property
159
+     * for the post entity type.
160
+     *
161
+     * @param array $jsonld The JSON-LD array.
162
+     * @param array $fields The entity types field array.
163
+     * @param WP_Post $post The target {@link WP_Post} instance.
164
+     * @param array $references The references array.
165
+     *
166
+     * @since 3.20.0 This code moved from the above function `convert`, used for entity types defined in
167
+     *  the {@link Wordlift_Schema_Service} class.
168
+     *
169
+     */
170
+    private function process_type_custom_fields( &$jsonld, $fields, $post, &$references ) {
171
+
172
+        // Set a reference to use in closures.
173
+        $converter = $this;
174
+
175
+        // Try each field on the entity.
176
+        foreach ( $fields as $key => $value ) {
177
+
178
+            // Get the predicate.
179
+            $name = $this->relative_to_context( $value['predicate'] );
180
+
181
+            // Get the value, the property service will get the right extractor
182
+            // for that property.
183
+            $value = $this->property_getter->get( $post->ID, $key );
184
+
185
+            if ( empty( $value ) ) {
186
+                continue;
187
+            }
188
+
189
+            // Map the value to the property name.
190
+            // If we got an array with just one value, we return that one value.
191
+            // If we got a Wordlift_Property_Entity_Reference we get the URL.
192
+            $jsonld[ $name ] = self::make_one( array_map( function ( $item ) use ( $converter, &$references ) {
193
+
194
+                if ( $item instanceof Wordlift_Property_Entity_Reference ) {
195
+
196
+                    $url = $item->getURL();
197
+
198
+                    // The refactored converters require the entity id.
199
+                    $references[] = $item->getID();
200
+
201
+                    return array(
202
+                        '@id' => $url,
203
+                    );
204
+                }
205
+
206
+                return $converter->relative_to_context( $item );
207
+            }, $value ) );
208
+
209
+        }
210
+
211
+    }
212
+
213
+    /**
214
+     * Process the properties attached to the {@link WP_Post}.
215
+     *
216
+     * @param array $jsonld The JSON-LD array.
217
+     * @param int $post_id The target {@link WP_Post} id.
218
+     *
219
+     * @since 3.20.0
220
+     *
221
+     */
222
+    private function process_post_properties( &$jsonld, $post_id ) {
223
+
224
+        // Get all the props.
225
+        $props = $this->schemaorg_property_service->get_all( $post_id );
226
+
227
+        // Process all the props.
228
+        foreach ( $props as $name => $instances ) {
229
+
230
+            // Get the values.
231
+            $values = array_map( function ( $instance ) {
232
+                return $instance['value'];
233
+            }, $instances );
234
+
235
+            // We might receive empty values, remove them.
236
+            $non_empty_values = array_filter( $values, function ( $value ) {
237
+                return ! empty( $value );
238
+            } );
239
+
240
+            // Skip empty properties.
241
+            if ( empty( $non_empty_values ) ) {
242
+                continue;
243
+            }
244
+
245
+            // @@todo: need to handle maybe Numbers and URLs differently.
246
+            // Make an array a single value when possible.
247
+            $jsonld[ $name ] = self::make_one( $non_empty_values );
248
+        }
249
+
250
+    }
251
+
252
+    /**
253
+     * Post process the generated JSON to reorganize values which are stored as 1st
254
+     * level in WP but are really 2nd level.
255
+     *
256
+     * @param array $jsonld An array of JSON-LD properties and values.
257
+     *
258
+     * @return array The array remapped.
259
+     * @since 3.8.0
260
+     *
261
+     */
262
+    private function post_process( $jsonld ) {
263
+
264
+        foreach ( $jsonld as $key => $value ) {
265
+            if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) {
266
+                $jsonld['address']['@type'] = 'PostalAddress';
267
+                $jsonld['address'][ $key ]  = $value;
268
+                unset( $jsonld[ $key ] );
269
+            }
270
+
271
+            if ( 'latitude' === $key || 'longitude' === $key ) {
272
+                $jsonld['geo']['@type'] = 'GeoCoordinates';
273
+                $jsonld['geo'][ $key ]  = $value;
274
+                unset( $jsonld[ $key ] );
275
+            }
276
+
277
+            if ( 'calories' === $key ) {
278
+                $jsonld['nutrition']['@type'] = 'NutritionInformation';
279
+                $jsonld['nutrition'][ $key ]  = $value;
280
+                unset( $jsonld[ $key ] );
281
+            }
282
+        }
283
+
284
+        return $jsonld;
285
+    }
286 286
 
287 287
 }
Please login to merge, or discard this patch.
Spacing   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -52,8 +52,8 @@  discard block
 block discarded – undo
52 52
 	 *
53 53
 	 * @since 3.8.0
54 54
 	 */
55
-	public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter ) {
56
-		parent::__construct( $entity_type_service, $entity_service, $user_service, $attachment_service );
55
+	public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service, $property_getter, $schemaorg_property_service, $post_to_jsonld_converter) {
56
+		parent::__construct($entity_type_service, $entity_service, $user_service, $attachment_service);
57 57
 
58 58
 		$this->property_getter            = $property_getter;
59 59
 		$this->schemaorg_property_service = $schemaorg_property_service;
@@ -73,28 +73,28 @@  discard block
 block discarded – undo
73 73
 	 * @since 3.8.0
74 74
 	 *
75 75
 	 */
76
-	public function convert( $post_id, &$references = array() ) {
76
+	public function convert($post_id, &$references = array()) {
77 77
 
78 78
 		// Get the post instance.
79
-		$post = get_post( $post_id );
80
-		if ( null === $post ) {
79
+		$post = get_post($post_id);
80
+		if (null === $post) {
81 81
 			// Post not found.
82 82
 			return null;
83 83
 		}
84 84
 
85 85
 		// Get the base JSON-LD and the list of entities referenced by this entity.
86
-		if ( has_term( 'article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id ) ) {
87
-			$jsonld = $this->post_to_jsonld_converter->convert( $post_id, $references );
86
+		if (has_term('article', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $post_id)) {
87
+			$jsonld = $this->post_to_jsonld_converter->convert($post_id, $references);
88 88
 		} else {
89
-			$jsonld = parent::convert( $post_id, $references );
89
+			$jsonld = parent::convert($post_id, $references);
90 90
 		}
91 91
 
92 92
 		// Get the entity name.
93 93
 		$jsonld['name'] = $post->post_title;
94 94
 
95 95
 		// 3.13.0, add alternate names.
96
-		$alternative_labels = $this->entity_service->get_alternative_labels( $post_id );
97
-		if ( 0 < count( $alternative_labels ) ) {
96
+		$alternative_labels = $this->entity_service->get_alternative_labels($post_id);
97
+		if (0 < count($alternative_labels)) {
98 98
 			$jsonld['alternateName'] = $alternative_labels;
99 99
 		}
100 100
 
@@ -102,10 +102,10 @@  discard block
 block discarded – undo
102 102
 		//
103 103
 		// This allows us to gather the basic properties as defined by the `Thing` entity type.
104 104
 		// Get the configured type custom fields.
105
-		$custom_fields = $this->entity_type_service->get_custom_fields( $post_id );
105
+		$custom_fields = $this->entity_type_service->get_custom_fields($post_id);
106 106
 
107
-		if ( isset( $custom_fields ) ) {
108
-			$this->process_type_custom_fields( $jsonld, $custom_fields, $post, $references );
107
+		if (isset($custom_fields)) {
108
+			$this->process_type_custom_fields($jsonld, $custom_fields, $post, $references);
109 109
 		}
110 110
 
111 111
 		/*
@@ -115,8 +115,8 @@  discard block
 block discarded – undo
115 115
 		 *
116 116
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/835
117 117
 		 */
118
-		if ( WL_ALL_ENTITY_TYPES ) {
119
-			$this->process_post_properties( $jsonld, $post_id );
118
+		if (WL_ALL_ENTITY_TYPES) {
119
+			$this->process_post_properties($jsonld, $post_id);
120 120
 		}
121 121
 
122 122
 		/**
@@ -133,10 +133,10 @@  discard block
 block discarded – undo
133 133
 		 *
134 134
 		 * @api
135 135
 		 */
136
-		$ret_val    = apply_filters( 'wl_entity_jsonld_array', array(
136
+		$ret_val = apply_filters('wl_entity_jsonld_array', array(
137 137
 			'jsonld'     => $jsonld,
138 138
 			'references' => $references,
139
-		), $post_id );
139
+		), $post_id);
140 140
 		$jsonld     = $ret_val['jsonld'];
141 141
 		$references = $ret_val['references'];
142 142
 
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
 		 *
152 152
 		 * @api
153 153
 		 */
154
-		return apply_filters( 'wl_entity_jsonld', $this->post_process( $jsonld ), $post_id, $references );
154
+		return apply_filters('wl_entity_jsonld', $this->post_process($jsonld), $post_id, $references);
155 155
 	}
156 156
 
157 157
 	/**
@@ -167,31 +167,31 @@  discard block
 block discarded – undo
167 167
 	 *  the {@link Wordlift_Schema_Service} class.
168 168
 	 *
169 169
 	 */
170
-	private function process_type_custom_fields( &$jsonld, $fields, $post, &$references ) {
170
+	private function process_type_custom_fields(&$jsonld, $fields, $post, &$references) {
171 171
 
172 172
 		// Set a reference to use in closures.
173 173
 		$converter = $this;
174 174
 
175 175
 		// Try each field on the entity.
176
-		foreach ( $fields as $key => $value ) {
176
+		foreach ($fields as $key => $value) {
177 177
 
178 178
 			// Get the predicate.
179
-			$name = $this->relative_to_context( $value['predicate'] );
179
+			$name = $this->relative_to_context($value['predicate']);
180 180
 
181 181
 			// Get the value, the property service will get the right extractor
182 182
 			// for that property.
183
-			$value = $this->property_getter->get( $post->ID, $key );
183
+			$value = $this->property_getter->get($post->ID, $key);
184 184
 
185
-			if ( empty( $value ) ) {
185
+			if (empty($value)) {
186 186
 				continue;
187 187
 			}
188 188
 
189 189
 			// Map the value to the property name.
190 190
 			// If we got an array with just one value, we return that one value.
191 191
 			// If we got a Wordlift_Property_Entity_Reference we get the URL.
192
-			$jsonld[ $name ] = self::make_one( array_map( function ( $item ) use ( $converter, &$references ) {
192
+			$jsonld[$name] = self::make_one(array_map(function($item) use ($converter, &$references) {
193 193
 
194
-				if ( $item instanceof Wordlift_Property_Entity_Reference ) {
194
+				if ($item instanceof Wordlift_Property_Entity_Reference) {
195 195
 
196 196
 					$url = $item->getURL();
197 197
 
@@ -203,8 +203,8 @@  discard block
 block discarded – undo
203 203
 					);
204 204
 				}
205 205
 
206
-				return $converter->relative_to_context( $item );
207
-			}, $value ) );
206
+				return $converter->relative_to_context($item);
207
+			}, $value));
208 208
 
209 209
 		}
210 210
 
@@ -219,32 +219,32 @@  discard block
 block discarded – undo
219 219
 	 * @since 3.20.0
220 220
 	 *
221 221
 	 */
222
-	private function process_post_properties( &$jsonld, $post_id ) {
222
+	private function process_post_properties(&$jsonld, $post_id) {
223 223
 
224 224
 		// Get all the props.
225
-		$props = $this->schemaorg_property_service->get_all( $post_id );
225
+		$props = $this->schemaorg_property_service->get_all($post_id);
226 226
 
227 227
 		// Process all the props.
228
-		foreach ( $props as $name => $instances ) {
228
+		foreach ($props as $name => $instances) {
229 229
 
230 230
 			// Get the values.
231
-			$values = array_map( function ( $instance ) {
231
+			$values = array_map(function($instance) {
232 232
 				return $instance['value'];
233
-			}, $instances );
233
+			}, $instances);
234 234
 
235 235
 			// We might receive empty values, remove them.
236
-			$non_empty_values = array_filter( $values, function ( $value ) {
237
-				return ! empty( $value );
236
+			$non_empty_values = array_filter($values, function($value) {
237
+				return ! empty($value);
238 238
 			} );
239 239
 
240 240
 			// Skip empty properties.
241
-			if ( empty( $non_empty_values ) ) {
241
+			if (empty($non_empty_values)) {
242 242
 				continue;
243 243
 			}
244 244
 
245 245
 			// @@todo: need to handle maybe Numbers and URLs differently.
246 246
 			// Make an array a single value when possible.
247
-			$jsonld[ $name ] = self::make_one( $non_empty_values );
247
+			$jsonld[$name] = self::make_one($non_empty_values);
248 248
 		}
249 249
 
250 250
 	}
@@ -259,25 +259,25 @@  discard block
 block discarded – undo
259 259
 	 * @since 3.8.0
260 260
 	 *
261 261
 	 */
262
-	private function post_process( $jsonld ) {
262
+	private function post_process($jsonld) {
263 263
 
264
-		foreach ( $jsonld as $key => $value ) {
265
-			if ( 'streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key ) {
264
+		foreach ($jsonld as $key => $value) {
265
+			if ('streetAddress' === $key || 'postalCode' === $key || 'addressLocality' === $key || 'addressRegion' === $key || 'addressCountry' === $key || 'postOfficeBoxNumber' === $key) {
266 266
 				$jsonld['address']['@type'] = 'PostalAddress';
267
-				$jsonld['address'][ $key ]  = $value;
268
-				unset( $jsonld[ $key ] );
267
+				$jsonld['address'][$key]  = $value;
268
+				unset($jsonld[$key]);
269 269
 			}
270 270
 
271
-			if ( 'latitude' === $key || 'longitude' === $key ) {
271
+			if ('latitude' === $key || 'longitude' === $key) {
272 272
 				$jsonld['geo']['@type'] = 'GeoCoordinates';
273
-				$jsonld['geo'][ $key ]  = $value;
274
-				unset( $jsonld[ $key ] );
273
+				$jsonld['geo'][$key]  = $value;
274
+				unset($jsonld[$key]);
275 275
 			}
276 276
 
277
-			if ( 'calories' === $key ) {
277
+			if ('calories' === $key) {
278 278
 				$jsonld['nutrition']['@type'] = 'NutritionInformation';
279
-				$jsonld['nutrition'][ $key ]  = $value;
280
-				unset( $jsonld[ $key ] );
279
+				$jsonld['nutrition'][$key]  = $value;
280
+				unset($jsonld[$key]);
281 281
 			}
282 282
 		}
283 283
 
Please login to merge, or discard this patch.