Completed
Pull Request — master (#1573)
by Naveen
01:14
created
src/includes/class-wordlift-entity-post-type-service.php 2 patches
Indentation   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -16,176 +16,176 @@
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Entity_Post_Type_Service {
18 18
 
19
-	/**
20
-	 * The entity post type.
21
-	 *
22
-	 * @since  3.6.0
23
-	 * @access private
24
-	 * @var string $post_type The entity post type.
25
-	 */
26
-	private $post_type;
27
-
28
-	/**
29
-	 * The entity type slug.
30
-	 *
31
-	 * @since  3.6.0
32
-	 * @access private
33
-	 * @var string $slug The entity type slug.
34
-	 */
35
-	private $slug;
36
-
37
-	/**
38
-	 * A {@link Wordlift_Log_Service} instance.
39
-	 *
40
-	 * @since  3.16.3
41
-	 * @access private
42
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
43
-	 */
44
-	private $log;
45
-
46
-	/**
47
-	 * A singleton instance of the entity type service.
48
-	 *
49
-	 * @since  3.6.0
50
-	 * @access private
51
-	 * @var Wordlift_Entity_Post_Type_Service
52
-	 */
53
-	private static $instance;
54
-
55
-	/**
56
-	 * Create an entity type service instance.
57
-	 *
58
-	 * @param string $post_type The post type, e.g. entity.
59
-	 * @param string $slug The entity type slug, if the slug is empty, the default slug will be used.
60
-	 *
61
-	 * @since 3.6.0
62
-	 */
63
-	public function __construct( $post_type, $slug ) {
64
-
65
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
66
-
67
-		$this->post_type = $post_type;
68
-
69
-		// We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default
70
-		// to the type name.
71
-		$this->slug = $slug ? $slug : $post_type;
72
-
73
-		self::$instance = $this;
74
-
75
-	}
76
-
77
-	/**
78
-	 * Get the entity type service singleton instance.
79
-	 *
80
-	 * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance.
81
-	 * @since 3.6.0
82
-	 */
83
-	public static function get_instance() {
84
-
85
-		return self::$instance;
86
-	}
87
-
88
-	/**
89
-	 * Get the entity type slug.
90
-	 *
91
-	 * @return string The entity type slug.
92
-	 * @since 3.6.0
93
-	 */
94
-	public function get_slug() {
95
-
96
-		return $this->slug;
97
-	}
98
-
99
-	/**
100
-	 * Get the entity post type name.
101
-	 *
102
-	 * @return string The entity post type.
103
-	 * @since 3.6.0
104
-	 */
105
-	public function get_post_type() {
106
-
107
-		return $this->post_type;
108
-	}
109
-
110
-	/**
111
-	 * Register the WordLift entity post type. This method is hooked to WordPress' init action.
112
-	 *
113
-	 * @since 3.6.0
114
-	 */
115
-	public function register() {
116
-
117
-		$this->log->debug( "Registering post type $this->post_type..." );
118
-
119
-		$labels = array(
120
-			'name'               => _x( 'Vocabulary', 'post type general name', 'wordlift' ),
121
-			'singular_name'      => _x( 'Entity', 'post type singular name', 'wordlift' ),
122
-			'add_new'            => _x( 'Add New Entity', 'entity', 'wordlift' ),
123
-			'add_new_item'       => __( 'Add New Entity', 'wordlift' ),
124
-			'edit_item'          => __( 'Edit Entity', 'wordlift' ),
125
-			'new_item'           => __( 'New Entity', 'wordlift' ),
126
-			'all_items'          => __( 'All Entities', 'wordlift' ),
127
-			'view_item'          => __( 'View Entity', 'wordlift' ),
128
-			'search_items'       => __( 'Search in Vocabulary', 'wordlift' ),
129
-			'not_found'          => __( 'No entities found', 'wordlift' ),
130
-			'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ),
131
-			'parent_item_colon'  => '',
132
-			'menu_name'          => __( 'Vocabulary', 'wordlift' ),
133
-		);
134
-
135
-		$args = array(
136
-			'labels'          => $labels,
137
-			'description'     => 'Holds our vocabulary (set of entities) and entity specific data',
138
-			'public'          => true,
139
-			'menu_position'   => 20,
140
-			// after the pages menu.
141
-			// Add support for 'authors' and 'revisions':
142
-			// * see https://github.com/insideout10/wordlift-plugin/issues/395
143
-			// * see https://github.com/insideout10/wordlift-plugin/issues/376
144
-			'supports'        => array(
145
-				'title',
146
-				'editor',
147
-				'thumbnail',
148
-				'excerpt',
149
-				'comments',
150
-				'author',
151
-				'revisions',
152
-				'custom-fields',
153
-			),
154
-			'has_archive'     => true,
155
-			'menu_icon'       => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg',
156
-			// Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service.
157
-			'rewrite'         => array( 'slug' => $this->slug ),
158
-			'capability_type' => array(
159
-				'wordlift_entity',
160
-				'wordlift_entities',
161
-			),
162
-			'capabilities'    => array(
163
-				'delete_posts' => 'delete_wordlift_entities',
164
-			),
165
-			'rest_base'       => 'entities',
166
-			'show_in_rest'    => true,
167
-			/**
168
-			 * Filter: wl_feature__enable__vocabulary.
169
-			 *
170
-			 * @param bool whether the vocabulary needs to be shown in menu.
171
-			 *
172
-			 * @return bool
173
-			 * @since 3.27.6
174
-			 */
175
-			'show_in_menu'    => apply_filters( 'wl_feature__enable__vocabulary', true ),
176
-		);
177
-
178
-		register_post_type( $this->post_type, $args );
179
-
180
-		// Enable WP's standard `category` taxonomy for entities.
181
-		//
182
-		// While this enables editors to bind entities to the WP posts' category
183
-		// taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter
184
-		// WP's main category query to include the `entity` post type.
185
-		//
186
-		// See https://github.com/insideout10/wordlift-plugin/issues/442
187
-		register_taxonomy_for_object_type( 'category', $this->post_type );
188
-
189
-	}
19
+    /**
20
+     * The entity post type.
21
+     *
22
+     * @since  3.6.0
23
+     * @access private
24
+     * @var string $post_type The entity post type.
25
+     */
26
+    private $post_type;
27
+
28
+    /**
29
+     * The entity type slug.
30
+     *
31
+     * @since  3.6.0
32
+     * @access private
33
+     * @var string $slug The entity type slug.
34
+     */
35
+    private $slug;
36
+
37
+    /**
38
+     * A {@link Wordlift_Log_Service} instance.
39
+     *
40
+     * @since  3.16.3
41
+     * @access private
42
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
43
+     */
44
+    private $log;
45
+
46
+    /**
47
+     * A singleton instance of the entity type service.
48
+     *
49
+     * @since  3.6.0
50
+     * @access private
51
+     * @var Wordlift_Entity_Post_Type_Service
52
+     */
53
+    private static $instance;
54
+
55
+    /**
56
+     * Create an entity type service instance.
57
+     *
58
+     * @param string $post_type The post type, e.g. entity.
59
+     * @param string $slug The entity type slug, if the slug is empty, the default slug will be used.
60
+     *
61
+     * @since 3.6.0
62
+     */
63
+    public function __construct( $post_type, $slug ) {
64
+
65
+        $this->log = Wordlift_Log_Service::get_logger( get_class() );
66
+
67
+        $this->post_type = $post_type;
68
+
69
+        // We cannot assign an empty slug to the register_post_type function, therefore if the slug is empty we default
70
+        // to the type name.
71
+        $this->slug = $slug ? $slug : $post_type;
72
+
73
+        self::$instance = $this;
74
+
75
+    }
76
+
77
+    /**
78
+     * Get the entity type service singleton instance.
79
+     *
80
+     * @return Wordlift_Entity_Post_Type_Service The entity type service singleton instance.
81
+     * @since 3.6.0
82
+     */
83
+    public static function get_instance() {
84
+
85
+        return self::$instance;
86
+    }
87
+
88
+    /**
89
+     * Get the entity type slug.
90
+     *
91
+     * @return string The entity type slug.
92
+     * @since 3.6.0
93
+     */
94
+    public function get_slug() {
95
+
96
+        return $this->slug;
97
+    }
98
+
99
+    /**
100
+     * Get the entity post type name.
101
+     *
102
+     * @return string The entity post type.
103
+     * @since 3.6.0
104
+     */
105
+    public function get_post_type() {
106
+
107
+        return $this->post_type;
108
+    }
109
+
110
+    /**
111
+     * Register the WordLift entity post type. This method is hooked to WordPress' init action.
112
+     *
113
+     * @since 3.6.0
114
+     */
115
+    public function register() {
116
+
117
+        $this->log->debug( "Registering post type $this->post_type..." );
118
+
119
+        $labels = array(
120
+            'name'               => _x( 'Vocabulary', 'post type general name', 'wordlift' ),
121
+            'singular_name'      => _x( 'Entity', 'post type singular name', 'wordlift' ),
122
+            'add_new'            => _x( 'Add New Entity', 'entity', 'wordlift' ),
123
+            'add_new_item'       => __( 'Add New Entity', 'wordlift' ),
124
+            'edit_item'          => __( 'Edit Entity', 'wordlift' ),
125
+            'new_item'           => __( 'New Entity', 'wordlift' ),
126
+            'all_items'          => __( 'All Entities', 'wordlift' ),
127
+            'view_item'          => __( 'View Entity', 'wordlift' ),
128
+            'search_items'       => __( 'Search in Vocabulary', 'wordlift' ),
129
+            'not_found'          => __( 'No entities found', 'wordlift' ),
130
+            'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ),
131
+            'parent_item_colon'  => '',
132
+            'menu_name'          => __( 'Vocabulary', 'wordlift' ),
133
+        );
134
+
135
+        $args = array(
136
+            'labels'          => $labels,
137
+            'description'     => 'Holds our vocabulary (set of entities) and entity specific data',
138
+            'public'          => true,
139
+            'menu_position'   => 20,
140
+            // after the pages menu.
141
+            // Add support for 'authors' and 'revisions':
142
+            // * see https://github.com/insideout10/wordlift-plugin/issues/395
143
+            // * see https://github.com/insideout10/wordlift-plugin/issues/376
144
+            'supports'        => array(
145
+                'title',
146
+                'editor',
147
+                'thumbnail',
148
+                'excerpt',
149
+                'comments',
150
+                'author',
151
+                'revisions',
152
+                'custom-fields',
153
+            ),
154
+            'has_archive'     => true,
155
+            'menu_icon'       => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg',
156
+            // Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service.
157
+            'rewrite'         => array( 'slug' => $this->slug ),
158
+            'capability_type' => array(
159
+                'wordlift_entity',
160
+                'wordlift_entities',
161
+            ),
162
+            'capabilities'    => array(
163
+                'delete_posts' => 'delete_wordlift_entities',
164
+            ),
165
+            'rest_base'       => 'entities',
166
+            'show_in_rest'    => true,
167
+            /**
168
+             * Filter: wl_feature__enable__vocabulary.
169
+             *
170
+             * @param bool whether the vocabulary needs to be shown in menu.
171
+             *
172
+             * @return bool
173
+             * @since 3.27.6
174
+             */
175
+            'show_in_menu'    => apply_filters( 'wl_feature__enable__vocabulary', true ),
176
+        );
177
+
178
+        register_post_type( $this->post_type, $args );
179
+
180
+        // Enable WP's standard `category` taxonomy for entities.
181
+        //
182
+        // While this enables editors to bind entities to the WP posts' category
183
+        // taxonomy, in Wordlift_Category_Taxonomy_Service we also need to alter
184
+        // WP's main category query to include the `entity` post type.
185
+        //
186
+        // See https://github.com/insideout10/wordlift-plugin/issues/442
187
+        register_taxonomy_for_object_type( 'category', $this->post_type );
188
+
189
+    }
190 190
 
191 191
 }
Please login to merge, or discard this patch.
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -60,9 +60,9 @@  discard block
 block discarded – undo
60 60
 	 *
61 61
 	 * @since 3.6.0
62 62
 	 */
63
-	public function __construct( $post_type, $slug ) {
63
+	public function __construct($post_type, $slug) {
64 64
 
65
-		$this->log = Wordlift_Log_Service::get_logger( get_class() );
65
+		$this->log = Wordlift_Log_Service::get_logger(get_class());
66 66
 
67 67
 		$this->post_type = $post_type;
68 68
 
@@ -114,22 +114,22 @@  discard block
 block discarded – undo
114 114
 	 */
115 115
 	public function register() {
116 116
 
117
-		$this->log->debug( "Registering post type $this->post_type..." );
117
+		$this->log->debug("Registering post type $this->post_type...");
118 118
 
119 119
 		$labels = array(
120
-			'name'               => _x( 'Vocabulary', 'post type general name', 'wordlift' ),
121
-			'singular_name'      => _x( 'Entity', 'post type singular name', 'wordlift' ),
122
-			'add_new'            => _x( 'Add New Entity', 'entity', 'wordlift' ),
123
-			'add_new_item'       => __( 'Add New Entity', 'wordlift' ),
124
-			'edit_item'          => __( 'Edit Entity', 'wordlift' ),
125
-			'new_item'           => __( 'New Entity', 'wordlift' ),
126
-			'all_items'          => __( 'All Entities', 'wordlift' ),
127
-			'view_item'          => __( 'View Entity', 'wordlift' ),
128
-			'search_items'       => __( 'Search in Vocabulary', 'wordlift' ),
129
-			'not_found'          => __( 'No entities found', 'wordlift' ),
130
-			'not_found_in_trash' => __( 'No entities found in the Trash', 'wordlift' ),
120
+			'name'               => _x('Vocabulary', 'post type general name', 'wordlift'),
121
+			'singular_name'      => _x('Entity', 'post type singular name', 'wordlift'),
122
+			'add_new'            => _x('Add New Entity', 'entity', 'wordlift'),
123
+			'add_new_item'       => __('Add New Entity', 'wordlift'),
124
+			'edit_item'          => __('Edit Entity', 'wordlift'),
125
+			'new_item'           => __('New Entity', 'wordlift'),
126
+			'all_items'          => __('All Entities', 'wordlift'),
127
+			'view_item'          => __('View Entity', 'wordlift'),
128
+			'search_items'       => __('Search in Vocabulary', 'wordlift'),
129
+			'not_found'          => __('No entities found', 'wordlift'),
130
+			'not_found_in_trash' => __('No entities found in the Trash', 'wordlift'),
131 131
 			'parent_item_colon'  => '',
132
-			'menu_name'          => __( 'Vocabulary', 'wordlift' ),
132
+			'menu_name'          => __('Vocabulary', 'wordlift'),
133 133
 		);
134 134
 
135 135
 		$args = array(
@@ -152,9 +152,9 @@  discard block
 block discarded – undo
152 152
 				'custom-fields',
153 153
 			),
154 154
 			'has_archive'     => true,
155
-			'menu_icon'       => WP_CONTENT_URL . '/plugins/wordlift/images/svg/wl-vocabulary-icon.svg',
155
+			'menu_icon'       => WP_CONTENT_URL.'/plugins/wordlift/images/svg/wl-vocabulary-icon.svg',
156 156
 			// Although we define our slug here, we further manage linking to entities using the Wordlift_Entity_Link_Service.
157
-			'rewrite'         => array( 'slug' => $this->slug ),
157
+			'rewrite'         => array('slug' => $this->slug),
158 158
 			'capability_type' => array(
159 159
 				'wordlift_entity',
160 160
 				'wordlift_entities',
@@ -172,10 +172,10 @@  discard block
 block discarded – undo
172 172
 			 * @return bool
173 173
 			 * @since 3.27.6
174 174
 			 */
175
-			'show_in_menu'    => apply_filters( 'wl_feature__enable__vocabulary', true ),
175
+			'show_in_menu'    => apply_filters('wl_feature__enable__vocabulary', true),
176 176
 		);
177 177
 
178
-		register_post_type( $this->post_type, $args );
178
+		register_post_type($this->post_type, $args);
179 179
 
180 180
 		// Enable WP's standard `category` taxonomy for entities.
181 181
 		//
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
 		// WP's main category query to include the `entity` post type.
185 185
 		//
186 186
 		// See https://github.com/insideout10/wordlift-plugin/issues/442
187
-		register_taxonomy_for_object_type( 'category', $this->post_type );
187
+		register_taxonomy_for_object_type('category', $this->post_type);
188 188
 
189 189
 	}
190 190
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-publisher-service.php 2 patches
Indentation   +227 added lines, -227 removed lines patch added patch discarded remove patch
@@ -18,235 +18,235 @@
 block discarded – undo
18 18
  */
19 19
 class Wordlift_Publisher_Service {
20 20
 
21
-	protected function __construct() {
22
-
23
-	}
24
-
25
-	private static $instance = null;
26
-
27
-	public static function get_instance() {
28
-
29
-		if ( ! isset( self::$instance ) ) {
30
-			self::$instance = new self();
31
-		}
32
-
33
-		return self::$instance;
34
-	}
35
-
36
-	/**
37
-	 * Counts the number of potential publishers.
38
-	 *
39
-	 * @return int The number of potential publishers.
40
-	 * @since 3.11.0
41
-	 */
42
-	public function count() {
43
-
44
-		// Search for entities which are either a Person
45
-		// or Organization.
46
-
47
-		// Get only the ids as all we need is the count.
48
-		$entities = get_posts(
49
-			array(
50
-				'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
51
-				'post_status'    => 'publish',
52
-				'posts_per_page' => - 1,
53
-				'tax_query'      => array(
54
-					array(
55
-						'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
56
-						'field'    => 'slug',
57
-						'terms'    => array( 'organization', 'person' ),
58
-					),
59
-				),
60
-				'fields'         => 'ids',
61
-			)
62
-		);
63
-
64
-		// Finally return the count.
65
-		return count( $entities );
66
-	}
67
-
68
-	/**
69
-	 * Search SQL filter for matching against post title only.
70
-	 *
71
-	 * @link    http://wordpress.stackexchange.com/a/11826/1685
72
-	 *
73
-	 * @since   3.15.0
74
-	 *
75
-	 * @param string   $search The search string.
76
-	 * @param WP_Query $wp_query The {@link WP_Query} instance.
77
-	 *
78
-	 * @return array|string An array of results.
79
-	 */
80
-	public function limit_search_to_title( $search, $wp_query ) {
81
-
82
-		// Bail out if the search or the `search_terms` haven't been set.
83
-		if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) {
84
-			return $search;
85
-		}
86
-
87
-		global $wpdb;
88
-
89
-		$query_vars = $wp_query->query_vars;
90
-		$percent    = ! empty( $query_vars['exact'] ) ? '' : '%';
91
-		$search     = array();
92
-
93
-		foreach ( (array) $query_vars['search_terms'] as $term ) {
94
-			$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent );
95
-		}
96
-
97
-		if ( ! is_user_logged_in() ) {
98
-			$search[] = "$wpdb->posts.post_password = ''";
99
-		}
100
-
101
-		$search = ' AND ' . implode( ' AND ', $search );
102
-
103
-		return $search;
104
-	}
105
-
106
-	/**
107
-	 * Query WP for potential publishers, i.e. {@link WP_Post}s which are associated`
108
-	 * with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`.
109
-	 *
110
-	 * @param string $filter The title filter.
111
-	 *
112
-	 * @return array An array of results in a select2 friendly format.
113
-	 * @since 3.11.0
114
-	 */
115
-	public function query( $filter = '' ) {
116
-
117
-		// Search for the filter in the titles only.
118
-		add_filter(
119
-			'posts_search',
120
-			array(
121
-				$this,
122
-				'limit_search_to_title',
123
-			),
124
-			10,
125
-			2
126
-		);
127
-
128
-		/*
21
+    protected function __construct() {
22
+
23
+    }
24
+
25
+    private static $instance = null;
26
+
27
+    public static function get_instance() {
28
+
29
+        if ( ! isset( self::$instance ) ) {
30
+            self::$instance = new self();
31
+        }
32
+
33
+        return self::$instance;
34
+    }
35
+
36
+    /**
37
+     * Counts the number of potential publishers.
38
+     *
39
+     * @return int The number of potential publishers.
40
+     * @since 3.11.0
41
+     */
42
+    public function count() {
43
+
44
+        // Search for entities which are either a Person
45
+        // or Organization.
46
+
47
+        // Get only the ids as all we need is the count.
48
+        $entities = get_posts(
49
+            array(
50
+                'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
51
+                'post_status'    => 'publish',
52
+                'posts_per_page' => - 1,
53
+                'tax_query'      => array(
54
+                    array(
55
+                        'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
56
+                        'field'    => 'slug',
57
+                        'terms'    => array( 'organization', 'person' ),
58
+                    ),
59
+                ),
60
+                'fields'         => 'ids',
61
+            )
62
+        );
63
+
64
+        // Finally return the count.
65
+        return count( $entities );
66
+    }
67
+
68
+    /**
69
+     * Search SQL filter for matching against post title only.
70
+     *
71
+     * @link    http://wordpress.stackexchange.com/a/11826/1685
72
+     *
73
+     * @since   3.15.0
74
+     *
75
+     * @param string   $search The search string.
76
+     * @param WP_Query $wp_query The {@link WP_Query} instance.
77
+     *
78
+     * @return array|string An array of results.
79
+     */
80
+    public function limit_search_to_title( $search, $wp_query ) {
81
+
82
+        // Bail out if the search or the `search_terms` haven't been set.
83
+        if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) {
84
+            return $search;
85
+        }
86
+
87
+        global $wpdb;
88
+
89
+        $query_vars = $wp_query->query_vars;
90
+        $percent    = ! empty( $query_vars['exact'] ) ? '' : '%';
91
+        $search     = array();
92
+
93
+        foreach ( (array) $query_vars['search_terms'] as $term ) {
94
+            $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent );
95
+        }
96
+
97
+        if ( ! is_user_logged_in() ) {
98
+            $search[] = "$wpdb->posts.post_password = ''";
99
+        }
100
+
101
+        $search = ' AND ' . implode( ' AND ', $search );
102
+
103
+        return $search;
104
+    }
105
+
106
+    /**
107
+     * Query WP for potential publishers, i.e. {@link WP_Post}s which are associated`
108
+     * with `wl_entity_type` (taxonomy) terms of `Organization` or `Person`.
109
+     *
110
+     * @param string $filter The title filter.
111
+     *
112
+     * @return array An array of results in a select2 friendly format.
113
+     * @since 3.11.0
114
+     */
115
+    public function query( $filter = '' ) {
116
+
117
+        // Search for the filter in the titles only.
118
+        add_filter(
119
+            'posts_search',
120
+            array(
121
+                $this,
122
+                'limit_search_to_title',
123
+            ),
124
+            10,
125
+            2
126
+        );
127
+
128
+        /*
129 129
 		 * Search for entities which are either a Person
130 130
 		 * or Organization. Sort the results by title in ascending order.
131 131
 		 */
132
-		$entities = get_posts(
133
-			array(
134
-				'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
135
-				'post_status'    => 'publish',
136
-				'posts_per_page' => - 1,
137
-				'tax_query'      => array(
138
-					array(
139
-						'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
140
-						'field'    => 'slug',
141
-						'terms'    => array( 'organization', 'person' ),
142
-					),
143
-				),
144
-				's'              => $filter,
145
-				'orderby'        => 'title',
146
-				'order'          => 'ASC',
147
-			)
148
-		);
149
-
150
-		// Remove the search filter added before the query.
151
-		remove_filter(
152
-			'posts_search',
153
-			array(
154
-				$this,
155
-				'limit_search_to_title',
156
-			),
157
-			10,
158
-			2
159
-		);
160
-
161
-		// Set a reference to ourselves to pass to the closure.
162
-		$publisher_service = $this;
163
-
164
-		// Map the results in a `Select2` compatible array.
165
-		return array_map(
166
-			function ( $entity ) use ( $publisher_service ) {
167
-				$type     = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
168
-				$thumb_id = get_post_thumbnail_id( $entity->ID );
169
-
170
-				return array(
171
-					'id'            => $entity->ID,
172
-					'text'          => $entity->post_title,
173
-					'type'          => $type[0]->name,
174
-					'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ),
175
-				);
176
-			},
177
-			$entities
178
-		);
179
-	}
180
-
181
-	/**
182
-	 * Get the thumbnail's URL.
183
-	 *
184
-	 * @param int    $attachment_id The attachment id.
185
-	 * @param string $size The attachment size (default = 'thumbnail').
186
-	 *
187
-	 * @return string|bool The image URL or false if not found.
188
-	 * @since 3.11.0
189
-	 */
190
-	public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) {
191
-
192
-		$image = wp_get_attachment_image_src( $attachment_id, $size );
193
-
194
-		return isset( $image['0'] ) ? $image['0'] : false;
195
-	}
196
-
197
-	/**
198
-	 * Add additional instructions to featured image metabox
199
-	 * when the entity type is the publisher.
200
-	 *
201
-	 * @param string $content Current metabox content.
202
-	 *
203
-	 * @return string $content metabox content with additional instructions.
204
-	 * @since  3.19.0
205
-	 */
206
-	public function add_featured_image_instruction( $content ) {
207
-		// Get the current post ID.
208
-		$post_id = get_the_ID();
209
-
210
-		// Get the publisher id.
211
-		$publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id();
212
-
213
-		// Bail if for some reason the post id is not set.
214
-		if (
215
-			empty( $post_id ) ||
216
-			$post_id !== (int) $publisher_id
217
-		) {
218
-			return $content;
219
-		}
220
-
221
-		$terms = wp_get_post_terms(
222
-			$post_id, // The post id.
223
-			Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, // The taxonomy slug.
224
-			array(
225
-				'fields' => 'slugs',
226
-				// We don't need all fields, but only slugs.
227
-			)
228
-		);
229
-
230
-		// Check that the entity type is "Organization".
231
-		if ( in_array( 'organization', $terms, true ) ) {
232
-			// Add the featured image description when the type is "Organization".
233
-
234
-			$link     = sprintf(
235
-				'<a target="_blank" href="%s">%s</a>',
236
-				esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ),
237
-				esc_html__( 'AMP logo guidelines', 'wordlift' )
238
-			);
239
-			$content .= sprintf(
240
-				'<p>'
241
-								/* translators: %s: AMP logo guidelines. */
242
-								 . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' )
243
-								 . '</p>',
244
-				$link
245
-			);
246
-		}
247
-
248
-		// Finally return the content.
249
-		return $content;
250
-	}
132
+        $entities = get_posts(
133
+            array(
134
+                'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
135
+                'post_status'    => 'publish',
136
+                'posts_per_page' => - 1,
137
+                'tax_query'      => array(
138
+                    array(
139
+                        'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
140
+                        'field'    => 'slug',
141
+                        'terms'    => array( 'organization', 'person' ),
142
+                    ),
143
+                ),
144
+                's'              => $filter,
145
+                'orderby'        => 'title',
146
+                'order'          => 'ASC',
147
+            )
148
+        );
149
+
150
+        // Remove the search filter added before the query.
151
+        remove_filter(
152
+            'posts_search',
153
+            array(
154
+                $this,
155
+                'limit_search_to_title',
156
+            ),
157
+            10,
158
+            2
159
+        );
160
+
161
+        // Set a reference to ourselves to pass to the closure.
162
+        $publisher_service = $this;
163
+
164
+        // Map the results in a `Select2` compatible array.
165
+        return array_map(
166
+            function ( $entity ) use ( $publisher_service ) {
167
+                $type     = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
168
+                $thumb_id = get_post_thumbnail_id( $entity->ID );
169
+
170
+                return array(
171
+                    'id'            => $entity->ID,
172
+                    'text'          => $entity->post_title,
173
+                    'type'          => $type[0]->name,
174
+                    'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ),
175
+                );
176
+            },
177
+            $entities
178
+        );
179
+    }
180
+
181
+    /**
182
+     * Get the thumbnail's URL.
183
+     *
184
+     * @param int    $attachment_id The attachment id.
185
+     * @param string $size The attachment size (default = 'thumbnail').
186
+     *
187
+     * @return string|bool The image URL or false if not found.
188
+     * @since 3.11.0
189
+     */
190
+    public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) {
191
+
192
+        $image = wp_get_attachment_image_src( $attachment_id, $size );
193
+
194
+        return isset( $image['0'] ) ? $image['0'] : false;
195
+    }
196
+
197
+    /**
198
+     * Add additional instructions to featured image metabox
199
+     * when the entity type is the publisher.
200
+     *
201
+     * @param string $content Current metabox content.
202
+     *
203
+     * @return string $content metabox content with additional instructions.
204
+     * @since  3.19.0
205
+     */
206
+    public function add_featured_image_instruction( $content ) {
207
+        // Get the current post ID.
208
+        $post_id = get_the_ID();
209
+
210
+        // Get the publisher id.
211
+        $publisher_id = Wordlift_Configuration_Service::get_instance()->get_publisher_id();
212
+
213
+        // Bail if for some reason the post id is not set.
214
+        if (
215
+            empty( $post_id ) ||
216
+            $post_id !== (int) $publisher_id
217
+        ) {
218
+            return $content;
219
+        }
220
+
221
+        $terms = wp_get_post_terms(
222
+            $post_id, // The post id.
223
+            Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, // The taxonomy slug.
224
+            array(
225
+                'fields' => 'slugs',
226
+                // We don't need all fields, but only slugs.
227
+            )
228
+        );
229
+
230
+        // Check that the entity type is "Organization".
231
+        if ( in_array( 'organization', $terms, true ) ) {
232
+            // Add the featured image description when the type is "Organization".
233
+
234
+            $link     = sprintf(
235
+                '<a target="_blank" href="%s">%s</a>',
236
+                esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ),
237
+                esc_html__( 'AMP logo guidelines', 'wordlift' )
238
+            );
239
+            $content .= sprintf(
240
+                '<p>'
241
+                                /* translators: %s: AMP logo guidelines. */
242
+                                    . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' )
243
+                                    . '</p>',
244
+                $link
245
+            );
246
+        }
247
+
248
+        // Finally return the content.
249
+        return $content;
250
+    }
251 251
 
