Completed
Pull Request — develop (#1701)
by
unknown
01:26
created
src/admin/class-wordlift-admin-dashboard-v2.php 2 patches
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -4,95 +4,95 @@  discard block
 block discarded – undo
4 4
 
5 5
 class Wordlift_Admin_Dashboard_V2 {
6 6
 
7
-	const TODAYS_TIP       = 'wl_todays_tip_data';
8
-	const AVERAGE_POSITION = 'wl_search_rankings_average_position';
9
-
10
-	/**
11
-	 * @var \Wordlift_Entity_Service $entity_service
12
-	 */
13
-	private $entity_service;
14
-
15
-	/**
16
-	 * Wordlift_Admin_Dashboard_V2 constructor.
17
-	 *
18
-	 * @since 3.20.0
19
-	 */
20
-	public function __construct( $entity_service ) {
21
-
22
-		add_action( 'wp_dashboard_setup', array( $this, 'dashboard_setup' ) );
23
-
24
-		// Define where to access the tip.
25
-		defined( 'WL_TODAYS_TIP_JSON_URL' ) || define( 'WL_TODAYS_TIP_JSON_URL', 'https://wordlift.io/blog' );
26
-		defined( 'WL_TODAYS_TIP_JSON_URL_IT' ) || define( 'WL_TODAYS_TIP_JSON_URL_IT', '/it/wp-json/wp/v2/posts?context=embed&per_page=1&categories=27' );
27
-		defined( 'WL_TODAYS_TIP_JSON_URL_EN' ) || define( 'WL_TODAYS_TIP_JSON_URL_EN', '/en/wp-json/wp/v2/posts?context=embed&per_page=1&categories=38' );
28
-		$this->entity_service = $entity_service;
29
-
30
-	}
31
-
32
-	/**
33
-	 * Set up the dashboard metabox.
34
-	 *
35
-	 * @since 3.20.0
36
-	 */
37
-	public function dashboard_setup() {
38
-		/**
39
-		 * Filter name: wl_feature__enable__wordlift-dashboard
40
-		 * Feature flag to enable / disable dashboard
41
-		 *
42
-		 * @since 3.30.0
43
-		 */
7
+    const TODAYS_TIP       = 'wl_todays_tip_data';
8
+    const AVERAGE_POSITION = 'wl_search_rankings_average_position';
9
+
10
+    /**
11
+     * @var \Wordlift_Entity_Service $entity_service
12
+     */
13
+    private $entity_service;
14
+
15
+    /**
16
+     * Wordlift_Admin_Dashboard_V2 constructor.
17
+     *
18
+     * @since 3.20.0
19
+     */
20
+    public function __construct( $entity_service ) {
21
+
22
+        add_action( 'wp_dashboard_setup', array( $this, 'dashboard_setup' ) );
23
+
24
+        // Define where to access the tip.
25
+        defined( 'WL_TODAYS_TIP_JSON_URL' ) || define( 'WL_TODAYS_TIP_JSON_URL', 'https://wordlift.io/blog' );
26
+        defined( 'WL_TODAYS_TIP_JSON_URL_IT' ) || define( 'WL_TODAYS_TIP_JSON_URL_IT', '/it/wp-json/wp/v2/posts?context=embed&per_page=1&categories=27' );
27
+        defined( 'WL_TODAYS_TIP_JSON_URL_EN' ) || define( 'WL_TODAYS_TIP_JSON_URL_EN', '/en/wp-json/wp/v2/posts?context=embed&per_page=1&categories=38' );
28
+        $this->entity_service = $entity_service;
29
+
30
+    }
31
+
32
+    /**
33
+     * Set up the dashboard metabox.
34
+     *
35
+     * @since 3.20.0
36
+     */
37
+    public function dashboard_setup() {
38
+        /**
39
+         * Filter name: wl_feature__enable__wordlift-dashboard
40
+         * Feature flag to enable / disable dashboard
41
+         *
42
+         * @since 3.30.0
43
+         */
44 44
         // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
45
-		if ( apply_filters( 'wl_feature__enable__wordlift-dashboard', true ) ) {
46
-			wp_add_dashboard_widget(
47
-				'wl-dashboard-v2',
48
-				__( 'WordLift Dashboard', 'wordlift' ),
49
-				array( $this, 'dashboard_setup_callback' )
50
-			);
51
-		}
52
-
53
-	}
54
-
55
-	/**
56
-	 * The dashboard setup callback.
57
-	 *
58
-	 * @since 3.20.0
59
-	 */
60
-	public function dashboard_setup_callback() {
61
-
62
-		require_once plugin_dir_path( __DIR__ ) . 'admin/partials/wordlift-admin-dashboard-v2.php';
63
-
64
-	}
65
-
66
-	/**
67
-	 * Get the top entities.
68
-	 *
69
-	 * @return array|object|null An array of top entities.
70
-	 * @since 3.20.0
71
-	 */
72
-	private function get_top_entities() {
73
-		/**
74
-		 * @since 3.27.7
75
-		 * @see https://github.com/insideout10/wordlift-plugin/issues/1214
76
-		 * Top entities are generated by cron now.
77
-		 */
78
-		return get_option( Top_Entities::OPTION_KEY, array() );
79
-	}
80
-
81
-	/**
82
-	 * Get the today's tip block.
83
-	 *
84
-	 * @since 3.20.0
85
-	 */
86
-	public static function get_todays_tip_block() {
45
+        if ( apply_filters( 'wl_feature__enable__wordlift-dashboard', true ) ) {
46
+            wp_add_dashboard_widget(
47
+                'wl-dashboard-v2',
48
+                __( 'WordLift Dashboard', 'wordlift' ),
49
+                array( $this, 'dashboard_setup_callback' )
50
+            );
51
+        }
52
+
53
+    }
54
+
55
+    /**
56
+     * The dashboard setup callback.
57
+     *
58
+     * @since 3.20.0
59
+     */
60
+    public function dashboard_setup_callback() {
61
+
62
+        require_once plugin_dir_path( __DIR__ ) . 'admin/partials/wordlift-admin-dashboard-v2.php';
63
+
64
+    }
65
+
66
+    /**
67
+     * Get the top entities.
68
+     *
69
+     * @return array|object|null An array of top entities.
70
+     * @since 3.20.0
71
+     */
72
+    private function get_top_entities() {
73
+        /**
74
+         * @since 3.27.7
75
+         * @see https://github.com/insideout10/wordlift-plugin/issues/1214
76
+         * Top entities are generated by cron now.
77
+         */
78
+        return get_option( Top_Entities::OPTION_KEY, array() );
79
+    }
80
+
81
+    /**
82
+     * Get the today's tip block.
83
+     *
84
+     * @since 3.20.0
85
+     */
86
+    public static function get_todays_tip_block() {
87 87
 
88 88
         // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
89
-		$data = @self::get_todays_tip_data();
89
+        $data = @self::get_todays_tip_data();
90 90
 
91
-		// Unable to get data from the local cache, nor from the remote URL.
92
-		if ( false === $data ) {
93
-			return;
94
-		}
95
-		?>
91
+        // Unable to get data from the local cache, nor from the remote URL.
92
+        if ( false === $data ) {
93
+            return;
94
+        }
95
+        ?>
96 96
 
97 97
 		<div id="wl-todays-tip" class="wl-dashboard__block wl-dashboard__block--todays-tip">
98 98
 			<header>
@@ -107,52 +107,52 @@  discard block
 block discarded – undo
107 107
 		</div>
108 108
 		<?php
109 109
 
110
-	}
111
-
112
-	/**
113
-	 * Get the today's tip data.
114
-	 *
115
-	 * @return array|false The today's tip data or false in case of error.
116
-	 * @since 3.20.0
117
-	 */
118
-	private static function get_todays_tip_data() {
119
-
120
-		// Return the transient.
121
-		if ( false !== get_transient( self::TODAYS_TIP ) ) {
122
-			return get_transient( self::TODAYS_TIP );
123
-		}
124
-
125
-		// If the transient isn't available, query the remote web site.
126
-		$url = WL_TODAYS_TIP_JSON_URL
127
-			   . ( 'it' === get_bloginfo( 'language' ) ? WL_TODAYS_TIP_JSON_URL_IT : WL_TODAYS_TIP_JSON_URL_EN );
128
-
129
-		$response = wp_remote_get( $url );
130
-
131
-		if ( is_wp_error( $response )
132
-			 || ! isset( $response['response']['code'] )
133
-			 || 2 !== (int) $response['response']['code'] / 100 ) {
134
-			return false;
135
-		}
136
-
137
-		$json = json_decode( $response['body'], true );
138
-
139
-		if ( empty( $json )
140
-			 || ! isset( $json[0]['title']['rendered'] )
141
-			 || ! isset( $json[0]['excerpt']['rendered'] )
142
-			 || ! isset( $json[0]['link'] ) ) {
143
-			return false;
144
-		}
145
-
146
-		$value = array(
147
-			'title'   => $json[0]['title']['rendered'],
148
-			'excerpt' => '<!-- cached -->' . $json[0]['excerpt']['rendered'],
149
-			'link'    => $json[0]['link'],
150
-		);
151
-
152
-		// Store the results for one day.
153
-		set_transient( self::TODAYS_TIP, $value, 86400 );
154
-
155
-		return $value;
156
-	}
110
+    }
111
+
112
+    /**
113
+     * Get the today's tip data.
114
+     *
115
+     * @return array|false The today's tip data or false in case of error.
116
+     * @since 3.20.0
117
+     */
118
+    private static function get_todays_tip_data() {
119
+
120
+        // Return the transient.
121
+        if ( false !== get_transient( self::TODAYS_TIP ) ) {
122
+            return get_transient( self::TODAYS_TIP );
123
+        }
124
+
125
+        // If the transient isn't available, query the remote web site.
126
+        $url = WL_TODAYS_TIP_JSON_URL
127
+                . ( 'it' === get_bloginfo( 'language' ) ? WL_TODAYS_TIP_JSON_URL_IT : WL_TODAYS_TIP_JSON_URL_EN );
128
+
129
+        $response = wp_remote_get( $url );
130
+
131
+        if ( is_wp_error( $response )
132
+             || ! isset( $response['response']['code'] )
133
+             || 2 !== (int) $response['response']['code'] / 100 ) {
134
+            return false;
135
+        }
136
+
137
+        $json = json_decode( $response['body'], true );
138
+
139
+        if ( empty( $json )
140
+             || ! isset( $json[0]['title']['rendered'] )
141
+             || ! isset( $json[0]['excerpt']['rendered'] )
142
+             || ! isset( $json[0]['link'] ) ) {
143
+            return false;
144
+        }
145
+
146
+        $value = array(
147
+            'title'   => $json[0]['title']['rendered'],
148
+            'excerpt' => '<!-- cached -->' . $json[0]['excerpt']['rendered'],
149
+            'link'    => $json[0]['link'],
150
+        );
151
+
152
+        // Store the results for one day.
153
+        set_transient( self::TODAYS_TIP, $value, 86400 );
154
+
155
+        return $value;
156
+    }
157 157
 
158 158
 }
Please login to merge, or discard this patch.
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -17,14 +17,14 @@  discard block
 block discarded – undo
17 17
 	 *
18 18
 	 * @since 3.20.0
19 19
 	 */
20
-	public function __construct( $entity_service ) {
20
+	public function __construct($entity_service) {
21 21
 
22
-		add_action( 'wp_dashboard_setup', array( $this, 'dashboard_setup' ) );
22
+		add_action('wp_dashboard_setup', array($this, 'dashboard_setup'));
23 23
 
24 24
 		// Define where to access the tip.
25
-		defined( 'WL_TODAYS_TIP_JSON_URL' ) || define( 'WL_TODAYS_TIP_JSON_URL', 'https://wordlift.io/blog' );
26
-		defined( 'WL_TODAYS_TIP_JSON_URL_IT' ) || define( 'WL_TODAYS_TIP_JSON_URL_IT', '/it/wp-json/wp/v2/posts?context=embed&per_page=1&categories=27' );
27
-		defined( 'WL_TODAYS_TIP_JSON_URL_EN' ) || define( 'WL_TODAYS_TIP_JSON_URL_EN', '/en/wp-json/wp/v2/posts?context=embed&per_page=1&categories=38' );
25
+		defined('WL_TODAYS_TIP_JSON_URL') || define('WL_TODAYS_TIP_JSON_URL', 'https://wordlift.io/blog');
26
+		defined('WL_TODAYS_TIP_JSON_URL_IT') || define('WL_TODAYS_TIP_JSON_URL_IT', '/it/wp-json/wp/v2/posts?context=embed&per_page=1&categories=27');
27
+		defined('WL_TODAYS_TIP_JSON_URL_EN') || define('WL_TODAYS_TIP_JSON_URL_EN', '/en/wp-json/wp/v2/posts?context=embed&per_page=1&categories=38');
28 28
 		$this->entity_service = $entity_service;
29 29
 
30 30
 	}
@@ -42,11 +42,11 @@  discard block
 block discarded – undo
42 42
 		 * @since 3.30.0
43 43
 		 */
44 44
         // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
45
-		if ( apply_filters( 'wl_feature__enable__wordlift-dashboard', true ) ) {
45
+		if (apply_filters('wl_feature__enable__wordlift-dashboard', true)) {
46 46
 			wp_add_dashboard_widget(
47 47
 				'wl-dashboard-v2',
48
-				__( 'WordLift Dashboard', 'wordlift' ),
49
-				array( $this, 'dashboard_setup_callback' )
48
+				__('WordLift Dashboard', 'wordlift'),
49
+				array($this, 'dashboard_setup_callback')
50 50
 			);
51 51
 		}
52 52
 
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
 	 */
60 60
 	public function dashboard_setup_callback() {
61 61
 
62
-		require_once plugin_dir_path( __DIR__ ) . 'admin/partials/wordlift-admin-dashboard-v2.php';
62
+		require_once plugin_dir_path(__DIR__).'admin/partials/wordlift-admin-dashboard-v2.php';
63 63
 
64 64
 	}
65 65
 
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/1214
76 76
 		 * Top entities are generated by cron now.
77 77
 		 */
78
-		return get_option( Top_Entities::OPTION_KEY, array() );
78
+		return get_option(Top_Entities::OPTION_KEY, array());
79 79
 	}
80 80
 
81 81
 	/**
@@ -89,20 +89,20 @@  discard block
 block discarded – undo
89 89
 		$data = @self::get_todays_tip_data();
90 90
 
91 91
 		// Unable to get data from the local cache, nor from the remote URL.
92
-		if ( false === $data ) {
92
+		if (false === $data) {
93 93
 			return;
94 94
 		}
95 95
 		?>
96 96
 
97 97
 		<div id="wl-todays-tip" class="wl-dashboard__block wl-dashboard__block--todays-tip">
98 98
 			<header>
99
-				<h3><?php esc_html_e( "Today's Tip", 'wordlift' ); ?></h3>
99
+				<h3><?php esc_html_e("Today's Tip", 'wordlift'); ?></h3>
100 100
 			</header>
101 101
 			<article>
102
-				<p><strong><?php echo esc_html( wp_strip_all_tags( $data['title'] ) ); ?></strong>
103
-					<?php echo esc_html( wp_strip_all_tags( $data['excerpt'] ) ); ?>
102
+				<p><strong><?php echo esc_html(wp_strip_all_tags($data['title'])); ?></strong>
103
+					<?php echo esc_html(wp_strip_all_tags($data['excerpt'])); ?>
104 104
 					<a target="_blank"
105
-					   href="<?php echo esc_attr( $data['link'] ); ?>"><?php echo esc_html( __( 'Read more', 'wordlift' ) ); ?></a>
105
+					   href="<?php echo esc_attr($data['link']); ?>"><?php echo esc_html(__('Read more', 'wordlift')); ?></a>
106 106
 				</p>
107 107
 		</div>
108 108
 		<?php
@@ -118,39 +118,39 @@  discard block
 block discarded – undo
118 118
 	private static function get_todays_tip_data() {
119 119
 
120 120
 		// Return the transient.
121
-		if ( false !== get_transient( self::TODAYS_TIP ) ) {
122
-			return get_transient( self::TODAYS_TIP );
121
+		if (false !== get_transient(self::TODAYS_TIP)) {
122
+			return get_transient(self::TODAYS_TIP);
123 123
 		}
124 124
 
125 125
 		// If the transient isn't available, query the remote web site.
126 126
 		$url = WL_TODAYS_TIP_JSON_URL
127
-			   . ( 'it' === get_bloginfo( 'language' ) ? WL_TODAYS_TIP_JSON_URL_IT : WL_TODAYS_TIP_JSON_URL_EN );
127
+			   . ('it' === get_bloginfo('language') ? WL_TODAYS_TIP_JSON_URL_IT : WL_TODAYS_TIP_JSON_URL_EN);
128 128
 
129
-		$response = wp_remote_get( $url );
129
+		$response = wp_remote_get($url);
130 130
 
131
-		if ( is_wp_error( $response )
132
-			 || ! isset( $response['response']['code'] )
133
-			 || 2 !== (int) $response['response']['code'] / 100 ) {
131
+		if (is_wp_error($response)
132
+			 || ! isset($response['response']['code'])
133
+			 || 2 !== (int) $response['response']['code'] / 100) {
134 134
 			return false;
135 135
 		}
136 136
 
137
-		$json = json_decode( $response['body'], true );
137
+		$json = json_decode($response['body'], true);
138 138
 
139
-		if ( empty( $json )
140
-			 || ! isset( $json[0]['title']['rendered'] )
141
-			 || ! isset( $json[0]['excerpt']['rendered'] )
142
-			 || ! isset( $json[0]['link'] ) ) {
139
+		if (empty($json)
140
+			 || ! isset($json[0]['title']['rendered'])
141
+			 || ! isset($json[0]['excerpt']['rendered'])
142
+			 || ! isset($json[0]['link'])) {
143 143
 			return false;
144 144
 		}
145 145
 
146 146
 		$value = array(
147 147
 			'title'   => $json[0]['title']['rendered'],
148
-			'excerpt' => '<!-- cached -->' . $json[0]['excerpt']['rendered'],
148
+			'excerpt' => '<!-- cached -->'.$json[0]['excerpt']['rendered'],
149 149
 			'link'    => $json[0]['link'],
150 150
 		);
151 151
 
152 152
 		// Store the results for one day.
153
-		set_transient( self::TODAYS_TIP, $value, 86400 );
153
+		set_transient(self::TODAYS_TIP, $value, 86400);
154 154
 
155 155
 		return $value;
156 156
 	}
Please login to merge, or discard this patch.
src/shortcodes/wordlift-shortcode-field.php 2 patches
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -11,36 +11,36 @@
 block discarded – undo
11 11
  */
12 12
 function wl_shortcode_field( $atts ) {
13 13
 
14
-	// Extract attributes and set default values.
15
-	$field_atts = shortcode_atts(
16
-		array(
17
-			'id'   => null,
18
-			'name' => null,
19
-		),
20
-		$atts
21
-	);
22
-
23
-	// Get id of the post
24
-	$entity_id = $field_atts['id'];
25
-	if ( $field_atts['id'] === null || ! is_numeric( $field_atts['id'] ) ) {
26
-		$entity_id = get_the_ID();
27
-	}
28
-
29
-	$property_name = $field_atts['name'];
30
-	if ( $property_name !== null ) {
31
-		$values = wl_schema_get_value( $entity_id, $property_name );
32
-	}
33
-
34
-	// Return
35
-	if ( is_array( $values ) ) {
36
-		return implode( ', ', $values );
37
-	} else {
38
-		return null;
39
-	}
14
+    // Extract attributes and set default values.
15
+    $field_atts = shortcode_atts(
16
+        array(
17
+            'id'   => null,
18
+            'name' => null,
19
+        ),
20
+        $atts
21
+    );
22
+
23
+    // Get id of the post
24
+    $entity_id = $field_atts['id'];
25
+    if ( $field_atts['id'] === null || ! is_numeric( $field_atts['id'] ) ) {
26
+        $entity_id = get_the_ID();
27
+    }
28
+
29
+    $property_name = $field_atts['name'];
30
+    if ( $property_name !== null ) {
31
+        $values = wl_schema_get_value( $entity_id, $property_name );
32
+    }
33
+
34
+    // Return
35
+    if ( is_array( $values ) ) {
36
+        return implode( ', ', $values );
37
+    } else {
38
+        return null;
39
+    }
40 40
 }
41 41
 
42 42
 function wl_register_shortcode_field() {
43
-	add_shortcode( 'wl_field', 'wl_shortcode_field' );
43
+    add_shortcode( 'wl_field', 'wl_shortcode_field' );
44 44
 }
45 45
 
46 46
 add_action( 'init', 'wl_register_shortcode_field' );
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -9,7 +9,7 @@  discard block
 block discarded – undo
9 9
  * @param array $atts An array of shortcode attributes.
10 10
  * @return string A dom element with requested property value(s).
11 11
  */
12
-function wl_shortcode_field( $atts ) {
12
+function wl_shortcode_field($atts) {
13 13
 
14 14
 	// Extract attributes and set default values.
15 15
 	$field_atts = shortcode_atts(
@@ -22,26 +22,26 @@  discard block
 block discarded – undo
22 22
 
23 23
 	// Get id of the post
24 24
 	$entity_id = $field_atts['id'];
25
-	if ( $field_atts['id'] === null || ! is_numeric( $field_atts['id'] ) ) {
25
+	if ($field_atts['id'] === null || ! is_numeric($field_atts['id'])) {
26 26
 		$entity_id = get_the_ID();
27 27
 	}
28 28
 
29 29
 	$property_name = $field_atts['name'];
30
-	if ( $property_name !== null ) {
31
-		$values = wl_schema_get_value( $entity_id, $property_name );
30
+	if ($property_name !== null) {
31
+		$values = wl_schema_get_value($entity_id, $property_name);
32 32
 	}
33 33
 
34 34
 	// Return
35
-	if ( is_array( $values ) ) {
36
-		return implode( ', ', $values );
35
+	if (is_array($values)) {
36
+		return implode(', ', $values);
37 37
 	} else {
38 38
 		return null;
39 39
 	}
40 40
 }
41 41
 
42 42
 function wl_register_shortcode_field() {
43
-	add_shortcode( 'wl_field', 'wl_shortcode_field' );
43
+	add_shortcode('wl_field', 'wl_shortcode_field');
44 44
 }
45 45
 
46
-add_action( 'init', 'wl_register_shortcode_field' );
46
+add_action('init', 'wl_register_shortcode_field');
47 47
 
Please login to merge, or discard this patch.
src/shortcodes/class-wordlift-shortcode-rest.php 2 patches
Indentation   +125 added lines, -125 removed lines patch added patch discarded remove patch
@@ -10,131 +10,131 @@
 block discarded – undo
10 10
  */
11 11
 abstract class Wordlift_Shortcode_REST {
12 12
 
13
-	/**
14
-	 * The cache_ttl, set by extending classes.
15
-	 */
16
-	const CACHE_TTL = 86400; // 24 hours
17
-
18
-	public function __construct( $endpoint, $args ) {
19
-
20
-		$scope          = $this;
21
-		$this->endpoint = $endpoint;
22
-		$this->args     = $args;
23
-
24
-		// Register rest route with callback
25
-		add_action(
26
-			'rest_api_init',
27
-			function () use ( $scope ) {
28
-				register_rest_route(
29
-					WL_REST_ROUTE_DEFAULT_NAMESPACE,
30
-					$scope->endpoint,
31
-					array(
32
-						'methods'             => WP_REST_Server::READABLE,
33
-						'permission_callback' => '__return_true',
34
-						'callback'            => array( $scope, 'rest_callback' ),
35
-						'args'                => $scope->args,
36
-					)
37
-				);
38
-			}
39
-		);
40
-
41
-		// Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit.
42
-		add_action(
43
-			'plugins_loaded',
44
-			function () use ( $scope ) {
45
-
46
-				if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
47
-					return;
48
-				}
49
-
50
-				remove_action( 'plugins_loaded', 'rocket_init' );
51
-				remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
52
-				remove_action( 'plugins_loaded', 'wpseo_init', 14 );
53
-			},
54
-			0
55
-		);
56
-
57
-		add_action(
58
-			'init',
59
-			function () use ( $scope ) {
60
-
61
-				if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
62
-					return;
63
-				}
64
-
65
-				remove_action( 'init', 'wp_widgets_init', 1 );
66
-				remove_action( 'init', 'gglcptch_init' );
67
-			},
68
-			0
69
-		);
70
-
71
-	}
72
-
73
-	abstract public function get_data( $request );
74
-
75
-	public function rest_callback( WP_REST_Request $request ) {
76
-
77
-		// Respond from origin if TTL is 0
78
-		if ( static::CACHE_TTL === 0 ) {
79
-
80
-			$data     = $this->get_data( $request );
81
-			$response = rest_ensure_response( $data );
82
-			if ( is_wp_error( $data ) ) {
83
-				return $response;
84
-			}
85
-			$response->header( 'Access-Control-Allow-Origin', '*' );
86
-			$response->header( 'X-WordLift-Cache', 'MISS' );
87
-
88
-			return $response;
89
-		}
90
-
91
-		// Create the cache key.
92
-		$cache_key_params = $request->get_params();
93
-		unset( $cache_key_params['uniqid'] );
94
-		unset( $cache_key_params['rest_route'] );
95
-		$cache_key = array( 'request_params' => $cache_key_params );
96
-
97
-		// Create the TTL cache and try to get the results.
98
-		$cache         = new Ttl_Cache( $this->endpoint, static::CACHE_TTL );
99
-		$cache_results = $cache->get( $cache_key );
100
-
101
-		if ( isset( $cache_results ) ) {
102
-
103
-			$response = rest_ensure_response( $cache_results );
104
-			$response->header( 'Access-Control-Allow-Origin', '*' );
105
-			$response->header( 'X-WordLift-Cache', 'HIT' );
106
-
107
-			return $response;
108
-		}
109
-
110
-		$data     = $this->get_data( $request );
111
-		$response = rest_ensure_response( $data );
112
-		if ( is_wp_error( $data ) ) {
113
-			return $response;
114
-		}
115
-		$response->header( 'Access-Control-Allow-Origin', '*' );
116
-		$response->header( 'X-WordLift-Cache', 'MISS' );
117
-
118
-		// Put the result before sending the json to the client, since sending the json will terminate us.
119
-		$cache->put( $cache_key, $data );
120
-
121
-		return $response;
122
-
123
-	}
124
-
125
-	private function is_endpoint() {
126
-		$compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint;
127
-
128
-		// Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably
129
-
130
-		if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( esc_url_raw( wp_unslash( (string) $_SERVER['REQUEST_URI'] ) ), $compare_route ) ) {
131
-			return true;
132
-		}
133
-		if ( ! empty( $_GET['rest_route'] ) && strpos( esc_url_raw( wp_unslash( (string) $_GET['rest_route'] ) ), $compare_route ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
134
-			return true;
135
-		}
13
+    /**
14
+     * The cache_ttl, set by extending classes.
15
+     */
16
+    const CACHE_TTL = 86400; // 24 hours
17
+
18
+    public function __construct( $endpoint, $args ) {
19
+
20
+        $scope          = $this;
21
+        $this->endpoint = $endpoint;
22
+        $this->args     = $args;
23
+
24
+        // Register rest route with callback
25
+        add_action(
26
+            'rest_api_init',
27
+            function () use ( $scope ) {
28
+                register_rest_route(
29
+                    WL_REST_ROUTE_DEFAULT_NAMESPACE,
30
+                    $scope->endpoint,
31
+                    array(
32
+                        'methods'             => WP_REST_Server::READABLE,
33
+                        'permission_callback' => '__return_true',
34
+                        'callback'            => array( $scope, 'rest_callback' ),
35
+                        'args'                => $scope->args,
36
+                    )
37
+                );
38
+            }
39
+        );
40
+
41
+        // Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit.
42
+        add_action(
43
+            'plugins_loaded',
44
+            function () use ( $scope ) {
45
+
46
+                if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
47
+                    return;
48
+                }
49
+
50
+                remove_action( 'plugins_loaded', 'rocket_init' );
51
+                remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
52
+                remove_action( 'plugins_loaded', 'wpseo_init', 14 );
53
+            },
54
+            0
55
+        );
56
+
57
+        add_action(
58
+            'init',
59
+            function () use ( $scope ) {
60
+
61
+                if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
62
+                    return;
63
+                }
64
+
65
+                remove_action( 'init', 'wp_widgets_init', 1 );
66
+                remove_action( 'init', 'gglcptch_init' );
67
+            },
68
+            0
69
+        );
70
+
71
+    }
72
+
73
+    abstract public function get_data( $request );
74
+
75
+    public function rest_callback( WP_REST_Request $request ) {
76
+
77
+        // Respond from origin if TTL is 0
78
+        if ( static::CACHE_TTL === 0 ) {
79
+
80
+            $data     = $this->get_data( $request );
81
+            $response = rest_ensure_response( $data );
82
+            if ( is_wp_error( $data ) ) {
83
+                return $response;
84
+            }
85
+            $response->header( 'Access-Control-Allow-Origin', '*' );
86
+            $response->header( 'X-WordLift-Cache', 'MISS' );
87
+
88
+            return $response;
89
+        }
90
+
91
+        // Create the cache key.
92
+        $cache_key_params = $request->get_params();
93
+        unset( $cache_key_params['uniqid'] );
94
+        unset( $cache_key_params['rest_route'] );
95
+        $cache_key = array( 'request_params' => $cache_key_params );
96
+
97
+        // Create the TTL cache and try to get the results.
98
+        $cache         = new Ttl_Cache( $this->endpoint, static::CACHE_TTL );
99
+        $cache_results = $cache->get( $cache_key );
100
+
101
+        if ( isset( $cache_results ) ) {
102
+
103
+            $response = rest_ensure_response( $cache_results );
104
+            $response->header( 'Access-Control-Allow-Origin', '*' );
105
+            $response->header( 'X-WordLift-Cache', 'HIT' );
106
+
107
+            return $response;
108
+        }
109
+
110
+        $data     = $this->get_data( $request );
111
+        $response = rest_ensure_response( $data );
112
+        if ( is_wp_error( $data ) ) {
113
+            return $response;
114
+        }
115
+        $response->header( 'Access-Control-Allow-Origin', '*' );
116
+        $response->header( 'X-WordLift-Cache', 'MISS' );
117
+
118
+        // Put the result before sending the json to the client, since sending the json will terminate us.
119
+        $cache->put( $cache_key, $data );
120
+
121
+        return $response;
122
+
123
+    }
124
+
125
+    private function is_endpoint() {
126
+        $compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint;
127
+
128
+        // Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably
129
+
130
+        if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( esc_url_raw( wp_unslash( (string) $_SERVER['REQUEST_URI'] ) ), $compare_route ) ) {
131
+            return true;
132
+        }
133
+        if ( ! empty( $_GET['rest_route'] ) && strpos( esc_url_raw( wp_unslash( (string) $_GET['rest_route'] ) ), $compare_route ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
134
+            return true;
135
+        }
136 136
 
137
-		return false;
138
-	}
137
+        return false;
138
+    }
139 139
 
140 140
 }
Please login to merge, or discard this patch.
Spacing   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
 	 */
16 16
 	const CACHE_TTL = 86400; // 24 hours
17 17
 
18
-	public function __construct( $endpoint, $args ) {
18
+	public function __construct($endpoint, $args) {
19 19
 
20 20
 		$scope          = $this;
21 21
 		$this->endpoint = $endpoint;
@@ -24,14 +24,14 @@  discard block
 block discarded – undo
24 24
 		// Register rest route with callback
25 25
 		add_action(
26 26
 			'rest_api_init',
27
-			function () use ( $scope ) {
27
+			function() use ($scope) {
28 28
 				register_rest_route(
29 29
 					WL_REST_ROUTE_DEFAULT_NAMESPACE,
30 30
 					$scope->endpoint,
31 31
 					array(
32 32
 						'methods'             => WP_REST_Server::READABLE,
33 33
 						'permission_callback' => '__return_true',
34
-						'callback'            => array( $scope, 'rest_callback' ),
34
+						'callback'            => array($scope, 'rest_callback'),
35 35
 						'args'                => $scope->args,
36 36
 					)
37 37
 				);
@@ -41,96 +41,96 @@  discard block
 block discarded – undo
41 41
 		// Optimizations: disable unneeded plugins on this specific REST call. WPSeo is slowing down the responses quite a bit.
42 42
 		add_action(
43 43
 			'plugins_loaded',
44
-			function () use ( $scope ) {
44
+			function() use ($scope) {
45 45
 
46
-				if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
46
+				if ( ! defined('REST_REQUEST') || ! REST_REQUEST || ! $scope->is_endpoint()) {
47 47
 					return;
48 48
 				}
49 49
 
50
-				remove_action( 'plugins_loaded', 'rocket_init' );
51
-				remove_action( 'plugins_loaded', 'wpseo_premium_init', 14 );
52
-				remove_action( 'plugins_loaded', 'wpseo_init', 14 );
50
+				remove_action('plugins_loaded', 'rocket_init');
51
+				remove_action('plugins_loaded', 'wpseo_premium_init', 14);
52
+				remove_action('plugins_loaded', 'wpseo_init', 14);
53 53
 			},
54 54
 			0
55 55
 		);
56 56
 
57 57
 		add_action(
58 58
 			'init',
59
-			function () use ( $scope ) {
59
+			function() use ($scope) {
60 60
 
61
-				if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! $scope->is_endpoint() ) {
61
+				if ( ! defined('REST_REQUEST') || ! REST_REQUEST || ! $scope->is_endpoint()) {
62 62
 					return;
63 63
 				}
64 64
 
65
-				remove_action( 'init', 'wp_widgets_init', 1 );
66
-				remove_action( 'init', 'gglcptch_init' );
65
+				remove_action('init', 'wp_widgets_init', 1);
66
+				remove_action('init', 'gglcptch_init');
67 67
 			},
68 68
 			0
69 69
 		);
70 70
 
71 71
 	}
72 72
 
73
-	abstract public function get_data( $request );
73
+	abstract public function get_data($request);
74 74
 
75
-	public function rest_callback( WP_REST_Request $request ) {
75
+	public function rest_callback(WP_REST_Request $request) {
76 76
 
77 77
 		// Respond from origin if TTL is 0
78
-		if ( static::CACHE_TTL === 0 ) {
78
+		if (static::CACHE_TTL === 0) {
79 79
 
80
-			$data     = $this->get_data( $request );
81
-			$response = rest_ensure_response( $data );
82
-			if ( is_wp_error( $data ) ) {
80
+			$data     = $this->get_data($request);
81
+			$response = rest_ensure_response($data);
82
+			if (is_wp_error($data)) {
83 83
 				return $response;
84 84
 			}
85
-			$response->header( 'Access-Control-Allow-Origin', '*' );
86
-			$response->header( 'X-WordLift-Cache', 'MISS' );
85
+			$response->header('Access-Control-Allow-Origin', '*');
86
+			$response->header('X-WordLift-Cache', 'MISS');
87 87
 
88 88
 			return $response;
89 89
 		}
90 90
 
91 91
 		// Create the cache key.
92 92
 		$cache_key_params = $request->get_params();
93
-		unset( $cache_key_params['uniqid'] );
94
-		unset( $cache_key_params['rest_route'] );
95
-		$cache_key = array( 'request_params' => $cache_key_params );
93
+		unset($cache_key_params['uniqid']);
94
+		unset($cache_key_params['rest_route']);
95
+		$cache_key = array('request_params' => $cache_key_params);
96 96
 
97 97
 		// Create the TTL cache and try to get the results.
98
-		$cache         = new Ttl_Cache( $this->endpoint, static::CACHE_TTL );
99
-		$cache_results = $cache->get( $cache_key );
98
+		$cache         = new Ttl_Cache($this->endpoint, static::CACHE_TTL);
99
+		$cache_results = $cache->get($cache_key);
100 100
 
101
-		if ( isset( $cache_results ) ) {
101
+		if (isset($cache_results)) {
102 102
 
103
-			$response = rest_ensure_response( $cache_results );
104
-			$response->header( 'Access-Control-Allow-Origin', '*' );
105
-			$response->header( 'X-WordLift-Cache', 'HIT' );
103
+			$response = rest_ensure_response($cache_results);
104
+			$response->header('Access-Control-Allow-Origin', '*');
105
+			$response->header('X-WordLift-Cache', 'HIT');
106 106
 
107 107
 			return $response;
108 108
 		}
109 109
 
110
-		$data     = $this->get_data( $request );
111
-		$response = rest_ensure_response( $data );
112
-		if ( is_wp_error( $data ) ) {
110
+		$data     = $this->get_data($request);
111
+		$response = rest_ensure_response($data);
112
+		if (is_wp_error($data)) {
113 113
 			return $response;
114 114
 		}
115
-		$response->header( 'Access-Control-Allow-Origin', '*' );
116
-		$response->header( 'X-WordLift-Cache', 'MISS' );
115
+		$response->header('Access-Control-Allow-Origin', '*');
116
+		$response->header('X-WordLift-Cache', 'MISS');
117 117
 
118 118
 		// Put the result before sending the json to the client, since sending the json will terminate us.
119
-		$cache->put( $cache_key, $data );
119
+		$cache->put($cache_key, $data);
120 120
 
121 121
 		return $response;
122 122
 
123 123
 	}
124 124
 
125 125
 	private function is_endpoint() {
126
-		$compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE . $this->endpoint;
126
+		$compare_route = WL_REST_ROUTE_DEFAULT_NAMESPACE.$this->endpoint;
127 127
 
128 128
 		// Directly accessing $_SERVER['REQUEST_URI'] or $_GET['rest_route'] here as it's too early to use global $wp reliably
129 129
 
130
-		if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( esc_url_raw( wp_unslash( (string) $_SERVER['REQUEST_URI'] ) ), $compare_route ) ) {
130
+		if (isset($_SERVER['REQUEST_URI']) && strpos(esc_url_raw(wp_unslash((string) $_SERVER['REQUEST_URI'])), $compare_route)) {
131 131
 			return true;
132 132
 		}
133
-		if ( ! empty( $_GET['rest_route'] ) && strpos( esc_url_raw( wp_unslash( (string) $_GET['rest_route'] ) ), $compare_route ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
133
+		if ( ! empty($_GET['rest_route']) && strpos(esc_url_raw(wp_unslash((string) $_GET['rest_route'])), $compare_route)) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended
134 134
 			return true;
135 135
 		}
136 136
 
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,46 +11,46 @@  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
-	check_ajax_referer( 'wl_geomap' );
15
-	// Get the post Id.
16
-	$post_id = isset( $_REQUEST['post_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_id'] ) ) : null;
14
+    check_ajax_referer( 'wl_geomap' );
15
+    // Get the post Id.
16
+    $post_id = isset( $_REQUEST['post_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_id'] ) ) : null;
17 17
 
18
-	$map_data = ( is_numeric( $post_id )
19
-		? wl_shortcode_geomap_ajax_single_post( (int) $post_id )
20
-		: wl_shortcode_geomap_ajax_all_posts() );
18
+    $map_data = ( is_numeric( $post_id )
19
+        ? wl_shortcode_geomap_ajax_single_post( (int) $post_id )
20
+        : wl_shortcode_geomap_ajax_all_posts() );
21 21
 
22
-	wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
22
+    wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
23 23
 
24 24
 }
25 25
 
26 26
 function wl_shortcode_geomap_ajax_all_posts() {
27
-	global $wpdb;
28
-
29
-	return $wpdb->get_results(
30
-		$wpdb->prepare(
31
-			'SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude'
32
-			. " FROM {$wpdb->posts} p1 "
33
-			. " INNER JOIN {$wpdb->prefix}wl_relation_instances ri"
34
-			. '  ON ri.object_id = p1.ID AND ri.predicate = %s'
35
-			. " INNER JOIN {$wpdb->postmeta} pm1"
36
-			. "  ON pm1.post_id = p1.ID AND pm1.meta_key = %s AND '0' != pm1.meta_value"
37
-			. " INNER JOIN {$wpdb->postmeta} pm2"
38
-			. "  ON pm2.post_id = p1.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value"
39
-			. ' WHERE p1.post_status = %s',
40
-			'where',
41
-			'wl_geo_latitude',
42
-			'wl_geo_longitude',
43
-			'publish'
44
-		)
45
-	);
27
+    global $wpdb;
28
+
29
+    return $wpdb->get_results(
30
+        $wpdb->prepare(
31
+            'SELECT DISTINCT p1.ID, p1.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude'
32
+            . " FROM {$wpdb->posts} p1 "
33
+            . " INNER JOIN {$wpdb->prefix}wl_relation_instances ri"
34
+            . '  ON ri.object_id = p1.ID AND ri.predicate = %s'
35
+            . " INNER JOIN {$wpdb->postmeta} pm1"
36
+            . "  ON pm1.post_id = p1.ID AND pm1.meta_key = %s AND '0' != pm1.meta_value"
37
+            . " INNER JOIN {$wpdb->postmeta} pm2"
38
+            . "  ON pm2.post_id = p1.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value"
39
+            . ' WHERE p1.post_status = %s',
40
+            'where',
41
+            'wl_geo_latitude',
42
+            'wl_geo_longitude',
43
+            'publish'
44
+        )
45
+    );
46 46
 }
47 47
 
48 48
 function wl_shortcode_geomap_ajax_single_post( $post_id ) {
49
-	global $wpdb;
49
+    global $wpdb;
50 50
 
51
-	return $wpdb->get_results(
52
-		$wpdb->prepare(
53
-			"
51
+    return $wpdb->get_results(
52
+        $wpdb->prepare(
53
+            "
54 54
 		SELECT p2.ID, p2.post_title, pm1.meta_value AS longitude, pm2.meta_value AS latitude
55 55
 		 FROM {$wpdb->prefix}wl_relation_instances ri
56 56
 		 INNER JOIN {$wpdb->posts} p2
@@ -69,87 +69,87 @@  discard block
 block discarded – undo
69 69
 		  ON pm2.post_id = p.ID AND pm2.meta_key = %s AND '0' != pm2.meta_value
70 70
 		 WHERE p.ID = %s
71 71
 		",
72
-			'publish',
73
-			'wl_geo_latitude',
74
-			'wl_geo_longitude',
75
-			$post_id,
76
-			'where',
77
-			// UNION
78
-			'wl_geo_latitude',
79
-			'wl_geo_longitude',
80
-			$post_id
81
-		)
82
-	);
72
+            'publish',
73
+            'wl_geo_latitude',
74
+            'wl_geo_longitude',
75
+            $post_id,
76
+            'where',
77
+            // UNION
78
+            'wl_geo_latitude',
79
+            'wl_geo_longitude',
80
+            $post_id
81
+        )
82
+    );
83 83
 }
84 84
 
85 85
 function wl_shortcode_geomap_get_subjects( $post_id, $exclude_post_id ) {
86 86
 
87
-	global $wpdb;
88
-
89
-	return $wpdb->get_results(
90
-		$wpdb->prepare(
91
-			'SELECT p.ID, p.post_title'
92
-			. " FROM {$wpdb->prefix}wl_relation_instances ri" .
93
-			" INNER JOIN {$wpdb->posts} p" .
94
-			'  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d' .
95
-			' WHERE ri.object_id = %d AND ri.predicate = %s',
96
-			'publish',
97
-			$exclude_post_id,
98
-			$post_id,
99
-			'where'
100
-		)
101
-	);
87
+    global $wpdb;
88
+
89
+    return $wpdb->get_results(
90
+        $wpdb->prepare(
91
+            'SELECT p.ID, p.post_title'
92
+            . " FROM {$wpdb->prefix}wl_relation_instances ri" .
93
+            " INNER JOIN {$wpdb->posts} p" .
94
+            '  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d' .
95
+            ' WHERE ri.object_id = %d AND ri.predicate = %s',
96
+            'publish',
97
+            $exclude_post_id,
98
+            $post_id,
99
+            'where'
100
+        )
101
+    );
102 102
 }
103 103
 
104 104
 function wl_shortcode_geomap_format_results( $results, $post_id = null ) {
105 105
 
106
-	$boundaries = array();
107
-	$features   = array_map(
108
-		function ( $item ) use ( &$boundaries, $post_id ) {
109
-
110
-			$thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
111
-			$thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
112
-
113
-			// Related posts.
114
-			// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
115
-			$subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
116
-			$subjects_inner_html = array_reduce(
117
-				$subjects,
118
-				function ( $carry, $subject ) {
119
-
120
-					$permalink = get_permalink( $subject->ID );
121
-
122
-					return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
123
-				},
124
-				''
125
-			);
126
-			$subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
127
-
128
-			$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 );
129
-			$latitude      = floatval( $item->latitude );
130
-			$longitude     = floatval( $item->longitude );
131
-			$coordinates   = array( $latitude, $longitude );
132
-			$geometry      = array(
133
-				'type'        => 'Point',
134
-				'coordinates' => $coordinates,
135
-			);
136
-
137
-			// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
138
-			$boundaries[] = array( $longitude, $latitude );
139
-
140
-			return array(
141
-				'type'       => 'Feature',
142
-				'properties' => array( 'popupContent' => $popup_content ),
143
-				'geometry'   => $geometry,
144
-			);
145
-		},
146
-		$results
147
-	);
148
-
149
-	return array(
150
-		'features'   => $features,
151
-		'boundaries' => $boundaries,
152
-	);
106
+    $boundaries = array();
107
+    $features   = array_map(
108
+        function ( $item ) use ( &$boundaries, $post_id ) {
109
+
110
+            $thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
111
+            $thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
112
+
113
+            // Related posts.
114
+            // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
115
+            $subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
116
+            $subjects_inner_html = array_reduce(
117
+                $subjects,
118
+                function ( $carry, $subject ) {
119
+
120
+                    $permalink = get_permalink( $subject->ID );
121
+
122
+                    return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
123
+                },
124
+                ''
125
+            );
126
+            $subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
127
+
128
+            $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 );
129
+            $latitude      = floatval( $item->latitude );
130
+            $longitude     = floatval( $item->longitude );
131
+            $coordinates   = array( $latitude, $longitude );
132
+            $geometry      = array(
133
+                'type'        => 'Point',
134
+                'coordinates' => $coordinates,
135
+            );
136
+
137
+            // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
138
+            $boundaries[] = array( $longitude, $latitude );
139
+
140
+            return array(
141
+                'type'       => 'Feature',
142
+                'properties' => array( 'popupContent' => $popup_content ),
143
+                'geometry'   => $geometry,
144
+            );
145
+        },
146
+        $results
147
+    );
148
+
149
+    return array(
150
+        'features'   => $features,
151
+        'boundaries' => $boundaries,
152
+    );
153 153
 }
154 154
 
155 155
 add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' );
Please login to merge, or discard this patch.
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -11,15 +11,15 @@  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
-	check_ajax_referer( 'wl_geomap' );
14
+	check_ajax_referer('wl_geomap');
15 15
 	// Get the post Id.
16
-	$post_id = isset( $_REQUEST['post_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_id'] ) ) : null;
16
+	$post_id = isset($_REQUEST['post_id']) ? sanitize_text_field(wp_unslash($_REQUEST['post_id'])) : null;
17 17
 
18
-	$map_data = ( is_numeric( $post_id )
19
-		? wl_shortcode_geomap_ajax_single_post( (int) $post_id )
20
-		: wl_shortcode_geomap_ajax_all_posts() );
18
+	$map_data = (is_numeric($post_id)
19
+		? wl_shortcode_geomap_ajax_single_post((int) $post_id)
20
+		: wl_shortcode_geomap_ajax_all_posts());
21 21
 
22
-	wl_core_send_json( wl_shortcode_geomap_format_results( $map_data, $post_id ) );
22
+	wl_core_send_json(wl_shortcode_geomap_format_results($map_data, $post_id));
23 23
 
24 24
 }
25 25
 
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
 	);
46 46
 }
47 47
 
48
-function wl_shortcode_geomap_ajax_single_post( $post_id ) {
48
+function wl_shortcode_geomap_ajax_single_post($post_id) {
49 49
 	global $wpdb;
50 50
 
51 51
 	return $wpdb->get_results(
@@ -82,16 +82,16 @@  discard block
 block discarded – undo
82 82
 	);
83 83
 }
84 84
 
85
-function wl_shortcode_geomap_get_subjects( $post_id, $exclude_post_id ) {
85
+function wl_shortcode_geomap_get_subjects($post_id, $exclude_post_id) {
86 86
 
87 87
 	global $wpdb;
88 88
 
89 89
 	return $wpdb->get_results(
90 90
 		$wpdb->prepare(
91 91
 			'SELECT p.ID, p.post_title'
92
-			. " FROM {$wpdb->prefix}wl_relation_instances ri" .
93
-			" INNER JOIN {$wpdb->posts} p" .
94
-			'  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d' .
92
+			. " FROM {$wpdb->prefix}wl_relation_instances ri".
93
+			" INNER JOIN {$wpdb->posts} p".
94
+			'  ON p.ID = ri.subject_id AND p.post_status = %s AND p.ID != %d'.
95 95
 			' WHERE ri.object_id = %d AND ri.predicate = %s',
96 96
 			'publish',
97 97
 			$exclude_post_id,
@@ -101,45 +101,45 @@  discard block
 block discarded – undo
101 101
 	);
102 102
 }
103 103
 
104
-function wl_shortcode_geomap_format_results( $results, $post_id = null ) {
104
+function wl_shortcode_geomap_format_results($results, $post_id = null) {
105 105
 
106 106
 	$boundaries = array();
107 107
 	$features   = array_map(
108
-		function ( $item ) use ( &$boundaries, $post_id ) {
108
+		function($item) use (&$boundaries, $post_id) {
109 109
 
110
-			$thumbnail_url  = get_the_post_thumbnail_url( $item->ID );
111
-			$thumbnail_html = ( $thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '' );
110
+			$thumbnail_url  = get_the_post_thumbnail_url($item->ID);
111
+			$thumbnail_html = ($thumbnail_url ? "<img src='$thumbnail_url' width='100%'>" : '');
112 112
 
113 113
 			// Related posts.
114 114
 			// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
115
-			$subjects            = wl_shortcode_geomap_get_subjects( $item->ID, $post_id );
115
+			$subjects            = wl_shortcode_geomap_get_subjects($item->ID, $post_id);
116 116
 			$subjects_inner_html = array_reduce(
117 117
 				$subjects,
118
-				function ( $carry, $subject ) {
118
+				function($carry, $subject) {
119 119
 
120
-					$permalink = get_permalink( $subject->ID );
120
+					$permalink = get_permalink($subject->ID);
121 121
 
122
-					return $carry . sprintf( '<li><a href="%s">%s</a></li>', $permalink, esc_html( $subject->post_title ) );
122
+					return $carry.sprintf('<li><a href="%s">%s</a></li>', $permalink, esc_html($subject->post_title));
123 123
 				},
124 124
 				''
125 125
 			);
126
-			$subjects_html       = ( ! empty( $subjects_inner_html ) ? '<ul>' . $subjects_inner_html . '</ul>' : '' );
126
+			$subjects_html = ( ! empty($subjects_inner_html) ? '<ul>'.$subjects_inner_html.'</ul>' : '');
127 127
 
128
-			$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 );
129
-			$latitude      = floatval( $item->latitude );
130
-			$longitude     = floatval( $item->longitude );
131
-			$coordinates   = array( $latitude, $longitude );
128
+			$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);
129
+			$latitude      = floatval($item->latitude);
130
+			$longitude     = floatval($item->longitude);
131
+			$coordinates   = array($latitude, $longitude);
132 132
 			$geometry      = array(
133 133
 				'type'        => 'Point',
134 134
 				'coordinates' => $coordinates,
135 135
 			);
136 136
 
137 137
 			// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
138
-			$boundaries[] = array( $longitude, $latitude );
138
+			$boundaries[] = array($longitude, $latitude);
139 139
 
140 140
 			return array(
141 141
 				'type'       => 'Feature',
142
-				'properties' => array( 'popupContent' => $popup_content ),
142
+				'properties' => array('popupContent' => $popup_content),
143 143
 				'geometry'   => $geometry,
144 144
 			);
145 145
 		},
@@ -152,5 +152,5 @@  discard block
 block discarded – undo
152 152
 	);
153 153
 }
154 154
 
155
-add_action( 'wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax' );
156
-add_action( 'wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax' );
155
+add_action('wp_ajax_wl_geomap', 'wl_shortcode_geomap_ajax');
156
+add_action('wp_ajax_nopriv_wl_geomap', 'wl_shortcode_geomap_ajax');
Please login to merge, or discard this patch.
src/shortcodes/class-wordlift-products-navigator-shortcode-rest.php 2 patches
Indentation   +287 added lines, -287 removed lines patch added patch discarded remove patch
@@ -4,243 +4,243 @@  discard block
 block discarded – undo
4 4
 
5 5
 class Wordlift_Products_Navigator_Shortcode_REST extends Wordlift_Shortcode_REST {
6 6
 
7
-	const CACHE_TTL = 3600; // 1 hour
8
-
9
-	public function __construct() {
10
-		parent::__construct(
11
-			'/products-navigator',
12
-			array(
13
-				'post_id' => array(
14
-					'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ),
15
-					'type'        => 'integer',
16
-					'required'    => true,
17
-				),
18
-				'uniqid'  => array(
19
-					'description' => __( 'Navigator uniqueid', 'wordlift' ),
20
-					'type'        => 'string',
21
-					'required'    => true,
22
-				),
23
-				'limit'   => array(
24
-					'default'           => 4,
25
-					'type'              => 'integer',
26
-					'sanitize_callback' => 'absint',
27
-				),
28
-				'offset'  => array(
29
-					'default'           => 0,
30
-					'type'              => 'integer',
31
-					'sanitize_callback' => 'absint',
32
-				),
33
-				'sort'    => array(
34
-					'default'           => 'ID DESC',
35
-					'sanitize_callback' => 'sanitize_sql_orderby',
36
-				),
37
-				'amp'     => array(
38
-					'sanitize_callback' => 'rest_sanitize_boolean',
39
-				),
40
-			)
41
-		);
42
-	}
43
-
44
-	public function get_data( $request ) {
45
-
46
-		// Sanitize and set defaults
47
-		$navigator_length = $request['limit'];
48
-		$navigator_offset = $request['offset'];
49
-		$order_by         = $request['sort'];
50
-		$post_id          = $request['post_id'];
51
-		$navigator_id     = $request['uniqid'];
52
-		$amp              = $request['amp'];
53
-
54
-		$post = get_post( $post_id );
55
-
56
-		// Post ID has to match an existing item
57
-		if ( null === $post ) {
58
-			return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) );
59
-		}
60
-
61
-		// Determine navigator type and call respective get_*_results
62
-		if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
63
-			$referencing_posts = $this->get_entity_results(
64
-				$post_id,
65
-				array(
66
-					'ID',
67
-					'post_title',
68
-				),
69
-				$order_by,
70
-				$navigator_length,
71
-				$navigator_offset
72
-			);
73
-		} else {
74
-			$referencing_posts = $this->get_post_results(
75
-				$post_id,
76
-				array(
77
-					'ID',
78
-					'post_title',
79
-				),
80
-				$order_by,
81
-				$navigator_length,
82
-				$navigator_offset
83
-			);
84
-		}
85
-
86
-		// Fetch directly referencing posts excluding referencing posts via entities
87
-		$directly_referencing_posts = $this->get_directly_referencing_posts(
88
-			$post_id,
89
-			array_map(
90
-				function ( $referencing_post ) {
91
-					return $referencing_post->ID;
92
-				},
93
-				$referencing_posts
94
-			)
95
-		);
96
-
97
-		// Combine directly referencing posts and referencing posts via entities
98
-		$referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts );
99
-
100
-		// loop over them and take the first one which is not already in the $related_posts
101
-		$results = array();
102
-		foreach ( $referencing_posts as $referencing_post ) {
103
-			$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
104
-			$product           = wc_get_product( $referencing_post->ID );
105
-
106
-			$result = array(
107
-				'product' => array(
108
-					'id'              => $referencing_post->ID,
109
-					'permalink'       => get_permalink( $referencing_post->ID ),
110
-					'title'           => $referencing_post->post_title,
111
-					'thumbnail'       => get_the_post_thumbnail_url( $referencing_post, 'medium' ),
112
-					'regular_price'   => $product->get_regular_price(),
113
-					'sale_price'      => $product->get_sale_price(),
114
-					'price'           => $product->get_price(),
115
-					'currency_symbol' => get_woocommerce_currency_symbol(),
116
-					'discount_pc'     => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0,
117
-					'average_rating'  => $product->get_average_rating(),
118
-					'rating_count'    => $product->get_rating_count(),
119
-					'rating_html'     => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ),
120
-				),
121
-				'entity'  => array(
122
-					'id'        => $referencing_post->entity_id,
123
-					'label'     => $serialized_entity['label'],
124
-					'mainType'  => $serialized_entity['mainType'],
125
-					'permalink' => get_permalink( $referencing_post->entity_id ),
126
-				),
127
-			);
128
-
129
-			$results[] = $result;
130
-
131
-		}
132
-
133
-		if ( count( $results ) < $navigator_length ) {
134
-			$results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
135
-		}
136
-
137
-		// Add filler posts if needed
138
-		$filler_count = $navigator_length - count( $results );
139
-		if ( $filler_count > 0 ) {
140
-			$referencing_post_ids = array_map(
141
-				function ( $p ) {
142
-					return $p->ID;
143
-				},
144
-				$referencing_posts
145
-			);
146
-			/**
147
-			 * @since 3.28.0
148
-			 * Filler posts are fetched using this util.
149
-			 */
150
-			$filler_posts_util       = new Filler_Posts_Util( $post_id, 'product' );
151
-			$post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids );
152
-			$filler_posts            = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded );
153
-			$results                 = array_merge( $results, $filler_posts );
154
-		}
155
-
156
-		// Apply filters after fillers are added
157
-		foreach ( $results as $result_index => $result ) {
158
-			$results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id );
159
-			$results[ $result_index ]['entity']  = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
160
-		}
161
-
162
-		$results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id );
163
-
164
-		return $amp ? array(
165
-			'items' => array(
166
-				array( 'values' => $results ),
167
-			),
168
-		) : $results;
169
-
170
-	}
171
-
172
-	private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) {
173
-
174
-		$directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id );
175
-
176
-		$post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids );
177
-
178
-		$directly_referencing_posts = get_posts(
179
-			array(
180
-				'meta_query'          => array(
181
-					array(
182
-						'key' => '_thumbnail_id',
183
-					),
184
-					array(
185
-						'key'   => '_stock_status',
186
-						'value' => 'instock',
187
-					),
188
-				),
189
-				'post__in'            => $post__in,
190
-				'post_type'           => 'product',
191
-				'ignore_sticky_posts' => 1,
192
-			)
193
-		);
194
-
195
-		$results = array();
196
-
197
-		foreach ( $directly_referencing_posts as $post ) {
198
-			$result             = new stdClass();
199
-			$result->ID         = $post->ID;
200
-			$result->post_title = $post->post_title;
201
-			$result->entity_id  = $post->ID;
202
-			$results[]          = $result;
203
-		}
204
-
205
-		return $results;
206
-	}
207
-
208
-	private function get_entity_results(
209
-		$post_id,
210
-		$fields = array(
211
-			'ID',
212
-			'post_title',
213
-		),
214
-		$order_by = 'ID DESC',
215
-		$limit = 10,
216
-		$offset = 0
217
-	) {
218
-		global $wpdb;
219
-
220
-		$select = implode(
221
-			', ',
222
-			array_map(
223
-				function ( $item ) {
224
-					return "p.$item AS $item";
225
-				},
226
-				(array) $fields
227
-			)
228
-		);
229
-
230
-		$order_by = implode(
231
-			', ',
232
-			array_map(
233
-				function ( $item ) {
234
-					return "p.$item";
235
-				},
236
-				(array) $order_by
237
-			)
238
-		);
7
+    const CACHE_TTL = 3600; // 1 hour
8
+
9
+    public function __construct() {
10
+        parent::__construct(
11
+            '/products-navigator',
12
+            array(
13
+                'post_id' => array(
14
+                    'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ),
15
+                    'type'        => 'integer',
16
+                    'required'    => true,
17
+                ),
18
+                'uniqid'  => array(
19
+                    'description' => __( 'Navigator uniqueid', 'wordlift' ),
20
+                    'type'        => 'string',
21
+                    'required'    => true,
22
+                ),
23
+                'limit'   => array(
24
+                    'default'           => 4,
25
+                    'type'              => 'integer',
26
+                    'sanitize_callback' => 'absint',
27
+                ),
28
+                'offset'  => array(
29
+                    'default'           => 0,
30
+                    'type'              => 'integer',
31
+                    'sanitize_callback' => 'absint',
32
+                ),
33
+                'sort'    => array(
34
+                    'default'           => 'ID DESC',
35
+                    'sanitize_callback' => 'sanitize_sql_orderby',
36
+                ),
37
+                'amp'     => array(
38
+                    'sanitize_callback' => 'rest_sanitize_boolean',
39
+                ),
40
+            )
41
+        );
42
+    }
43
+
44
+    public function get_data( $request ) {
45
+
46
+        // Sanitize and set defaults
47
+        $navigator_length = $request['limit'];
48
+        $navigator_offset = $request['offset'];
49
+        $order_by         = $request['sort'];
50
+        $post_id          = $request['post_id'];
51
+        $navigator_id     = $request['uniqid'];
52
+        $amp              = $request['amp'];
53
+
54
+        $post = get_post( $post_id );
55
+
56
+        // Post ID has to match an existing item
57
+        if ( null === $post ) {
58
+            return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) );
59
+        }
60
+
61
+        // Determine navigator type and call respective get_*_results
62
+        if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
63
+            $referencing_posts = $this->get_entity_results(
64
+                $post_id,
65
+                array(
66
+                    'ID',
67
+                    'post_title',
68
+                ),
69
+                $order_by,
70
+                $navigator_length,
71
+                $navigator_offset
72
+            );
73
+        } else {
74
+            $referencing_posts = $this->get_post_results(
75
+                $post_id,
76
+                array(
77
+                    'ID',
78
+                    'post_title',
79
+                ),
80
+                $order_by,
81
+                $navigator_length,
82
+                $navigator_offset
83
+            );
84
+        }
85
+
86
+        // Fetch directly referencing posts excluding referencing posts via entities
87
+        $directly_referencing_posts = $this->get_directly_referencing_posts(
88
+            $post_id,
89
+            array_map(
90
+                function ( $referencing_post ) {
91
+                    return $referencing_post->ID;
92
+                },
93
+                $referencing_posts
94
+            )
95
+        );
96
+
97
+        // Combine directly referencing posts and referencing posts via entities
98
+        $referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts );
99
+
100
+        // loop over them and take the first one which is not already in the $related_posts
101
+        $results = array();
102
+        foreach ( $referencing_posts as $referencing_post ) {
103
+            $serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
104
+            $product           = wc_get_product( $referencing_post->ID );
105
+
106
+            $result = array(
107
+                'product' => array(
108
+                    'id'              => $referencing_post->ID,
109
+                    'permalink'       => get_permalink( $referencing_post->ID ),
110
+                    'title'           => $referencing_post->post_title,
111
+                    'thumbnail'       => get_the_post_thumbnail_url( $referencing_post, 'medium' ),
112
+                    'regular_price'   => $product->get_regular_price(),
113
+                    'sale_price'      => $product->get_sale_price(),
114
+                    'price'           => $product->get_price(),
115
+                    'currency_symbol' => get_woocommerce_currency_symbol(),
116
+                    'discount_pc'     => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0,
117
+                    'average_rating'  => $product->get_average_rating(),
118
+                    'rating_count'    => $product->get_rating_count(),
119
+                    'rating_html'     => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ),
120
+                ),
121
+                'entity'  => array(
122
+                    'id'        => $referencing_post->entity_id,
123
+                    'label'     => $serialized_entity['label'],
124
+                    'mainType'  => $serialized_entity['mainType'],
125
+                    'permalink' => get_permalink( $referencing_post->entity_id ),
126
+                ),
127
+            );
128
+
129
+            $results[] = $result;
130
+
131
+        }
132
+
133
+        if ( count( $results ) < $navigator_length ) {
134
+            $results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
135
+        }
136
+
137
+        // Add filler posts if needed
138
+        $filler_count = $navigator_length - count( $results );
139
+        if ( $filler_count > 0 ) {
140
+            $referencing_post_ids = array_map(
141
+                function ( $p ) {
142
+                    return $p->ID;
143
+                },
144
+                $referencing_posts
145
+            );
146
+            /**
147
+             * @since 3.28.0
148
+             * Filler posts are fetched using this util.
149
+             */
150
+            $filler_posts_util       = new Filler_Posts_Util( $post_id, 'product' );
151
+            $post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids );
152
+            $filler_posts            = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded );
153
+            $results                 = array_merge( $results, $filler_posts );
154
+        }
155
+
156
+        // Apply filters after fillers are added
157
+        foreach ( $results as $result_index => $result ) {
158
+            $results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id );
159
+            $results[ $result_index ]['entity']  = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
160
+        }
161
+
162
+        $results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id );
163
+
164
+        return $amp ? array(
165
+            'items' => array(
166
+                array( 'values' => $results ),
167
+            ),
168
+        ) : $results;
169
+
170
+    }
171
+
172
+    private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) {
173
+
174
+        $directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id );
175
+
176
+        $post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids );
177
+
178
+        $directly_referencing_posts = get_posts(
179
+            array(
180
+                'meta_query'          => array(
181
+                    array(
182
+                        'key' => '_thumbnail_id',
183
+                    ),
184
+                    array(
185
+                        'key'   => '_stock_status',
186
+                        'value' => 'instock',
187
+                    ),
188
+                ),
189
+                'post__in'            => $post__in,
190
+                'post_type'           => 'product',
191
+                'ignore_sticky_posts' => 1,
192
+            )
193
+        );
194
+
195
+        $results = array();
196
+
197
+        foreach ( $directly_referencing_posts as $post ) {
198
+            $result             = new stdClass();
199
+            $result->ID         = $post->ID;
200
+            $result->post_title = $post->post_title;
201
+            $result->entity_id  = $post->ID;
202
+            $results[]          = $result;
203
+        }
204
+
205
+        return $results;
206
+    }
207
+
208
+    private function get_entity_results(
209
+        $post_id,
210
+        $fields = array(
211
+            'ID',
212
+            'post_title',
213
+        ),
214
+        $order_by = 'ID DESC',
215
+        $limit = 10,
216
+        $offset = 0
217
+    ) {
218
+        global $wpdb;
219
+
220
+        $select = implode(
221
+            ', ',
222
+            array_map(
223
+                function ( $item ) {
224
+                    return "p.$item AS $item";
225
+                },
226
+                (array) $fields
227
+            )
228
+        );
229
+
230
+        $order_by = implode(
231
+            ', ',
232
+            array_map(
233
+                function ( $item ) {
234
+                    return "p.$item";
235
+                },
236
+                (array) $order_by
237
+            )
238
+        );
239 239
 // phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder
240
-		/** @noinspection SqlNoDataSourceInspection */
241
-		return $wpdb->get_results(
242
-			$wpdb->prepare(
243
-				"
240
+        /** @noinspection SqlNoDataSourceInspection */
241
+        return $wpdb->get_results(
242
+            $wpdb->prepare(
243
+                "
244 244
 SELECT %4\$s, p2.ID as entity_id
245 245
  FROM {$wpdb->prefix}wl_relation_instances r1
246 246
 	-- get the ID of the post entity in common between the object and the subject 2. 
@@ -273,51 +273,51 @@  discard block
 block discarded – undo
273 273
  LIMIT %2\$d
274 274
  OFFSET %3\$d
275 275
 ",
276
-				$post_id,
277
-				$limit,
278
-				$offset,
279
-				$select,
280
-				$order_by
281
-			)
282
-		);
283
-	}
276
+                $post_id,
277
+                $limit,
278
+                $offset,
279
+                $select,
280
+                $order_by
281
+            )
282
+        );
283
+    }
284 284
 // phpcs:enable
