@@ -18,334 +18,334 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Relation_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The singleton instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Relation_Service $instance The singleton instance. |
|
| 27 | - */ |
|
| 28 | - private static $instance; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The relation table name in MySQL, set during instantiation. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var string $relation_table The relation table name. |
|
| 36 | - */ |
|
| 37 | - private $relation_table; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Create a {@link Wordlift_Relation_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.0 |
|
| 43 | - */ |
|
| 44 | - public function __construct() { |
|
| 45 | - global $wpdb; |
|
| 46 | - |
|
| 47 | - // The relations table. |
|
| 48 | - $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 49 | - |
|
| 50 | - self::$instance = $this; |
|
| 51 | - |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Get the singleton instance. |
|
| 56 | - * |
|
| 57 | - * @since 3.15.0 |
|
| 58 | - * @access public |
|
| 59 | - * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
|
| 60 | - * singleton instance. |
|
| 61 | - */ |
|
| 62 | - public static function get_instance() { |
|
| 63 | - |
|
| 64 | - return self::$instance; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 69 | - * |
|
| 70 | - * @since 3.15.0 |
|
| 71 | - * |
|
| 72 | - * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 73 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 74 | - * '*' to return all fields, by default '*'. |
|
| 75 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 76 | - * @param null|string $status The status, by default all. |
|
| 77 | - * @param array $excludes An array of ids to exclude from the results. |
|
| 78 | - * @param null|int $limit The maximum number of results, by default |
|
| 79 | - * no limit. |
|
| 80 | - * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 81 | - * |
|
| 82 | - * @return array|object|null Database query results |
|
| 83 | - */ |
|
| 84 | - public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) { |
|
| 85 | - global $wpdb; |
|
| 86 | - |
|
| 87 | - // The output fields. |
|
| 88 | - $actual_fields = self::fields( $fields ); |
|
| 89 | - |
|
| 90 | - $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 91 | - |
|
| 92 | - $sql = |
|
| 93 | - " |
|
| 21 | + /** |
|
| 22 | + * The singleton instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Relation_Service $instance The singleton instance. |
|
| 27 | + */ |
|
| 28 | + private static $instance; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The relation table name in MySQL, set during instantiation. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var string $relation_table The relation table name. |
|
| 36 | + */ |
|
| 37 | + private $relation_table; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Create a {@link Wordlift_Relation_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.0 |
|
| 43 | + */ |
|
| 44 | + public function __construct() { |
|
| 45 | + global $wpdb; |
|
| 46 | + |
|
| 47 | + // The relations table. |
|
| 48 | + $this->relation_table = "{$wpdb->prefix}wl_relation_instances"; |
|
| 49 | + |
|
| 50 | + self::$instance = $this; |
|
| 51 | + |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Get the singleton instance. |
|
| 56 | + * |
|
| 57 | + * @since 3.15.0 |
|
| 58 | + * @access public |
|
| 59 | + * @return \Wordlift_Relation_Service The {@link Wordlift_Relation_Service} |
|
| 60 | + * singleton instance. |
|
| 61 | + */ |
|
| 62 | + public static function get_instance() { |
|
| 63 | + |
|
| 64 | + return self::$instance; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Get the articles referencing the specified entity {@link WP_Post}. |
|
| 69 | + * |
|
| 70 | + * @since 3.15.0 |
|
| 71 | + * |
|
| 72 | + * @param int|array $object_id The entity {@link WP_Post}'s id. |
|
| 73 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 74 | + * '*' to return all fields, by default '*'. |
|
| 75 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 76 | + * @param null|string $status The status, by default all. |
|
| 77 | + * @param array $excludes An array of ids to exclude from the results. |
|
| 78 | + * @param null|int $limit The maximum number of results, by default |
|
| 79 | + * no limit. |
|
| 80 | + * @param null|array $include The {@link WP_Post}s' ids to include. |
|
| 81 | + * |
|
| 82 | + * @return array|object|null Database query results |
|
| 83 | + */ |
|
| 84 | + public function get_article_subjects( $object_id, $fields = '*', $predicate = null, $status = null, $excludes = array(), $limit = null, $include = null ) { |
|
| 85 | + global $wpdb; |
|
| 86 | + |
|
| 87 | + // The output fields. |
|
| 88 | + $actual_fields = self::fields( $fields ); |
|
| 89 | + |
|
| 90 | + $objects = $this->article_id_to_entity_id( $object_id ); |
|
| 91 | + |
|
| 92 | + $sql = |
|
| 93 | + " |
|
| 94 | 94 | SELECT DISTINCT p.$actual_fields |
| 95 | 95 | FROM {$this->relation_table} r |
| 96 | 96 | INNER JOIN $wpdb->posts p |
| 97 | 97 | ON p.id = r.subject_id |
| 98 | 98 | " |
| 99 | - // Add the status clause. |
|
| 100 | - . self::and_status( $status ) |
|
| 101 | - . self::inner_join_is_article() |
|
| 102 | - . self::where_object_id( $objects ) |
|
| 103 | - // Since `object_id` can be an article ID we need to exclude it from |
|
| 104 | - // the results. |
|
| 105 | - . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 106 | - . self::and_article_in( $include ) |
|
| 107 | - . " AND p.post_type IN ( 'post', 'page', 'entity' ) " |
|
| 108 | - . self::and_predicate( $predicate ) |
|
| 109 | - . self::limit( $limit ); |
|
| 110 | - |
|
| 111 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Add the limit clause if specified. |
|
| 116 | - * |
|
| 117 | - * @since 3.15.0 |
|
| 118 | - * |
|
| 119 | - * @param null|int $limit The maximum number of results. |
|
| 120 | - * |
|
| 121 | - * @return string The limit clause (empty if no limit has been specified). |
|
| 122 | - */ |
|
| 123 | - private static function limit( $limit = null ) { |
|
| 124 | - |
|
| 125 | - if ( null === $limit ) { |
|
| 126 | - return ''; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - return "LIMIT $limit"; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 134 | - * or get the entities if it's a post). |
|
| 135 | - * |
|
| 136 | - * @since 3.15.0 |
|
| 137 | - * |
|
| 138 | - * @param int|array $object_id An array of posts/entities' ids. |
|
| 139 | - * |
|
| 140 | - * @return array An array of entities' ids. |
|
| 141 | - */ |
|
| 142 | - private function article_id_to_entity_id( $object_id ) { |
|
| 143 | - |
|
| 144 | - $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 145 | - |
|
| 146 | - $relation_service = $this; |
|
| 147 | - |
|
| 148 | - return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 149 | - if ( $entity_service->is_entity( $item ) ) { |
|
| 150 | - return array_merge( $carry, (array) $item ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 154 | - }, array() ); |
|
| 155 | - |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Add the WHERE clause. |
|
| 160 | - * |
|
| 161 | - * @since 3.15.0 |
|
| 162 | - * |
|
| 163 | - * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 164 | - * |
|
| 165 | - * @return string The WHERE clause. |
|
| 166 | - */ |
|
| 167 | - private static function where_object_id( $object_id ) { |
|
| 168 | - |
|
| 169 | - return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Add the exclude clause. |
|
| 174 | - * |
|
| 175 | - * @since 3.15.0 |
|
| 176 | - * |
|
| 177 | - * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 178 | - * |
|
| 179 | - * @return string The exclude clause. |
|
| 180 | - */ |
|
| 181 | - private static function and_article_not_in( $exclude ) { |
|
| 182 | - |
|
| 183 | - return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Add the include clause. |
|
| 188 | - * |
|
| 189 | - * @since 3.15.0 |
|
| 190 | - * |
|
| 191 | - * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 192 | - * |
|
| 193 | - * @return string An empty string if $include is null otherwise the include |
|
| 194 | - * clause. |
|
| 195 | - */ |
|
| 196 | - private static function and_article_in( $include = null ) { |
|
| 197 | - |
|
| 198 | - if ( null === $include ) { |
|
| 199 | - return ''; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 207 | - * |
|
| 208 | - * @since 3.15.0 |
|
| 209 | - * |
|
| 210 | - * @param int $object_id The object {@link WP_Post}'s id. |
|
| 211 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 212 | - * '*' to return all fields, by default '*'. |
|
| 213 | - * @param null|string $status The status, by default all. |
|
| 214 | - * |
|
| 215 | - * @return array|object|null Database query results |
|
| 216 | - */ |
|
| 217 | - public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 218 | - global $wpdb; |
|
| 219 | - |
|
| 220 | - // The output fields. |
|
| 221 | - $actual_fields = self::fields( $fields ); |
|
| 222 | - |
|
| 223 | - $sql = $wpdb->prepare( |
|
| 224 | - " |
|
| 99 | + // Add the status clause. |
|
| 100 | + . self::and_status( $status ) |
|
| 101 | + . self::inner_join_is_article() |
|
| 102 | + . self::where_object_id( $objects ) |
|
| 103 | + // Since `object_id` can be an article ID we need to exclude it from |
|
| 104 | + // the results. |
|
| 105 | + . self::and_article_not_in( array_merge( $excludes, (array) $object_id ) ) |
|
| 106 | + . self::and_article_in( $include ) |
|
| 107 | + . " AND p.post_type IN ( 'post', 'page', 'entity' ) " |
|
| 108 | + . self::and_predicate( $predicate ) |
|
| 109 | + . self::limit( $limit ); |
|
| 110 | + |
|
| 111 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Add the limit clause if specified. |
|
| 116 | + * |
|
| 117 | + * @since 3.15.0 |
|
| 118 | + * |
|
| 119 | + * @param null|int $limit The maximum number of results. |
|
| 120 | + * |
|
| 121 | + * @return string The limit clause (empty if no limit has been specified). |
|
| 122 | + */ |
|
| 123 | + private static function limit( $limit = null ) { |
|
| 124 | + |
|
| 125 | + if ( null === $limit ) { |
|
| 126 | + return ''; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + return "LIMIT $limit"; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Map the provided ids into entities (i.e. return the id if it's an entity |
|
| 134 | + * or get the entities if it's a post). |
|
| 135 | + * |
|
| 136 | + * @since 3.15.0 |
|
| 137 | + * |
|
| 138 | + * @param int|array $object_id An array of posts/entities' ids. |
|
| 139 | + * |
|
| 140 | + * @return array An array of entities' ids. |
|
| 141 | + */ |
|
| 142 | + private function article_id_to_entity_id( $object_id ) { |
|
| 143 | + |
|
| 144 | + $entity_service = Wordlift_Entity_Service::get_instance(); |
|
| 145 | + |
|
| 146 | + $relation_service = $this; |
|
| 147 | + |
|
| 148 | + return array_reduce( (array) $object_id, function ( $carry, $item ) use ( $entity_service, $relation_service ) { |
|
| 149 | + if ( $entity_service->is_entity( $item ) ) { |
|
| 150 | + return array_merge( $carry, (array) $item ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return array_merge( $carry, $relation_service->get_objects( $item, 'ids' ) ); |
|
| 154 | + }, array() ); |
|
| 155 | + |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Add the WHERE clause. |
|
| 160 | + * |
|
| 161 | + * @since 3.15.0 |
|
| 162 | + * |
|
| 163 | + * @param int|array $object_id An array of {@link WP_Post}s' ids. |
|
| 164 | + * |
|
| 165 | + * @return string The WHERE clause. |
|
| 166 | + */ |
|
| 167 | + private static function where_object_id( $object_id ) { |
|
| 168 | + |
|
| 169 | + return ' WHERE r.object_id IN ( ' . implode( ',', wp_parse_id_list( (array) $object_id ) ) . ' )'; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Add the exclude clause. |
|
| 174 | + * |
|
| 175 | + * @since 3.15.0 |
|
| 176 | + * |
|
| 177 | + * @param int|array $exclude An array of {@link WP_Post}s' ids to exclude. |
|
| 178 | + * |
|
| 179 | + * @return string The exclude clause. |
|
| 180 | + */ |
|
| 181 | + private static function and_article_not_in( $exclude ) { |
|
| 182 | + |
|
| 183 | + return ' AND NOT p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $exclude ) ) . ' )'; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Add the include clause. |
|
| 188 | + * |
|
| 189 | + * @since 3.15.0 |
|
| 190 | + * |
|
| 191 | + * @param null|int|array $include An array of {@link WP_Post}s' ids. |
|
| 192 | + * |
|
| 193 | + * @return string An empty string if $include is null otherwise the include |
|
| 194 | + * clause. |
|
| 195 | + */ |
|
| 196 | + private static function and_article_in( $include = null ) { |
|
| 197 | + |
|
| 198 | + if ( null === $include ) { |
|
| 199 | + return ''; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return ' AND p.ID IN ( ' . implode( ',', wp_parse_id_list( (array) $include ) ) . ' )'; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Get the entities' {@link WP_Post}s' ids referencing the specified {@link WP_Post}. |
|
| 207 | + * |
|
| 208 | + * @since 3.15.0 |
|
| 209 | + * |
|
| 210 | + * @param int $object_id The object {@link WP_Post}'s id. |
|
| 211 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 212 | + * '*' to return all fields, by default '*'. |
|
| 213 | + * @param null|string $status The status, by default all. |
|
| 214 | + * |
|
| 215 | + * @return array|object|null Database query results |
|
| 216 | + */ |
|
| 217 | + public function get_non_article_subjects( $object_id, $fields = '*', $status = null ) { |
|
| 218 | + global $wpdb; |
|
| 219 | + |
|
| 220 | + // The output fields. |
|
| 221 | + $actual_fields = self::fields( $fields ); |
|
| 222 | + |
|
| 223 | + $sql = $wpdb->prepare( |
|
| 224 | + " |
|
| 225 | 225 | SELECT p.$actual_fields |
| 226 | 226 | FROM {$this->relation_table} r |
| 227 | 227 | INNER JOIN $wpdb->posts p |
| 228 | 228 | ON p.id = r.subject_id |
| 229 | 229 | " |
| 230 | - // Add the status clause. |
|
| 231 | - . self::and_status( $status ) |
|
| 232 | - . self::inner_join_is_not_article() . |
|
| 233 | - " |
|
| 230 | + // Add the status clause. |
|
| 231 | + . self::and_status( $status ) |
|
| 232 | + . self::inner_join_is_not_article() . |
|
| 233 | + " |
|
| 234 | 234 | WHERE r.object_id = %d |
| 235 | 235 | AND p.post_type IN ( 'post', 'page', 'entity' ) |
| 236 | 236 | " |
| 237 | - , |
|
| 238 | - $object_id |
|
| 239 | - ); |
|
| 240 | - |
|
| 241 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Get the entities referenced by the specified {@link WP_Post}. |
|
| 246 | - * |
|
| 247 | - * @since 3.15.0 |
|
| 248 | - * |
|
| 249 | - * @param int $subject_id The {@link WP_Post}'s id. |
|
| 250 | - * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 251 | - * '*' to return all fields, by default '*'. |
|
| 252 | - * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 253 | - * @param null|string $status The status, by default all. |
|
| 254 | - * |
|
| 255 | - * @return array|object|null Database query results |
|
| 256 | - */ |
|
| 257 | - public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 258 | - global $wpdb; |
|
| 259 | - |
|
| 260 | - // The output fields. |
|
| 261 | - $actual_fields = self::fields( $fields ); |
|
| 262 | - |
|
| 263 | - $sql = $wpdb->prepare( |
|
| 264 | - " |
|
| 237 | + , |
|
| 238 | + $object_id |
|
| 239 | + ); |
|
| 240 | + |
|
| 241 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Get the entities referenced by the specified {@link WP_Post}. |
|
| 246 | + * |
|
| 247 | + * @since 3.15.0 |
|
| 248 | + * |
|
| 249 | + * @param int $subject_id The {@link WP_Post}'s id. |
|
| 250 | + * @param string $fields The fields to return, 'ids' to only return ids or |
|
| 251 | + * '*' to return all fields, by default '*'. |
|
| 252 | + * @param null|string $predicate The predicate (who|what|...), by default all. |
|
| 253 | + * @param null|string $status The status, by default all. |
|
| 254 | + * |
|
| 255 | + * @return array|object|null Database query results |
|
| 256 | + */ |
|
| 257 | + public function get_objects( $subject_id, $fields = '*', $predicate = null, $status = null ) { |
|
| 258 | + global $wpdb; |
|
| 259 | + |
|
| 260 | + // The output fields. |
|
| 261 | + $actual_fields = self::fields( $fields ); |
|
| 262 | + |
|
| 263 | + $sql = $wpdb->prepare( |
|
| 264 | + " |
|
| 265 | 265 | SELECT p.$actual_fields |
| 266 | 266 | FROM {$this->relation_table} r |
| 267 | 267 | INNER JOIN $wpdb->posts p |
| 268 | 268 | ON p.id = r.object_id |
| 269 | 269 | " |
| 270 | - // Add the status clause. |
|
| 271 | - . self::and_status( $status ) |
|
| 272 | - . self::inner_join_is_not_article() |
|
| 273 | - . " |
|
| 270 | + // Add the status clause. |
|
| 271 | + . self::and_status( $status ) |
|
| 272 | + . self::inner_join_is_not_article() |
|
| 273 | + . " |
|
| 274 | 274 | WHERE r.subject_id = %d |
| 275 | 275 | AND p.post_type IN ( 'post', 'page', 'entity' ) |
| 276 | 276 | " |
| 277 | - . self::and_predicate( $predicate ) |
|
| 278 | - , |
|
| 279 | - $subject_id |
|
| 280 | - ); |
|
| 281 | - |
|
| 282 | - return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Add the `post_status` clause. |
|
| 287 | - * |
|
| 288 | - * @since 3.15.0 |
|
| 289 | - * |
|
| 290 | - * @param null|string|array $status The status values. |
|
| 291 | - * |
|
| 292 | - * @return string An empty string if $status is null, otherwise the status clause. |
|
| 293 | - */ |
|
| 294 | - private static function and_status( $status = null ) { |
|
| 295 | - |
|
| 296 | - if ( null === $status ) { |
|
| 297 | - return ''; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Add the `predicate` clause. |
|
| 305 | - * |
|
| 306 | - * @since 3.15.0 |
|
| 307 | - * |
|
| 308 | - * @param null|string|array $predicate An array of predicates. |
|
| 309 | - * |
|
| 310 | - * @return string An empty string if $predicate is null otherwise the predicate |
|
| 311 | - * clause. |
|
| 312 | - */ |
|
| 313 | - private static function and_predicate( $predicate = null ) { |
|
| 314 | - |
|
| 315 | - if ( null === $predicate ) { |
|
| 316 | - return ''; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * The select fields. |
|
| 324 | - * |
|
| 325 | - * @since 3.15.0 |
|
| 326 | - * |
|
| 327 | - * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 328 | - * |
|
| 329 | - * @return string The `id` field if `ids` otherwise `*`. |
|
| 330 | - */ |
|
| 331 | - private static function fields( $fields = '*' ) { |
|
| 332 | - |
|
| 333 | - // The output fields. |
|
| 334 | - return 'ids' === $fields ? 'id' : '*'; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * The inner join clause for articles. |
|
| 339 | - * |
|
| 340 | - * @since 3.15.0 |
|
| 341 | - * |
|
| 342 | - * @return string The articles inner join clause. |
|
| 343 | - */ |
|
| 344 | - private static function inner_join_is_article() { |
|
| 345 | - global $wpdb; |
|
| 346 | - |
|
| 347 | - return $wpdb->prepare( |
|
| 348 | - " |
|
| 277 | + . self::and_predicate( $predicate ) |
|
| 278 | + , |
|
| 279 | + $subject_id |
|
| 280 | + ); |
|
| 281 | + |
|
| 282 | + return '*' === $actual_fields ? $wpdb->get_results( $sql ) : $wpdb->get_col( $sql ); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Add the `post_status` clause. |
|
| 287 | + * |
|
| 288 | + * @since 3.15.0 |
|
| 289 | + * |
|
| 290 | + * @param null|string|array $status The status values. |
|
| 291 | + * |
|
| 292 | + * @return string An empty string if $status is null, otherwise the status clause. |
|
| 293 | + */ |
|
| 294 | + private static function and_status( $status = null ) { |
|
| 295 | + |
|
| 296 | + if ( null === $status ) { |
|
| 297 | + return ''; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + return " AND p.post_status IN ('" . implode( "', '", array_map( 'esc_sql', (array) $status ) ) . "')"; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Add the `predicate` clause. |
|
| 305 | + * |
|
| 306 | + * @since 3.15.0 |
|
| 307 | + * |
|
| 308 | + * @param null|string|array $predicate An array of predicates. |
|
| 309 | + * |
|
| 310 | + * @return string An empty string if $predicate is null otherwise the predicate |
|
| 311 | + * clause. |
|
| 312 | + */ |
|
| 313 | + private static function and_predicate( $predicate = null ) { |
|
| 314 | + |
|
| 315 | + if ( null === $predicate ) { |
|
| 316 | + return ''; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return " AND r.predicate IN ('" . implode( "', '", array_map( 'esc_sql', (array) $predicate ) ) . "')"; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * The select fields. |
|
| 324 | + * |
|
| 325 | + * @since 3.15.0 |
|
| 326 | + * |
|
| 327 | + * @param string $fields Either 'ids' or '*', by default '*'. |
|
| 328 | + * |
|
| 329 | + * @return string The `id` field if `ids` otherwise `*`. |
|
| 330 | + */ |
|
| 331 | + private static function fields( $fields = '*' ) { |
|
| 332 | + |
|
| 333 | + // The output fields. |
|
| 334 | + return 'ids' === $fields ? 'id' : '*'; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * The inner join clause for articles. |
|
| 339 | + * |
|
| 340 | + * @since 3.15.0 |
|
| 341 | + * |
|
| 342 | + * @return string The articles inner join clause. |
|
| 343 | + */ |
|
| 344 | + private static function inner_join_is_article() { |
|
| 345 | + global $wpdb; |
|
| 346 | + |
|
| 347 | + return $wpdb->prepare( |
|
| 348 | + " |
|
| 349 | 349 | INNER JOIN $wpdb->term_relationships tr |
| 350 | 350 | ON p.id = tr.object_id |
| 351 | 351 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -355,23 +355,23 @@ discard block |
||
| 355 | 355 | ON t.term_id = tt.term_id |
| 356 | 356 | AND t.slug = %s |
| 357 | 357 | ", |
| 358 | - 'wl_entity_type', |
|
| 359 | - 'article' |
|
| 360 | - ); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * The inner join clause for non-articles. |
|
| 365 | - * |
|
| 366 | - * @since 3.15.0 |
|
| 367 | - * |
|
| 368 | - * @return string The non-articles inner join clause. |
|
| 369 | - */ |
|
| 370 | - private static function inner_join_is_not_article() { |
|
| 371 | - global $wpdb; |
|
| 372 | - |
|
| 373 | - return $wpdb->prepare( |
|
| 374 | - " |
|
| 358 | + 'wl_entity_type', |
|
| 359 | + 'article' |
|
| 360 | + ); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * The inner join clause for non-articles. |
|
| 365 | + * |
|
| 366 | + * @since 3.15.0 |
|
| 367 | + * |
|
| 368 | + * @return string The non-articles inner join clause. |
|
| 369 | + */ |
|
| 370 | + private static function inner_join_is_not_article() { |
|
| 371 | + global $wpdb; |
|
| 372 | + |
|
| 373 | + return $wpdb->prepare( |
|
| 374 | + " |
|
| 375 | 375 | INNER JOIN $wpdb->term_relationships tr |
| 376 | 376 | ON p.id = tr.object_id |
| 377 | 377 | INNER JOIN $wpdb->term_taxonomy tt |
@@ -381,9 +381,9 @@ discard block |
||
| 381 | 381 | ON t.term_id = tt.term_id |
| 382 | 382 | AND NOT t.slug = %s |
| 383 | 383 | ", |
| 384 | - 'wl_entity_type', |
|
| 385 | - 'article' |
|
| 386 | - ); |
|
| 387 | - } |
|
| 384 | + 'wl_entity_type', |
|
| 385 | + 'article' |
|
| 386 | + ); |
|
| 387 | + } |
|
| 388 | 388 | |
| 389 | 389 | } |
@@ -22,107 +22,107 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Wordlift_Public { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The ID of this plugin. |
|
| 27 | - * |
|
| 28 | - * @since 1.0.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var string $plugin_name The ID of this plugin. |
|
| 31 | - */ |
|
| 32 | - private $plugin_name; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The version of this plugin. |
|
| 36 | - * |
|
| 37 | - * @since 1.0.0 |
|
| 38 | - * @access private |
|
| 39 | - * @var string $version The current version of this plugin. |
|
| 40 | - */ |
|
| 41 | - private $version; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Initialize the class and set its properties. |
|
| 45 | - * |
|
| 46 | - * @since 1.0.0 |
|
| 47 | - * |
|
| 48 | - * @param string $plugin_name The name of the plugin. |
|
| 49 | - * @param string $version The version of this plugin. |
|
| 50 | - */ |
|
| 51 | - public function __construct( $plugin_name, $version ) { |
|
| 52 | - |
|
| 53 | - $this->plugin_name = $plugin_name; |
|
| 54 | - $this->version = $version; |
|
| 55 | - |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Register the stylesheets for the public-facing side of the site. |
|
| 60 | - * |
|
| 61 | - * @since 1.0.0 |
|
| 62 | - */ |
|
| 63 | - public function enqueue_styles() { |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * This function is provided for demonstration purposes only. |
|
| 67 | - * |
|
| 68 | - * An instance of this class should be passed to the run() function |
|
| 69 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 70 | - * in that particular class. |
|
| 71 | - * |
|
| 72 | - * The Wordlift_Loader will then create the relationship |
|
| 73 | - * between the defined hooks and the functions defined in this |
|
| 74 | - * class. |
|
| 75 | - */ |
|
| 76 | - |
|
| 77 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' ); |
|
| 78 | - |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Register the stylesheets for the public-facing side of the site. |
|
| 83 | - * |
|
| 84 | - * @since 1.0.0 |
|
| 85 | - */ |
|
| 86 | - public function enqueue_scripts() { |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * This function is provided for demonstration purposes only. |
|
| 90 | - * |
|
| 91 | - * An instance of this class should be passed to the run() function |
|
| 92 | - * defined in Wordlift_Loader as all of the hooks are defined |
|
| 93 | - * in that particular class. |
|
| 94 | - * |
|
| 95 | - * The Wordlift_Loader will then create the relationship |
|
| 96 | - * between the defined hooks and the functions defined in this |
|
| 97 | - * class. |
|
| 98 | - */ |
|
| 99 | - |
|
| 100 | - // Prepare a settings array for client-side functions. |
|
| 101 | - $settings = array( |
|
| 102 | - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 103 | - ); |
|
| 104 | - |
|
| 105 | - // If we're in a single page, then print out the post id. |
|
| 106 | - if ( is_singular() ) { |
|
| 107 | - $settings['postId'] = get_the_ID(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Add flag that we are on home/blog page. |
|
| 111 | - if ( is_home() || is_front_page() ) { |
|
| 112 | - $settings['isHome'] = true; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - // Add the JSON-LD enabled flag, when set to false, the JSON-lD won't |
|
| 116 | - // be loaded. |
|
| 117 | - // |
|
| 118 | - // @see https://github.com/insideout10/wordlift-plugin/issues/642. |
|
| 119 | - $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', true ); |
|
| 120 | - |
|
| 121 | - // Note that we switched the js to be loaded in footer, since it is loading |
|
| 122 | - // the json-ld representation. |
|
| 123 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wordlift-public.js', array( 'jquery' ), $this->version, true ); |
|
| 124 | - wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 125 | - |
|
| 126 | - } |
|
| 25 | + /** |
|
| 26 | + * The ID of this plugin. |
|
| 27 | + * |
|
| 28 | + * @since 1.0.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var string $plugin_name The ID of this plugin. |
|
| 31 | + */ |
|
| 32 | + private $plugin_name; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The version of this plugin. |
|
| 36 | + * |
|
| 37 | + * @since 1.0.0 |
|
| 38 | + * @access private |
|
| 39 | + * @var string $version The current version of this plugin. |
|
| 40 | + */ |
|
| 41 | + private $version; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Initialize the class and set its properties. |
|
| 45 | + * |
|
| 46 | + * @since 1.0.0 |
|
| 47 | + * |
|
| 48 | + * @param string $plugin_name The name of the plugin. |
|
| 49 | + * @param string $version The version of this plugin. |
|
| 50 | + */ |
|
| 51 | + public function __construct( $plugin_name, $version ) { |
|
| 52 | + |
|
| 53 | + $this->plugin_name = $plugin_name; |
|
| 54 | + $this->version = $version; |
|
| 55 | + |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Register the stylesheets for the public-facing side of the site. |
|
| 60 | + * |
|
| 61 | + * @since 1.0.0 |
|
| 62 | + */ |
|
| 63 | + public function enqueue_styles() { |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * This function is provided for demonstration purposes only. |
|
| 67 | + * |
|
| 68 | + * An instance of this class should be passed to the run() function |
|
| 69 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 70 | + * in that particular class. |
|
| 71 | + * |
|
| 72 | + * The Wordlift_Loader will then create the relationship |
|
| 73 | + * between the defined hooks and the functions defined in this |
|
| 74 | + * class. |
|
| 75 | + */ |
|
| 76 | + |
|
| 77 | + wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' ); |
|
| 78 | + |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Register the stylesheets for the public-facing side of the site. |
|
| 83 | + * |
|
| 84 | + * @since 1.0.0 |
|
| 85 | + */ |
|
| 86 | + public function enqueue_scripts() { |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * This function is provided for demonstration purposes only. |
|
| 90 | + * |
|
| 91 | + * An instance of this class should be passed to the run() function |
|
| 92 | + * defined in Wordlift_Loader as all of the hooks are defined |
|
| 93 | + * in that particular class. |
|
| 94 | + * |
|
| 95 | + * The Wordlift_Loader will then create the relationship |
|
| 96 | + * between the defined hooks and the functions defined in this |
|
| 97 | + * class. |
|
| 98 | + */ |
|
| 99 | + |
|
| 100 | + // Prepare a settings array for client-side functions. |
|
| 101 | + $settings = array( |
|
| 102 | + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 103 | + ); |
|
| 104 | + |
|
| 105 | + // If we're in a single page, then print out the post id. |
|
| 106 | + if ( is_singular() ) { |
|
| 107 | + $settings['postId'] = get_the_ID(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Add flag that we are on home/blog page. |
|
| 111 | + if ( is_home() || is_front_page() ) { |
|
| 112 | + $settings['isHome'] = true; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + // Add the JSON-LD enabled flag, when set to false, the JSON-lD won't |
|
| 116 | + // be loaded. |
|
| 117 | + // |
|
| 118 | + // @see https://github.com/insideout10/wordlift-plugin/issues/642. |
|
| 119 | + $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', true ); |
|
| 120 | + |
|
| 121 | + // Note that we switched the js to be loaded in footer, since it is loading |
|
| 122 | + // the json-ld representation. |
|
| 123 | + wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wordlift-public.js', array( 'jquery' ), $this->version, true ); |
|
| 124 | + wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | 128 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @param string $plugin_name The name of the plugin. |
| 49 | 49 | * @param string $version The version of this plugin. |
| 50 | 50 | */ |
| 51 | - public function __construct( $plugin_name, $version ) { |
|
| 51 | + public function __construct($plugin_name, $version) { |
|
| 52 | 52 | |
| 53 | 53 | $this->plugin_name = $plugin_name; |
| 54 | 54 | $this->version = $version; |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | * class. |
| 75 | 75 | */ |
| 76 | 76 | |
| 77 | - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wordlift-public.css', array(), $this->version, 'all' ); |
|
| 77 | + wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__).'css/wordlift-public.css', array(), $this->version, 'all'); |
|
| 78 | 78 | |
| 79 | 79 | } |
| 80 | 80 | |
@@ -99,16 +99,16 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | // Prepare a settings array for client-side functions. |
| 101 | 101 | $settings = array( |
| 102 | - 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
|
| 102 | + 'ajaxUrl' => admin_url('admin-ajax.php'), |
|
| 103 | 103 | ); |
| 104 | 104 | |
| 105 | 105 | // If we're in a single page, then print out the post id. |
| 106 | - if ( is_singular() ) { |
|
| 106 | + if (is_singular()) { |
|
| 107 | 107 | $settings['postId'] = get_the_ID(); |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // Add flag that we are on home/blog page. |
| 111 | - if ( is_home() || is_front_page() ) { |
|
| 111 | + if (is_home() || is_front_page()) { |
|
| 112 | 112 | $settings['isHome'] = true; |
| 113 | 113 | } |
| 114 | 114 | |
@@ -116,12 +116,12 @@ discard block |
||
| 116 | 116 | // be loaded. |
| 117 | 117 | // |
| 118 | 118 | // @see https://github.com/insideout10/wordlift-plugin/issues/642. |
| 119 | - $settings['jsonld_enabled'] = apply_filters( 'wl_jsonld_enabled', true ); |
|
| 119 | + $settings['jsonld_enabled'] = apply_filters('wl_jsonld_enabled', true); |
|
| 120 | 120 | |
| 121 | 121 | // Note that we switched the js to be loaded in footer, since it is loading |
| 122 | 122 | // the json-ld representation. |
| 123 | - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wordlift-public.js', array( 'jquery' ), $this->version, true ); |
|
| 124 | - wp_localize_script( $this->plugin_name, 'wlSettings', $settings ); |
|
| 123 | + wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__).'js/wordlift-public.js', array('jquery'), $this->version, true); |
|
| 124 | + wp_localize_script($this->plugin_name, 'wlSettings', $settings); |
|
| 125 | 125 | |
| 126 | 126 | } |
| 127 | 127 | |
@@ -19,122 +19,122 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Admin_Publisher_Element extends Wordlift_Admin_Author_Element { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.11.0 |
|
| 26 | - * @access private |
|
| 27 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 28 | - */ |
|
| 29 | - private $configuration_service; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The {@link Wordlift_Publisher_Service} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.11.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 37 | - */ |
|
| 38 | - private $publisher_service; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var Wordlift_Admin_Tabs_Element |
|
| 42 | - */ |
|
| 43 | - private $tabs_element; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Create a {@link Wordlift_Admin_Publisher_Element} instance. |
|
| 47 | - * |
|
| 48 | - * @since 3.11.0 |
|
| 49 | - * |
|
| 50 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 51 | - * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 52 | - * @param \Wordlift_Admin_Tabs_Element $tabs_element The {@link Wordlift_Admin_Tabs_Element} instance. |
|
| 53 | - * @param \Wordlift_Admin_Select2_Element $select_element The {@link Wordlift_Admin_Select_Element} instance. |
|
| 54 | - */ |
|
| 55 | - function __construct( $configuration_service, $publisher_service, $tabs_element, $select_element ) { |
|
| 56 | - parent::__construct( $publisher_service, $select_element ); |
|
| 57 | - |
|
| 58 | - $this->configuration_service = $configuration_service; |
|
| 59 | - $this->publisher_service = $publisher_service; |
|
| 60 | - |
|
| 61 | - // Child elements. |
|
| 62 | - $this->tabs_element = $tabs_element; |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @inheritdoc |
|
| 68 | - */ |
|
| 69 | - public function render( $args ) { |
|
| 70 | - |
|
| 71 | - // Parse the arguments and merge with default values. |
|
| 72 | - $params = wp_parse_args( $args, array( |
|
| 73 | - 'id' => uniqid( 'wl-input-' ), |
|
| 74 | - 'name' => uniqid( 'wl-input-' ), |
|
| 75 | - ) ); |
|
| 76 | - |
|
| 77 | - // Get the number of potential candidates as publishers. |
|
| 78 | - $count = $this->publisher_service->count(); |
|
| 79 | - |
|
| 80 | - $this->tabs_element->render( array( |
|
| 81 | - 'tabs' => array( |
|
| 82 | - array( |
|
| 83 | - 'label' => __( 'Select an Existing Publisher', 'wordlift' ), |
|
| 84 | - 'callback' => array( $this, 'select' ), |
|
| 85 | - 'args' => $params, |
|
| 86 | - ), |
|
| 87 | - array( |
|
| 88 | - 'label' => __( 'Create a New Publisher', 'wordlift' ), |
|
| 89 | - 'callback' => array( $this, 'create' ), |
|
| 90 | - 'args' => $params, |
|
| 91 | - ), |
|
| 92 | - ), |
|
| 93 | - // Set the default tab according to the number of potential publishers |
|
| 94 | - // configured in WP: 0 = select, 1 = create. |
|
| 95 | - 'active' => 0 === $count ? 1 : 0, |
|
| 96 | - ) ); |
|
| 97 | - |
|
| 98 | - // Finally return the element instance. |
|
| 99 | - return $this; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Render the publisher's select. |
|
| 104 | - * |
|
| 105 | - * @since 3.11.0 |
|
| 106 | - * |
|
| 107 | - * @param array $params An array of parameters. |
|
| 108 | - */ |
|
| 109 | - public function select( $params ) { |
|
| 110 | - |
|
| 111 | - // Get the configured publisher id. In case a publisher id is already configured |
|
| 112 | - // this must be pre-loaded in the options. |
|
| 113 | - $publisher_id = $this->configuration_service->get_publisher_id(); |
|
| 114 | - |
|
| 115 | - // Get the publisher data. |
|
| 116 | - $data = $this->publisher_service->query(); |
|
| 117 | - array_unshift( $data, array( |
|
| 118 | - 'id' => '', |
|
| 119 | - 'text' => _x( '(none)', 'Publisher Select in Settings Screen.', 'wordlift' ), |
|
| 120 | - 'type' => '', |
|
| 121 | - 'thumbnail_url' => false, |
|
| 122 | - ) ); |
|
| 123 | - |
|
| 124 | - // Call the select internal render. |
|
| 125 | - $this->do_render( $params, $publisher_id, $data ); |
|
| 126 | - |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Render the 'create publisher' form. |
|
| 131 | - * |
|
| 132 | - * @since 3.11.0 |
|
| 133 | - * |
|
| 134 | - * @param array $params An array of parameters. |
|
| 135 | - */ |
|
| 136 | - public function create( $params ) { |
|
| 137 | - ?> |
|
| 22 | + /** |
|
| 23 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.11.0 |
|
| 26 | + * @access private |
|
| 27 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 28 | + */ |
|
| 29 | + private $configuration_service; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The {@link Wordlift_Publisher_Service} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.11.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 37 | + */ |
|
| 38 | + private $publisher_service; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var Wordlift_Admin_Tabs_Element |
|
| 42 | + */ |
|
| 43 | + private $tabs_element; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Create a {@link Wordlift_Admin_Publisher_Element} instance. |
|
| 47 | + * |
|
| 48 | + * @since 3.11.0 |
|
| 49 | + * |
|
| 50 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 51 | + * @param \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance. |
|
| 52 | + * @param \Wordlift_Admin_Tabs_Element $tabs_element The {@link Wordlift_Admin_Tabs_Element} instance. |
|
| 53 | + * @param \Wordlift_Admin_Select2_Element $select_element The {@link Wordlift_Admin_Select_Element} instance. |
|
| 54 | + */ |
|
| 55 | + function __construct( $configuration_service, $publisher_service, $tabs_element, $select_element ) { |
|
| 56 | + parent::__construct( $publisher_service, $select_element ); |
|
| 57 | + |
|
| 58 | + $this->configuration_service = $configuration_service; |
|
| 59 | + $this->publisher_service = $publisher_service; |
|
| 60 | + |
|
| 61 | + // Child elements. |
|
| 62 | + $this->tabs_element = $tabs_element; |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @inheritdoc |
|
| 68 | + */ |
|
| 69 | + public function render( $args ) { |
|
| 70 | + |
|
| 71 | + // Parse the arguments and merge with default values. |
|
| 72 | + $params = wp_parse_args( $args, array( |
|
| 73 | + 'id' => uniqid( 'wl-input-' ), |
|
| 74 | + 'name' => uniqid( 'wl-input-' ), |
|
| 75 | + ) ); |
|
| 76 | + |
|
| 77 | + // Get the number of potential candidates as publishers. |
|
| 78 | + $count = $this->publisher_service->count(); |
|
| 79 | + |
|
| 80 | + $this->tabs_element->render( array( |
|
| 81 | + 'tabs' => array( |
|
| 82 | + array( |
|
| 83 | + 'label' => __( 'Select an Existing Publisher', 'wordlift' ), |
|
| 84 | + 'callback' => array( $this, 'select' ), |
|
| 85 | + 'args' => $params, |
|
| 86 | + ), |
|
| 87 | + array( |
|
| 88 | + 'label' => __( 'Create a New Publisher', 'wordlift' ), |
|
| 89 | + 'callback' => array( $this, 'create' ), |
|
| 90 | + 'args' => $params, |
|
| 91 | + ), |
|
| 92 | + ), |
|
| 93 | + // Set the default tab according to the number of potential publishers |
|
| 94 | + // configured in WP: 0 = select, 1 = create. |
|
| 95 | + 'active' => 0 === $count ? 1 : 0, |
|
| 96 | + ) ); |
|
| 97 | + |
|
| 98 | + // Finally return the element instance. |
|
| 99 | + return $this; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Render the publisher's select. |
|
| 104 | + * |
|
| 105 | + * @since 3.11.0 |
|
| 106 | + * |
|
| 107 | + * @param array $params An array of parameters. |
|
| 108 | + */ |
|
| 109 | + public function select( $params ) { |
|
| 110 | + |
|
| 111 | + // Get the configured publisher id. In case a publisher id is already configured |
|
| 112 | + // this must be pre-loaded in the options. |
|
| 113 | + $publisher_id = $this->configuration_service->get_publisher_id(); |
|
| 114 | + |
|
| 115 | + // Get the publisher data. |
|
| 116 | + $data = $this->publisher_service->query(); |
|
| 117 | + array_unshift( $data, array( |
|
| 118 | + 'id' => '', |
|
| 119 | + 'text' => _x( '(none)', 'Publisher Select in Settings Screen.', 'wordlift' ), |
|
| 120 | + 'type' => '', |
|
| 121 | + 'thumbnail_url' => false, |
|
| 122 | + ) ); |
|
| 123 | + |
|
| 124 | + // Call the select internal render. |
|
| 125 | + $this->do_render( $params, $publisher_id, $data ); |
|
| 126 | + |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Render the 'create publisher' form. |
|
| 131 | + * |
|
| 132 | + * @since 3.11.0 |
|
| 133 | + * |
|
| 134 | + * @param array $params An array of parameters. |
|
| 135 | + */ |
|
| 136 | + public function create( $params ) { |
|
| 137 | + ?> |
|
| 138 | 138 | <p> |
| 139 | 139 | <strong><?php esc_html_e( 'Are you publishing as an individual or as a company?', 'wordlift' ) ?></strong> |
| 140 | 140 | </p> |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | name="wl_publisher[type]" value="person" |
| 145 | 145 | checked="checked"> |
| 146 | 146 | <label for="wl-publisher-person"><?php |
| 147 | - esc_html_e( 'Person', 'wordlift' ) ?></label> |
|
| 147 | + esc_html_e( 'Person', 'wordlift' ) ?></label> |
|
| 148 | 148 | </span> |
| 149 | 149 | <span> |
| 150 | 150 | <input id="wl-publisher-company" type="radio" |
| 151 | 151 | name="wl_publisher[type]" value="organization""> |
| 152 | 152 | <label for="wl-publisher-company"><?php |
| 153 | - esc_html_e( 'Company', 'wordlift' ) ?></label> |
|
| 153 | + esc_html_e( 'Company', 'wordlift' ) ?></label> |
|
| 154 | 154 | </span> |
| 155 | 155 | </p> |
| 156 | 156 | <p id="wl-publisher-name"> |
@@ -167,10 +167,10 @@ discard block |
||
| 167 | 167 | <img id="wl-publisher-media-uploader-preview" /> |
| 168 | 168 | <button type="button" class="button" |
| 169 | 169 | id="wl-publisher-media-uploader"><?php |
| 170 | - esc_html_e( 'Select an existing image or upload a new one', 'wordlift' ); ?></button> |
|
| 170 | + esc_html_e( 'Select an existing image or upload a new one', 'wordlift' ); ?></button> |
|
| 171 | 171 | </p> |
| 172 | 172 | </div> |
| 173 | 173 | <?php |
| 174 | - } |
|
| 174 | + } |
|
| 175 | 175 | |
| 176 | 176 | } |
@@ -52,8 +52,8 @@ discard block |
||
| 52 | 52 | * @param \Wordlift_Admin_Tabs_Element $tabs_element The {@link Wordlift_Admin_Tabs_Element} instance. |
| 53 | 53 | * @param \Wordlift_Admin_Select2_Element $select_element The {@link Wordlift_Admin_Select_Element} instance. |
| 54 | 54 | */ |
| 55 | - function __construct( $configuration_service, $publisher_service, $tabs_element, $select_element ) { |
|
| 56 | - parent::__construct( $publisher_service, $select_element ); |
|
| 55 | + function __construct($configuration_service, $publisher_service, $tabs_element, $select_element) { |
|
| 56 | + parent::__construct($publisher_service, $select_element); |
|
| 57 | 57 | |
| 58 | 58 | $this->configuration_service = $configuration_service; |
| 59 | 59 | $this->publisher_service = $publisher_service; |
@@ -66,34 +66,34 @@ discard block |
||
| 66 | 66 | /** |
| 67 | 67 | * @inheritdoc |
| 68 | 68 | */ |
| 69 | - public function render( $args ) { |
|
| 69 | + public function render($args) { |
|
| 70 | 70 | |
| 71 | 71 | // Parse the arguments and merge with default values. |
| 72 | - $params = wp_parse_args( $args, array( |
|
| 73 | - 'id' => uniqid( 'wl-input-' ), |
|
| 74 | - 'name' => uniqid( 'wl-input-' ), |
|
| 75 | - ) ); |
|
| 72 | + $params = wp_parse_args($args, array( |
|
| 73 | + 'id' => uniqid('wl-input-'), |
|
| 74 | + 'name' => uniqid('wl-input-'), |
|
| 75 | + )); |
|
| 76 | 76 | |
| 77 | 77 | // Get the number of potential candidates as publishers. |
| 78 | 78 | $count = $this->publisher_service->count(); |
| 79 | 79 | |
| 80 | - $this->tabs_element->render( array( |
|
| 80 | + $this->tabs_element->render(array( |
|
| 81 | 81 | 'tabs' => array( |
| 82 | 82 | array( |
| 83 | - 'label' => __( 'Select an Existing Publisher', 'wordlift' ), |
|
| 84 | - 'callback' => array( $this, 'select' ), |
|
| 83 | + 'label' => __('Select an Existing Publisher', 'wordlift'), |
|
| 84 | + 'callback' => array($this, 'select'), |
|
| 85 | 85 | 'args' => $params, |
| 86 | 86 | ), |
| 87 | 87 | array( |
| 88 | - 'label' => __( 'Create a New Publisher', 'wordlift' ), |
|
| 89 | - 'callback' => array( $this, 'create' ), |
|
| 88 | + 'label' => __('Create a New Publisher', 'wordlift'), |
|
| 89 | + 'callback' => array($this, 'create'), |
|
| 90 | 90 | 'args' => $params, |
| 91 | 91 | ), |
| 92 | 92 | ), |
| 93 | 93 | // Set the default tab according to the number of potential publishers |
| 94 | 94 | // configured in WP: 0 = select, 1 = create. |
| 95 | 95 | 'active' => 0 === $count ? 1 : 0, |
| 96 | - ) ); |
|
| 96 | + )); |
|
| 97 | 97 | |
| 98 | 98 | // Finally return the element instance. |
| 99 | 99 | return $this; |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | * |
| 107 | 107 | * @param array $params An array of parameters. |
| 108 | 108 | */ |
| 109 | - public function select( $params ) { |
|
| 109 | + public function select($params) { |
|
| 110 | 110 | |
| 111 | 111 | // Get the configured publisher id. In case a publisher id is already configured |
| 112 | 112 | // this must be pre-loaded in the options. |
@@ -114,15 +114,15 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | // Get the publisher data. |
| 116 | 116 | $data = $this->publisher_service->query(); |
| 117 | - array_unshift( $data, array( |
|
| 117 | + array_unshift($data, array( |
|
| 118 | 118 | 'id' => '', |
| 119 | - 'text' => _x( '(none)', 'Publisher Select in Settings Screen.', 'wordlift' ), |
|
| 119 | + 'text' => _x('(none)', 'Publisher Select in Settings Screen.', 'wordlift'), |
|
| 120 | 120 | 'type' => '', |
| 121 | 121 | 'thumbnail_url' => false, |
| 122 | - ) ); |
|
| 122 | + )); |
|
| 123 | 123 | |
| 124 | 124 | // Call the select internal render. |
| 125 | - $this->do_render( $params, $publisher_id, $data ); |
|
| 125 | + $this->do_render($params, $publisher_id, $data); |
|
| 126 | 126 | |
| 127 | 127 | } |
| 128 | 128 | |
@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @param array $params An array of parameters. |
| 135 | 135 | */ |
| 136 | - public function create( $params ) { |
|
| 136 | + public function create($params) { |
|
| 137 | 137 | ?> |
| 138 | 138 | <p> |
| 139 | - <strong><?php esc_html_e( 'Are you publishing as an individual or as a company?', 'wordlift' ) ?></strong> |
|
| 139 | + <strong><?php esc_html_e('Are you publishing as an individual or as a company?', 'wordlift') ?></strong> |
|
| 140 | 140 | </p> |
| 141 | 141 | <p id="wl-publisher-type"> |
| 142 | 142 | <span> |
@@ -144,30 +144,30 @@ discard block |
||
| 144 | 144 | name="wl_publisher[type]" value="person" |
| 145 | 145 | checked="checked"> |
| 146 | 146 | <label for="wl-publisher-person"><?php |
| 147 | - esc_html_e( 'Person', 'wordlift' ) ?></label> |
|
| 147 | + esc_html_e('Person', 'wordlift') ?></label> |
|
| 148 | 148 | </span> |
| 149 | 149 | <span> |
| 150 | 150 | <input id="wl-publisher-company" type="radio" |
| 151 | 151 | name="wl_publisher[type]" value="organization""> |
| 152 | 152 | <label for="wl-publisher-company"><?php |
| 153 | - esc_html_e( 'Company', 'wordlift' ) ?></label> |
|
| 153 | + esc_html_e('Company', 'wordlift') ?></label> |
|
| 154 | 154 | </span> |
| 155 | 155 | </p> |
| 156 | 156 | <p id="wl-publisher-name"> |
| 157 | 157 | <input type="text" name="wl_publisher[name]" |
| 158 | - placeholder="<?php echo esc_attr__( "What's your name?", 'wordlift' ) ?>"> |
|
| 158 | + placeholder="<?php echo esc_attr__("What's your name?", 'wordlift') ?>"> |
|
| 159 | 159 | </p> |
| 160 | 160 | <div id="wl-publisher-logo"> |
| 161 | 161 | <input type="hidden" id="wl-publisher-media-uploader-id" |
| 162 | 162 | name="wl_publisher[thumbnail_id]" /> |
| 163 | 163 | <p> |
| 164 | - <b><?php esc_html_e( "Choose the publisher's Logo", 'wordlift' ) ?></b> |
|
| 164 | + <b><?php esc_html_e("Choose the publisher's Logo", 'wordlift') ?></b> |
|
| 165 | 165 | </p> |
| 166 | 166 | <p> |
| 167 | 167 | <img id="wl-publisher-media-uploader-preview" /> |
| 168 | 168 | <button type="button" class="button" |
| 169 | 169 | id="wl-publisher-media-uploader"><?php |
| 170 | - esc_html_e( 'Select an existing image or upload a new one', 'wordlift' ); ?></button> |
|
| 170 | + esc_html_e('Select an existing image or upload a new one', 'wordlift'); ?></button> |
|
| 171 | 171 | </p> |
| 172 | 172 | </div> |
| 173 | 173 | <?php |
@@ -18,236 +18,236 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Content_Filter_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The pattern to find entities in text. |
|
| 23 | - * |
|
| 24 | - * @since 3.8.0 |
|
| 25 | - */ |
|
| 26 | - const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.8.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.13.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $configuration_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The `link by default` setting. |
|
| 48 | - * |
|
| 49 | - * @since 3.13.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var bool True if link by default is enabled otherwise false. |
|
| 52 | - */ |
|
| 53 | - private $is_link_by_default; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 57 | - * |
|
| 58 | - * @since 3.14.2 |
|
| 59 | - * @access private |
|
| 60 | - * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 61 | - */ |
|
| 62 | - private static $instance; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 66 | - * |
|
| 67 | - * @since 3.8.0 |
|
| 68 | - * |
|
| 69 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 70 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 71 | - */ |
|
| 72 | - public function __construct( $entity_service, $configuration_service ) { |
|
| 73 | - |
|
| 74 | - $this->entity_service = $entity_service; |
|
| 75 | - $this->configuration_service = $configuration_service; |
|
| 76 | - |
|
| 77 | - self::$instance = $this; |
|
| 78 | - |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 83 | - * |
|
| 84 | - * @since 3.14.2 |
|
| 85 | - * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 86 | - */ |
|
| 87 | - public static function get_instance() { |
|
| 88 | - |
|
| 89 | - return self::$instance; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Mangle the content by adding links to the entity pages. This function is |
|
| 94 | - * hooked to the 'the_content' WP's filter. |
|
| 95 | - * |
|
| 96 | - * @since 3.8.0 |
|
| 97 | - * |
|
| 98 | - * @param string $content The content being filtered. |
|
| 99 | - * |
|
| 100 | - * @return string The filtered content. |
|
| 101 | - */ |
|
| 102 | - public function the_content( $content ) { |
|
| 103 | - |
|
| 104 | - // Links should be added only on the front end and not for RSS. |
|
| 105 | - if ( is_feed() ) { |
|
| 106 | - return $content; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // Preload the `link by default` setting. |
|
| 110 | - $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 111 | - |
|
| 112 | - // Replace each match of the entity tag with the entity link. If an error |
|
| 113 | - // occurs fail silently returning the original content. |
|
| 114 | - return preg_replace_callback( self::PATTERN, array( |
|
| 115 | - $this, |
|
| 116 | - 'link', |
|
| 117 | - ), $content ) ?: $content; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Get the entity match and replace it with a page link. |
|
| 122 | - * |
|
| 123 | - * @since 3.8.0 |
|
| 124 | - * |
|
| 125 | - * @param array $matches An array of matches. |
|
| 126 | - * |
|
| 127 | - * @return string The replaced text with the link to the entity page. |
|
| 128 | - */ |
|
| 129 | - private function link( $matches ) { |
|
| 130 | - |
|
| 131 | - // Get the entity itemid URI and label. |
|
| 132 | - $css_class = $matches[2]; |
|
| 133 | - $uri = $matches[3]; |
|
| 134 | - $label = $matches[4]; |
|
| 135 | - |
|
| 136 | - // Get the entity post by URI. |
|
| 137 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 138 | - if ( null === $post ) { |
|
| 139 | - |
|
| 140 | - // If the entity post is not found return the label w/o the markup |
|
| 141 | - // around it. |
|
| 142 | - // |
|
| 143 | - // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 144 | - return $label; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 148 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 149 | - |
|
| 150 | - // Don't link if links are disabled and the entity is not link or the |
|
| 151 | - // entity is do not link. |
|
| 152 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 153 | - |
|
| 154 | - // Return the label if it's don't link. |
|
| 155 | - if ( $dont_link ) { |
|
| 156 | - return $label; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Get the link. |
|
| 160 | - $href = get_permalink( $post ); |
|
| 161 | - |
|
| 162 | - // Get an alternative title attribute. |
|
| 163 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 164 | - |
|
| 165 | - // Return the link. |
|
| 166 | - return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Get a `title` attribute with an alternative label for the link. |
|
| 171 | - * |
|
| 172 | - * If an alternative title isn't available an empty string is returned. |
|
| 173 | - * |
|
| 174 | - * @since 3.15.0 |
|
| 175 | - * |
|
| 176 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 177 | - * @param string $label The main link label. |
|
| 178 | - * |
|
| 179 | - * @return string A `title` attribute with an alternative label or an empty |
|
| 180 | - * string if none available. |
|
| 181 | - */ |
|
| 182 | - private function get_title_attribute( $post_id, $label ) { |
|
| 183 | - |
|
| 184 | - // Get an alternative title. |
|
| 185 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 186 | - if ( ! empty( $title ) ) { |
|
| 187 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return ''; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Get a string to be used as a title attribute in links to a post |
|
| 195 | - * |
|
| 196 | - * @since 3.15.0 |
|
| 197 | - * |
|
| 198 | - * @param int $post_id The post id of the post being linked. |
|
| 199 | - * @param string $ignore_label A label to ignore. |
|
| 200 | - * |
|
| 201 | - * @return string The title to be used in the link. An empty string when |
|
| 202 | - * there is no alternative that is not the $ignore_label. |
|
| 203 | - */ |
|
| 204 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 205 | - |
|
| 206 | - // Get possible alternative labels we can select from. |
|
| 207 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 208 | - |
|
| 209 | - /* |
|
| 21 | + /** |
|
| 22 | + * The pattern to find entities in text. |
|
| 23 | + * |
|
| 24 | + * @since 3.8.0 |
|
| 25 | + */ |
|
| 26 | + const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.8.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.13.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $configuration_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The `link by default` setting. |
|
| 48 | + * |
|
| 49 | + * @since 3.13.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var bool True if link by default is enabled otherwise false. |
|
| 52 | + */ |
|
| 53 | + private $is_link_by_default; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 57 | + * |
|
| 58 | + * @since 3.14.2 |
|
| 59 | + * @access private |
|
| 60 | + * @var \Wordlift_Content_Filter_Service $instance The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 61 | + */ |
|
| 62 | + private static $instance; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 66 | + * |
|
| 67 | + * @since 3.8.0 |
|
| 68 | + * |
|
| 69 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 70 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 71 | + */ |
|
| 72 | + public function __construct( $entity_service, $configuration_service ) { |
|
| 73 | + |
|
| 74 | + $this->entity_service = $entity_service; |
|
| 75 | + $this->configuration_service = $configuration_service; |
|
| 76 | + |
|
| 77 | + self::$instance = $this; |
|
| 78 | + |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 83 | + * |
|
| 84 | + * @since 3.14.2 |
|
| 85 | + * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 86 | + */ |
|
| 87 | + public static function get_instance() { |
|
| 88 | + |
|
| 89 | + return self::$instance; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Mangle the content by adding links to the entity pages. This function is |
|
| 94 | + * hooked to the 'the_content' WP's filter. |
|
| 95 | + * |
|
| 96 | + * @since 3.8.0 |
|
| 97 | + * |
|
| 98 | + * @param string $content The content being filtered. |
|
| 99 | + * |
|
| 100 | + * @return string The filtered content. |
|
| 101 | + */ |
|
| 102 | + public function the_content( $content ) { |
|
| 103 | + |
|
| 104 | + // Links should be added only on the front end and not for RSS. |
|
| 105 | + if ( is_feed() ) { |
|
| 106 | + return $content; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // Preload the `link by default` setting. |
|
| 110 | + $this->is_link_by_default = $this->configuration_service->is_link_by_default(); |
|
| 111 | + |
|
| 112 | + // Replace each match of the entity tag with the entity link. If an error |
|
| 113 | + // occurs fail silently returning the original content. |
|
| 114 | + return preg_replace_callback( self::PATTERN, array( |
|
| 115 | + $this, |
|
| 116 | + 'link', |
|
| 117 | + ), $content ) ?: $content; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Get the entity match and replace it with a page link. |
|
| 122 | + * |
|
| 123 | + * @since 3.8.0 |
|
| 124 | + * |
|
| 125 | + * @param array $matches An array of matches. |
|
| 126 | + * |
|
| 127 | + * @return string The replaced text with the link to the entity page. |
|
| 128 | + */ |
|
| 129 | + private function link( $matches ) { |
|
| 130 | + |
|
| 131 | + // Get the entity itemid URI and label. |
|
| 132 | + $css_class = $matches[2]; |
|
| 133 | + $uri = $matches[3]; |
|
| 134 | + $label = $matches[4]; |
|
| 135 | + |
|
| 136 | + // Get the entity post by URI. |
|
| 137 | + $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 138 | + if ( null === $post ) { |
|
| 139 | + |
|
| 140 | + // If the entity post is not found return the label w/o the markup |
|
| 141 | + // around it. |
|
| 142 | + // |
|
| 143 | + // See https://github.com/insideout10/wordlift-plugin/issues/461. |
|
| 144 | + return $label; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 148 | + $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 149 | + |
|
| 150 | + // Don't link if links are disabled and the entity is not link or the |
|
| 151 | + // entity is do not link. |
|
| 152 | + $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 153 | + |
|
| 154 | + // Return the label if it's don't link. |
|
| 155 | + if ( $dont_link ) { |
|
| 156 | + return $label; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Get the link. |
|
| 160 | + $href = get_permalink( $post ); |
|
| 161 | + |
|
| 162 | + // Get an alternative title attribute. |
|
| 163 | + $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 164 | + |
|
| 165 | + // Return the link. |
|
| 166 | + return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Get a `title` attribute with an alternative label for the link. |
|
| 171 | + * |
|
| 172 | + * If an alternative title isn't available an empty string is returned. |
|
| 173 | + * |
|
| 174 | + * @since 3.15.0 |
|
| 175 | + * |
|
| 176 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 177 | + * @param string $label The main link label. |
|
| 178 | + * |
|
| 179 | + * @return string A `title` attribute with an alternative label or an empty |
|
| 180 | + * string if none available. |
|
| 181 | + */ |
|
| 182 | + private function get_title_attribute( $post_id, $label ) { |
|
| 183 | + |
|
| 184 | + // Get an alternative title. |
|
| 185 | + $title = $this->get_link_title( $post_id, $label ); |
|
| 186 | + if ( ! empty( $title ) ) { |
|
| 187 | + return 'title="' . esc_attr( $title ) . '"'; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return ''; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Get a string to be used as a title attribute in links to a post |
|
| 195 | + * |
|
| 196 | + * @since 3.15.0 |
|
| 197 | + * |
|
| 198 | + * @param int $post_id The post id of the post being linked. |
|
| 199 | + * @param string $ignore_label A label to ignore. |
|
| 200 | + * |
|
| 201 | + * @return string The title to be used in the link. An empty string when |
|
| 202 | + * there is no alternative that is not the $ignore_label. |
|
| 203 | + */ |
|
| 204 | + function get_link_title( $post_id, $ignore_label ) { |
|
| 205 | + |
|
| 206 | + // Get possible alternative labels we can select from. |
|
| 207 | + $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 208 | + |
|
| 209 | + /* |
|
| 210 | 210 | * Since the original text might use an alternative label than the |
| 211 | 211 | * Entity title, add the title itself which is not returned by the api. |
| 212 | 212 | */ |
| 213 | - $labels[] = get_the_title( $post_id ); |
|
| 214 | - |
|
| 215 | - // Add some randomness to the label selection. |
|
| 216 | - shuffle( $labels ); |
|
| 217 | - |
|
| 218 | - // Select the first label which is not to be ignored. |
|
| 219 | - $title = ''; |
|
| 220 | - foreach ( $labels as $label ) { |
|
| 221 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 222 | - $title = $label; |
|
| 223 | - break; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - return $title; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 232 | - * the provided content. |
|
| 233 | - * |
|
| 234 | - * @since 3.14.2 |
|
| 235 | - * |
|
| 236 | - * @param string $content The content. |
|
| 237 | - * |
|
| 238 | - * @return array An array of URIs. |
|
| 239 | - */ |
|
| 240 | - public function get_entity_uris( $content ) { |
|
| 241 | - |
|
| 242 | - $matches = array(); |
|
| 243 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 244 | - |
|
| 245 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 246 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
| 247 | - // is not sequential. |
|
| 248 | - // |
|
| 249 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 250 | - return array_values( array_unique( $matches[3] ) ); |
|
| 251 | - } |
|
| 213 | + $labels[] = get_the_title( $post_id ); |
|
| 214 | + |
|
| 215 | + // Add some randomness to the label selection. |
|
| 216 | + shuffle( $labels ); |
|
| 217 | + |
|
| 218 | + // Select the first label which is not to be ignored. |
|
| 219 | + $title = ''; |
|
| 220 | + foreach ( $labels as $label ) { |
|
| 221 | + if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 222 | + $title = $label; |
|
| 223 | + break; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + return $title; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 232 | + * the provided content. |
|
| 233 | + * |
|
| 234 | + * @since 3.14.2 |
|
| 235 | + * |
|
| 236 | + * @param string $content The content. |
|
| 237 | + * |
|
| 238 | + * @return array An array of URIs. |
|
| 239 | + */ |
|
| 240 | + public function get_entity_uris( $content ) { |
|
| 241 | + |
|
| 242 | + $matches = array(); |
|
| 243 | + preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 244 | + |
|
| 245 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 246 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
| 247 | + // is not sequential. |
|
| 248 | + // |
|
| 249 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 250 | + return array_values( array_unique( $matches[3] ) ); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | 253 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
| 70 | 70 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 71 | 71 | */ |
| 72 | - public function __construct( $entity_service, $configuration_service ) { |
|
| 72 | + public function __construct($entity_service, $configuration_service) { |
|
| 73 | 73 | |
| 74 | 74 | $this->entity_service = $entity_service; |
| 75 | 75 | $this->configuration_service = $configuration_service; |
@@ -99,10 +99,10 @@ discard block |
||
| 99 | 99 | * |
| 100 | 100 | * @return string The filtered content. |
| 101 | 101 | */ |
| 102 | - public function the_content( $content ) { |
|
| 102 | + public function the_content($content) { |
|
| 103 | 103 | |
| 104 | 104 | // Links should be added only on the front end and not for RSS. |
| 105 | - if ( is_feed() ) { |
|
| 105 | + if (is_feed()) { |
|
| 106 | 106 | return $content; |
| 107 | 107 | } |
| 108 | 108 | |
@@ -111,10 +111,10 @@ discard block |
||
| 111 | 111 | |
| 112 | 112 | // Replace each match of the entity tag with the entity link. If an error |
| 113 | 113 | // occurs fail silently returning the original content. |
| 114 | - return preg_replace_callback( self::PATTERN, array( |
|
| 114 | + return preg_replace_callback(self::PATTERN, array( |
|
| 115 | 115 | $this, |
| 116 | 116 | 'link', |
| 117 | - ), $content ) ?: $content; |
|
| 117 | + ), $content) ?: $content; |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | * |
| 127 | 127 | * @return string The replaced text with the link to the entity page. |
| 128 | 128 | */ |
| 129 | - private function link( $matches ) { |
|
| 129 | + private function link($matches) { |
|
| 130 | 130 | |
| 131 | 131 | // Get the entity itemid URI and label. |
| 132 | 132 | $css_class = $matches[2]; |
@@ -134,8 +134,8 @@ discard block |
||
| 134 | 134 | $label = $matches[4]; |
| 135 | 135 | |
| 136 | 136 | // Get the entity post by URI. |
| 137 | - $post = $this->entity_service->get_entity_post_by_uri( $uri ); |
|
| 138 | - if ( null === $post ) { |
|
| 137 | + $post = $this->entity_service->get_entity_post_by_uri($uri); |
|
| 138 | + if (null === $post) { |
|
| 139 | 139 | |
| 140 | 140 | // If the entity post is not found return the label w/o the markup |
| 141 | 141 | // around it. |
@@ -144,23 +144,23 @@ discard block |
||
| 144 | 144 | return $label; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 148 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 147 | + $no_link = - 1 < strpos($css_class, 'wl-no-link'); |
|
| 148 | + $link = - 1 < strpos($css_class, 'wl-link'); |
|
| 149 | 149 | |
| 150 | 150 | // Don't link if links are disabled and the entity is not link or the |
| 151 | 151 | // entity is do not link. |
| 152 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 152 | + $dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link; |
|
| 153 | 153 | |
| 154 | 154 | // Return the label if it's don't link. |
| 155 | - if ( $dont_link ) { |
|
| 155 | + if ($dont_link) { |
|
| 156 | 156 | return $label; |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // Get the link. |
| 160 | - $href = get_permalink( $post ); |
|
| 160 | + $href = get_permalink($post); |
|
| 161 | 161 | |
| 162 | 162 | // Get an alternative title attribute. |
| 163 | - $title_attribute = $this->get_title_attribute( $post->ID, $label ); |
|
| 163 | + $title_attribute = $this->get_title_attribute($post->ID, $label); |
|
| 164 | 164 | |
| 165 | 165 | // Return the link. |
| 166 | 166 | return "<a class='wl-entity-page-link' $title_attribute href='$href'>$label</a>"; |
@@ -179,12 +179,12 @@ discard block |
||
| 179 | 179 | * @return string A `title` attribute with an alternative label or an empty |
| 180 | 180 | * string if none available. |
| 181 | 181 | */ |
| 182 | - private function get_title_attribute( $post_id, $label ) { |
|
| 182 | + private function get_title_attribute($post_id, $label) { |
|
| 183 | 183 | |
| 184 | 184 | // Get an alternative title. |
| 185 | - $title = $this->get_link_title( $post_id, $label ); |
|
| 186 | - if ( ! empty( $title ) ) { |
|
| 187 | - return 'title="' . esc_attr( $title ) . '"'; |
|
| 185 | + $title = $this->get_link_title($post_id, $label); |
|
| 186 | + if ( ! empty($title)) { |
|
| 187 | + return 'title="'.esc_attr($title).'"'; |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | return ''; |
@@ -201,24 +201,24 @@ discard block |
||
| 201 | 201 | * @return string The title to be used in the link. An empty string when |
| 202 | 202 | * there is no alternative that is not the $ignore_label. |
| 203 | 203 | */ |
| 204 | - function get_link_title( $post_id, $ignore_label ) { |
|
| 204 | + function get_link_title($post_id, $ignore_label) { |
|
| 205 | 205 | |
| 206 | 206 | // Get possible alternative labels we can select from. |
| 207 | - $labels = $this->entity_service->get_alternative_labels( $post_id ); |
|
| 207 | + $labels = $this->entity_service->get_alternative_labels($post_id); |
|
| 208 | 208 | |
| 209 | 209 | /* |
| 210 | 210 | * Since the original text might use an alternative label than the |
| 211 | 211 | * Entity title, add the title itself which is not returned by the api. |
| 212 | 212 | */ |
| 213 | - $labels[] = get_the_title( $post_id ); |
|
| 213 | + $labels[] = get_the_title($post_id); |
|
| 214 | 214 | |
| 215 | 215 | // Add some randomness to the label selection. |
| 216 | - shuffle( $labels ); |
|
| 216 | + shuffle($labels); |
|
| 217 | 217 | |
| 218 | 218 | // Select the first label which is not to be ignored. |
| 219 | 219 | $title = ''; |
| 220 | - foreach ( $labels as $label ) { |
|
| 221 | - if ( 0 !== strcasecmp( $label, $ignore_label ) ) { |
|
| 220 | + foreach ($labels as $label) { |
|
| 221 | + if (0 !== strcasecmp($label, $ignore_label)) { |
|
| 222 | 222 | $title = $label; |
| 223 | 223 | break; |
| 224 | 224 | } |
@@ -237,17 +237,17 @@ discard block |
||
| 237 | 237 | * |
| 238 | 238 | * @return array An array of URIs. |
| 239 | 239 | */ |
| 240 | - public function get_entity_uris( $content ) { |
|
| 240 | + public function get_entity_uris($content) { |
|
| 241 | 241 | |
| 242 | 242 | $matches = array(); |
| 243 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 243 | + preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches); |
|
| 244 | 244 | |
| 245 | 245 | // We need to use `array_values` here in order to avoid further `json_encode` |
| 246 | 246 | // to turn it into an object (since if the 3rd match isn't found the index |
| 247 | 247 | // is not sequential. |
| 248 | 248 | // |
| 249 | 249 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
| 250 | - return array_values( array_unique( $matches[3] ) ); |
|
| 250 | + return array_values(array_unique($matches[3])); |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | } |
@@ -13,158 +13,158 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Jsonld_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 18 | - * |
|
| 19 | - * @since 3.8.0 |
|
| 20 | - * @access private |
|
| 21 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 22 | - */ |
|
| 23 | - private $entity_service; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * A {@link Wordlift_Post_Converter} instance. |
|
| 27 | - * |
|
| 28 | - * @since 3.8.0 |
|
| 29 | - * @access private |
|
| 30 | - * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 31 | - */ |
|
| 32 | - private $converter; |
|
| 33 | - |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 37 | - * |
|
| 38 | - * @since 3.14.0 |
|
| 39 | - * @access private |
|
| 40 | - * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 41 | - */ |
|
| 42 | - private $website_converter; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The singleton instance for the JSON-LD service. |
|
| 46 | - * |
|
| 47 | - * @since 3.15.1 |
|
| 48 | - * |
|
| 49 | - * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 50 | - */ |
|
| 51 | - private static $instance; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Create a JSON-LD service. |
|
| 55 | - * |
|
| 56 | - * @since 3.8.0 |
|
| 57 | - * |
|
| 58 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 59 | - * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 60 | - * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 61 | - */ |
|
| 62 | - public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 63 | - |
|
| 64 | - $this->entity_service = $entity_service; |
|
| 65 | - $this->converter = $converter; |
|
| 66 | - $this->website_converter = $website_converter; |
|
| 67 | - |
|
| 68 | - self::$instance = $this; |
|
| 69 | - |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Get the singleton instance for the JSON-LD service. |
|
| 74 | - * |
|
| 75 | - * @since 3.15.1 |
|
| 76 | - * |
|
| 77 | - * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 78 | - */ |
|
| 79 | - public static function get_instance() { |
|
| 80 | - |
|
| 81 | - return self::$instance; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 86 | - * |
|
| 87 | - * @since 3.8.0 |
|
| 88 | - */ |
|
| 89 | - public function get() { |
|
| 90 | - // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 91 | - // |
|
| 92 | - // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 93 | - // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 94 | - @ob_clean(); |
|
| 95 | - |
|
| 96 | - // Get the parameter from the request. |
|
| 97 | - $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 98 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 99 | - |
|
| 100 | - // Send the generated JSON-LD. |
|
| 101 | - wp_send_json( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 102 | - |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Get the JSON-LD. |
|
| 107 | - * |
|
| 108 | - * @since 3.15.1 |
|
| 109 | - * |
|
| 110 | - * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 111 | - * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 112 | - * |
|
| 113 | - * @return array A JSON-LD structure. |
|
| 114 | - */ |
|
| 115 | - public function get_jsonld( $is_homepage = false, $post_id = null ) { |
|
| 116 | - |
|
| 117 | - // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 118 | - // e-mails with a low apdex score. |
|
| 119 | - // |
|
| 120 | - // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 121 | - Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 122 | - |
|
| 123 | - // Switch to Website converter if is home page. |
|
| 124 | - if ( $is_homepage ) { |
|
| 125 | - /** |
|
| 126 | - * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 127 | - * |
|
| 128 | - * @since 3.14.0 |
|
| 129 | - * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 130 | - */ |
|
| 131 | - if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 132 | - // Set a reference to the website_converter. |
|
| 133 | - $website_converter = $this->website_converter; |
|
| 134 | - |
|
| 135 | - // Send JSON-LD. |
|
| 136 | - return $website_converter->create_schema(); |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // If no id has been provided return an empty array. |
|
| 141 | - if ( ! isset( $post_id ) ) { |
|
| 142 | - return array(); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // An array of references which is captured when converting an URI to a |
|
| 146 | - // json which we gather to further expand our json-ld. |
|
| 147 | - $references = array(); |
|
| 148 | - |
|
| 149 | - // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 150 | - $entity_to_jsonld_converter = $this->converter; |
|
| 151 | - |
|
| 152 | - // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
| 153 | - // in the references array. |
|
| 154 | - $jsonld = array_merge( |
|
| 155 | - array( $entity_to_jsonld_converter->convert( $post_id, $references ) ), |
|
| 156 | - // Convert each URI in the references array to JSON-LD. We don't output |
|
| 157 | - // entities already output above (hence the array_diff). |
|
| 158 | - array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
|
| 159 | - |
|
| 160 | - // "2nd level properties" may not output here, e.g. a post |
|
| 161 | - // mentioning an event, located in a place: the place is referenced |
|
| 162 | - // via the `@id` but no other properties are loaded. |
|
| 163 | - return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 164 | - }, $references ) ); |
|
| 165 | - |
|
| 166 | - // Finally send the JSON-LD. |
|
| 167 | - return $jsonld; |
|
| 168 | - } |
|
| 16 | + /** |
|
| 17 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 18 | + * |
|
| 19 | + * @since 3.8.0 |
|
| 20 | + * @access private |
|
| 21 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 22 | + */ |
|
| 23 | + private $entity_service; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * A {@link Wordlift_Post_Converter} instance. |
|
| 27 | + * |
|
| 28 | + * @since 3.8.0 |
|
| 29 | + * @access private |
|
| 30 | + * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance. |
|
| 31 | + */ |
|
| 32 | + private $converter; |
|
| 33 | + |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 37 | + * |
|
| 38 | + * @since 3.14.0 |
|
| 39 | + * @access private |
|
| 40 | + * @var \Wordlift_Website_Jsonld_Converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 41 | + */ |
|
| 42 | + private $website_converter; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The singleton instance for the JSON-LD service. |
|
| 46 | + * |
|
| 47 | + * @since 3.15.1 |
|
| 48 | + * |
|
| 49 | + * @var \Wordlift_Jsonld_Service $instance The singleton instance for the JSON-LD service. |
|
| 50 | + */ |
|
| 51 | + private static $instance; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Create a JSON-LD service. |
|
| 55 | + * |
|
| 56 | + * @since 3.8.0 |
|
| 57 | + * |
|
| 58 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 59 | + * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
|
| 60 | + * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
|
| 61 | + */ |
|
| 62 | + public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 63 | + |
|
| 64 | + $this->entity_service = $entity_service; |
|
| 65 | + $this->converter = $converter; |
|
| 66 | + $this->website_converter = $website_converter; |
|
| 67 | + |
|
| 68 | + self::$instance = $this; |
|
| 69 | + |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Get the singleton instance for the JSON-LD service. |
|
| 74 | + * |
|
| 75 | + * @since 3.15.1 |
|
| 76 | + * |
|
| 77 | + * @return \Wordlift_Jsonld_Service The singleton instance for the JSON-LD service. |
|
| 78 | + */ |
|
| 79 | + public static function get_instance() { |
|
| 80 | + |
|
| 81 | + return self::$instance; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Process calls to the AJAX 'wl_jsonld' endpoint. |
|
| 86 | + * |
|
| 87 | + * @since 3.8.0 |
|
| 88 | + */ |
|
| 89 | + public function get() { |
|
| 90 | + // Clear the buffer to be sure someone doesn't mess with our response. |
|
| 91 | + // |
|
| 92 | + // See https://github.com/insideout10/wordlift-plugin/issues/406. |
|
| 93 | + // See https://codex.wordpress.org/AJAX_in_Plugins. |
|
| 94 | + @ob_clean(); |
|
| 95 | + |
|
| 96 | + // Get the parameter from the request. |
|
| 97 | + $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 98 | + $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 99 | + |
|
| 100 | + // Send the generated JSON-LD. |
|
| 101 | + wp_send_json( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 102 | + |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Get the JSON-LD. |
|
| 107 | + * |
|
| 108 | + * @since 3.15.1 |
|
| 109 | + * |
|
| 110 | + * @param bool $is_homepage Whether the JSON-LD for the homepage is being requested. |
|
| 111 | + * @param int|null $post_id The JSON-LD for the specified {@link WP_Post} id. |
|
| 112 | + * |
|
| 113 | + * @return array A JSON-LD structure. |
|
| 114 | + */ |
|
| 115 | + public function get_jsonld( $is_homepage = false, $post_id = null ) { |
|
| 116 | + |
|
| 117 | + // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
|
| 118 | + // e-mails with a low apdex score. |
|
| 119 | + // |
|
| 120 | + // See https://github.com/insideout10/wordlift-plugin/issues/521 |
|
| 121 | + Wordlift_NewRelic_Adapter::ignore_apdex(); |
|
| 122 | + |
|
| 123 | + // Switch to Website converter if is home page. |
|
| 124 | + if ( $is_homepage ) { |
|
| 125 | + /** |
|
| 126 | + * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
|
| 127 | + * |
|
| 128 | + * @since 3.14.0 |
|
| 129 | + * @api bool $display_search Whether or not to display json+ld search on the frontend. |
|
| 130 | + */ |
|
| 131 | + if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 132 | + // Set a reference to the website_converter. |
|
| 133 | + $website_converter = $this->website_converter; |
|
| 134 | + |
|
| 135 | + // Send JSON-LD. |
|
| 136 | + return $website_converter->create_schema(); |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + // If no id has been provided return an empty array. |
|
| 141 | + if ( ! isset( $post_id ) ) { |
|
| 142 | + return array(); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // An array of references which is captured when converting an URI to a |
|
| 146 | + // json which we gather to further expand our json-ld. |
|
| 147 | + $references = array(); |
|
| 148 | + |
|
| 149 | + // Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
| 150 | + $entity_to_jsonld_converter = $this->converter; |
|
| 151 | + |
|
| 152 | + // Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
| 153 | + // in the references array. |
|
| 154 | + $jsonld = array_merge( |
|
| 155 | + array( $entity_to_jsonld_converter->convert( $post_id, $references ) ), |
|
| 156 | + // Convert each URI in the references array to JSON-LD. We don't output |
|
| 157 | + // entities already output above (hence the array_diff). |
|
| 158 | + array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
|
| 159 | + |
|
| 160 | + // "2nd level properties" may not output here, e.g. a post |
|
| 161 | + // mentioning an event, located in a place: the place is referenced |
|
| 162 | + // via the `@id` but no other properties are loaded. |
|
| 163 | + return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 164 | + }, $references ) ); |
|
| 165 | + |
|
| 166 | + // Finally send the JSON-LD. |
|
| 167 | + return $jsonld; |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | 170 | } |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * @param \Wordlift_Post_Converter $converter A {@link Wordlift_Uri_To_Jsonld_Converter} instance. |
| 60 | 60 | * @param \Wordlift_Website_Jsonld_Converter $website_converter A {@link Wordlift_Website_Jsonld_Converter} instance. |
| 61 | 61 | */ |
| 62 | - public function __construct( $entity_service, $converter, $website_converter ) { |
|
| 62 | + public function __construct($entity_service, $converter, $website_converter) { |
|
| 63 | 63 | |
| 64 | 64 | $this->entity_service = $entity_service; |
| 65 | 65 | $this->converter = $converter; |
@@ -94,11 +94,11 @@ discard block |
||
| 94 | 94 | @ob_clean(); |
| 95 | 95 | |
| 96 | 96 | // Get the parameter from the request. |
| 97 | - $is_homepage = isset( $_REQUEST['homepage'] ); |
|
| 98 | - $post_id = isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : null; |
|
| 97 | + $is_homepage = isset($_REQUEST['homepage']); |
|
| 98 | + $post_id = isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ? intval($_REQUEST['id']) : null; |
|
| 99 | 99 | |
| 100 | 100 | // Send the generated JSON-LD. |
| 101 | - wp_send_json( $this->get_jsonld( $is_homepage, $post_id ) ); |
|
| 101 | + wp_send_json($this->get_jsonld($is_homepage, $post_id)); |
|
| 102 | 102 | |
| 103 | 103 | } |
| 104 | 104 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @return array A JSON-LD structure. |
| 114 | 114 | */ |
| 115 | - public function get_jsonld( $is_homepage = false, $post_id = null ) { |
|
| 115 | + public function get_jsonld($is_homepage = false, $post_id = null) { |
|
| 116 | 116 | |
| 117 | 117 | // Tell NewRelic to ignore us, otherwise NewRelic customers might receive |
| 118 | 118 | // e-mails with a low apdex score. |
@@ -121,14 +121,14 @@ discard block |
||
| 121 | 121 | Wordlift_NewRelic_Adapter::ignore_apdex(); |
| 122 | 122 | |
| 123 | 123 | // Switch to Website converter if is home page. |
| 124 | - if ( $is_homepage ) { |
|
| 124 | + if ($is_homepage) { |
|
| 125 | 125 | /** |
| 126 | 126 | * Filter: 'wordlift_disable_website_json_ld' - Allow disabling of the json+ld output. |
| 127 | 127 | * |
| 128 | 128 | * @since 3.14.0 |
| 129 | 129 | * @api bool $display_search Whether or not to display json+ld search on the frontend. |
| 130 | 130 | */ |
| 131 | - if ( ! apply_filters( 'wordlift_disable_website_json_ld', false ) ) { |
|
| 131 | + if ( ! apply_filters('wordlift_disable_website_json_ld', false)) { |
|
| 132 | 132 | // Set a reference to the website_converter. |
| 133 | 133 | $website_converter = $this->website_converter; |
| 134 | 134 | |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // If no id has been provided return an empty array. |
| 141 | - if ( ! isset( $post_id ) ) { |
|
| 141 | + if ( ! isset($post_id)) { |
|
| 142 | 142 | return array(); |
| 143 | 143 | } |
| 144 | 144 | |
@@ -152,16 +152,16 @@ discard block |
||
| 152 | 152 | // Convert each URI to a JSON-LD array, while gathering referenced entities. |
| 153 | 153 | // in the references array. |
| 154 | 154 | $jsonld = array_merge( |
| 155 | - array( $entity_to_jsonld_converter->convert( $post_id, $references ) ), |
|
| 155 | + array($entity_to_jsonld_converter->convert($post_id, $references)), |
|
| 156 | 156 | // Convert each URI in the references array to JSON-LD. We don't output |
| 157 | 157 | // entities already output above (hence the array_diff). |
| 158 | - array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) { |
|
| 158 | + array_map(function($item) use ($entity_to_jsonld_converter, $references) { |
|
| 159 | 159 | |
| 160 | 160 | // "2nd level properties" may not output here, e.g. a post |
| 161 | 161 | // mentioning an event, located in a place: the place is referenced |
| 162 | 162 | // via the `@id` but no other properties are loaded. |
| 163 | - return $entity_to_jsonld_converter->convert( $item, $references ); |
|
| 164 | - }, $references ) ); |
|
| 163 | + return $entity_to_jsonld_converter->convert($item, $references); |
|
| 164 | + }, $references) ); |
|
| 165 | 165 | |
| 166 | 166 | // Finally send the JSON-LD. |
| 167 | 167 | return $jsonld; |