252 252
 }
Please login to merge, or discard this patch.
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
 
27 27
 	public static function get_instance() {
28 28
 
29
-		if ( ! isset( self::$instance ) ) {
29
+		if ( ! isset(self::$instance)) {
30 30
 			self::$instance = new self();
31 31
 		}
32 32
 
@@ -49,12 +49,12 @@  discard block
 block discarded – undo
49 49
 			array(
50 50
 				'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
51 51
 				'post_status'    => 'publish',
52
-				'posts_per_page' => - 1,
52
+				'posts_per_page' => -1,
53 53
 				'tax_query'      => array(
54 54
 					array(
55 55
 						'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
56 56
 						'field'    => 'slug',
57
-						'terms'    => array( 'organization', 'person' ),
57
+						'terms'    => array('organization', 'person'),
58 58
 					),
59 59
 				),
60 60
 				'fields'         => 'ids',
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 		);
63 63
 
64 64
 		// Finally return the count.
65
-		return count( $entities );
65
+		return count($entities);
66 66
 	}
67 67
 
68 68
 	/**
@@ -77,28 +77,28 @@  discard block
 block discarded – undo
77 77
 	 *
78 78
 	 * @return array|string An array of results.
79 79
 	 */
80
-	public function limit_search_to_title( $search, $wp_query ) {
80
+	public function limit_search_to_title($search, $wp_query) {
81 81
 
82 82
 		// Bail out if the search or the `search_terms` haven't been set.
83
-		if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) {
83
+		if (empty($search) || empty($wp_query->query_vars['search_terms'])) {
84 84
 			return $search;
85 85
 		}
86 86
 
87 87
 		global $wpdb;
88 88
 
89 89
 		$query_vars = $wp_query->query_vars;
90
-		$percent    = ! empty( $query_vars['exact'] ) ? '' : '%';
90
+		$percent    = ! empty($query_vars['exact']) ? '' : '%';
91 91
 		$search     = array();
92 92
 
93
-		foreach ( (array) $query_vars['search_terms'] as $term ) {
94
-			$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $percent . $wpdb->esc_like( $term ) . $percent );
93
+		foreach ((array) $query_vars['search_terms'] as $term) {
94
+			$search[] = $wpdb->prepare("$wpdb->posts.post_title LIKE %s", $percent.$wpdb->esc_like($term).$percent);
95 95
 		}
96 96
 
97
-		if ( ! is_user_logged_in() ) {
97
+		if ( ! is_user_logged_in()) {
98 98
 			$search[] = "$wpdb->posts.post_password = ''";
99 99
 		}
100 100
 
101
-		$search = ' AND ' . implode( ' AND ', $search );
101
+		$search = ' AND '.implode(' AND ', $search);
102 102
 
103 103
 		return $search;
104 104
 	}
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
 	 * @return array An array of results in a select2 friendly format.
113 113
 	 * @since 3.11.0
114 114
 	 */
115
-	public function query( $filter = '' ) {
115
+	public function query($filter = '') {
116 116
 
117 117
 		// Search for the filter in the titles only.
118 118
 		add_filter(
@@ -133,12 +133,12 @@  discard block
 block discarded – undo
133 133
 			array(
134 134
 				'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
135 135
 				'post_status'    => 'publish',
136
-				'posts_per_page' => - 1,
136
+				'posts_per_page' => -1,
137 137
 				'tax_query'      => array(
138 138
 					array(
139 139
 						'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
140 140
 						'field'    => 'slug',
141
-						'terms'    => array( 'organization', 'person' ),
141
+						'terms'    => array('organization', 'person'),
142 142
 					),
143 143
 				),
144 144
 				's'              => $filter,
@@ -163,15 +163,15 @@  discard block
 block discarded – undo
163 163
 
164 164
 		// Map the results in a `Select2` compatible array.
165 165
 		return array_map(
166
-			function ( $entity ) use ( $publisher_service ) {
167
-				$type     = wp_get_post_terms( $entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME );
168
-				$thumb_id = get_post_thumbnail_id( $entity->ID );
166
+			function($entity) use ($publisher_service) {
167
+				$type     = wp_get_post_terms($entity->ID, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME);
168
+				$thumb_id = get_post_thumbnail_id($entity->ID);
169 169
 
170 170
 				return array(
171 171
 					'id'            => $entity->ID,
172 172
 					'text'          => $entity->post_title,
173 173
 					'type'          => $type[0]->name,
174
-					'thumbnail_url' => $publisher_service->get_attachment_image_url( $thumb_id ),
174
+					'thumbnail_url' => $publisher_service->get_attachment_image_url($thumb_id),
175 175
 				);
176 176
 			},
177 177
 			$entities
@@ -187,11 +187,11 @@  discard block
 block discarded – undo
187 187
 	 * @return string|bool The image URL or false if not found.
188 188
 	 * @since 3.11.0
189 189
 	 */
190
-	public function get_attachment_image_url( $attachment_id, $size = 'thumbnail' ) {
190
+	public function get_attachment_image_url($attachment_id, $size = 'thumbnail') {
191 191
 
192
-		$image = wp_get_attachment_image_src( $attachment_id, $size );
192
+		$image = wp_get_attachment_image_src($attachment_id, $size);
193 193
 
194
-		return isset( $image['0'] ) ? $image['0'] : false;
194
+		return isset($image['0']) ? $image['0'] : false;
195 195
 	}
196 196
 
197 197
 	/**
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 	 * @return string $content metabox content with additional instructions.
204 204
 	 * @since  3.19.0
205 205
 	 */
206
-	public function add_featured_image_instruction( $content ) {
206
+	public function add_featured_image_instruction($content) {
207 207
 		// Get the current post ID.
208 208
 		$post_id = get_the_ID();
209 209
 
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
 
213 213
 		// Bail if for some reason the post id is not set.
214 214
 		if (
215
-			empty( $post_id ) ||
215
+			empty($post_id) ||
216 216
 			$post_id !== (int) $publisher_id
217 217
 		) {
218 218
 			return $content;
@@ -228,18 +228,18 @@  discard block
 block discarded – undo
228 228
 		);
229 229
 
230 230
 		// Check that the entity type is "Organization".
231
-		if ( in_array( 'organization', $terms, true ) ) {
231
+		if (in_array('organization', $terms, true)) {
232 232
 			// Add the featured image description when the type is "Organization".
233 233
 
234
-			$link     = sprintf(
234
+			$link = sprintf(
235 235
 				'<a target="_blank" href="%s">%s</a>',
236
-				esc_attr__( 'https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift' ),
237
-				esc_html__( 'AMP logo guidelines', 'wordlift' )
236
+				esc_attr__('https://developers.google.com/search/docs/data-types/article#logo-guidelines', 'wordlift'),
237
+				esc_html__('AMP logo guidelines', 'wordlift')
238 238
 			);
239 239
 			$content .= sprintf(
240 240
 				'<p>'
241 241
 								/* translators: %s: AMP logo guidelines. */
242
-								 . esc_html_x( 'According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift' )
242
+								 . esc_html_x('According to the %s, the logo should fit in a 60x600px rectangle, and either be exactly 60px high (preferred), or exactly 600px wide. For example, 450x45px would not be acceptable, even though it fits in the 600x60px rectangle. To comply with the guidelines, WordLift will automatically resize the Featured Image for structured data formats.', 'After "According to the" goes the link to the "AMP logo guidelines".', 'wordlift')
243 243
 								 . '</p>',
244 244
 				$link
245 245
 			);
Please login to merge, or discard this patch.
src/includes/class-wordlift-uri-service.php 2 patches
Indentation   +188 added lines, -188 removed lines patch added patch discarded remove patch
@@ -20,199 +20,199 @@
 block discarded – undo
20 20
  */
21 21
 class Wordlift_Uri_Service {
22 22
 
23
-	/**
24
-	 * The title regex to sanitize titles in paths.
25
-	 *
26
-	 * According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved:
27
-	 * ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
28
-	 * "$" | ","
29
-	 *
30
-	 * We also remove the space and the UTF-8 BOM sequence.
31
-	 *
32
-	 * @since 3.7.1
33
-	 */
34
-	const INVALID_CHARACTERS = "/[ ;\\/?:@&=\\+\\\$,]|(?:\\xEF\\xBB\\xBF)/";
35
-
36
-	/**
37
-	 * A {@link Wordlift_Log_Service} instance.
38
-	 *
39
-	 * @since  3.6.0
40
-	 * @access private
41
-	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
42
-	 */
43
-	private $log;
44
-
45
-	/**
46
-	 * The global WordPress database connection.
47
-	 *
48
-	 * @since  3.6.0
49
-	 * @access private
50
-	 * @var \wpdb $wpdb The global WordPress database connection.
51
-	 */
52
-	private $wpdb;
53
-
54
-	/**
55
-	 * The {@link Wordlift_Uri_Service} singleton instance.
56
-	 *
57
-	 * @since  3.7.2
58
-	 * @access private
59
-	 * @var \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance.
60
-	 */
61
-	private static $instance;
62
-
63
-	/**
64
-	 * Create an instance of Wordlift_Uri_Service.
65
-	 *
66
-	 * @param \wpdb $wpdb The global WordPress database connection.
67
-	 *
68
-	 * @since 3.6.0
69
-	 */
70
-	public function __construct( $wpdb ) {
71
-
72
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' );
73
-
74
-		$this->wpdb = $wpdb;
75
-
76
-		self::$instance = $this;
77
-
78
-	}
79
-
80
-	/**
81
-	 * Get the {@link Wordlift_Uri_Service} singleton instance.
82
-	 *
83
-	 * @return \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance.
84
-	 * @since 3.7.2
85
-	 */
86
-	public static function get_instance() {
87
-
88
-		return self::$instance;
89
-	}
90
-
91
-	/**
92
-	 * Sanitizes an URI path by replacing the not allowed characters with an underscore.
93
-	 *
94
-	 * @param string $path The path to sanitize.
95
-	 * @param string $char The replacement character (by default an underscore).
96
-	 *
97
-	 * @return string The sanitized path.
98
-	 * @since 3.7.2
99
-	 * @uses  sanitize_title() to manage not ASCII chars
100
-	 *
101
-	 * @see   https://codex.wordpress.org/Function_Reference/sanitize_title
102
-	 */
103
-	public function sanitize_path( $path, $char = '_' ) {
104
-
105
-		// Ensure the path is ASCII.
106
-		// see https://github.com/insideout10/wordlift-plugin/issues/386
107
-		// $path_ascii = mb_convert_encoding( $path, 'ASCII' );
108
-
109
-		return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) );
110
-	}
111
-
112
-	/**
113
-	 * Build an entity uri for a given title. The uri is composed using a given
114
-	 * post_type and a title. If already exists an entity e2 with a given uri a
115
-	 * numeric suffix is added. If a schema type is given entities with same label
116
-	 * and same type are overridden.
117
-	 *
118
-	 * @param string  $title A post title.
119
-	 * @param string  $post_type A post type. Default value is 'entity'
120
-	 * @param string  $schema_type A schema org type.
121
-	 * @param integer $increment_digit A digit used to call recursively the same function.
122
-	 *
123
-	 * @return string Returns an uri.
124
-	 * @deprecated don't use anywhere else, since we're refactoring the way URIs are built in the global function wl_build_entity_uri.
125
-	 * @since 3.5.0
126
-	 */
127
-	public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) {
128
-
129
-		// Get the entity slug suffix digit
130
-		$suffix_digit = $increment_digit + 1;
131
-
132
-		// Get a sanitized uri for a given title.
133
-		/*
23
+    /**
24
+     * The title regex to sanitize titles in paths.
25
+     *
26
+     * According to RFC2396 (http://www.ietf.org/rfc/rfc2396.txt) these characters are reserved:
27
+     * ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
28
+     * "$" | ","
29
+     *
30
+     * We also remove the space and the UTF-8 BOM sequence.
31
+     *
32
+     * @since 3.7.1
33
+     */
34
+    const INVALID_CHARACTERS = "/[ ;\\/?:@&=\\+\\\$,]|(?:\\xEF\\xBB\\xBF)/";
35
+
36
+    /**
37
+     * A {@link Wordlift_Log_Service} instance.
38
+     *
39
+     * @since  3.6.0
40
+     * @access private
41
+     * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
42
+     */
43
+    private $log;
44
+
45
+    /**
46
+     * The global WordPress database connection.
47
+     *
48
+     * @since  3.6.0
49
+     * @access private
50
+     * @var \wpdb $wpdb The global WordPress database connection.
51
+     */
52
+    private $wpdb;
53
+
54
+    /**
55
+     * The {@link Wordlift_Uri_Service} singleton instance.
56
+     *
57
+     * @since  3.7.2
58
+     * @access private
59
+     * @var \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance.
60
+     */
61
+    private static $instance;
62
+
63
+    /**
64
+     * Create an instance of Wordlift_Uri_Service.
65
+     *
66
+     * @param \wpdb $wpdb The global WordPress database connection.
67
+     *
68
+     * @since 3.6.0
69
+     */
70
+    public function __construct( $wpdb ) {
71
+
72
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' );
73
+
74
+        $this->wpdb = $wpdb;
75
+
76
+        self::$instance = $this;
77
+
78
+    }
79
+
80
+    /**
81
+     * Get the {@link Wordlift_Uri_Service} singleton instance.
82
+     *
83
+     * @return \Wordlift_Uri_Service The {@link Wordlift_Uri_Service} singleton instance.
84
+     * @since 3.7.2
85
+     */
86
+    public static function get_instance() {
87
+
88
+        return self::$instance;
89
+    }
90
+
91
+    /**
92
+     * Sanitizes an URI path by replacing the not allowed characters with an underscore.
93
+     *
94
+     * @param string $path The path to sanitize.
95
+     * @param string $char The replacement character (by default an underscore).
96
+     *
97
+     * @return string The sanitized path.
98
+     * @since 3.7.2
99
+     * @uses  sanitize_title() to manage not ASCII chars
100
+     *
101
+     * @see   https://codex.wordpress.org/Function_Reference/sanitize_title
102
+     */
103
+    public function sanitize_path( $path, $char = '_' ) {
104
+
105
+        // Ensure the path is ASCII.
106
+        // see https://github.com/insideout10/wordlift-plugin/issues/386
107
+        // $path_ascii = mb_convert_encoding( $path, 'ASCII' );
108
+
109
+        return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) );
110
+    }
111
+
112
+    /**
113
+     * Build an entity uri for a given title. The uri is composed using a given
114
+     * post_type and a title. If already exists an entity e2 with a given uri a
115
+     * numeric suffix is added. If a schema type is given entities with same label
116
+     * and same type are overridden.
117
+     *
118
+     * @param string  $title A post title.
119
+     * @param string  $post_type A post type. Default value is 'entity'
120
+     * @param string  $schema_type A schema org type.
121
+     * @param integer $increment_digit A digit used to call recursively the same function.
122
+     *
123
+     * @return string Returns an uri.
124
+     * @deprecated don't use anywhere else, since we're refactoring the way URIs are built in the global function wl_build_entity_uri.
125
+     * @since 3.5.0
126
+     */
127
+    public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) {
128
+
129
+        // Get the entity slug suffix digit
130
+        $suffix_digit = $increment_digit + 1;
131
+
132
+        // Get a sanitized uri for a given title.
133
+        /*
134 134
 		 * The call takes into consideration URL encoding.
135 135
 		 *
136 136
 		 * @see https://github.com/insideout10/wordlift-plugin/issues/885
137 137
 		 *
138 138
 		 * @since 3.20.0
139 139
 		 */
140
-		$entity_slug = urldecode( wl_sanitize_uri_path( $title ) )
141
-					   . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
142
-
143
-		// Compose a candidate uri.
144
-		$new_entity_uri = sprintf(
145
-			'%s/%s/%s',
146
-			untrailingslashit( wl_configuration_get_redlink_dataset_uri() ),
147
-			$post_type,
148
-			$entity_slug
149
-		);
150
-
151
-		$this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
152
-
153
-		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $new_entity_uri );
154
-		$post_id = $content && is_a( $content->get_bag(), '\WP_Post' ) ? $content->get_bag()->ID : null;
155
-
156
-		// If the post does not exist, then the new uri is returned
157
-		if ( ! is_numeric( $post_id ) ) {
158
-			$this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" );
159
-
160
-			return $new_entity_uri;
161
-		}
162
-
163
-		// If schema_type is equal to schema org type of post x, then the new uri is returned
164
-		$schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
165
-
166
-		// @todo: we shouldn't rely on css classes to take such decisions.
167
-		if ( $schema_type === $schema_post_type['css_class'] ) {
168
-			$this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" );
169
-
170
-			return $new_entity_uri;
171
-		}
172
-
173
-		// Otherwise the same function is called recursively
174
-		return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit );
175
-	}
176
-
177
-	/**
178
-	 *
179
-	 * @param string  $title A post title.
180
-	 * @param string  $taxonomy A post type. Default value is 'entity'
181
-	 * @param integer $increment_digit A digit used to call recursively the same function.
182
-	 *
183
-	 * @return string Returns an uri.
184
-	 * @since 3.32.0
185
-	 */
186
-	public function build_term_uri( $title, $taxonomy, $increment_digit = 0 ) {
187
-
188
-		// Get the entity slug suffix digit
189
-		$suffix_digit = $increment_digit + 1;
190
-
191
-		$entity_slug = urldecode( $title )
192
-					   . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
193
-
194
-		// Compose a candidate uri.
195
-		$new_entity_uri = sprintf(
196
-			'%s/term/%s/%s',
197
-			Wordlift_Configuration_Service::get_instance()->get_dataset_uri(),
198
-			$taxonomy,
199
-			$entity_slug
200
-		);
201
-
202
-		$this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
203
-
204
-		$content = Wordpress_Term_Content_Service::get_instance()->get_by_entity_id( $new_entity_uri );
205
-		$term_id = $content ? $content->get_bag()->term_id : null;
206
-
207
-		// If the post does not exist, then the new uri is returned
208
-		if ( ! is_numeric( $term_id ) ) {
209
-			$this->log->trace( "Going to return uri [ new_term_entity_uri :: $new_entity_uri ]" );
210
-
211
-			return $new_entity_uri;
212
-		}
213
-
214
-		// Otherwise the same function is called recursively
215
-		return $this->build_term_uri( $title, $taxonomy, ++ $increment_digit );
216
-	}
140
+        $entity_slug = urldecode( wl_sanitize_uri_path( $title ) )
141
+                        . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
142
+
143
+        // Compose a candidate uri.
144
+        $new_entity_uri = sprintf(
145
+            '%s/%s/%s',
146
+            untrailingslashit( wl_configuration_get_redlink_dataset_uri() ),
147
+            $post_type,
148
+            $entity_slug
149
+        );
150
+
151
+        $this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
152
+
153
+        $content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $new_entity_uri );
154
+        $post_id = $content && is_a( $content->get_bag(), '\WP_Post' ) ? $content->get_bag()->ID : null;
155
+
156
+        // If the post does not exist, then the new uri is returned
157
+        if ( ! is_numeric( $post_id ) ) {
158
+            $this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" );
159
+
160
+            return $new_entity_uri;
161
+        }
162
+
163
+        // If schema_type is equal to schema org type of post x, then the new uri is returned
164
+        $schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
165
+
166
+        // @todo: we shouldn't rely on css classes to take such decisions.
167
+        if ( $schema_type === $schema_post_type['css_class'] ) {
168
+            $this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" );
169
+
170
+            return $new_entity_uri;
171
+        }
172
+
173
+        // Otherwise the same function is called recursively
174
+        return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit );
175
+    }
176
+
177
+    /**
178
+     *
179
+     * @param string  $title A post title.
180
+     * @param string  $taxonomy A post type. Default value is 'entity'
181
+     * @param integer $increment_digit A digit used to call recursively the same function.
182
+     *
183
+     * @return string Returns an uri.
184
+     * @since 3.32.0
185
+     */
186
+    public function build_term_uri( $title, $taxonomy, $increment_digit = 0 ) {
187
+
188
+        // Get the entity slug suffix digit
189
+        $suffix_digit = $increment_digit + 1;
190
+
191
+        $entity_slug = urldecode( $title )
192
+                        . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
193
+
194
+        // Compose a candidate uri.
195
+        $new_entity_uri = sprintf(
196
+            '%s/term/%s/%s',
197
+            Wordlift_Configuration_Service::get_instance()->get_dataset_uri(),
198
+            $taxonomy,
199
+            $entity_slug
200
+        );
201
+
202
+        $this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
203
+
204
+        $content = Wordpress_Term_Content_Service::get_instance()->get_by_entity_id( $new_entity_uri );
205
+        $term_id = $content ? $content->get_bag()->term_id : null;
206
+
207
+        // If the post does not exist, then the new uri is returned
208
+        if ( ! is_numeric( $term_id ) ) {
209
+            $this->log->trace( "Going to return uri [ new_term_entity_uri :: $new_entity_uri ]" );
210
+
211
+            return $new_entity_uri;
212
+        }
213
+
214
+        // Otherwise the same function is called recursively
215
+        return $this->build_term_uri( $title, $taxonomy, ++ $increment_digit );
216
+    }
217 217
 
218 218
 }
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -67,9 +67,9 @@  discard block
 block discarded – undo
67 67
 	 *
68 68
 	 * @since 3.6.0
69 69
 	 */
70
-	public function __construct( $wpdb ) {
70
+	public function __construct($wpdb) {
71 71
 
72
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Uri_Service' );
72
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Uri_Service');
73 73
 
74 74
 		$this->wpdb = $wpdb;
75 75
 
@@ -100,13 +100,13 @@  discard block
 block discarded – undo
100 100
 	 *
101 101
 	 * @see   https://codex.wordpress.org/Function_Reference/sanitize_title
102 102
 	 */
103
-	public function sanitize_path( $path, $char = '_' ) {
103
+	public function sanitize_path($path, $char = '_') {
104 104
 
105 105
 		// Ensure the path is ASCII.
106 106
 		// see https://github.com/insideout10/wordlift-plugin/issues/386
107 107
 		// $path_ascii = mb_convert_encoding( $path, 'ASCII' );
108 108
 
109
-		return sanitize_title( preg_replace( self::INVALID_CHARACTERS, $char, stripslashes( $path ) ) );
109
+		return sanitize_title(preg_replace(self::INVALID_CHARACTERS, $char, stripslashes($path)));
110 110
 	}
111 111
 
112 112
 	/**
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
 	 * @deprecated don't use anywhere else, since we're refactoring the way URIs are built in the global function wl_build_entity_uri.
125 125
 	 * @since 3.5.0
126 126
 	 */
127
-	public function build_uri( $title, $post_type, $schema_type = null, $increment_digit = 0 ) {
127
+	public function build_uri($title, $post_type, $schema_type = null, $increment_digit = 0) {
128 128
 
129 129
 		// Get the entity slug suffix digit
130 130
 		$suffix_digit = $increment_digit + 1;
@@ -137,41 +137,41 @@  discard block
 block discarded – undo
137 137
 		 *
138 138
 		 * @since 3.20.0
139 139
 		 */
140
-		$entity_slug = urldecode( wl_sanitize_uri_path( $title ) )
141
-					   . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
140
+		$entity_slug = urldecode(wl_sanitize_uri_path($title))
141
+					   . (0 === $increment_digit ? '' : '_'.$suffix_digit);
142 142
 
143 143
 		// Compose a candidate uri.
144 144
 		$new_entity_uri = sprintf(
145 145
 			'%s/%s/%s',
146
-			untrailingslashit( wl_configuration_get_redlink_dataset_uri() ),
146
+			untrailingslashit(wl_configuration_get_redlink_dataset_uri()),
147 147
 			$post_type,
148 148
 			$entity_slug
149 149
 		);
150 150
 
151
-		$this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
151
+		$this->log->trace("Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]");
152 152
 
153
-		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $new_entity_uri );
154
-		$post_id = $content && is_a( $content->get_bag(), '\WP_Post' ) ? $content->get_bag()->ID : null;
153
+		$content = Wordpress_Content_Service::get_instance()->get_by_entity_id($new_entity_uri);
154
+		$post_id = $content && is_a($content->get_bag(), '\WP_Post') ? $content->get_bag()->ID : null;
155 155
 
156 156
 		// If the post does not exist, then the new uri is returned
157
-		if ( ! is_numeric( $post_id ) ) {
158
-			$this->log->trace( "Going to return uri [ new_entity_uri :: $new_entity_uri ]" );
157
+		if ( ! is_numeric($post_id)) {
158
+			$this->log->trace("Going to return uri [ new_entity_uri :: $new_entity_uri ]");
159 159
 
160 160
 			return $new_entity_uri;
161 161
 		}
162 162
 
163 163
 		// If schema_type is equal to schema org type of post x, then the new uri is returned
164
-		$schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id );
164
+		$schema_post_type = Wordlift_Entity_Type_Service::get_instance()->get($post_id);
165 165
 
166 166
 		// @todo: we shouldn't rely on css classes to take such decisions.
167
-		if ( $schema_type === $schema_post_type['css_class'] ) {
168
-			$this->log->trace( "An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]" );
167
+		if ($schema_type === $schema_post_type['css_class']) {
168
+			$this->log->trace("An entity with the same title and type already exists! Return uri [ new_entity_uri :: $new_entity_uri ]");
169 169
 
170 170
 			return $new_entity_uri;
171 171
 		}
172 172
 
173 173
 		// Otherwise the same function is called recursively
174
-		return $this->build_uri( $title, $post_type, $schema_type, ++ $increment_digit );
174
+		return $this->build_uri($title, $post_type, $schema_type, ++ $increment_digit);
175 175
 	}
176 176
 
177 177
 	/**
@@ -183,13 +183,13 @@  discard block
 block discarded – undo
183 183
 	 * @return string Returns an uri.
184 184
 	 * @since 3.32.0
185 185
 	 */
186
-	public function build_term_uri( $title, $taxonomy, $increment_digit = 0 ) {
186
+	public function build_term_uri($title, $taxonomy, $increment_digit = 0) {
187 187
 
188 188
 		// Get the entity slug suffix digit
189 189
 		$suffix_digit = $increment_digit + 1;
190 190
 
191
-		$entity_slug = urldecode( $title )
192
-					   . ( 0 === $increment_digit ? '' : '_' . $suffix_digit );
191
+		$entity_slug = urldecode($title)
192
+					   . (0 === $increment_digit ? '' : '_'.$suffix_digit);
193 193
 
194 194
 		// Compose a candidate uri.
195 195
 		$new_entity_uri = sprintf(
@@ -199,20 +199,20 @@  discard block
 block discarded – undo
199 199
 			$entity_slug
200 200
 		);
201 201
 
202
-		$this->log->trace( "Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]" );
202
+		$this->log->trace("Going to check if uri is used [ new_entity_uri :: $new_entity_uri ] [ increment_digit :: $increment_digit ]");
203 203
 
204
-		$content = Wordpress_Term_Content_Service::get_instance()->get_by_entity_id( $new_entity_uri );
204
+		$content = Wordpress_Term_Content_Service::get_instance()->get_by_entity_id($new_entity_uri);
205 205
 		$term_id = $content ? $content->get_bag()->term_id : null;
206 206
 
207 207
 		// If the post does not exist, then the new uri is returned
208
-		if ( ! is_numeric( $term_id ) ) {
209
-			$this->log->trace( "Going to return uri [ new_term_entity_uri :: $new_entity_uri ]" );
208
+		if ( ! is_numeric($term_id)) {
209
+			$this->log->trace("Going to return uri [ new_term_entity_uri :: $new_entity_uri ]");
210 210
 
211 211
 			return $new_entity_uri;
212 212
 		}
213 213
 
214 214
 		// Otherwise the same function is called recursively
215
-		return $this->build_term_uri( $title, $taxonomy, ++ $increment_digit );
215
+		return $this->build_term_uri($title, $taxonomy, ++ $increment_digit);
216 216
 	}
217 217
 
218 218
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-batch-action.php 2 patches
Indentation   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -8,61 +8,61 @@
 block discarded – undo
8 8
 
9 9
 class Wordlift_Batch_Action {
10 10
 
11
-	public static function process( $post_type, $offset, $query, $callback ) {
11
+    public static function process( $post_type, $offset, $query, $callback ) {
12 12
 
13
-		$posts_per_page = 10;
13
+        $posts_per_page = 10;
14 14
 
15
-		$args = array_merge(
16
-			self::get_args( $post_type, $query ),
17
-			array(
18
-				'offset'         => $offset,
19
-				'posts_per_page' => $posts_per_page,
20
-			)
21
-		);
15
+        $args = array_merge(
16
+            self::get_args( $post_type, $query ),
17
+            array(
18
+                'offset'         => $offset,
19
+                'posts_per_page' => $posts_per_page,
20
+            )
21
+        );
22 22
 
23
-		$post_ids = get_posts( $args );
23
+        $post_ids = get_posts( $args );
24 24
 
25
-		foreach ( $post_ids as $post_id ) {
26
-			call_user_func( $callback, $post_id );
27
-		}
25
+        foreach ( $post_ids as $post_id ) {
26
+            call_user_func( $callback, $post_id );
27
+        }
28 28
 
29
-		return array(
30
-			'current' => $offset,
31
-			'next'    => $offset + $posts_per_page,
32
-			'count'   => self::count( $post_type, $query ),
33
-		);
34
-	}
29
+        return array(
30
+            'current' => $offset,
31
+            'next'    => $offset + $posts_per_page,
32
+            'count'   => self::count( $post_type, $query ),
33
+        );
34
+    }
35 35
 
36
-	public static function count( $post_type, $query ) {
37
-		$args = array_merge(
38
-			self::get_args( $post_type, $query ),
39
-			array(
40
-				'posts_per_page' => - 1,
41
-			)
42
-		);
36
+    public static function count( $post_type, $query ) {
37
+        $args = array_merge(
38
+            self::get_args( $post_type, $query ),
39
+            array(
40
+                'posts_per_page' => - 1,
41
+            )
42
+        );
43 43
 
44
-		return count( get_posts( $args ) );
45
-	}
44
+        return count( get_posts( $args ) );
45
+    }
46 46
 
47
-	private static function get_args( $post_type, $query ) {
47
+    private static function get_args( $post_type, $query ) {
48 48
 
49
-		return array_merge(
50
-			array(
51
-				'fields'        => 'ids',
52
-				'post_type'     => $post_type,
53
-				'post_status'   => array(
54
-					'publish',
55
-					'future',
56
-					'draft',
57
-					'pending',
58
-					'private',
59
-					'auto-draft',
60
-					'inherit',
61
-				),
62
-				'cache_results' => false,
63
-			),
64
-			$query
65
-		);
66
-	}
49
+        return array_merge(
50
+            array(
51
+                'fields'        => 'ids',
52
+                'post_type'     => $post_type,
53
+                'post_status'   => array(
54
+                    'publish',
55
+                    'future',
56
+                    'draft',
57
+                    'pending',
58
+                    'private',
59
+                    'auto-draft',
60
+                    'inherit',
61
+                ),
62
+                'cache_results' => false,
63
+            ),
64
+            $query
65
+        );
66
+    }
67 67
 
68 68
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -8,43 +8,43 @@
 block discarded – undo
8 8
 
9 9
 class Wordlift_Batch_Action {
10 10
 
11
-	public static function process( $post_type, $offset, $query, $callback ) {
11
+	public static function process($post_type, $offset, $query, $callback) {
12 12
 
13 13
 		$posts_per_page = 10;
14 14
 
15 15
 		$args = array_merge(
16
-			self::get_args( $post_type, $query ),
16
+			self::get_args($post_type, $query),
17 17
 			array(
18 18
 				'offset'         => $offset,
19 19
 				'posts_per_page' => $posts_per_page,
20 20
 			)
21 21
 		);
22 22
 
23
-		$post_ids = get_posts( $args );
23
+		$post_ids = get_posts($args);
24 24
 
25
-		foreach ( $post_ids as $post_id ) {
26
-			call_user_func( $callback, $post_id );
25
+		foreach ($post_ids as $post_id) {
26
+			call_user_func($callback, $post_id);
27 27
 		}
28 28
 
29 29
 		return array(
30 30
 			'current' => $offset,
31 31
 			'next'    => $offset + $posts_per_page,
32
-			'count'   => self::count( $post_type, $query ),
32
+			'count'   => self::count($post_type, $query),
33 33
 		);
34 34
 	}
35 35
 
36
-	public static function count( $post_type, $query ) {
36
+	public static function count($post_type, $query) {
37 37
 		$args = array_merge(
38
-			self::get_args( $post_type, $query ),
38
+			self::get_args($post_type, $query),
39 39
 			array(
40
-				'posts_per_page' => - 1,
40
+				'posts_per_page' => -1,
41 41
 			)
42 42
 		);
43 43
 
44
-		return count( get_posts( $args ) );
44
+		return count(get_posts($args));
45 45
 	}
46 46
 
47
-	private static function get_args( $post_type, $query ) {
47
+	private static function get_args($post_type, $query) {
48 48
 
49 49
 		return array_merge(
50 50
 			array(
Please login to merge, or discard this patch.
src/includes/class-wordlift-sanitizer.php 2 patches
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -2,26 +2,26 @@
 block discarded – undo
2 2
 
3 3
 class Wordlift_Sanitizer {
4 4
 
5
-	/**
6
-	 * Only accept URIs
7
-	 *
8
-	 * @param string $value
9
-	 *
10
-	 * @return null
11
-	 */
12
-	public static function sanitize_url( $value ) {
5
+    /**
6
+     * Only accept URIs
7
+     *
8
+     * @param string $value
9
+     *
10
+     * @return null
11
+     */
12
+    public static function sanitize_url( $value ) {
13 13
 
14
-		// Initially this function used also filter_var( $value, FILTER_VALIDATE_URL )
15
-		// but URLs with UTF-8 characters are not valid. We store those anyway in the DB as it's up to the browser
16
-		// to do proper url encoding when requesting the URL.
17
-		//
18
-		// see also http://stackoverflow.com/questions/2137080/php-filter-var-filter-validate-url
14
+        // Initially this function used also filter_var( $value, FILTER_VALIDATE_URL )
15
+        // but URLs with UTF-8 characters are not valid. We store those anyway in the DB as it's up to the browser
16
+        // to do proper url encoding when requesting the URL.
17
+        //
18
+        // see also http://stackoverflow.com/questions/2137080/php-filter-var-filter-validate-url
19 19
 
20
-		if ( $value !== null && '' !== $value ) {
21
-			return $value;
22
-		}
20
+        if ( $value !== null && '' !== $value ) {
21
+            return $value;
22
+        }
23 23
 
24
-		return null;
25
-	}
24
+        return null;
25
+    }
26 26
 
27 27
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -9,7 +9,7 @@  discard block
 block discarded – undo
9 9
 	 *
10 10
 	 * @return null
11 11
 	 */
12
-	public static function sanitize_url( $value ) {
12
+	public static function sanitize_url($value) {
13 13
 
14 14
 		// Initially this function used also filter_var( $value, FILTER_VALIDATE_URL )
15 15
 		// but URLs with UTF-8 characters are not valid. We store those anyway in the DB as it's up to the browser
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
 		//
18 18
 		// see also http://stackoverflow.com/questions/2137080/php-filter-var-filter-validate-url
19 19
 
20
-		if ( $value !== null && '' !== $value ) {
20
+		if ($value !== null && '' !== $value) {
21 21
 			return $value;
22 22
 		}
23 23
 
Please login to merge, or discard this patch.
src/includes/class-wordlift-entity-link-service.php 2 patches
Indentation   +184 added lines, -184 removed lines patch added patch discarded remove patch
@@ -25,194 +25,194 @@
 block discarded – undo
25 25
  */
26 26
 class Wordlift_Entity_Link_Service {
27 27
 
28
-	/**
29
-	 * The entity type service.
30
-	 *
31
-	 * @since  3.6.0
32
-	 * @access private
33
-	 * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service.
34
-	 */
35
-	private $entity_type_service;
36
-
37
-	/**
38
-	 * The entity post type slug.
39
-	 *
40
-	 * @since  3.6.0
41
-	 * @access private
42
-	 * @var string $slug The entity post type slug.
43
-	 */
44
-	private $slug;
45
-
46
-	/**
47
-	 * A logger instance.
48
-	 *
49
-	 * @since  3.6.0
50
-	 * @access private
51
-	 * @var Wordlift_Log_Service
52
-	 */
53
-	private $log;
54
-
55
-	/**
56
-	 * Wordlift_Entity_Link_Service constructor.
57
-	 *
58
-	 * @since 3.6.0
59
-	 *
60
-	 * @param Wordlift_Entity_Post_Type_Service $entity_type_service
61
-	 * @param string                            $slug The entity post type slug.
62
-	 */
63
-	public function __construct( $entity_type_service, $slug ) {
64
-
65
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
66
-
67
-		$this->entity_type_service = $entity_type_service;
68
-		$this->slug                = $slug;
69
-
70
-	}
71
-
72
-	/**
73
-	 * Intercept link generation to posts in order to customize links to entities.
74
-	 *
75
-	 * @since 3.6.0
76
-	 *
77
-	 * @param string  $post_link The post's permalink.
78
-	 * @param WP_Post $post      The post in question.
79
-	 *
80
-	 * @return string The link to the post.
81
-	 */
82
-	public function post_type_link( $post_link, $post ) {
83
-
84
-		// Return the post link if this is not our post type.
85
-		if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
86
-			return $post_link;
87
-		}
88
-
89
-		// Replace /slug/post_name/ with /post_name/
90
-		// The slug comes from the Entity Type Service since that service is responsible for registering the default
91
-		// slug.
92
-		return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
93
-	}
94
-
95
-	/**
96
-	 * Alter the query to look for our own custom type.
97
-	 *
98
-	 * @since 3.6.0
99
-	 *
100
-	 * @param WP_Query $query
101
-	 */
102
-	public function pre_get_posts( $query ) {
103
-
104
-		// If a slug has been set, we don't need to alter the query.
105
-		if ( ! empty( $this->slug ) ) {
106
-			return;
107
-		}
108
-
109
-		// Check if it's a query we should extend with our own custom post type.
110
-		//
111
-		// The `$query->query` count could be > 2 if the preview parameter is passed too.
112
-		//
113
-		// See https://github.com/insideout10/wordlift-plugin/issues/439
114
-		if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
115
-			return;
116
-		}
117
-
118
-		// Add our own post type to the query.
119
-		$post_types = '' === $query->get( 'post_type' )
120
-			? Wordlift_Entity_Service::valid_entity_post_types()
121
-			: array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() );
122
-		$query->set( 'post_type', $post_types );
123
-
124
-	}
125
-
126
-	/**
127
-	 * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved.
128
-	 *
129
-	 * @since 3.6.0
130
-	 *
131
-	 * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
132
-	 * @param string $slug      The post slug.
133
-	 * @param string $post_type Post type.
134
-	 *
135
-	 * @return bool Whether the slug is bad.
136
-	 */
137
-	public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
138
-
139
-		// The list of post types that might have conflicting slugs.
140
-		$post_types = Wordlift_Entity_Service::valid_entity_post_types();
141
-
142
-		// Ignore post types different from the ones we need to check.
143
-		if ( ! in_array( $post_type, $post_types, true ) ) {
144
-			return $bad_slug;
145
-		}
146
-
147
-		// We remove the request post type since WordPress is already checking that the slug doesn't conflict.
148
-		$exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
149
-
150
-		$this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' );
151
-
152
-		return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type );
153
-	}
154
-
155
-	/**
156
-	 * Check whether a slug exists already for the specified post types.
157
-	 *
158
-	 * @since 3.6.0
159
-	 *
160
-	 * @param string $slug       The slug.
161
-	 * @param array  $post_types An array of post types.
162
-	 *
163
-	 * @return bool True if the slug exists, otherwise false.
164
-	 */
165
-	private function slug_exists( $slug, $post_types ) {
166
-		global $wpdb;
167
-
168
-		// Loop through all post types and check
169
-		// whether they have archive pages and if
170
-		// the archive slug matches the post slug.
171
-		//
172
-		// Note that the condition below checks only post types used by WordLift.
173
-		// We don't check other post types for archive pages,
174
-		// because this is a job of WordPress.
175
-		//
176
-		// There is a open ticket that should solve this, when it's merged:
177
-		// https://core.trac.wordpress.org/ticket/13459
178
-		$all_post_types = Wordlift_Entity_Service::valid_entity_post_types();
179
-		foreach ( $all_post_types as $post_type ) {
180
-
181
-			// Get the post type object for current post type.
182
-			$post_type_object = get_post_type_object( $post_type );
183
-
184
-			if (
185
-				// Check whether the post type object is not empty.
186
-				! empty( $post_type_object ) &&
187
-				// And the post type has archive page.
188
-				$post_type_object->has_archive &&
189
-				// And `rewrite` options exists..
190
-				! empty( $post_type_object->rewrite ) &&
191
-				// And the `rewrite` slug property is not empty.
192
-				! empty( $post_type_object->rewrite['slug'] ) &&
193
-				// And if the rewrite slug equals to the slug.
194
-				$post_type_object->rewrite['slug'] === $slug
195
-			) {
196
-				// Return true which means that the slug is already in use.
197
-				return true;
198
-			}
199
-		}
200
-
201
-		return null !== $wpdb->get_var(
202
-			$wpdb->prepare(
203
-				sprintf(
204
-					"SELECT post_name
28
+    /**
29
+     * The entity type service.
30
+     *
31
+     * @since  3.6.0
32
+     * @access private
33
+     * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service.
34
+     */
35
+    private $entity_type_service;
36
+
37
+    /**
38
+     * The entity post type slug.
39
+     *
40
+     * @since  3.6.0
41
+     * @access private
42
+     * @var string $slug The entity post type slug.
43
+     */
44
+    private $slug;
45
+
46
+    /**
47
+     * A logger instance.
48
+     *
49
+     * @since  3.6.0
50
+     * @access private
51
+     * @var Wordlift_Log_Service
52
+     */
53
+    private $log;
54
+
55
+    /**
56
+     * Wordlift_Entity_Link_Service constructor.
57
+     *
58
+     * @since 3.6.0
59
+     *
60
+     * @param Wordlift_Entity_Post_Type_Service $entity_type_service
61
+     * @param string                            $slug The entity post type slug.
62
+     */
63
+    public function __construct( $entity_type_service, $slug ) {
64
+
65
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
66
+
67
+        $this->entity_type_service = $entity_type_service;
68
+        $this->slug                = $slug;
69
+
70
+    }
71
+
72
+    /**
73
+     * Intercept link generation to posts in order to customize links to entities.
74
+     *
75
+     * @since 3.6.0
76
+     *
77
+     * @param string  $post_link The post's permalink.
78
+     * @param WP_Post $post      The post in question.
79
+     *
80
+     * @return string The link to the post.
81
+     */
82
+    public function post_type_link( $post_link, $post ) {
83
+
84
+        // Return the post link if this is not our post type.
85
+        if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
86
+            return $post_link;
87
+        }
88
+
89
+        // Replace /slug/post_name/ with /post_name/
90
+        // The slug comes from the Entity Type Service since that service is responsible for registering the default
91
+        // slug.
92
+        return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
93
+    }
94
+
95
+    /**
96
+     * Alter the query to look for our own custom type.
97
+     *
98
+     * @since 3.6.0
99
+     *
100
+     * @param WP_Query $query
101
+     */
102
+    public function pre_get_posts( $query ) {
103
+
104
+        // If a slug has been set, we don't need to alter the query.
105
+        if ( ! empty( $this->slug ) ) {
106
+            return;
107
+        }
108
+
109
+        // Check if it's a query we should extend with our own custom post type.
110
+        //
111
+        // The `$query->query` count could be > 2 if the preview parameter is passed too.
112
+        //
113
+        // See https://github.com/insideout10/wordlift-plugin/issues/439
114
+        if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
115
+            return;
116
+        }
117
+
118
+        // Add our own post type to the query.
119
+        $post_types = '' === $query->get( 'post_type' )
120
+            ? Wordlift_Entity_Service::valid_entity_post_types()
121
+            : array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() );
122
+        $query->set( 'post_type', $post_types );
123
+
124
+    }
125
+
126
+    /**
127
+     * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved.
128
+     *
129
+     * @since 3.6.0
130
+     *
131
+     * @param bool   $bad_slug  Whether the post slug would be bad as a flat slug.
132
+     * @param string $slug      The post slug.
133
+     * @param string $post_type Post type.
134
+     *
135
+     * @return bool Whether the slug is bad.
136
+     */
137
+    public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
138
+
139
+        // The list of post types that might have conflicting slugs.
140
+        $post_types = Wordlift_Entity_Service::valid_entity_post_types();
141
+
142
+        // Ignore post types different from the ones we need to check.
143
+        if ( ! in_array( $post_type, $post_types, true ) ) {
144
+            return $bad_slug;
145
+        }
146
+
147
+        // We remove the request post type since WordPress is already checking that the slug doesn't conflict.
148
+        $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
149
+
150
+        $this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' );
151
+
152
+        return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type );
153
+    }
154
+
155
+    /**
156
+     * Check whether a slug exists already for the specified post types.
157
+     *
158
+     * @since 3.6.0
159
+     *
160
+     * @param string $slug       The slug.
161
+     * @param array  $post_types An array of post types.
162
+     *
163
+     * @return bool True if the slug exists, otherwise false.
164
+     */
165
+    private function slug_exists( $slug, $post_types ) {
166
+        global $wpdb;
167
+
168
+        // Loop through all post types and check
169
+        // whether they have archive pages and if
170
+        // the archive slug matches the post slug.
171
+        //
172
+        // Note that the condition below checks only post types used by WordLift.
173
+        // We don't check other post types for archive pages,
174
+        // because this is a job of WordPress.
175
+        //
176
+        // There is a open ticket that should solve this, when it's merged:
177
+        // https://core.trac.wordpress.org/ticket/13459
178
+        $all_post_types = Wordlift_Entity_Service::valid_entity_post_types();
179
+        foreach ( $all_post_types as $post_type ) {
180
+
181
+            // Get the post type object for current post type.
182
+            $post_type_object = get_post_type_object( $post_type );
183
+
184
+            if (
185
+                // Check whether the post type object is not empty.
186
+                ! empty( $post_type_object ) &&
187
+                // And the post type has archive page.
188
+                $post_type_object->has_archive &&
189
+                // And `rewrite` options exists..
190
+                ! empty( $post_type_object->rewrite ) &&
191
+                // And the `rewrite` slug property is not empty.
192
+                ! empty( $post_type_object->rewrite['slug'] ) &&
193
+                // And if the rewrite slug equals to the slug.
194
+                $post_type_object->rewrite['slug'] === $slug
195
+            ) {
196
+                // Return true which means that the slug is already in use.
197
+                return true;
198
+            }
199
+        }
200
+
201
+        return null !== $wpdb->get_var(
202
+            $wpdb->prepare(
203
+                sprintf(
204
+                    "SELECT post_name
205 205
 			FROM $wpdb->posts
206 206
 			WHERE post_name = %s
207 207
 			AND post_type IN (%s)
208 208
 			LIMIT 1
209 209
 			",
210
-					'%s',
211
-					implode( ',', array_fill( 0, count( $post_types ), '%s' ) )
212
-				),
213
-				array_merge( array( $slug ), $post_types )
214
-			)
215
-		);
216
-	}
210
+                    '%s',
211
+                    implode( ',', array_fill( 0, count( $post_types ), '%s' ) )
212
+                ),
213
+                array_merge( array( $slug ), $post_types )
214
+            )
215
+        );
216
+    }
217 217
 
218 218
 }
Please login to merge, or discard this patch.
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -60,9 +60,9 @@  discard block
 block discarded – undo
60 60
 	 * @param Wordlift_Entity_Post_Type_Service $entity_type_service
61 61
 	 * @param string                            $slug The entity post type slug.
62 62
 	 */
63
-	public function __construct( $entity_type_service, $slug ) {
63
+	public function __construct($entity_type_service, $slug) {
64 64
 
65
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' );
65
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Link_Service');
66 66
 
67 67
 		$this->entity_type_service = $entity_type_service;
68 68
 		$this->slug                = $slug;
@@ -79,17 +79,17 @@  discard block
 block discarded – undo
79 79
 	 *
80 80
 	 * @return string The link to the post.
81 81
 	 */
82
-	public function post_type_link( $post_link, $post ) {
82
+	public function post_type_link($post_link, $post) {
83 83
 
84 84
 		// Return the post link if this is not our post type.
85
-		if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) {
85
+		if ( ! empty($this->slug) || $this->entity_type_service->get_post_type() !== get_post_type($post)) {
86 86
 			return $post_link;
87 87
 		}
88 88
 
89 89
 		// Replace /slug/post_name/ with /post_name/
90 90
 		// The slug comes from the Entity Type Service since that service is responsible for registering the default
91 91
 		// slug.
92
-		return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link );
92
+		return str_replace("/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link);
93 93
 	}
94 94
 
95 95
 	/**
@@ -99,10 +99,10 @@  discard block
 block discarded – undo
99 99
 	 *
100 100
 	 * @param WP_Query $query
101 101
 	 */
102
-	public function pre_get_posts( $query ) {
102
+	public function pre_get_posts($query) {
103 103
 
104 104
 		// If a slug has been set, we don't need to alter the query.
105
-		if ( ! empty( $this->slug ) ) {
105
+		if ( ! empty($this->slug)) {
106 106
 			return;
107 107
 		}
108 108
 
@@ -111,15 +111,15 @@  discard block
 block discarded – undo
111 111
 		// The `$query->query` count could be > 2 if the preview parameter is passed too.
112 112
 		//
113 113
 		// See https://github.com/insideout10/wordlift-plugin/issues/439
114
-		if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) {
114
+		if ( ! $query->is_main_query() || 2 > count($query->query) || ! isset($query->query['page']) || empty($query->query['name'])) {
115 115
 			return;
116 116
 		}
117 117
 
118 118
 		// Add our own post type to the query.
119
-		$post_types = '' === $query->get( 'post_type' )
119
+		$post_types = '' === $query->get('post_type')
120 120
 			? Wordlift_Entity_Service::valid_entity_post_types()
121
-			: array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() );
122
-		$query->set( 'post_type', $post_types );
121
+			: array_merge((array) $query->get('post_type'), (array) $this->entity_type_service->get_post_type());
122
+		$query->set('post_type', $post_types);
123 123
 
124 124
 	}
125 125
 
@@ -134,22 +134,22 @@  discard block
 block discarded – undo
134 134
 	 *
135 135
 	 * @return bool Whether the slug is bad.
136 136
 	 */
137
-	public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) {
137
+	public function wp_unique_post_slug_is_bad_flat_slug($bad_slug, $slug, $post_type) {
138 138
 
139 139
 		// The list of post types that might have conflicting slugs.
140 140
 		$post_types = Wordlift_Entity_Service::valid_entity_post_types();
141 141
 
142 142
 		// Ignore post types different from the ones we need to check.
143
-		if ( ! in_array( $post_type, $post_types, true ) ) {
143
+		if ( ! in_array($post_type, $post_types, true)) {
144 144
 			return $bad_slug;
145 145
 		}
146 146
 
147 147
 		// We remove the request post type since WordPress is already checking that the slug doesn't conflict.
148
-		$exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) );
148
+		$exists = $this->slug_exists($slug, array_diff($post_types, array($post_type)));
149 149
 
150
-		$this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' );
150
+		$this->log->debug("Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: ".($exists ? 'yes' : 'no').' ]');
151 151
 
152
-		return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type );
152
+		return apply_filters('wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type);
153 153
 	}
154 154
 
155 155
 	/**
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 	 *
163 163
 	 * @return bool True if the slug exists, otherwise false.
164 164
 	 */
165
-	private function slug_exists( $slug, $post_types ) {
165
+	private function slug_exists($slug, $post_types) {
166 166
 		global $wpdb;
167 167
 
168 168
 		// Loop through all post types and check
@@ -176,20 +176,20 @@  discard block
 block discarded – undo
176 176
 		// There is a open ticket that should solve this, when it's merged:
177 177
 		// https://core.trac.wordpress.org/ticket/13459
178 178
 		$all_post_types = Wordlift_Entity_Service::valid_entity_post_types();
179
-		foreach ( $all_post_types as $post_type ) {
179
+		foreach ($all_post_types as $post_type) {
180 180
 
181 181
 			// Get the post type object for current post type.
182
-			$post_type_object = get_post_type_object( $post_type );
182
+			$post_type_object = get_post_type_object($post_type);
183 183
 
184 184
 			if (
185 185
 				// Check whether the post type object is not empty.
186
-				! empty( $post_type_object ) &&
186
+				! empty($post_type_object) &&
187 187
 				// And the post type has archive page.
188 188
 				$post_type_object->has_archive &&
189 189
 				// And `rewrite` options exists..
190
-				! empty( $post_type_object->rewrite ) &&
190
+				! empty($post_type_object->rewrite) &&
191 191
 				// And the `rewrite` slug property is not empty.
192
-				! empty( $post_type_object->rewrite['slug'] ) &&
192
+				! empty($post_type_object->rewrite['slug']) &&
193 193
 				// And if the rewrite slug equals to the slug.
194 194
 				$post_type_object->rewrite['slug'] === $slug
195 195
 			) {
@@ -208,9 +208,9 @@  discard block
 block discarded – undo
208 208
 			LIMIT 1
209 209
 			",
210 210
 					'%s',
211
-					implode( ',', array_fill( 0, count( $post_types ), '%s' ) )
211
+					implode(',', array_fill(0, count($post_types), '%s'))
212 212
 				),
213
-				array_merge( array( $slug ), $post_types )
213
+				array_merge(array($slug), $post_types)
214 214
 			)
215 215
 		);
216 216
 	}
Please login to merge, or discard this patch.
src/includes/class-wordlift-ui-service.php 2 patches
Indentation   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -6,82 +6,82 @@
 block discarded – undo
6 6
  */
7 7
 class Wordlift_UI_Service {
8 8
 
9
-	/**
10
-	 * The button element HTML code.
11
-	 *
12
-	 * @since 3.2.0
13
-	 */
14
-	const BUTTON_HTML = '<a id="%s" class="button wl-button">%s</a>';
9
+    /**
10
+     * The button element HTML code.
11
+     *
12
+     * @since 3.2.0
13
+     */
14
+    const BUTTON_HTML = '<a id="%s" class="button wl-button">%s</a>';
15 15
 
16
-	/**
17
-	 * The template HTML code.
18
-	 *
19
-	 * @since 3.2.0
20
-	 */
21
-	const TEMPLATE_HTML = '<script id="%s" type="text/template">%s</script>';
16
+    /**
17
+     * The template HTML code.
18
+     *
19
+     * @since 3.2.0
20
+     */
21
+    const TEMPLATE_HTML = '<script id="%s" type="text/template">%s</script>';
22 22
 
23
-	/**
24
-	 * Get the button HTML.
25
-	 *
26
-	 * @param string $element_id The button element id.
27
-	 * @param string $label The button (translated) label.
28
-	 *
29
-	 * @return string The button HTML code.
30
-	 * @since 3.2.0
31
-	 */
32
-	public static function get_button_html( $element_id, $label ) {
23
+    /**
24
+     * Get the button HTML.
25
+     *
26
+     * @param string $element_id The button element id.
27
+     * @param string $label The button (translated) label.
28
+     *
29
+     * @return string The button HTML code.
30
+     * @since 3.2.0
31
+     */
32
+    public static function get_button_html( $element_id, $label ) {
33 33
 
34
-		return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) );
35
-	}
34
+        return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) );
35
+    }
36 36
 
37
-	/**
38
-	 * Echo the button HTML.
39
-	 *
40
-	 * @param string $element_id The button element id.
41
-	 * @param string $label The button (translated) label.
42
-	 *
43
-	 * @since 3.2.0
44
-	 */
45
-	public static function print_button( $element_id, $label ) {
37
+    /**
38
+     * Echo the button HTML.
39
+     *
40
+     * @param string $element_id The button element id.
41
+     * @param string $label The button (translated) label.
42
+     *
43
+     * @since 3.2.0
44
+     */
45
+    public static function print_button( $element_id, $label ) {
46 46
 
47
-		echo wp_kses(
48
-			self::get_button_html( $element_id, $label ),
49
-			array(
50
-				'a' => array(
51
-					'id'    => array(),
52
-					'class' => array(),
53
-				),
54
-			)
55
-		);
47
+        echo wp_kses(
48
+            self::get_button_html( $element_id, $label ),
49
+            array(
50
+                'a' => array(
51
+                    'id'    => array(),
52
+                    'class' => array(),
53
+                ),
54
+            )
55
+        );
56 56
 
57
-	}
57
+    }
58 58
 
59
-	/**
60
-	 * Get the HTML code for a template tag.
61
-	 *
62
-	 * @param string $element_id The element id.
63
-	 * @param string $body The element content.
64
-	 *
65
-	 * @return string The HTML code.
66
-	 * @since 3.2.0
67
-	 */
68
-	public static function get_template_html( $element_id, $body ) {
59
+    /**
60
+     * Get the HTML code for a template tag.
61
+     *
62
+     * @param string $element_id The element id.
63
+     * @param string $body The element content.
64
+     *
65
+     * @return string The HTML code.
66
+     * @since 3.2.0
67
+     */
68
+    public static function get_template_html( $element_id, $body ) {
69 69
 
70
-		return sprintf( self::TEMPLATE_HTML, $element_id, $body );
71
-	}
70
+        return sprintf( self::TEMPLATE_HTML, $element_id, $body );
71
+    }
72 72
 
73
-	/**
74
-	 * Echo the HTML code for a template tag.
75
-	 *
76
-	 * @param string $element_id The element id.
77
-	 * @param string $body The element content.
78
-	 *
79
-	 * @since 3.2.0
80
-	 */
81
-	public static function print_template( $element_id, $body ) {
73
+    /**
74
+     * Echo the HTML code for a template tag.
75
+     *
76
+     * @param string $element_id The element id.
77
+     * @param string $body The element content.
78
+     *
79
+     * @since 3.2.0
80
+     */
81
+    public static function print_template( $element_id, $body ) {
82 82
 
83
-		echo( self::get_template_html( $element_id, $body ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
83
+        echo( self::get_template_html( $element_id, $body ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
84 84
 
85
-	}
85
+    }
86 86
 
87 87
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -29,9 +29,9 @@  discard block
 block discarded – undo
29 29
 	 * @return string The button HTML code.
30 30
 	 * @since 3.2.0
31 31
 	 */
32
-	public static function get_button_html( $element_id, $label ) {
32
+	public static function get_button_html($element_id, $label) {
33 33
 
34
-		return sprintf( self::BUTTON_HTML, $element_id, esc_html( $label ) );
34
+		return sprintf(self::BUTTON_HTML, $element_id, esc_html($label));
35 35
 	}
36 36
 
37 37
 	/**
@@ -42,10 +42,10 @@  discard block
 block discarded – undo
42 42
 	 *
43 43
 	 * @since 3.2.0
44 44
 	 */
45
-	public static function print_button( $element_id, $label ) {
45
+	public static function print_button($element_id, $label) {
46 46
 
47 47
 		echo wp_kses(
48
-			self::get_button_html( $element_id, $label ),
48
+			self::get_button_html($element_id, $label),
49 49
 			array(
50 50
 				'a' => array(
51 51
 					'id'    => array(),
@@ -65,9 +65,9 @@  discard block
 block discarded – undo
65 65
 	 * @return string The HTML code.
66 66
 	 * @since 3.2.0
67 67
 	 */
68
-	public static function get_template_html( $element_id, $body ) {
68
+	public static function get_template_html($element_id, $body) {
69 69
 
70
-		return sprintf( self::TEMPLATE_HTML, $element_id, $body );
70
+		return sprintf(self::TEMPLATE_HTML, $element_id, $body);
71 71
 	}
72 72
 
73 73
 	/**
@@ -78,9 +78,9 @@  discard block
 block discarded – undo
78 78
 	 *
79 79
 	 * @since 3.2.0
80 80
 	 */
81
-	public static function print_template( $element_id, $body ) {
81
+	public static function print_template($element_id, $body) {
82 82
 
83
-		echo( self::get_template_html( $element_id, $body ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
83
+		echo(self::get_template_html($element_id, $body)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
84 84
 
85 85
 	}
86 86
 
Please login to merge, or discard this patch.
src/includes/batch/class-wordlift-batch-operation-ajax-adapter.php 2 patches
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -14,143 +14,143 @@
 block discarded – undo
14 14
  */
15 15
 class Wordlift_Batch_Operation_Ajax_Adapter {
16 16
 
17
-	/**
18
-	 * The access levels.
19
-	 *
20
-	 * @since 3.20.0
21
-	 */
22
-	const ACCESS_ANONYMOUS = 1;
23
-	const ACCESS_ADMIN     = 2;
24
-	const ACCESS_ALL       = 3;
25
-
26
-	/**
27
-	 * A {@link Wordlift_Batch_Operation_Interface} instance.
28
-	 *
29
-	 * @since 3.20.0
30
-	 * @access private
31
-	 * @var \Wordlift_Batch_Operation_Interface $operation A {@link Wordlift_Batch_Operation_Interface} instance.
32
-	 */
33
-	private $operation;
34
-
35
-	/**
36
-	 * The ajax action name.
37
-	 *
38
-	 * @since 3.20.0
39
-	 * @access private
40
-	 * @var string $action The ajax action name.
41
-	 */
42
-	private $action;
43
-
44
-	/**
45
-	 * Wordlift_Batch_Operation_Ajax_Adapter constructor.
46
-	 *
47
-	 * @param \Wordlift_Batch_Operation_Interface $operation The batch operation.
48
-	 * @param string                              $action The action name.
49
-	 * @param int                                 $access The access level.
50
-	 */
51
-	public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) {
52
-
53
-		$this->operation = $operation;
54
-
55
-		if ( $access & self::ACCESS_ADMIN ) {
56
-			add_action( "wp_ajax_$action", array( $this, 'process' ) );
57
-			add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) );
58
-
59
-			// Add the nonce for the `schemaorg_sync` action.
60
-			add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) );
61
-		}
62
-
63
-		if ( $access & self::ACCESS_ANONYMOUS ) {
64
-			add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) );
65
-			add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) );
66
-		}
67
-
68
-		$this->action = $action;
69
-	}
70
-
71
-	/**
72
-	 * Hook to `wl_admin_settings`, adds the nonce.
73
-	 *
74
-	 * @param array $params An array of settings.
75
-	 *
76
-	 * @return array The updated array of settings.
77
-	 * @since 3.20.0
78
-	 */
79
-	public function add_nonce( $params ) {
80
-
81
-		return array_merge(
82
-			$params,
83
-			array(
84
-				"{$this->action}_nonce" => $this->create_nonce(),
85
-			)
86
-		);
87
-	}
88
-
89
-	/**
90
-	 * Process the requested operation.
91
-	 *
92
-	 * @since 3.20.0
93
-	 */
94
-	public function process() {
95
-		$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
96
-		// Validate the nonce.
97
-		if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
98
-			wp_send_json_error( 'Invalid nonce.' );
99
-		}
100
-
101
-		$offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0;
102
-		$limit  = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10;
103
-
104
-		// Run the batch operation.
105
-		$result = $this->operation->process( $offset, $limit );
106
-
107
-		// Send the results along with a potentially updated nonce.
108
-		wp_send_json_success(
109
-			array_merge(
110
-				$result,
111
-				array(
112
-					'_nonce' => $this->create_nonce(),
113
-				)
114
-			)
115
-		);
116
-
117
-	}
118
-
119
-	/**
120
-	 * Count the number of elements that would be affected by the operation.
121
-	 *
122
-	 * @since 3.20.0
123
-	 */
124
-	public function count() {
125
-
126
-		// Validate the nonce.
127
-		$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
128
-		if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
129
-			wp_send_json_error( 'Invalid nonce.' );
130
-		}
131
-
132
-		// Run the batch operation.
133
-		$result = $this->operation->count();
134
-
135
-		// Send the results along with a potentially updated nonce.
136
-		wp_send_json_success(
137
-			array(
138
-				'count'  => $result,
139
-				'_nonce' => $this->create_nonce(),
140
-			)
141
-		);
142
-
143
-	}
144
-
145
-	/**
146
-	 * Create a nonce for the ajax operation.
147
-	 *
148
-	 * @return string The nonce.
149
-	 * @since 3.20.0
150
-	 */
151
-	public function create_nonce() {
152
-
153
-		return wp_create_nonce( $this->action );
154
-	}
17
+    /**
18
+     * The access levels.
19
+     *
20
+     * @since 3.20.0
21
+     */
22
+    const ACCESS_ANONYMOUS = 1;
23
+    const ACCESS_ADMIN     = 2;
24
+    const ACCESS_ALL       = 3;
25
+
26
+    /**
27
+     * A {@link Wordlift_Batch_Operation_Interface} instance.
28
+     *
29
+     * @since 3.20.0
30
+     * @access private
31
+     * @var \Wordlift_Batch_Operation_Interface $operation A {@link Wordlift_Batch_Operation_Interface} instance.
32
+     */
33
+    private $operation;
34
+
35
+    /**
36
+     * The ajax action name.
37
+     *
38
+     * @since 3.20.0
39
+     * @access private
40
+     * @var string $action The ajax action name.
41
+     */
42
+    private $action;
43
+
44
+    /**
45
+     * Wordlift_Batch_Operation_Ajax_Adapter constructor.
46
+     *
47
+     * @param \Wordlift_Batch_Operation_Interface $operation The batch operation.
48
+     * @param string                              $action The action name.
49
+     * @param int                                 $access The access level.
50
+     */
51
+    public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) {
52
+
53
+        $this->operation = $operation;
54
+
55
+        if ( $access & self::ACCESS_ADMIN ) {
56
+            add_action( "wp_ajax_$action", array( $this, 'process' ) );
57
+            add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) );
58
+
59
+            // Add the nonce for the `schemaorg_sync` action.
60
+            add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) );
61
+        }
62
+
63
+        if ( $access & self::ACCESS_ANONYMOUS ) {
64
+            add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) );
65
+            add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) );
66
+        }
67
+
68
+        $this->action = $action;
69
+    }
70
+
71
+    /**
72
+     * Hook to `wl_admin_settings`, adds the nonce.
73
+     *
74
+     * @param array $params An array of settings.
75
+     *
76
+     * @return array The updated array of settings.
77
+     * @since 3.20.0
78
+     */
79
+    public function add_nonce( $params ) {
80
+
81
+        return array_merge(
82
+            $params,
83
+            array(
84
+                "{$this->action}_nonce" => $this->create_nonce(),
85
+            )
86
+        );
87
+    }
88
+
89
+    /**
90
+     * Process the requested operation.
91
+     *
92
+     * @since 3.20.0
93
+     */
94
+    public function process() {
95
+        $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
96
+        // Validate the nonce.
97
+        if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
98
+            wp_send_json_error( 'Invalid nonce.' );
99
+        }
100
+
101
+        $offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0;
102
+        $limit  = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10;
103
+
104
+        // Run the batch operation.
105
+        $result = $this->operation->process( $offset, $limit );
106
+
107
+        // Send the results along with a potentially updated nonce.
108
+        wp_send_json_success(
109
+            array_merge(
110
+                $result,
111
+                array(
112
+                    '_nonce' => $this->create_nonce(),
113
+                )
114
+            )
115
+        );
116
+
117
+    }
118
+
119
+    /**
120
+     * Count the number of elements that would be affected by the operation.
121
+     *
122
+     * @since 3.20.0
123
+     */
124
+    public function count() {
125
+
126
+        // Validate the nonce.
127
+        $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
128
+        if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
129
+            wp_send_json_error( 'Invalid nonce.' );
130
+        }
131
+
132
+        // Run the batch operation.
133
+        $result = $this->operation->count();
134
+
135
+        // Send the results along with a potentially updated nonce.
136
+        wp_send_json_success(
137
+            array(
138
+                'count'  => $result,
139
+                '_nonce' => $this->create_nonce(),
140
+            )
141
+        );
142
+
143
+    }
144
+
145
+    /**
146
+     * Create a nonce for the ajax operation.
147
+     *
148
+     * @return string The nonce.
149
+     * @since 3.20.0
150
+     */
151
+    public function create_nonce() {
152
+
153
+        return wp_create_nonce( $this->action );
154
+    }
155 155
 
156 156
 }
Please login to merge, or discard this patch.
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -48,21 +48,21 @@  discard block
 block discarded – undo
48 48
 	 * @param string                              $action The action name.
49 49
 	 * @param int                                 $access The access level.
50 50
 	 */
51
-	public function __construct( $operation, $action, $access = self::ACCESS_ADMIN ) {
51
+	public function __construct($operation, $action, $access = self::ACCESS_ADMIN) {
52 52
 
53 53
 		$this->operation = $operation;
54 54
 
55
-		if ( $access & self::ACCESS_ADMIN ) {
56
-			add_action( "wp_ajax_$action", array( $this, 'process' ) );
57
-			add_action( "wp_ajax_{$action}_count", array( $this, 'count' ) );
55
+		if ($access & self::ACCESS_ADMIN) {
56
+			add_action("wp_ajax_$action", array($this, 'process'));
57
+			add_action("wp_ajax_{$action}_count", array($this, 'count'));
58 58
 
59 59
 			// Add the nonce for the `schemaorg_sync` action.
60
-			add_filter( 'wl_admin_settings', array( $this, 'add_nonce' ) );
60
+			add_filter('wl_admin_settings', array($this, 'add_nonce'));
61 61
 		}
62 62
 
63
-		if ( $access & self::ACCESS_ANONYMOUS ) {
64
-			add_action( "wp_ajax_nopriv_$action", array( $this, 'process' ) );
65
-			add_action( "wp_ajax_nopriv_{$action}_count", array( $this, 'count' ) );
63
+		if ($access & self::ACCESS_ANONYMOUS) {
64
+			add_action("wp_ajax_nopriv_$action", array($this, 'process'));
65
+			add_action("wp_ajax_nopriv_{$action}_count", array($this, 'count'));
66 66
 		}
67 67
 
68 68
 		$this->action = $action;
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
76 76
 	 * @return array The updated array of settings.
77 77
 	 * @since 3.20.0
78 78
 	 */
79
-	public function add_nonce( $params ) {
79
+	public function add_nonce($params) {
80 80
 
81 81
 		return array_merge(
82 82
 			$params,
@@ -92,17 +92,17 @@  discard block
 block discarded – undo
92 92
 	 * @since 3.20.0
93 93
 	 */
94 94
 	public function process() {
95
-		$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
95
+		$nonce = isset($_POST['_nonce']) ? sanitize_text_field(wp_unslash((string) $_POST['_nonce'])) : '';
96 96
 		// Validate the nonce.
97
-		if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
98
-			wp_send_json_error( 'Invalid nonce.' );
97
+		if ( ! wp_verify_nonce($nonce, $this->action)) {
98
+			wp_send_json_error('Invalid nonce.');
99 99
 		}
100 100
 
101
-		$offset = isset( $_POST['offset'] ) ? (int) $_POST['offset'] : 0;
102
-		$limit  = isset( $_POST['limit'] ) ? (int) $_POST['limit'] : 10;
101
+		$offset = isset($_POST['offset']) ? (int) $_POST['offset'] : 0;
102
+		$limit  = isset($_POST['limit']) ? (int) $_POST['limit'] : 10;
103 103
 
104 104
 		// Run the batch operation.
105
-		$result = $this->operation->process( $offset, $limit );
105
+		$result = $this->operation->process($offset, $limit);
106 106
 
107 107
 		// Send the results along with a potentially updated nonce.
108 108
 		wp_send_json_success(
@@ -124,9 +124,9 @@  discard block
 block discarded – undo
124 124
 	public function count() {
125 125
 
126 126
 		// Validate the nonce.
127
-		$nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['_nonce'] ) ) : '';
128
-		if ( ! wp_verify_nonce( $nonce, $this->action ) ) {
129
-			wp_send_json_error( 'Invalid nonce.' );
127
+		$nonce = isset($_POST['_nonce']) ? sanitize_text_field(wp_unslash((string) $_POST['_nonce'])) : '';
128
+		if ( ! wp_verify_nonce($nonce, $this->action)) {
129
+			wp_send_json_error('Invalid nonce.');
130 130
 		}
131 131
 
132 132
 		// Run the batch operation.
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
 	 */
151 151
 	public function create_nonce() {
152 152
 
153
-		return wp_create_nonce( $this->action );
153
+		return wp_create_nonce($this->action);
154 154
 	}
155 155
 
156 156
 }
Please login to merge, or discard this patch.
src/includes/class-wordlift-timeline-service.php 2 patches
Indentation   +348 added lines, -348 removed lines patch added patch discarded remove patch
@@ -16,353 +16,353 @@
 block discarded – undo
16 16
  */
17 17
 class Wordlift_Timeline_Service {
18 18
 
19
-	/**
20
-	 * The Log service.
21
-	 *
22
-	 * @since  3.1.0
23
-	 * @access private
24
-	 * @var \Wordlift_Log_Service $log The Log service.
25
-	 */
26
-	private $log;
27
-
28
-	/**
29
-	 * The number of words to use for the excerpt, set in the `to_json` function
30
-	 * and used by a filter.
31
-	 *
32
-	 * @since  3.7.0
33
-	 * @access private
34
-	 * @var int $excerpt_length The number of words to use for the excerpt.
35
-	 */
36
-	private $excerpt_length;
37
-
38
-	/**
39
-	 * A singleton instance of the Timeline service (useful for unit tests).
40
-	 *
41
-	 * @since  3.1.0
42
-	 * @access private
43
-	 * @var \Wordlift_Timeline_Service $instance The singleton instance.
44
-	 */
45
-	private static $instance;
46
-
47
-	/**
48
-	 * Create a Wordlift_Timeline_Service instance.
49
-	 *
50
-	 * @since 3.1.0
51
-	 */
52
-	public function __construct() {
53
-
54
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
55
-
56
-		self::$instance = $this;
57
-
58
-	}
59
-
60
-	/**
61
-	 * Get the singleton instance of the Wordlift_Timeline_Service
62
-	 *
63
-	 * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service.
64
-	 * @since 3.1.0
65
-	 */
66
-	public static function get_instance() {
67
-
68
-		return self::$instance;
69
-	}
70
-
71
-	/**
72
-	 * Retrieve timeline events and output them in JSON.
73
-	 *
74
-	 * @since 3.1.0
75
-	 */
76
-	public function ajax_timeline() {
77
-
78
-		check_ajax_referer( 'wl_timeline' );
79
-
80
-		// Get the ID of the post who requested the timeline.
81
-		$post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null );
82
-
83
-		// Get the events and transform them for the JSON response, then send them to the client.
84
-		wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
85
-
86
-	}
87
-
88
-	/**
89
-	 * Retrieve timeline events.
90
-	 *
91
-	 * @param int $post_id The post ID.
92
-	 *
93
-	 * @return array An array of event posts.
94
-	 * @since 3.1.0
95
-	 *
96
-	 * @uses  wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post.
97
-	 */
98
-	public function get_events( $post_id = null ) {
99
-
100
-		// Get the entity IDs either from the entities related to the specified post or from the last 50 published
101
-		// posts if no post has been specified.
102
-		$ids = ( is_numeric( $post_id )
103
-			? wl_core_get_related_entity_ids( $post_id )
104
-			: $this->get_all_related_to_last_50_published_posts() );
105
-
106
-		// Add the post itself if it's an entity.
107
-		if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) {
108
-			$ids[] = $post_id;
109
-		}
110
-
111
-		// If there's no entities, return an empty array right away.
112
-		if ( 0 === count( $ids ) ) {
113
-			$this->log->trace( "No events found [ post id :: $post_id ]" );
114
-
115
-			return array();
116
-		}
117
-
118
-		$this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
119
-
120
-		$args = array(
121
-			'post__in'       => $ids,
122
-			'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
123
-			'post_status'    => 'publish',
124
-			'posts_per_page' => - 1,
125
-			'meta_query'     => array(
126
-				'relation' => 'AND',
127
-				array(
128
-					'key'     => Wordlift_Schema_Service::FIELD_DATE_START,
129
-					'value'   => null,
130
-					'compare' => '!=',
131
-				),
132
-				array(
133
-					'key'     => Wordlift_Schema_Service::FIELD_DATE_END,
134
-					'value'   => null,
135
-					'compare' => '!=',
136
-				),
137
-			),
138
-			'tax_query'      => array(
139
-				array(
140
-					'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
141
-					'field'    => 'slug',
142
-					'terms'    => 'event',
143
-				),
144
-			),
145
-			'orderby'        => 'meta_value',
146
-			'order'          => 'DESC',
147
-			'meta_key'       => Wordlift_Schema_Service::FIELD_DATE_START,
148
-		);
149
-
150
-		return get_posts( $args );
151
-	}
152
-
153
-	/**
154
-	 * Convert timeline events to JSON. This function sets the global post in order
155
-	 * to get an automatic excerpt. Since we're being called inside an AJAX request,
156
-	 * we're not taking care of restoring any previous post: there isn't any.
157
-	 *
158
-	 * @param array $posts An array of posts.
159
-	 *
160
-	 * @return array|string An array of timeline events or an empty string if no posts are provided.
161
-	 * @since 3.1.0
162
-	 */
163
-	public function to_json( $posts ) {
164
-		check_ajax_referer( 'wl_timeline' );
165
-		// If there are no events, return empty JSON
166
-		if ( empty( $posts ) || $posts === null ) {
167
-			return '';
168
-		}
169
-
170
-		// {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
171
-		$display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media';
172
-
173
-		// The number of words for the excerpt (by default 55, as WordPress).
174
-		$excerpt_length       = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55;
175
-		$this->excerpt_length = $excerpt_length;
176
-		add_filter( 'excerpt_length', array( $this, 'excerpt_length' ), 10, 0 );
177
-
178
-		// Add a filter to remove the [...] after excerpts, since we're adding
179
-		// a link to the post itself.
180
-		add_filter( 'excerpt_more', array( $this, 'excerpt_more' ), 10, 0 );
181
-
182
-		// Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
183
-		$start_at_slide = 0;
184
-		$event_index    = - 1;
185
-		$now            = time();
186
-
187
-		// Prepare the timeline variable.
188
-		$timeline = array();
189
-
190
-		// Populate the arrays.
191
-		$timeline['events'] = array_map(
192
-			function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
193
-
194
-				$date = array();
195
-
196
-				// Get the start and end dates.
197
-				// We have to remove double quotes from date to make timeline work properly
198
-				$start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) );
199
-				$end_date   = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) );
200
-
201
-				// Set the starting slide.
202
-				// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
203
-				$event_index ++;
204
-				// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
205
-				if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
206
-					// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
207
-					$start_at_slide = $event_index;
208
-				}
209
-
210
-				// Load thumbnail
211
-				$thumbnail_id = get_post_thumbnail_id( $item->ID );
212
-				$attachment   = wp_get_attachment_image_src( $thumbnail_id );
213
-				if ( '' !== (string) $thumbnail_id && 0 !== $thumbnail_id
214
-				     && false !== $attachment
215
-				) {
216
-
217
-					// Set the thumbnail URL.
218
-					// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
219
-					if ( 'background' === $display_images_as ) {
220
-						$date['background'] = array( 'url' => $attachment[0] );
221
-						$date['media']      = array( 'thumbnail' => $attachment[0] );
222
-					} else {
223
-						$date['media'] = array(
224
-							'url'       => $attachment[0],
225
-							'thumbnail' => $attachment[0],
226
-						);
227
-					}
228
-				}
229
-
230
-				// Set the start/end dates by converting them to TimelineJS required format.
231
-				$date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
232
-				$date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
233
-
234
-				setup_postdata( $GLOBALS['post'] = $item ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
235
-
236
-				$more_link_text = sprintf(
237
-					'<span aria-label="%1$s">%2$s</span>',
238
-					sprintf(
239
-					/* translators: %s: Name of current post */
240
-						__( 'Continue reading %s', 'wordlift' ),
241
-						the_title_attribute( array( 'echo' => false ) )
242
-					),
243
-					__( '(more&hellip;)', 'wordlift' )
244
-				);
245
-
246
-				// Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
247
-				$date['text'] = array(
248
-					'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
249
-				);
250
-
251
-				// If we have an excerpt, set it.
252
-				// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
253
-				if ( 0 < $excerpt_length ) {
254
-					$date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
255
-				}
256
-
257
-				return $date;
258
-
259
-			},
260
-			$posts
261
-		);
262
-
263
-		// Finally remove the excerpt filter.
264
-		remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
265
-
266
-		// The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
267
-		return array(
268
-			'timeline'       => $timeline,
269
-			'start_at_slide' => $start_at_slide,
270
-		);
271
-	}
272
-
273
-	/**
274
-	 * This function filters {@link excerpt_more} by removing it, since we're
275
-	 * adding the 'read more' link. This filter is set by {@see to_json}.
276
-	 *
277
-	 * @return string An empty string.
278
-	 * @since 3.7.0
279
-	 */
280
-	public function excerpt_more() {
281
-
282
-		return '';
283
-	}
284
-
285
-	/**
286
-	 * A filter for the excerpt length, set by the `to_json` function, to tailor
287
-	 * how many words to return according to the client setting.
288
-	 *
289
-	 * @return int The number of words for the preset.
290
-	 * @since 3.7.0
291
-	 */
292
-	public function excerpt_length() {
293
-
294
-		return $this->excerpt_length;
295
-	}
296
-
297
-	/**
298
-	 * Convert the date to a date array.
299
-	 *
300
-	 * @param $value int A date value.
301
-	 *
302
-	 * @return array An array containing year, month and day values.
303
-	 * @since 3.7.0
304
-	 */
305
-	public static function date( $value ) {
306
-
307
-		return array(
308
-			'year'  => (int) gmdate( 'Y', $value ),
309
-			'month' => (int) gmdate( 'm', $value ),
310
-			'day'   => (int) gmdate( 'd', $value ),
311
-
312
-		);
313
-	}
314
-
315
-	/**
316
-	 * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to
317
-	 * its specific function).
318
-	 *
319
-	 * @return array An array of post IDs.
320
-	 * @since 3.1.0
321
-	 */
322
-	public function get_all_related_to_last_50_published_posts() {
323
-
324
-		// Global timeline. Get entities from the latest posts.
325
-		$latest_posts_ids = get_posts(
326
-			array(
327
-				'numberposts' => 50,
328
-				'fields'      => 'ids', // only get post IDs
329
-				'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
330
-				'tax_query'   => array(
331
-					'relation' => 'OR',
332
-					array(
333
-						'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
334
-						'operator' => 'NOT EXISTS',
335
-					),
336
-					array(
337
-						'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
338
-						'field'    => 'slug',
339
-						'terms'    => 'article',
340
-					),
341
-				),
342
-				'post_status' => 'publish',
343
-			)
344
-		);
345
-
346
-		if ( empty( $latest_posts_ids ) ) {
347
-			// There are no posts.
348
-			return array();
349
-		}
350
-
351
-		// Collect entities related to latest posts
352
-		$entity_ids = array();
353
-		foreach ( $latest_posts_ids as $id ) {
354
-			$entity_ids = array_merge(
355
-				$entity_ids,
356
-				wl_core_get_related_entity_ids(
357
-					$id,
358
-					array(
359
-						'status' => 'publish',
360
-					)
361
-				)
362
-			);
363
-		}
364
-
365
-		return $entity_ids;
366
-	}
19
+    /**
20
+     * The Log service.
21
+     *
22
+     * @since  3.1.0
23
+     * @access private
24
+     * @var \Wordlift_Log_Service $log The Log service.
25
+     */
26
+    private $log;
27
+
28
+    /**
29
+     * The number of words to use for the excerpt, set in the `to_json` function
30
+     * and used by a filter.
31
+     *
32
+     * @since  3.7.0
33
+     * @access private
34
+     * @var int $excerpt_length The number of words to use for the excerpt.
35
+     */
36
+    private $excerpt_length;
37
+
38
+    /**
39
+     * A singleton instance of the Timeline service (useful for unit tests).
40
+     *
41
+     * @since  3.1.0
42
+     * @access private
43
+     * @var \Wordlift_Timeline_Service $instance The singleton instance.
44
+     */
45
+    private static $instance;
46
+
47
+    /**
48
+     * Create a Wordlift_Timeline_Service instance.
49
+     *
50
+     * @since 3.1.0
51
+     */
52
+    public function __construct() {
53
+
54
+        $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
55
+
56
+        self::$instance = $this;
57
+
58
+    }
59
+
60
+    /**
61
+     * Get the singleton instance of the Wordlift_Timeline_Service
62
+     *
63
+     * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service.
64
+     * @since 3.1.0
65
+     */
66
+    public static function get_instance() {
67
+
68
+        return self::$instance;
69
+    }
70
+
71
+    /**
72
+     * Retrieve timeline events and output them in JSON.
73
+     *
74
+     * @since 3.1.0
75
+     */
76
+    public function ajax_timeline() {
77
+
78
+        check_ajax_referer( 'wl_timeline' );
79
+
80
+        // Get the ID of the post who requested the timeline.
81
+        $post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null );
82
+
83
+        // Get the events and transform them for the JSON response, then send them to the client.
84
+        wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
85
+
86
+    }
87
+
88
+    /**
89
+     * Retrieve timeline events.
90
+     *
91
+     * @param int $post_id The post ID.
92
+     *
93
+     * @return array An array of event posts.
94
+     * @since 3.1.0
95
+     *
96
+     * @uses  wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post.
97
+     */
98
+    public function get_events( $post_id = null ) {
99
+
100
+        // Get the entity IDs either from the entities related to the specified post or from the last 50 published
101
+        // posts if no post has been specified.
102
+        $ids = ( is_numeric( $post_id )
103
+            ? wl_core_get_related_entity_ids( $post_id )
104
+            : $this->get_all_related_to_last_50_published_posts() );
105
+
106
+        // Add the post itself if it's an entity.
107
+        if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) {
108
+            $ids[] = $post_id;
109
+        }
110
+
111
+        // If there's no entities, return an empty array right away.
112
+        if ( 0 === count( $ids ) ) {
113
+            $this->log->trace( "No events found [ post id :: $post_id ]" );
114
+
115
+            return array();
116
+        }
117
+
118
+        $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
119
+
120
+        $args = array(
121
+            'post__in'       => $ids,
122
+            'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
123
+            'post_status'    => 'publish',
124
+            'posts_per_page' => - 1,
125
+            'meta_query'     => array(
126
+                'relation' => 'AND',
127
+                array(
128
+                    'key'     => Wordlift_Schema_Service::FIELD_DATE_START,
129
+                    'value'   => null,
130
+                    'compare' => '!=',
131
+                ),
132
+                array(
133
+                    'key'     => Wordlift_Schema_Service::FIELD_DATE_END,
134
+                    'value'   => null,
135
+                    'compare' => '!=',
136
+                ),
137
+            ),
138
+            'tax_query'      => array(
139
+                array(
140
+                    'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
141
+                    'field'    => 'slug',
142
+                    'terms'    => 'event',
143
+                ),
144
+            ),
145
+            'orderby'        => 'meta_value',
146
+            'order'          => 'DESC',
147
+            'meta_key'       => Wordlift_Schema_Service::FIELD_DATE_START,
148
+        );
149
+
150
+        return get_posts( $args );
151
+    }
152
+
153
+    /**
154
+     * Convert timeline events to JSON. This function sets the global post in order
155
+     * to get an automatic excerpt. Since we're being called inside an AJAX request,
156
+     * we're not taking care of restoring any previous post: there isn't any.
157
+     *
158
+     * @param array $posts An array of posts.
159
+     *
160
+     * @return array|string An array of timeline events or an empty string if no posts are provided.
161
+     * @since 3.1.0
162
+     */
163
+    public function to_json( $posts ) {
164
+        check_ajax_referer( 'wl_timeline' );
165
+        // If there are no events, return empty JSON
166
+        if ( empty( $posts ) || $posts === null ) {
167
+            return '';
168
+        }
169
+
170
+        // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
171
+        $display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media';
172
+
173
+        // The number of words for the excerpt (by default 55, as WordPress).
174
+        $excerpt_length       = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55;
175
+        $this->excerpt_length = $excerpt_length;
176
+        add_filter( 'excerpt_length', array( $this, 'excerpt_length' ), 10, 0 );
177
+
178
+        // Add a filter to remove the [...] after excerpts, since we're adding
179
+        // a link to the post itself.
180
+        add_filter( 'excerpt_more', array( $this, 'excerpt_more' ), 10, 0 );
181
+
182
+        // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
183
+        $start_at_slide = 0;
184
+        $event_index    = - 1;
185
+        $now            = time();
186
+
187
+        // Prepare the timeline variable.
188
+        $timeline = array();
189
+
190
+        // Populate the arrays.
191
+        $timeline['events'] = array_map(
192
+            function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
193
+
194
+                $date = array();
195
+
196
+                // Get the start and end dates.
197
+                // We have to remove double quotes from date to make timeline work properly
198
+                $start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) );
199
+                $end_date   = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) );
200
+
201
+                // Set the starting slide.
202
+                // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
203
+                $event_index ++;
204
+                // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
205
+                if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
206
+                    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
207
+                    $start_at_slide = $event_index;
208
+                }
209
+
210
+                // Load thumbnail
211
+                $thumbnail_id = get_post_thumbnail_id( $item->ID );
212
+                $attachment   = wp_get_attachment_image_src( $thumbnail_id );
213
+                if ( '' !== (string) $thumbnail_id && 0 !== $thumbnail_id
214
+                     && false !== $attachment
215
+                ) {
216
+
217
+                    // Set the thumbnail URL.
218
+                    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
219
+                    if ( 'background' === $display_images_as ) {
220
+                        $date['background'] = array( 'url' => $attachment[0] );
221
+                        $date['media']      = array( 'thumbnail' => $attachment[0] );
222
+                    } else {
223
+                        $date['media'] = array(
224
+                            'url'       => $attachment[0],
225
+                            'thumbnail' => $attachment[0],
226
+                        );
227
+                    }
228
+                }
229
+
230
+                // Set the start/end dates by converting them to TimelineJS required format.
231
+                $date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
232
+                $date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
233
+
234
+                setup_postdata( $GLOBALS['post'] = $item ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
235
+
236
+                $more_link_text = sprintf(
237
+                    '<span aria-label="%1$s">%2$s</span>',
238
+                    sprintf(
239
+                    /* translators: %s: Name of current post */
240
+                        __( 'Continue reading %s', 'wordlift' ),
241
+                        the_title_attribute( array( 'echo' => false ) )
242
+                    ),
243
+                    __( '(more&hellip;)', 'wordlift' )
244
+                );
245
+
246
+                // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
247
+                $date['text'] = array(
248
+                    'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
249
+                );
250
+
251
+                // If we have an excerpt, set it.
252
+                // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
253
+                if ( 0 < $excerpt_length ) {
254
+                    $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
255
+                }
256
+
257
+                return $date;
258
+
259
+            },
260
+            $posts
261
+        );
262
+
263
+        // Finally remove the excerpt filter.
264
+        remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
265
+
266
+        // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
267
+        return array(
268
+            'timeline'       => $timeline,
269
+            'start_at_slide' => $start_at_slide,
270
+        );
271
+    }
272
+
273
+    /**
274
+     * This function filters {@link excerpt_more} by removing it, since we're
275
+     * adding the 'read more' link. This filter is set by {@see to_json}.
276
+     *
277
+     * @return string An empty string.
278
+     * @since 3.7.0
279
+     */
280
+    public function excerpt_more() {
281
+
282
+        return '';
283
+    }
284
+
285
+    /**
286
+     * A filter for the excerpt length, set by the `to_json` function, to tailor
287
+     * how many words to return according to the client setting.
288
+     *
289
+     * @return int The number of words for the preset.
290
+     * @since 3.7.0
291
+     */
292
+    public function excerpt_length() {
293
+
294
+        return $this->excerpt_length;
295
+    }
296
+
297
+    /**
298
+     * Convert the date to a date array.
299
+     *
300
+     * @param $value int A date value.
301
+     *
302
+     * @return array An array containing year, month and day values.
303
+     * @since 3.7.0
304
+     */
305
+    public static function date( $value ) {
306
+
307
+        return array(
308
+            'year'  => (int) gmdate( 'Y', $value ),
309
+            'month' => (int) gmdate( 'm', $value ),
310
+            'day'   => (int) gmdate( 'd', $value ),
311
+
312
+        );
313
+    }
314
+
315
+    /**
316
+     * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to
317
+     * its specific function).
318
+     *
319
+     * @return array An array of post IDs.
320
+     * @since 3.1.0
321
+     */
322
+    public function get_all_related_to_last_50_published_posts() {
323
+
324
+        // Global timeline. Get entities from the latest posts.
325
+        $latest_posts_ids = get_posts(
326
+            array(
327
+                'numberposts' => 50,
328
+                'fields'      => 'ids', // only get post IDs
329
+                'post_type'   => Wordlift_Entity_Service::valid_entity_post_types(),
330
+                'tax_query'   => array(
331
+                    'relation' => 'OR',
332
+                    array(
333
+                        'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
334
+                        'operator' => 'NOT EXISTS',
335
+                    ),
336
+                    array(
337
+                        'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME,
338
+                        'field'    => 'slug',
339
+                        'terms'    => 'article',
340
+                    ),
341
+                ),
342
+                'post_status' => 'publish',
343
+            )
344
+        );
345
+
346
+        if ( empty( $latest_posts_ids ) ) {
347
+            // There are no posts.
348
+            return array();
349
+        }
350
+
351
+        // Collect entities related to latest posts
352
+        $entity_ids = array();
353
+        foreach ( $latest_posts_ids as $id ) {
354
+            $entity_ids = array_merge(
355
+                $entity_ids,
356
+                wl_core_get_related_entity_ids(
357
+                    $id,
358
+                    array(
359
+                        'status' => 'publish',
360
+                    )
361
+                )
362
+            );
363
+        }
364
+
365
+        return $entity_ids;
366
+    }
367 367
 
368 368
 }
Please login to merge, or discard this patch.
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -51,7 +51,7 @@  discard block
 block discarded – undo
51 51
 	 */
52 52
 	public function __construct() {
53 53
 
54
-		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' );
54
+		$this->log = Wordlift_Log_Service::get_logger('Wordlift_Timeline_Service');
55 55
 
56 56
 		self::$instance = $this;
57 57
 
@@ -75,13 +75,13 @@  discard block
 block discarded – undo
75 75
 	 */
76 76
 	public function ajax_timeline() {
77 77
 
78
-		check_ajax_referer( 'wl_timeline' );
78
+		check_ajax_referer('wl_timeline');
79 79
 
80 80
 		// Get the ID of the post who requested the timeline.
81
-		$post_id = ( isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : null );
81
+		$post_id = (isset($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : null);
82 82
 
83 83
 		// Get the events and transform them for the JSON response, then send them to the client.
84
-		wp_send_json( $this->to_json( $this->get_events( $post_id ) ) );
84
+		wp_send_json($this->to_json($this->get_events($post_id)));
85 85
 
86 86
 	}
87 87
 
@@ -95,33 +95,33 @@  discard block
 block discarded – undo
95 95
 	 *
96 96
 	 * @uses  wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post.
97 97
 	 */
98
-	public function get_events( $post_id = null ) {
98
+	public function get_events($post_id = null) {
99 99
 
100 100
 		// Get the entity IDs either from the entities related to the specified post or from the last 50 published
101 101
 		// posts if no post has been specified.
102
-		$ids = ( is_numeric( $post_id )
103
-			? wl_core_get_related_entity_ids( $post_id )
104
-			: $this->get_all_related_to_last_50_published_posts() );
102
+		$ids = (is_numeric($post_id)
103
+			? wl_core_get_related_entity_ids($post_id)
104
+			: $this->get_all_related_to_last_50_published_posts());
105 105
 
106 106
 		// Add the post itself if it's an entity.
107
-		if ( is_numeric( $post_id ) && Wordlift_Entity_Service::get_instance()->is_entity( $post_id ) ) {
107
+		if (is_numeric($post_id) && Wordlift_Entity_Service::get_instance()->is_entity($post_id)) {
108 108
 			$ids[] = $post_id;
109 109
 		}
110 110
 
111 111
 		// If there's no entities, return an empty array right away.
112
-		if ( 0 === count( $ids ) ) {
113
-			$this->log->trace( "No events found [ post id :: $post_id ]" );
112
+		if (0 === count($ids)) {
113
+			$this->log->trace("No events found [ post id :: $post_id ]");
114 114
 
115 115
 			return array();
116 116
 		}
117 117
 
118
-		$this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' );
118
+		$this->log->trace('Getting events [ entity ids :: '.join(', ', $ids).' ]');
119 119
 
120 120
 		$args = array(
121 121
 			'post__in'       => $ids,
122 122
 			'post_type'      => Wordlift_Entity_Service::valid_entity_post_types(),
123 123
 			'post_status'    => 'publish',
124
-			'posts_per_page' => - 1,
124
+			'posts_per_page' => -1,
125 125
 			'meta_query'     => array(
126 126
 				'relation' => 'AND',
127 127
 				array(
@@ -147,7 +147,7 @@  discard block
 block discarded – undo
147 147
 			'meta_key'       => Wordlift_Schema_Service::FIELD_DATE_START,
148 148
 		);
149 149
 
150
-		return get_posts( $args );
150
+		return get_posts($args);
151 151
 	}
152 152
 
153 153
 	/**
@@ -160,24 +160,24 @@  discard block
 block discarded – undo
160 160
 	 * @return array|string An array of timeline events or an empty string if no posts are provided.
161 161
 	 * @since 3.1.0
162 162
 	 */
163
-	public function to_json( $posts ) {
164
-		check_ajax_referer( 'wl_timeline' );
163
+	public function to_json($posts) {
164
+		check_ajax_referer('wl_timeline');
165 165
 		// If there are no events, return empty JSON
166
-		if ( empty( $posts ) || $posts === null ) {
166
+		if (empty($posts) || $posts === null) {
167 167
 			return '';
168 168
 		}
169 169
 
170 170
 		// {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background.
171
-		$display_images_as = isset( $_REQUEST['display_images_as'] ) ? sanitize_text_field( wp_unslash( (string) $_REQUEST['display_images_as'] ) ) : 'media';
171
+		$display_images_as = isset($_REQUEST['display_images_as']) ? sanitize_text_field(wp_unslash((string) $_REQUEST['display_images_as'])) : 'media';
172 172
 
173 173
 		// The number of words for the excerpt (by default 55, as WordPress).
174
-		$excerpt_length       = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['excerpt_length'] ) ) : 55;
174
+		$excerpt_length       = isset($_REQUEST['excerpt_length']) && is_numeric($_REQUEST['excerpt_length']) ? sanitize_text_field(wp_unslash($_REQUEST['excerpt_length'])) : 55;
175 175
 		$this->excerpt_length = $excerpt_length;
176
-		add_filter( 'excerpt_length', array( $this, 'excerpt_length' ), 10, 0 );
176
+		add_filter('excerpt_length', array($this, 'excerpt_length'), 10, 0);
177 177
 
178 178
 		// Add a filter to remove the [...] after excerpts, since we're adding
179 179
 		// a link to the post itself.
180
-		add_filter( 'excerpt_more', array( $this, 'excerpt_more' ), 10, 0 );
180
+		add_filter('excerpt_more', array($this, 'excerpt_more'), 10, 0);
181 181
 
182 182
 		// Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates.
183 183
 		$start_at_slide = 0;
@@ -189,36 +189,36 @@  discard block
 block discarded – undo
189 189
 
190 190
 		// Populate the arrays.
191 191
 		$timeline['events'] = array_map(
192
-			function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) {
192
+			function($item) use (&$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length) {
193 193
 
194 194
 				$date = array();
195 195
 
196 196
 				// Get the start and end dates.
197 197
 				// We have to remove double quotes from date to make timeline work properly
198
-				$start_date = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ) );
199
-				$end_date   = strtotime( str_replace( '"', '', get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ) );
198
+				$start_date = strtotime(str_replace('"', '', get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true)));
199
+				$end_date   = strtotime(str_replace('"', '', get_post_meta($item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true)));
200 200
 
201 201
 				// Set the starting slide.
202 202
 				// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
203
-				$event_index ++;
203
+				$event_index++;
204 204
 				// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
205
-				if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) {
205
+				if (0 === $start_at_slide && $now >= $start_date && $now <= $end_date) {
206 206
 					// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
207 207
 					$start_at_slide = $event_index;
208 208
 				}
209 209
 
210 210
 				// Load thumbnail
211
-				$thumbnail_id = get_post_thumbnail_id( $item->ID );
212
-				$attachment   = wp_get_attachment_image_src( $thumbnail_id );
213
-				if ( '' !== (string) $thumbnail_id && 0 !== $thumbnail_id
211
+				$thumbnail_id = get_post_thumbnail_id($item->ID);
212
+				$attachment   = wp_get_attachment_image_src($thumbnail_id);
213
+				if ('' !== (string) $thumbnail_id && 0 !== $thumbnail_id
214 214
 				     && false !== $attachment
215 215
 				) {
216 216
 
217 217
 					// Set the thumbnail URL.
218 218
 					// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
219
-					if ( 'background' === $display_images_as ) {
220
-						$date['background'] = array( 'url' => $attachment[0] );
221
-						$date['media']      = array( 'thumbnail' => $attachment[0] );
219
+					if ('background' === $display_images_as) {
220
+						$date['background'] = array('url' => $attachment[0]);
221
+						$date['media']      = array('thumbnail' => $attachment[0]);
222 222
 					} else {
223 223
 						$date['media'] = array(
224 224
 							'url'       => $attachment[0],
@@ -228,30 +228,30 @@  discard block
 block discarded – undo
228 228
 				}
229 229
 
230 230
 				// Set the start/end dates by converting them to TimelineJS required format.
231
-				$date['start_date'] = Wordlift_Timeline_Service::date( $start_date );
232
-				$date['end_date']   = Wordlift_Timeline_Service::date( $end_date );
231
+				$date['start_date'] = Wordlift_Timeline_Service::date($start_date);
232
+				$date['end_date']   = Wordlift_Timeline_Service::date($end_date);
233 233
 
234
-				setup_postdata( $GLOBALS['post'] = $item ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
234
+				setup_postdata($GLOBALS['post'] = $item); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
235 235
 
236 236
 				$more_link_text = sprintf(
237 237
 					'<span aria-label="%1$s">%2$s</span>',
238 238
 					sprintf(
239 239
 					/* translators: %s: Name of current post */
240
-						__( 'Continue reading %s', 'wordlift' ),
241
-						the_title_attribute( array( 'echo' => false ) )
240
+						__('Continue reading %s', 'wordlift'),
241
+						the_title_attribute(array('echo' => false))
242 242
 					),
243
-					__( '(more&hellip;)', 'wordlift' )
243
+					__('(more&hellip;)', 'wordlift')
244 244
 				);
245 245
 
246 246
 				// Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352).
247 247
 				$date['text'] = array(
248
-					'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>',
248
+					'headline' => '<a href="'.get_permalink($item->ID).'">'.$item->post_title.'</a>',
249 249
 				);
250 250
 
251 251
 				// If we have an excerpt, set it.
252 252
 				// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
253
-				if ( 0 < $excerpt_length ) {
254
-					$date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text );
253
+				if (0 < $excerpt_length) {
254
+					$date['text']['text'] = sprintf('%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text);
255 255
 				}
256 256
 
257 257
 				return $date;
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 		);
262 262
 
263 263
 		// Finally remove the excerpt filter.
264
-		remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) );
264
+		remove_filter('excerpt_length', array($this, 'excerpt_length'));
265 265
 
266 266
 		// The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html
267 267
 		return array(
@@ -302,12 +302,12 @@  discard block
 block discarded – undo
302 302
 	 * @return array An array containing year, month and day values.
303 303
 	 * @since 3.7.0
304 304
 	 */
305
-	public static function date( $value ) {
305
+	public static function date($value) {
306 306
 
307 307
 		return array(
308
-			'year'  => (int) gmdate( 'Y', $value ),
309
-			'month' => (int) gmdate( 'm', $value ),
310
-			'day'   => (int) gmdate( 'd', $value ),
308
+			'year'  => (int) gmdate('Y', $value),
309
+			'month' => (int) gmdate('m', $value),
310
+			'day'   => (int) gmdate('d', $value),
311 311
 
312 312
 		);
313 313
 	}
@@ -343,14 +343,14 @@  discard block
 block discarded – undo
343 343
 			)
344 344
 		);
345 345
 
346
-		if ( empty( $latest_posts_ids ) ) {
346
+		if (empty($latest_posts_ids)) {
347 347
 			// There are no posts.
348 348
 			return array();
349 349
 		}
350 350
 
351 351
 		// Collect entities related to latest posts
352 352
 		$entity_ids = array();
353
-		foreach ( $latest_posts_ids as $id ) {
353
+		foreach ($latest_posts_ids as $id) {
354 354
 			$entity_ids = array_merge(
355 355
 				$entity_ids,
356 356
 				wl_core_get_related_entity_ids(
Please login to merge, or discard this patch.