285
-	private function get_post_results(
286
-		$post_id,
287
-		$fields = array(
288
-			'ID',
289
-			'post_title',
290
-		),
291
-		$order_by = 'ID DESC',
292
-		$limit = 10,
293
-		$offset = 0
294
-	) {
295
-		global $wpdb;
296
-
297
-		$select = implode(
298
-			', ',
299
-			array_map(
300
-				function ( $item ) {
301
-					return "p.$item AS $item";
302
-				},
303
-				(array) $fields
304
-			)
305
-		);
306
-
307
-		$order_by = implode(
308
-			', ',
309
-			array_map(
310
-				function ( $item ) {
311
-					return "p.$item";
312
-				},
313
-				(array) $order_by
314
-			)
315
-		);
285
+    private function get_post_results(
286
+        $post_id,
287
+        $fields = array(
288
+            'ID',
289
+            'post_title',
290
+        ),
291
+        $order_by = 'ID DESC',
292
+        $limit = 10,
293
+        $offset = 0
294
+    ) {
295
+        global $wpdb;
296
+
297
+        $select = implode(
298
+            ', ',
299
+            array_map(
300
+                function ( $item ) {
301
+                    return "p.$item AS $item";
302
+                },
303
+                (array) $fields
304
+            )
305
+        );
306
+
307
+        $order_by = implode(
308
+            ', ',
309
+            array_map(
310
+                function ( $item ) {
311
+                    return "p.$item";
312
+                },
313
+                (array) $order_by
314
+            )
315
+        );
316 316
 // phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
317
-		/** @noinspection SqlNoDataSourceInspection */
318
-		return $wpdb->get_results(
319
-			$wpdb->prepare(
320
-				"
317
+        /** @noinspection SqlNoDataSourceInspection */
318
+        return $wpdb->get_results(
319
+            $wpdb->prepare(
320
+                "
321 321
 SELECT %4\$s, p2.ID as entity_id
322 322
  FROM {$wpdb->prefix}wl_relation_instances r1
323 323
     INNER JOIN {$wpdb->prefix}wl_relation_instances r2
@@ -353,13 +353,13 @@  discard block
 block discarded – undo
353 353
  LIMIT %2\$d
354 354
  OFFSET %3\$d
355 355
 ",
356
-				$post_id,
357
-				$limit,
358
-				$offset,
359
-				$select,
360
-				$order_by
361
-			)
362
-		);
363
-	}
356
+                $post_id,
357
+                $limit,
358
+                $offset,
359
+                $select,
360
+                $order_by
361
+            )
362
+        );
363
+    }
364 364
 // phpcs:enable
365 365
 }
Please login to merge, or discard this patch.
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -11,12 +11,12 @@  discard block
 block discarded – undo
11 11
 			'/products-navigator',
12 12
 			array(
13 13
 				'post_id' => array(
14
-					'description' => __( 'Post ID for which Navigator has to be queried', 'wordlift' ),
14
+					'description' => __('Post ID for which Navigator has to be queried', 'wordlift'),
15 15
 					'type'        => 'integer',
16 16
 					'required'    => true,
17 17
 				),
18 18
 				'uniqid'  => array(
19
-					'description' => __( 'Navigator uniqueid', 'wordlift' ),
19
+					'description' => __('Navigator uniqueid', 'wordlift'),
20 20
 					'type'        => 'string',
21 21
 					'required'    => true,
22 22
 				),
@@ -41,7 +41,7 @@  discard block
 block discarded – undo
41 41
 		);
42 42
 	}
43 43
 
44
-	public function get_data( $request ) {
44
+	public function get_data($request) {
45 45
 
46 46
 		// Sanitize and set defaults
47 47
 		$navigator_length = $request['limit'];
@@ -51,15 +51,15 @@  discard block
 block discarded – undo
51 51
 		$navigator_id     = $request['uniqid'];
52 52
 		$amp              = $request['amp'];
53 53
 
54
-		$post = get_post( $post_id );
54
+		$post = get_post($post_id);
55 55
 
56 56
 		// Post ID has to match an existing item
57
-		if ( null === $post ) {
58
-			return new WP_Error( 'rest_invalid_post_id', __( 'Invalid post_id', 'wordlift' ), array( 'status' => 404 ) );
57
+		if (null === $post) {
58
+			return new WP_Error('rest_invalid_post_id', __('Invalid post_id', 'wordlift'), array('status' => 404));
59 59
 		}
60 60
 
61 61
 		// Determine navigator type and call respective get_*_results
62
-		if ( get_post_type( $post_id ) === Wordlift_Entity_Service::TYPE_NAME ) {
62
+		if (get_post_type($post_id) === Wordlift_Entity_Service::TYPE_NAME) {
63 63
 			$referencing_posts = $this->get_entity_results(
64 64
 				$post_id,
65 65
 				array(
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
 		$directly_referencing_posts = $this->get_directly_referencing_posts(
88 88
 			$post_id,
89 89
 			array_map(
90
-				function ( $referencing_post ) {
90
+				function($referencing_post) {
91 91
 					return $referencing_post->ID;
92 92
 				},
93 93
 				$referencing_posts
@@ -95,34 +95,34 @@  discard block
 block discarded – undo
95 95
 		);
96 96
 
97 97
 		// Combine directly referencing posts and referencing posts via entities
98
-		$referencing_posts = array_merge( $directly_referencing_posts, $referencing_posts );
98
+		$referencing_posts = array_merge($directly_referencing_posts, $referencing_posts);
99 99
 
100 100
 		// loop over them and take the first one which is not already in the $related_posts
101 101
 		$results = array();
102
-		foreach ( $referencing_posts as $referencing_post ) {
103
-			$serialized_entity = wl_serialize_entity( $referencing_post->entity_id );
104
-			$product           = wc_get_product( $referencing_post->ID );
102
+		foreach ($referencing_posts as $referencing_post) {
103
+			$serialized_entity = wl_serialize_entity($referencing_post->entity_id);
104
+			$product           = wc_get_product($referencing_post->ID);
105 105
 
106 106
 			$result = array(
107 107
 				'product' => array(
108 108
 					'id'              => $referencing_post->ID,
109
-					'permalink'       => get_permalink( $referencing_post->ID ),
109
+					'permalink'       => get_permalink($referencing_post->ID),
110 110
 					'title'           => $referencing_post->post_title,
111
-					'thumbnail'       => get_the_post_thumbnail_url( $referencing_post, 'medium' ),
111
+					'thumbnail'       => get_the_post_thumbnail_url($referencing_post, 'medium'),
112 112
 					'regular_price'   => $product->get_regular_price(),
113 113
 					'sale_price'      => $product->get_sale_price(),
114 114
 					'price'           => $product->get_price(),
115 115
 					'currency_symbol' => get_woocommerce_currency_symbol(),
116
-					'discount_pc'     => ( $product->get_sale_price() && ( $product->get_regular_price() > 0 ) ) ? round( 1 - ( $product->get_sale_price() / $product->get_regular_price() ), 2 ) * 100 : 0,
116
+					'discount_pc'     => ($product->get_sale_price() && ($product->get_regular_price() > 0)) ? round(1 - ($product->get_sale_price() / $product->get_regular_price()), 2) * 100 : 0,
117 117
 					'average_rating'  => $product->get_average_rating(),
118 118
 					'rating_count'    => $product->get_rating_count(),
119
-					'rating_html'     => wc_get_rating_html( $product->get_average_rating(), $product->get_rating_count() ),
119
+					'rating_html'     => wc_get_rating_html($product->get_average_rating(), $product->get_rating_count()),
120 120
 				),
121 121
 				'entity'  => array(
122 122
 					'id'        => $referencing_post->entity_id,
123 123
 					'label'     => $serialized_entity['label'],
124 124
 					'mainType'  => $serialized_entity['mainType'],
125
-					'permalink' => get_permalink( $referencing_post->entity_id ),
125
+					'permalink' => get_permalink($referencing_post->entity_id),
126 126
 				),
127 127
 			);
128 128
 
@@ -130,15 +130,15 @@  discard block
 block discarded – undo
130 130
 
131 131
 		}
132 132
 
133
-		if ( count( $results ) < $navigator_length ) {
134
-			$results = apply_filters( 'wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length );
133
+		if (count($results) < $navigator_length) {
134
+			$results = apply_filters('wl_products_navigator_data_placeholder', $results, $navigator_id, $navigator_offset, $navigator_length);
135 135
 		}
136 136
 
137 137
 		// Add filler posts if needed
138
-		$filler_count = $navigator_length - count( $results );
139
-		if ( $filler_count > 0 ) {
138
+		$filler_count = $navigator_length - count($results);
139
+		if ($filler_count > 0) {
140 140
 			$referencing_post_ids = array_map(
141
-				function ( $p ) {
141
+				function($p) {
142 142
 					return $p->ID;
143 143
 				},
144 144
 				$referencing_posts
@@ -147,33 +147,33 @@  discard block
 block discarded – undo
147 147
 			 * @since 3.28.0
148 148
 			 * Filler posts are fetched using this util.
149 149
 			 */
150
-			$filler_posts_util       = new Filler_Posts_Util( $post_id, 'product' );
151
-			$post_ids_to_be_excluded = array_merge( array( $post_id ), $referencing_post_ids );
152
-			$filler_posts            = $filler_posts_util->get_product_navigator_response( $filler_count, $post_ids_to_be_excluded );
153
-			$results                 = array_merge( $results, $filler_posts );
150
+			$filler_posts_util       = new Filler_Posts_Util($post_id, 'product');
151
+			$post_ids_to_be_excluded = array_merge(array($post_id), $referencing_post_ids);
152
+			$filler_posts            = $filler_posts_util->get_product_navigator_response($filler_count, $post_ids_to_be_excluded);
153
+			$results                 = array_merge($results, $filler_posts);
154 154
 		}
155 155
 
156 156
 		// Apply filters after fillers are added
157
-		foreach ( $results as $result_index => $result ) {
158
-			$results[ $result_index ]['product'] = apply_filters( 'wl_products_navigator_data_post', $result['product'], intval( $result['product']['id'] ), $navigator_id );
159
-			$results[ $result_index ]['entity']  = apply_filters( 'wl_products_navigator_data_entity', $result['entity'], intval( $result['entity']['id'] ), $navigator_id );
157
+		foreach ($results as $result_index => $result) {
158
+			$results[$result_index]['product'] = apply_filters('wl_products_navigator_data_post', $result['product'], intval($result['product']['id']), $navigator_id);
159
+			$results[$result_index]['entity']  = apply_filters('wl_products_navigator_data_entity', $result['entity'], intval($result['entity']['id']), $navigator_id);
160 160
 		}
161 161
 
162
-		$results = apply_filters( 'wl_products_navigator_results', $results, $navigator_id );
162
+		$results = apply_filters('wl_products_navigator_results', $results, $navigator_id);
163 163
 
164 164
 		return $amp ? array(
165 165
 			'items' => array(
166
-				array( 'values' => $results ),
166
+				array('values' => $results),
167 167
 			),
168 168
 		) : $results;
169 169
 
170 170
 	}
171 171
 
172
-	private function get_directly_referencing_posts( $post_id, $referencing_post_ids ) {
172
+	private function get_directly_referencing_posts($post_id, $referencing_post_ids) {
173 173
 
174
-		$directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities( $post_id );
174
+		$directly_referencing_post_ids = Wordlift_Entity_Service::get_instance()->get_related_entities($post_id);
175 175
 
176
-		$post__in = array_diff( $directly_referencing_post_ids, $referencing_post_ids );
176
+		$post__in = array_diff($directly_referencing_post_ids, $referencing_post_ids);
177 177
 
178 178
 		$directly_referencing_posts = get_posts(
179 179
 			array(
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
 
195 195
 		$results = array();
196 196
 
197
-		foreach ( $directly_referencing_posts as $post ) {
197
+		foreach ($directly_referencing_posts as $post) {
198 198
 			$result             = new stdClass();
199 199
 			$result->ID         = $post->ID;
200 200
 			$result->post_title = $post->post_title;
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
 		$select = implode(
221 221
 			', ',
222 222
 			array_map(
223
-				function ( $item ) {
223
+				function($item) {
224 224
 					return "p.$item AS $item";
225 225
 				},
226 226
 				(array) $fields
@@ -230,7 +230,7 @@  discard block
 block discarded – undo
230 230
 		$order_by = implode(
231 231
 			', ',
232 232
 			array_map(
233
-				function ( $item ) {
233
+				function($item) {
234 234
 					return "p.$item";
235 235
 				},
236 236
 				(array) $order_by
@@ -297,7 +297,7 @@  discard block
 block discarded – undo
297 297
 		$select = implode(
298 298
 			', ',
299 299
 			array_map(
300
-				function ( $item ) {
300
+				function($item) {
301 301
 					return "p.$item AS $item";
302 302
 				},
303 303
 				(array) $fields
@@ -307,7 +307,7 @@  discard block
 block discarded – undo
307 307
 		$order_by = implode(
308 308
 			', ',
309 309
 			array_map(
310
-				function ( $item ) {
310
+				function($item) {
311 311
 					return "p.$item";
312 312
 				},
313 313
 				(array) $order_by
Please login to merge, or discard this patch.
src/shortcodes/wordlift-shortcode-chord.php 2 patches
Indentation   +164 added lines, -164 removed lines patch added patch discarded remove patch
@@ -15,47 +15,47 @@  discard block
 block discarded – undo
15 15
  * @return mixed
16 16
  */
17 17
 function wl_shortcode_chord_most_referenced_entity_id() {
18
-	// Get the last 20 articles by post date.
19
-	// For each article get the entities they reference.
20
-	$post_ids = get_posts(
21
-		array(
22
-			'numberposts' => 20,
23
-			'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
24
-			'fields'      => 'ids', // Only get post IDs.
25
-			'post_status' => 'publish',
26
-			'tax_query'   => array(
27
-				'relation' => 'OR',
28
-				array(
29
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
30
-					'operator' => 'NOT EXISTS',
31
-				),
32
-				array(
33
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
34
-					'field'    => 'slug',
35
-					'terms'    => 'article',
36
-				),
37
-			),
38
-			'orderby'     => 'post_date',
39
-			'order'       => 'DESC',
40
-		)
41
-	);
42
-
43
-	if ( empty( $post_ids ) ) {
44
-		return null;
45
-	}
46
-
47
-	$entities = array();
48
-	foreach ( $post_ids as $id ) {
49
-		$entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) );
50
-	}
51
-
52
-	$famous_entities = array_count_values( $entities );
53
-	arsort( $famous_entities );
54
-	if ( count( $famous_entities ) >= 1 ) {
55
-		return key( $famous_entities );
56
-	} else {
57
-		return $post_ids[0];
58
-	}
18
+    // Get the last 20 articles by post date.
19
+    // For each article get the entities they reference.
20
+    $post_ids = get_posts(
21
+        array(
22
+            'numberposts' => 20,
23
+            'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
24
+            'fields'      => 'ids', // Only get post IDs.
25
+            'post_status' => 'publish',
26
+            'tax_query'   => array(
27
+                'relation' => 'OR',
28
+                array(
29
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
30
+                    'operator' => 'NOT EXISTS',
31
+                ),
32
+                array(
33
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
34
+                    'field'    => 'slug',
35
+                    'terms'    => 'article',
36
+                ),
37
+            ),
38
+            'orderby'     => 'post_date',
39
+            'order'       => 'DESC',
40
+        )
41
+    );
42
+
43
+    if ( empty( $post_ids ) ) {
44
+        return null;
45
+    }
46
+
47
+    $entities = array();
48
+    foreach ( $post_ids as $id ) {
49
+        $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) );
50
+    }
51
+
52
+    $famous_entities = array_count_values( $entities );
53
+    arsort( $famous_entities );
54
+    if ( count( $famous_entities ) >= 1 ) {
55
+        return key( $famous_entities );
56
+    } else {
57
+        return $post_ids[0];
58
+    }
59 59
 
60 60
 }
61 61
 
@@ -72,66 +72,66 @@  discard block
 block discarded – undo
72 72
  */
73 73
 function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) {
74 74
 
75
-	if ( $related !== null ) {
76
-		if ( 0 === $depth ) {
77
-			return $related;
78
-		}
79
-	}
80
-
81
-	wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( $related === null ? 'yes' : 'no' ) . ' ]' );
82
-
83
-	// Create a related array which will hold entities and relations.
84
-	if ( $related === null ) {
85
-		$related = array(
86
-			'entities'  => array( $entity_id ),
87
-			'relations' => array(),
88
-		);
89
-	}
90
-
91
-	// Get related entities
92
-	$related_entity_ids = wl_core_get_related_entity_ids(
93
-		$entity_id,
94
-		array(
95
-			'status' => 'publish',
96
-		)
97
-	);
98
-
99
-	// If the current node is an entity, add related posts too
100
-	$related_post_ids = ( Wordlift_Entity_Service::get_instance()
101
-												 ->is_entity( $entity_id ) ) ?
102
-		wl_core_get_related_post_ids(
103
-			$entity_id,
104
-			array(
105
-				'status' => 'publish',
106
-			)
107
-		) :
108
-		array();
109
-
110
-	// Merge results and remove duplicated entries
111
-	$related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) );
112
-
113
-	// TODO: List of entities ($rel) should be ordered by interest factors.
114
-	shuffle( $related_ids );
115
-
116
-	// Now we have all the related IDs.
117
-	foreach ( $related_ids as $related_id ) {
118
-
119
-		if ( count( $related['entities'] ) >= $max_size ) {
120
-			return $related;
121
-		}
122
-
123
-		$related['relations'][] = array( $entity_id, $related_id );
124
-
125
-		if ( ! in_array( $related_id, $related['entities'], true ) ) {
126
-			// Found new related entity!
127
-			$related['entities'][] = $related_id;
128
-
129
-			$related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size );
130
-		}
131
-	}
132
-
133
-	// End condition 2: no more entities to search for.
134
-	return $related;
75
+    if ( $related !== null ) {
76
+        if ( 0 === $depth ) {
77
+            return $related;
78
+        }
79
+    }
80
+
81
+    wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( $related === null ? 'yes' : 'no' ) . ' ]' );
82
+
83
+    // Create a related array which will hold entities and relations.
84
+    if ( $related === null ) {
85
+        $related = array(
86
+            'entities'  => array( $entity_id ),
87
+            'relations' => array(),
88
+        );
89
+    }
90
+
91
+    // Get related entities
92
+    $related_entity_ids = wl_core_get_related_entity_ids(
93
+        $entity_id,
94
+        array(
95
+            'status' => 'publish',
96
+        )
97
+    );
98
+
99
+    // If the current node is an entity, add related posts too
100
+    $related_post_ids = ( Wordlift_Entity_Service::get_instance()
101
+                                                    ->is_entity( $entity_id ) ) ?
102
+        wl_core_get_related_post_ids(
103
+            $entity_id,
104
+            array(
105
+                'status' => 'publish',
106
+            )
107
+        ) :
108
+        array();
109
+
110
+    // Merge results and remove duplicated entries
111
+    $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) );
112
+
113
+    // TODO: List of entities ($rel) should be ordered by interest factors.
114
+    shuffle( $related_ids );
115
+
116
+    // Now we have all the related IDs.
117
+    foreach ( $related_ids as $related_id ) {
118
+
119
+        if ( count( $related['entities'] ) >= $max_size ) {
120
+            return $related;
121
+        }
122
+
123
+        $related['relations'][] = array( $entity_id, $related_id );
124
+
125
+        if ( ! in_array( $related_id, $related['entities'], true ) ) {
126
+            // Found new related entity!
127
+            $related['entities'][] = $related_id;
128
+
129
+            $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size );
130
+        }
131
+    }
132
+
133
+    // End condition 2: no more entities to search for.
134
+    return $related;
135 135
 }
136 136
 
137 137
 /**
@@ -145,63 +145,63 @@  discard block
 block discarded – undo
145 145
  */
146 146
 function wl_shortcode_chord_get_graph( $data ) {
147 147
 
148
-	// Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class).
149
-	array_walk(
150
-		$data['entities'],
151
-		function ( &$item ) {
152
-			$post = get_post( $item );
153
-
154
-			// Skip non-existing posts.
155
-			if ( $post === null ) {
156
-				wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" );
157
-
158
-				return $item;
159
-			}
160
-
161
-			// Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set).
162
-			$term = Wordlift_Entity_Type_Service::get_instance()->get( $item );
163
-
164
-			// The following log may create a circular loop.
165
-			// wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" );
166
-
167
-			// TODO: get all images
168
-			$thumbnail    = null;
169
-			$thumbnail_id = get_post_thumbnail_id( $post->ID );
170
-			if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
171
-				$attachment = wp_get_attachment_image_src( $thumbnail_id );
172
-				if ( false !== $attachment ) {
173
-					$thumbnail = esc_attr( $attachment[0] );
174
-				}
175
-			}
176
-
177
-			$entity = array(
178
-				'uri'        => wl_get_entity_uri( $item ),
179
-				'url'        => get_permalink( $item ),
180
-				'label'      => $post->post_title,
181
-				'type'       => $post->post_type,
182
-				'thumbnails' => array( $thumbnail ),
183
-				'css_class'  => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ),
184
-			);
185
-
186
-			$item = $entity;
187
-		}
188
-	);
189
-
190
-	// Refactor the relations.
191
-	array_walk(
192
-		$data['relations'],
193
-		function ( &$item ) {
194
-			$relation = array(
195
-				's' => wl_get_entity_uri( $item[0] ),
196
-				'o' => wl_get_entity_uri( $item[1] ),
197
-			);
198
-
199
-			$item = $relation;
200
-		}
201
-	);
202
-
203
-	// Return the JSON representation.
204
-	return $data;
148
+    // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class).
149
+    array_walk(
150
+        $data['entities'],
151
+        function ( &$item ) {
152
+            $post = get_post( $item );
153
+
154
+            // Skip non-existing posts.
155
+            if ( $post === null ) {
156
+                wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" );
157
+
158
+                return $item;
159
+            }
160
+
161
+            // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set).
162
+            $term = Wordlift_Entity_Type_Service::get_instance()->get( $item );
163
+
164
+            // The following log may create a circular loop.
165
+            // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" );
166
+
167
+            // TODO: get all images
168
+            $thumbnail    = null;
169
+            $thumbnail_id = get_post_thumbnail_id( $post->ID );
170
+            if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
171
+                $attachment = wp_get_attachment_image_src( $thumbnail_id );
172
+                if ( false !== $attachment ) {
173
+                    $thumbnail = esc_attr( $attachment[0] );
174
+                }
175
+            }
176
+
177
+            $entity = array(
178
+                'uri'        => wl_get_entity_uri( $item ),
179
+                'url'        => get_permalink( $item ),
180
+                'label'      => $post->post_title,
181
+                'type'       => $post->post_type,
182
+                'thumbnails' => array( $thumbnail ),
183
+                'css_class'  => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ),
184
+            );
185
+
186
+            $item = $entity;
187
+        }
188
+    );
189
+
190
+    // Refactor the relations.
191
+    array_walk(
192
+        $data['relations'],
193
+        function ( &$item ) {
194
+            $relation = array(
195
+                's' => wl_get_entity_uri( $item[0] ),
196
+                'o' => wl_get_entity_uri( $item[1] ),
197
+            );
198
+
199
+            $item = $relation;
200
+        }
201
+    );
202
+
203
+    // Return the JSON representation.
204
+    return $data;
205 205
 }
206 206
 
207 207
 /**
@@ -212,15 +212,15 @@  discard block
 block discarded – undo
212 212
  */
213 213
 function wl_shortcode_chord_ajax() {
214 214
 
215
-	check_ajax_referer( 'wl_chord', 'wl_chord_nonce' );
215
+    check_ajax_referer( 'wl_chord', 'wl_chord_nonce' );
216 216
 
217
-	$post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0;
218
-	$depth   = isset( $_REQUEST['depth'] ) ? (int) $_REQUEST['depth'] : 2;
217
+    $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0;
218
+    $depth   = isset( $_REQUEST['depth'] ) ? (int) $_REQUEST['depth'] : 2;
219 219
 
220
-	$relations = wl_shortcode_chord_get_relations( $post_id, $depth );
221
-	$graph     = wl_shortcode_chord_get_graph( $relations );
220
+    $relations = wl_shortcode_chord_get_relations( $post_id, $depth );
221
+    $graph     = wl_shortcode_chord_get_graph( $relations );
222 222
 
223
-	wl_core_send_json( $graph );
223
+    wl_core_send_json( $graph );
224 224
 }
225 225
 
226 226
 add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' );
Please login to merge, or discard this patch.
Spacing   +49 added lines, -50 removed lines patch added patch discarded remove patch
@@ -40,19 +40,19 @@  discard block
 block discarded – undo
40 40
 		)
41 41
 	);
42 42
 
43
-	if ( empty( $post_ids ) ) {
43
+	if (empty($post_ids)) {
44 44
 		return null;
45 45
 	}
46 46
 
47 47
 	$entities = array();
48
-	foreach ( $post_ids as $id ) {
49
-		$entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) );
48
+	foreach ($post_ids as $id) {
49
+		$entities = array_merge($entities, wl_core_get_related_entity_ids($id));
50 50
 	}
51 51
 
52
-	$famous_entities = array_count_values( $entities );
53
-	arsort( $famous_entities );
54
-	if ( count( $famous_entities ) >= 1 ) {
55
-		return key( $famous_entities );
52
+	$famous_entities = array_count_values($entities);
53
+	arsort($famous_entities);
54
+	if (count($famous_entities) >= 1) {
55
+		return key($famous_entities);
56 56
 	} else {
57 57
 		return $post_ids[0];
58 58
 	}
@@ -70,20 +70,20 @@  discard block
 block discarded – undo
70 70
  * @return array
71 71
  * @uses wl_core_get_related_post_ids() to get the list of post ids that reference an entity.
72 72
  */
73
-function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) {
73
+function wl_shortcode_chord_get_relations($entity_id, $depth = 2, $related = null, $max_size = 9) {
74 74
 
75
-	if ( $related !== null ) {
76
-		if ( 0 === $depth ) {
75
+	if ($related !== null) {
76
+		if (0 === $depth) {
77 77
 			return $related;
78 78
 		}
79 79
 	}
80 80
 
81
-	wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( $related === null ? 'yes' : 'no' ) . ' ]' );
81
+	wl_write_log("wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: ".($related === null ? 'yes' : 'no').' ]');
82 82
 
83 83
 	// Create a related array which will hold entities and relations.
84
-	if ( $related === null ) {
84
+	if ($related === null) {
85 85
 		$related = array(
86
-			'entities'  => array( $entity_id ),
86
+			'entities'  => array($entity_id),
87 87
 			'relations' => array(),
88 88
 		);
89 89
 	}
@@ -97,36 +97,35 @@  discard block
 block discarded – undo
97 97
 	);
98 98
 
99 99
 	// If the current node is an entity, add related posts too
100
-	$related_post_ids = ( Wordlift_Entity_Service::get_instance()
101
-												 ->is_entity( $entity_id ) ) ?
100
+	$related_post_ids = (Wordlift_Entity_Service::get_instance()
101
+												 ->is_entity($entity_id)) ?
102 102
 		wl_core_get_related_post_ids(
103 103
 			$entity_id,
104 104
 			array(
105 105
 				'status' => 'publish',
106 106
 			)
107
-		) :
108
-		array();
107
+		) : array();
109 108
 
110 109
 	// Merge results and remove duplicated entries
111
-	$related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) );
110
+	$related_ids = array_unique(array_merge($related_post_ids, $related_entity_ids));
112 111
 
113 112
 	// TODO: List of entities ($rel) should be ordered by interest factors.
114
-	shuffle( $related_ids );
113
+	shuffle($related_ids);
115 114
 
116 115
 	// Now we have all the related IDs.
117
-	foreach ( $related_ids as $related_id ) {
116
+	foreach ($related_ids as $related_id) {
118 117
 
119
-		if ( count( $related['entities'] ) >= $max_size ) {
118
+		if (count($related['entities']) >= $max_size) {
120 119
 			return $related;
121 120
 		}
122 121
 
123
-		$related['relations'][] = array( $entity_id, $related_id );
122
+		$related['relations'][] = array($entity_id, $related_id);
124 123
 
125
-		if ( ! in_array( $related_id, $related['entities'], true ) ) {
124
+		if ( ! in_array($related_id, $related['entities'], true)) {
126 125
 			// Found new related entity!
127 126
 			$related['entities'][] = $related_id;
128 127
 
129
-			$related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size );
128
+			$related = wl_shortcode_chord_get_relations($related_id, ($depth - 1), $related, $max_size);
130 129
 		}
131 130
 	}
132 131
 
@@ -143,44 +142,44 @@  discard block
 block discarded – undo
143 142
  *
144 143
  * @return mixed|string
145 144
  */
146
-function wl_shortcode_chord_get_graph( $data ) {
145
+function wl_shortcode_chord_get_graph($data) {
147 146
 
148 147
 	// Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class).
149 148
 	array_walk(
150 149
 		$data['entities'],
151
-		function ( &$item ) {
152
-			$post = get_post( $item );
150
+		function(&$item) {
151
+			$post = get_post($item);
153 152
 
154 153
 			// Skip non-existing posts.
155
-			if ( $post === null ) {
156
-				wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" );
154
+			if ($post === null) {
155
+				wl_write_log("wl_shortcode_chord_get_graph : post not found [ post id :: $item ]");
157 156
 
158 157
 				return $item;
159 158
 			}
160 159
 
161 160
 			// Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set).
162
-			$term = Wordlift_Entity_Type_Service::get_instance()->get( $item );
161
+			$term = Wordlift_Entity_Type_Service::get_instance()->get($item);
163 162
 
164 163
 			// The following log may create a circular loop.
165 164
 			// wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" );
166 165
 
167 166
 			// TODO: get all images
168 167
 			$thumbnail    = null;
169
-			$thumbnail_id = get_post_thumbnail_id( $post->ID );
170
-			if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) {
171
-				$attachment = wp_get_attachment_image_src( $thumbnail_id );
172
-				if ( false !== $attachment ) {
173
-					$thumbnail = esc_attr( $attachment[0] );
168
+			$thumbnail_id = get_post_thumbnail_id($post->ID);
169
+			if ('' !== $thumbnail_id && 0 !== $thumbnail_id) {
170
+				$attachment = wp_get_attachment_image_src($thumbnail_id);
171
+				if (false !== $attachment) {
172
+					$thumbnail = esc_attr($attachment[0]);
174 173
 				}
175 174
 			}
176 175
 
177 176
 			$entity = array(
178
-				'uri'        => wl_get_entity_uri( $item ),
179
-				'url'        => get_permalink( $item ),
177
+				'uri'        => wl_get_entity_uri($item),
178
+				'url'        => get_permalink($item),
180 179
 				'label'      => $post->post_title,
181 180
 				'type'       => $post->post_type,
182
-				'thumbnails' => array( $thumbnail ),
183
-				'css_class'  => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ),
181
+				'thumbnails' => array($thumbnail),
182
+				'css_class'  => (isset($term['css_class']) ? $term['css_class'] : ''),
184 183
 			);
185 184
 
186 185
 			$item = $entity;
@@ -190,10 +189,10 @@  discard block
 block discarded – undo
190 189
 	// Refactor the relations.
191 190
 	array_walk(
192 191
 		$data['relations'],
193
-		function ( &$item ) {
192
+		function(&$item) {
194 193
 			$relation = array(
195
-				's' => wl_get_entity_uri( $item[0] ),
196
-				'o' => wl_get_entity_uri( $item[1] ),
194
+				's' => wl_get_entity_uri($item[0]),
195
+				'o' => wl_get_entity_uri($item[1]),
197 196
 			);
198 197
 
199 198
 			$item = $relation;
@@ -212,16 +211,16 @@  discard block
 block discarded – undo
212 211
  */
213 212
 function wl_shortcode_chord_ajax() {
214 213
 
215
-	check_ajax_referer( 'wl_chord', 'wl_chord_nonce' );
214
+	check_ajax_referer('wl_chord', 'wl_chord_nonce');
216 215
 
217
-	$post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0;
218
-	$depth   = isset( $_REQUEST['depth'] ) ? (int) $_REQUEST['depth'] : 2;
216
+	$post_id = isset($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : 0;
217
+	$depth   = isset($_REQUEST['depth']) ? (int) $_REQUEST['depth'] : 2;
219 218
 
220
-	$relations = wl_shortcode_chord_get_relations( $post_id, $depth );
221
-	$graph     = wl_shortcode_chord_get_graph( $relations );
219
+	$relations = wl_shortcode_chord_get_relations($post_id, $depth);
220
+	$graph     = wl_shortcode_chord_get_graph($relations);
222 221
 
223
-	wl_core_send_json( $graph );
222
+	wl_core_send_json($graph);
224 223
 }
225 224
 
226
-add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' );
227
-add_action( 'wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax' );
225
+add_action('wp_ajax_wl_chord', 'wl_shortcode_chord_ajax');
226
+add_action('wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax');
Please login to merge, or discard this patch.
src/wordlift-editor.php 2 patches
Indentation   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -9,32 +9,32 @@  discard block
 block discarded – undo
9 9
  */
10 10
 function wordlift_mce_css( $mce_css ) {
11 11
 
12
-	/*
12
+    /*
13 13
 	 * Call the `wl_can_see_classification_box` filter to determine whether we can display the classification box.
14 14
 	 *
15 15
 	 * @since 3.20.3
16 16
 	 *
17 17
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/914
18 18
 	 */
19
-	if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
20
-		return $mce_css;
21
-	}
22
-
23
-	if ( ! empty( $mce_css ) ) {
24
-		$mce_css .= ',';
25
-	}
26
-
27
-	/**
28
-	 * Replacing the legacy `wordlift-reloaded.min.css` with tiny-mce.css.
29
-	 *
30
-	 * tiny-mce.css is generated using the new webpack project and its rules are shared with Gutenberg.
31
-	 *
32
-	 * @author David Riccitelli <[email protected]>
33
-	 * @since 3.23.0
34
-	 */
35
-	$mce_css .= plugin_dir_url( __FILE__ ) . 'js/dist/tiny-mce.css';
36
-
37
-	return $mce_css;
19
+    if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
20
+        return $mce_css;
21
+    }
22
+
23
+    if ( ! empty( $mce_css ) ) {
24
+        $mce_css .= ',';
25
+    }
26
+
27
+    /**
28
+     * Replacing the legacy `wordlift-reloaded.min.css` with tiny-mce.css.
29
+     *
30
+     * tiny-mce.css is generated using the new webpack project and its rules are shared with Gutenberg.
31
+     *
32
+     * @author David Riccitelli <[email protected]>
33
+     * @since 3.23.0
34
+     */
35
+    $mce_css .= plugin_dir_url( __FILE__ ) . 'js/dist/tiny-mce.css';
36
+
37
+    return $mce_css;
38 38
 }
39 39
 
40 40
 // hook the TinyMCE custom styles function.
@@ -49,13 +49,13 @@  discard block
 block discarded – undo
49 49
  */
50 50
 function wordlift_filter_tiny_mce_before_init( $options ) {
51 51
 
52
-	if ( ! isset( $options['extended_valid_elements'] ) ) {
53
-		$options['extended_valid_elements'] = '';
54
-	}
52
+    if ( ! isset( $options['extended_valid_elements'] ) ) {
53
+        $options['extended_valid_elements'] = '';
54
+    }
55 55
 
56
-	$options['extended_valid_elements'] .= ',span[*]';
56
+    $options['extended_valid_elements'] .= ',span[*]';
57 57
 
58
-	return $options;
58
+    return $options;
59 59
 }
60 60
 
61 61
 add_filter( 'tiny_mce_before_init', 'wordlift_filter_tiny_mce_before_init', PHP_INT_MAX );
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
  *
8 8
  * @return string The updated list of styles, including the custom style provided by WordLift.
9 9
  */
10
-function wordlift_mce_css( $mce_css ) {
10
+function wordlift_mce_css($mce_css) {
11 11
 
12 12
 	/*
13 13
 	 * Call the `wl_can_see_classification_box` filter to determine whether we can display the classification box.
@@ -16,11 +16,11 @@  discard block
 block discarded – undo
16 16
 	 *
17 17
 	 * @see https://github.com/insideout10/wordlift-plugin/issues/914
18 18
 	 */
19
-	if ( ! apply_filters( 'wl_can_see_classification_box', true ) ) {
19
+	if ( ! apply_filters('wl_can_see_classification_box', true)) {
20 20
 		return $mce_css;
21 21
 	}
22 22
 
23
-	if ( ! empty( $mce_css ) ) {
23
+	if ( ! empty($mce_css)) {
24 24
 		$mce_css .= ',';
25 25
 	}
26 26
 
@@ -32,13 +32,13 @@  discard block
 block discarded – undo
32 32
 	 * @author David Riccitelli <[email protected]>
33 33
 	 * @since 3.23.0
34 34
 	 */
35
-	$mce_css .= plugin_dir_url( __FILE__ ) . 'js/dist/tiny-mce.css';
35
+	$mce_css .= plugin_dir_url(__FILE__).'js/dist/tiny-mce.css';
36 36
 
37 37
 	return $mce_css;
38 38
 }
39 39
 
40 40
 // hook the TinyMCE custom styles function.
41
-add_filter( 'mce_css', 'wordlift_mce_css' );
41
+add_filter('mce_css', 'wordlift_mce_css');
42 42
 
43 43
 /**
44 44
  * Set TinyMCE options, in particular enable microdata tagging.
@@ -47,9 +47,9 @@  discard block
 block discarded – undo
47 47
  *
48 48
  * @return mixed
49 49
  */
50
-function wordlift_filter_tiny_mce_before_init( $options ) {
50
+function wordlift_filter_tiny_mce_before_init($options) {
51 51
 
52
-	if ( ! isset( $options['extended_valid_elements'] ) ) {
52
+	if ( ! isset($options['extended_valid_elements'])) {
53 53
 		$options['extended_valid_elements'] = '';
54 54
 	}
55 55
 
@@ -58,4 +58,4 @@  discard block
 block discarded – undo
58 58
 	return $options;
59 59
 }
60 60
 
61
-add_filter( 'tiny_mce_before_init', 'wordlift_filter_tiny_mce_before_init', PHP_INT_MAX );
61
+add_filter('tiny_mce_before_init', 'wordlift_filter_tiny_mce_before_init', PHP_INT_MAX);
Please login to merge, or discard this patch.
src/public/class-wordlift-amp-service.php 2 patches
Indentation   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -21,76 +21,76 @@
 block discarded – undo
21 21
  */
22 22
 class Wordlift_AMP_Service {
23 23
 
24
-	/**
25
-	 * The {@link \Wordlift_Jsonld_Service} instance.
26
-	 *
27
-	 * @since 3.19.1
28
-	 * @access private
29
-	 * @var \Wordlift_Jsonld_Service $jsonld_service The {@link \Wordlift_Jsonld_Service} instance.
30
-	 */
31
-	private $jsonld_service;
24
+    /**
25
+     * The {@link \Wordlift_Jsonld_Service} instance.
26
+     *
27
+     * @since 3.19.1
28
+     * @access private
29
+     * @var \Wordlift_Jsonld_Service $jsonld_service The {@link \Wordlift_Jsonld_Service} instance.
30
+     */
31
+    private $jsonld_service;
32 32
 
33
-	/**
34
-	 * Create a {@link Wordlift_AMP_Service} instance.
35
-	 *
36
-	 * @since 3.19.1
37
-	 *
38
-	 * @param \Wordlift_Jsonld_Service $jsonld_service
39
-	 */
40
-	public function __construct( $jsonld_service ) {
33
+    /**
34
+     * Create a {@link Wordlift_AMP_Service} instance.
35
+     *
36
+     * @since 3.19.1
37
+     *
38
+     * @param \Wordlift_Jsonld_Service $jsonld_service
39
+     */
40
+    public function __construct( $jsonld_service ) {
41 41
 
42
-		$this->jsonld_service = $jsonld_service;
42
+        $this->jsonld_service = $jsonld_service;
43 43
 
44
-		add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin' ) );
45
-		add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata' ), 99, 2 );
44
+        add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin' ) );
45
+        add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata' ), 99, 2 );
46 46
 
47
-	}
47
+    }
48 48
 
49
-	/**
50
-	 * Register the `entity` post type with the AMP plugin.
51
-	 *
52
-	 * @since 3.12.0
53
-	 */
54
-	public function register_entity_cpt_with_amp_plugin() {
49
+    /**
50
+     * Register the `entity` post type with the AMP plugin.
51
+     *
52
+     * @since 3.12.0
53
+     */
54
+    public function register_entity_cpt_with_amp_plugin() {
55 55
 
56
-		if ( ! defined( 'AMP_QUERY_VAR' ) ) {
57
-			return;
58
-		}
56
+        if ( ! defined( 'AMP_QUERY_VAR' ) ) {
57
+            return;
58
+        }
59 59
 
60
-		foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) {
61
-			// Do not change anything for posts and pages.
62
-			if ( 'post' === $post_type || 'page' === $post_type ) {
63
-				continue;
64
-			}
65
-			add_post_type_support( $post_type, AMP_QUERY_VAR );
66
-		}
60
+        foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) {
61
+            // Do not change anything for posts and pages.
62
+            if ( 'post' === $post_type || 'page' === $post_type ) {
63
+                continue;
64
+            }
65
+            add_post_type_support( $post_type, AMP_QUERY_VAR );
66
+        }
67 67
 
68
-	}
68
+    }
69 69
 
70
-	/**
71
-	 * Filters Schema.org metadata for a post.
72
-	 *
73
-	 * @since 3.19.1
74
-	 *
75
-	 * @param array   $metadata Metadata.
76
-	 * @param WP_Post $post Post.
77
-	 *
78
-	 * @return array Return WordLift's generated JSON-LD.
79
-	 */
80
-	public function amp_post_template_metadata( $metadata, $post ) {
70
+    /**
71
+     * Filters Schema.org metadata for a post.
72
+     *
73
+     * @since 3.19.1
74
+     *
75
+     * @param array   $metadata Metadata.
76
+     * @param WP_Post $post Post.
77
+     *
78
+     * @return array Return WordLift's generated JSON-LD.
79
+     */
80
+    public function amp_post_template_metadata( $metadata, $post ) {
81 81
 
82
-		return $this->jsonld_service->get_jsonld( false, $post->ID );
83
-	}
82
+        return $this->jsonld_service->get_jsonld( false, $post->ID );
83
+    }
84 84
 
85
-	/**
86
-	 * Check if current page is amp endpoint.
87
-	 *
88
-	 * @since 3.20.0
89
-	 *
90
-	 * @return bool
91
-	 */
92
-	public static function is_amp_endpoint() {
93
-		return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
94
-	}
85
+    /**
86
+     * Check if current page is amp endpoint.
87
+     *
88
+     * @since 3.20.0
89
+     *
90
+     * @return bool
91
+     */
92
+    public static function is_amp_endpoint() {
93
+        return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
94
+    }
95 95
 
96 96
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -37,12 +37,12 @@  discard block
 block discarded – undo
37 37
 	 *
38 38
 	 * @param \Wordlift_Jsonld_Service $jsonld_service
39 39
 	 */
40
-	public function __construct( $jsonld_service ) {
40
+	public function __construct($jsonld_service) {
41 41
 
42 42
 		$this->jsonld_service = $jsonld_service;
43 43
 
44
-		add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin' ) );
45
-		add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata' ), 99, 2 );
44
+		add_action('amp_init', array($this, 'register_entity_cpt_with_amp_plugin'));
45
+		add_filter('amp_post_template_metadata', array($this, 'amp_post_template_metadata'), 99, 2);
46 46
 
47 47
 	}
48 48
 
@@ -53,16 +53,16 @@  discard block
 block discarded – undo
53 53
 	 */
54 54
 	public function register_entity_cpt_with_amp_plugin() {
55 55
 
56
-		if ( ! defined( 'AMP_QUERY_VAR' ) ) {
56
+		if ( ! defined('AMP_QUERY_VAR')) {
57 57
 			return;
58 58
 		}
59 59
 
60
-		foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) {
60
+		foreach (Wordlift_Entity_Service::valid_entity_post_types() as $post_type) {
61 61
 			// Do not change anything for posts and pages.
62
-			if ( 'post' === $post_type || 'page' === $post_type ) {
62
+			if ('post' === $post_type || 'page' === $post_type) {
63 63
 				continue;
64 64
 			}
65
-			add_post_type_support( $post_type, AMP_QUERY_VAR );
65
+			add_post_type_support($post_type, AMP_QUERY_VAR);
66 66
 		}
67 67
 
68 68
 	}
@@ -77,9 +77,9 @@  discard block
 block discarded – undo
77 77
 	 *
78 78
 	 * @return array Return WordLift's generated JSON-LD.
79 79
 	 */
80
-	public function amp_post_template_metadata( $metadata, $post ) {
80
+	public function amp_post_template_metadata($metadata, $post) {
81 81
 
82
-		return $this->jsonld_service->get_jsonld( false, $post->ID );
82
+		return $this->jsonld_service->get_jsonld(false, $post->ID);
83 83
 	}
84 84
 
85 85
 	/**
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
 	 * @return bool
91 91
 	 */
92 92
 	public static function is_amp_endpoint() {
93
-		return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
93
+		return function_exists('is_amp_endpoint') && is_amp_endpoint();
94 94
 	}
95 95
 
96 96
 }
Please login to merge, or discard this patch.
src/public/class-wordlift-sharethis-service.php 2 patches
Indentation   +111 added lines, -111 removed lines patch added patch discarded remove patch
@@ -17,116 +17,116 @@
 block discarded – undo
17 17
  */
18 18
 class Wordlift_ShareThis_Service {
19 19
 
20
-	/**
21
-	 * The ShareThis function which prints the buttons.
22
-	 *
23
-	 * @since 3.2.0
24
-	 */
25
-	const ADD_WIDGET_FUNCTION_NAME = 'st_add_widget';
26
-
27
-	/**
28
-	 * The Log service.
29
-	 *
30
-	 * @since  3.2.0
31
-	 * @access private
32
-	 * @var \Wordlift_Log_Service $log_service The Log service.
33
-	 */
34
-	private $log_service;
35
-
36
-	/**
37
-	 * Create an instance of the ShareThis service.
38
-	 *
39
-	 * @since 3.2.0
40
-	 */
41
-	public function __construct() {
42
-
43
-		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' );
44
-
45
-	}
46
-
47
-	/**
48
-	 * Receive <em>the_content</em> filter calls from WordPress.
49
-	 *
50
-	 * @since 3.2.0
51
-	 *
52
-	 * @param string $content The post content.
53
-	 *
54
-	 * @return string The updated post content.
55
-	 */
56
-	public function the_content( $content ) {
57
-
58
-		return $this->call_sharethis( 'the_content', $content );
59
-	}
60
-
61
-	/**
62
-	 * Receive <em>the_excerpt</em> filter calls from WordPress.
63
-	 *
64
-	 * @since 3.2.0
65
-	 *
66
-	 * @param string $content The post excerpt.
67
-	 *
68
-	 * @return string The updated post excerpt.
69
-	 */
70
-	public function the_excerpt( $content ) {
71
-
72
-		return $this->call_sharethis( 'the_excerpt', $content );
73
-	}
74
-
75
-	/**
76
-	 * Call the ShareThis function.
77
-	 *
78
-	 * @since 3.2.0
79
-	 *
80
-	 * @param string $tag The filter tag.
81
-	 * @param string $content The post content.
82
-	 *
83
-	 * @return string The updated post content.
84
-	 */
85
-	private function call_sharethis( $tag, $content ) {
86
-
87
-		// Get the current post.
88
-		global $post;
89
-
90
-		// Bail out if the global $post instance isn't set.
91
-		if ( ! isset( $post ) ) {
92
-			return $content;
93
-		}
94
-
95
-		// Bail out if the current entity is a post/page since this is already handled by ShareThis.
96
-		//
97
-		// See https://github.com/insideout10/wordlift-plugin/issues/819
98
-		if ( 'post' === $post->post_type || 'page' === $post->post_type ) {
99
-			return $content;
100
-		}
101
-
102
-		// If it's not the entity type, return.
103
-		$entity_service = Wordlift_Entity_Service::get_instance();
104
-		if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) {
105
-			return $content;
106
-		}
107
-
108
-		// If the ShareThis function doesn't exist, return.
109
-		if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) {
110
-			return $content;
111
-		}
112
-
113
-		// If ShareThis hasn't been added as a filter, return.
114
-		if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) {
115
-			return $content;
116
-		}
117
-
118
-		// Temporary pop the post type and replace it with post.
119
-		$post_type       = $post->post_type;
120
-		$post->post_type = 'post';
121
-
122
-		// Call ShareThis (disguised as a post).
123
-		$content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) );
124
-
125
-		// Restore our post type.
126
-		$post->post_type = $post_type;
127
-
128
-		// Finally return the content.
129
-		return $content;
130
-	}
20
+    /**
21
+     * The ShareThis function which prints the buttons.
22
+     *
23
+     * @since 3.2.0
24
+     */
25
+    const ADD_WIDGET_FUNCTION_NAME = 'st_add_widget';
26
+
27
+    /**
28
+     * The Log service.
29
+     *
30
+     * @since  3.2.0
31
+     * @access private
32
+     * @var \Wordlift_Log_Service $log_service The Log service.
33
+     */
34
+    private $log_service;
35
+
36
+    /**
37
+     * Create an instance of the ShareThis service.
38
+     *
39
+     * @since 3.2.0
40
+     */
41
+    public function __construct() {
42
+
43
+        $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' );
44
+
45
+    }
46
+
47
+    /**
48
+     * Receive <em>the_content</em> filter calls from WordPress.
49
+     *
50
+     * @since 3.2.0
51
+     *
52
+     * @param string $content The post content.
53
+     *
54
+     * @return string The updated post content.
55
+     */
56
+    public function the_content( $content ) {
57
+
58
+        return $this->call_sharethis( 'the_content', $content );
59
+    }
60
+
61
+    /**
62
+     * Receive <em>the_excerpt</em> filter calls from WordPress.
63
+     *
64
+     * @since 3.2.0
65
+     *
66
+     * @param string $content The post excerpt.
67
+     *
68
+     * @return string The updated post excerpt.
69
+     */
70
+    public function the_excerpt( $content ) {
71
+
72
+        return $this->call_sharethis( 'the_excerpt', $content );
73
+    }
74
+
75
+    /**
76
+     * Call the ShareThis function.
77
+     *
78
+     * @since 3.2.0
79
+     *
80
+     * @param string $tag The filter tag.
81
+     * @param string $content The post content.
82
+     *
83
+     * @return string The updated post content.
84
+     */
85
+    private function call_sharethis( $tag, $content ) {
86
+
87
+        // Get the current post.
88
+        global $post;
89
+
90
+        // Bail out if the global $post instance isn't set.
91
+        if ( ! isset( $post ) ) {
92
+            return $content;
93
+        }
94
+
95
+        // Bail out if the current entity is a post/page since this is already handled by ShareThis.
96
+        //
97
+        // See https://github.com/insideout10/wordlift-plugin/issues/819
98
+        if ( 'post' === $post->post_type || 'page' === $post->post_type ) {
99
+            return $content;
100
+        }
101
+
102
+        // If it's not the entity type, return.
103
+        $entity_service = Wordlift_Entity_Service::get_instance();
104
+        if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) {
105
+            return $content;
106
+        }
107
+
108
+        // If the ShareThis function doesn't exist, return.
109
+        if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) {
110
+            return $content;
111
+        }
112
+
113
+        // If ShareThis hasn't been added as a filter, return.
114
+        if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) {
115
+            return $content;
116
+        }
117
+
118
+        // Temporary pop the post type and replace it with post.
119
+        $post_type       = $post->post_type;
120
+        $post->post_type = 'post';
121
+
122
+        // Call ShareThis (disguised as a post).
123
+        $content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) );
124
+
125
+        // Restore our post type.
126
+        $post->post_type = $post_type;
127
+
128
+        // Finally return the content.
129
+        return $content;
130
+    }
131 131
 
132 132
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -40,7 +40,7 @@  discard block
 block discarded – undo
40 40
 	 */
41 41
 	public function __construct() {
42 42
 
43
-		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' );
43
+		$this->log_service = Wordlift_Log_Service::get_logger('Wordlift_ShareThis_Service');
44 44
 
45 45
 	}
46 46
 
@@ -53,9 +53,9 @@  discard block
 block discarded – undo
53 53
 	 *
54 54
 	 * @return string The updated post content.
55 55
 	 */
56
-	public function the_content( $content ) {
56
+	public function the_content($content) {
57 57
 
58
-		return $this->call_sharethis( 'the_content', $content );
58
+		return $this->call_sharethis('the_content', $content);
59 59
 	}
60 60
 
61 61
 	/**
@@ -67,9 +67,9 @@  discard block
 block discarded – undo
67 67
 	 *
68 68
 	 * @return string The updated post excerpt.
69 69
 	 */
70
-	public function the_excerpt( $content ) {
70
+	public function the_excerpt($content) {
71 71
 
72
-		return $this->call_sharethis( 'the_excerpt', $content );
72
+		return $this->call_sharethis('the_excerpt', $content);
73 73
 	}
74 74
 
75 75
 	/**
@@ -82,36 +82,36 @@  discard block
 block discarded – undo
82 82
 	 *
83 83
 	 * @return string The updated post content.
84 84
 	 */
85
-	private function call_sharethis( $tag, $content ) {
85
+	private function call_sharethis($tag, $content) {
86 86
 
87 87
 		// Get the current post.
88 88
 		global $post;
89 89
 
90 90
 		// Bail out if the global $post instance isn't set.
91
-		if ( ! isset( $post ) ) {
91
+		if ( ! isset($post)) {
92 92
 			return $content;
93 93
 		}
94 94
 
95 95
 		// Bail out if the current entity is a post/page since this is already handled by ShareThis.
96 96
 		//
97 97
 		// See https://github.com/insideout10/wordlift-plugin/issues/819
98
-		if ( 'post' === $post->post_type || 'page' === $post->post_type ) {
98
+		if ('post' === $post->post_type || 'page' === $post->post_type) {
99 99
 			return $content;
100 100
 		}
101 101
 
102 102
 		// If it's not the entity type, return.
103 103
 		$entity_service = Wordlift_Entity_Service::get_instance();
104
-		if ( null === $post || ! $entity_service->is_entity( get_the_ID() ) ) {
104
+		if (null === $post || ! $entity_service->is_entity(get_the_ID())) {
105 105
 			return $content;
106 106
 		}
107 107
 
108 108
 		// If the ShareThis function doesn't exist, return.
109
-		if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) {
109
+		if ( ! function_exists(self::ADD_WIDGET_FUNCTION_NAME)) {
110 110
 			return $content;
111 111
 		}
112 112
 
113 113
 		// If ShareThis hasn't been added as a filter, return.
114
-		if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) {
114
+		if ( ! has_filter($tag, self::ADD_WIDGET_FUNCTION_NAME)) {
115 115
 			return $content;
116 116
 		}
117 117
 
@@ -120,7 +120,7 @@  discard block
 block discarded – undo
120 120
 		$post->post_type = 'post';
121 121
 
122 122
 		// Call ShareThis (disguised as a post).
123
-		$content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) );
123
+		$content = call_user_func_array(self::ADD_WIDGET_FUNCTION_NAME, array($content));
124 124
 
125 125
 		// Restore our post type.
126 126
 		$post->post_type = $post_type;
Please login to merge, or discard this patch.