@@ -10,59 +10,59 @@ |
||
10 | 10 | |
11 | 11 | interface Content_Service { |
12 | 12 | |
13 | - /** |
|
14 | - * Get an {@link Content} given a URI. |
|
15 | - * |
|
16 | - * @param string $uri The URI. |
|
17 | - * |
|
18 | - * @return Content|null The found {@link Content} or null if not found. |
|
19 | - * @throws Exception if the URI is absolute and not within the dataset URI scope or the dataset URI isn't set. |
|
20 | - */ |
|
21 | - public function get_by_entity_id( $uri ); |
|
13 | + /** |
|
14 | + * Get an {@link Content} given a URI. |
|
15 | + * |
|
16 | + * @param string $uri The URI. |
|
17 | + * |
|
18 | + * @return Content|null The found {@link Content} or null if not found. |
|
19 | + * @throws Exception if the URI is absolute and not within the dataset URI scope or the dataset URI isn't set. |
|
20 | + */ |
|
21 | + public function get_by_entity_id( $uri ); |
|
22 | 22 | |
23 | - /** |
|
24 | - * Get an {@link Content} given a URI. The search is performed also in sameAs. |
|
25 | - * |
|
26 | - * @param string $uri The URI. |
|
27 | - * |
|
28 | - * @return Content|null The found {@link Content} or null if not found. |
|
29 | - */ |
|
30 | - public function get_by_entity_id_or_same_as( $uri ); |
|
23 | + /** |
|
24 | + * Get an {@link Content} given a URI. The search is performed also in sameAs. |
|
25 | + * |
|
26 | + * @param string $uri The URI. |
|
27 | + * |
|
28 | + * @return Content|null The found {@link Content} or null if not found. |
|
29 | + */ |
|
30 | + public function get_by_entity_id_or_same_as( $uri ); |
|
31 | 31 | |
32 | - /** |
|
33 | - * Get the {@link Content}'s URI given an {@link Content_Id}. |
|
34 | - * |
|
35 | - * @param Content_Id $content_id An {@link Content_Id}. |
|
36 | - * |
|
37 | - * @return string|null An absolute URI or null if not found. |
|
38 | - */ |
|
39 | - public function get_entity_id( $content_id ); |
|
32 | + /** |
|
33 | + * Get the {@link Content}'s URI given an {@link Content_Id}. |
|
34 | + * |
|
35 | + * @param Content_Id $content_id An {@link Content_Id}. |
|
36 | + * |
|
37 | + * @return string|null An absolute URI or null if not found. |
|
38 | + */ |
|
39 | + public function get_entity_id( $content_id ); |
|
40 | 40 | |
41 | - /** |
|
42 | - * Set the {@link Content}'s URI for the specified {@link Content_Id}. |
|
43 | - * |
|
44 | - * @param Content_Id $content_id An {@link Content_Id}. |
|
45 | - * @param string $uri The URI. |
|
46 | - * |
|
47 | - * @return void |
|
48 | - * @throws Exception if the URI is absolute and not within the dataset URI scope or the dataset URI isn't set. |
|
49 | - */ |
|
50 | - public function set_entity_id( $content_id, $uri ); |
|
41 | + /** |
|
42 | + * Set the {@link Content}'s URI for the specified {@link Content_Id}. |
|
43 | + * |
|
44 | + * @param Content_Id $content_id An {@link Content_Id}. |
|
45 | + * @param string $uri The URI. |
|
46 | + * |
|
47 | + * @return void |
|
48 | + * @throws Exception if the URI is absolute and not within the dataset URI scope or the dataset URI isn't set. |
|
49 | + */ |
|
50 | + public function set_entity_id( $content_id, $uri ); |
|
51 | 51 | |
52 | - /** |
|
53 | - * Whether the {@link Content_Service} supports the provided {@link Content_Id}. |
|
54 | - * |
|
55 | - * @param Content_Id $content_id |
|
56 | - * |
|
57 | - * @return bool |
|
58 | - */ |
|
59 | - public function supports( $content_id ); |
|
52 | + /** |
|
53 | + * Whether the {@link Content_Service} supports the provided {@link Content_Id}. |
|
54 | + * |
|
55 | + * @param Content_Id $content_id |
|
56 | + * |
|
57 | + * @return bool |
|
58 | + */ |
|
59 | + public function supports( $content_id ); |
|
60 | 60 | |
61 | - /** |
|
62 | - * Delete the content with the specified ID. |
|
63 | - * |
|
64 | - * @param Content_Id $content_id |
|
65 | - */ |
|
66 | - public function delete( $content_id ); |
|
61 | + /** |
|
62 | + * Delete the content with the specified ID. |
|
63 | + * |
|
64 | + * @param Content_Id $content_id |
|
65 | + */ |
|
66 | + public function delete( $content_id ); |
|
67 | 67 | |
68 | 68 | } |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | * @return Content|null The found {@link Content} or null if not found. |
19 | 19 | * @throws Exception if the URI is absolute and not within the dataset URI scope or the dataset URI isn't set. |
20 | 20 | */ |
21 | - public function get_by_entity_id( $uri ); |
|
21 | + public function get_by_entity_id($uri); |
|
22 | 22 | |
23 | 23 | /** |
24 | 24 | * Get an {@link Content} given a URI. The search is performed also in sameAs. |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @return Content|null The found {@link Content} or null if not found. |
29 | 29 | */ |
30 | - public function get_by_entity_id_or_same_as( $uri ); |
|
30 | + public function get_by_entity_id_or_same_as($uri); |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * Get the {@link Content}'s URI given an {@link Content_Id}. |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | * |
37 | 37 | * @return string|null An absolute URI or null if not found. |
38 | 38 | */ |
39 | - public function get_entity_id( $content_id ); |
|
39 | + public function get_entity_id($content_id); |
|
40 | 40 | |
41 | 41 | /** |
42 | 42 | * Set the {@link Content}'s URI for the specified {@link Content_Id}. |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | * @return void |
48 | 48 | * @throws Exception if the URI is absolute and not within the dataset URI scope or the dataset URI isn't set. |
49 | 49 | */ |
50 | - public function set_entity_id( $content_id, $uri ); |
|
50 | + public function set_entity_id($content_id, $uri); |
|
51 | 51 | |
52 | 52 | /** |
53 | 53 | * Whether the {@link Content_Service} supports the provided {@link Content_Id}. |
@@ -56,13 +56,13 @@ discard block |
||
56 | 56 | * |
57 | 57 | * @return bool |
58 | 58 | */ |
59 | - public function supports( $content_id ); |
|
59 | + public function supports($content_id); |
|
60 | 60 | |
61 | 61 | /** |
62 | 62 | * Delete the content with the specified ID. |
63 | 63 | * |
64 | 64 | * @param Content_Id $content_id |
65 | 65 | */ |
66 | - public function delete( $content_id ); |
|
66 | + public function delete($content_id); |
|
67 | 67 | |
68 | 68 | } |
@@ -4,54 +4,54 @@ |
||
4 | 4 | |
5 | 5 | class Features_Registry { |
6 | 6 | |
7 | - /** |
|
8 | - * @var array<Feature> |
|
9 | - */ |
|
10 | - private $features = array(); |
|
11 | - |
|
12 | - private static $instance = null; |
|
13 | - |
|
14 | - public static function get_instance() { |
|
15 | - if ( null === self::$instance ) { |
|
16 | - self::$instance = new Features_Registry(); |
|
17 | - } |
|
18 | - |
|
19 | - return self::$instance; |
|
20 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * @param $feature Feature |
|
24 | - */ |
|
25 | - public function register_feature( $feature ) { |
|
26 | - $this->features[] = $feature; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * @param $feature_slug string |
|
31 | - * @param $default_value bool |
|
32 | - * @param $callback callable |
|
33 | - */ |
|
34 | - public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
35 | - $this->features[] = new Feature( |
|
36 | - $feature_slug, |
|
37 | - $default_value, |
|
38 | - $callback |
|
39 | - ); |
|
40 | - } |
|
41 | - |
|
42 | - public function initialize_all_features() { |
|
43 | - foreach ( $this->features as $feature ) { |
|
44 | - /** |
|
45 | - * @var $feature Feature |
|
46 | - */ |
|
47 | - $feature_slug = $feature->feature_slug; |
|
48 | - if ( apply_filters( "wl_feature__enable__${feature_slug}", $feature->default_value ) ) { |
|
49 | - call_user_func( $feature->callback ); |
|
50 | - } |
|
51 | - } |
|
52 | - } |
|
53 | - |
|
54 | - public function clear_all() { |
|
55 | - $this->features = array(); |
|
56 | - } |
|
7 | + /** |
|
8 | + * @var array<Feature> |
|
9 | + */ |
|
10 | + private $features = array(); |
|
11 | + |
|
12 | + private static $instance = null; |
|
13 | + |
|
14 | + public static function get_instance() { |
|
15 | + if ( null === self::$instance ) { |
|
16 | + self::$instance = new Features_Registry(); |
|
17 | + } |
|
18 | + |
|
19 | + return self::$instance; |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * @param $feature Feature |
|
24 | + */ |
|
25 | + public function register_feature( $feature ) { |
|
26 | + $this->features[] = $feature; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * @param $feature_slug string |
|
31 | + * @param $default_value bool |
|
32 | + * @param $callback callable |
|
33 | + */ |
|
34 | + public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
35 | + $this->features[] = new Feature( |
|
36 | + $feature_slug, |
|
37 | + $default_value, |
|
38 | + $callback |
|
39 | + ); |
|
40 | + } |
|
41 | + |
|
42 | + public function initialize_all_features() { |
|
43 | + foreach ( $this->features as $feature ) { |
|
44 | + /** |
|
45 | + * @var $feature Feature |
|
46 | + */ |
|
47 | + $feature_slug = $feature->feature_slug; |
|
48 | + if ( apply_filters( "wl_feature__enable__${feature_slug}", $feature->default_value ) ) { |
|
49 | + call_user_func( $feature->callback ); |
|
50 | + } |
|
51 | + } |
|
52 | + } |
|
53 | + |
|
54 | + public function clear_all() { |
|
55 | + $this->features = array(); |
|
56 | + } |
|
57 | 57 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | private static $instance = null; |
13 | 13 | |
14 | 14 | public static function get_instance() { |
15 | - if ( null === self::$instance ) { |
|
15 | + if (null === self::$instance) { |
|
16 | 16 | self::$instance = new Features_Registry(); |
17 | 17 | } |
18 | 18 | |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | /** |
23 | 23 | * @param $feature Feature |
24 | 24 | */ |
25 | - public function register_feature( $feature ) { |
|
25 | + public function register_feature($feature) { |
|
26 | 26 | $this->features[] = $feature; |
27 | 27 | } |
28 | 28 | |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @param $default_value bool |
32 | 32 | * @param $callback callable |
33 | 33 | */ |
34 | - public function register_feature_from_slug( $feature_slug, $default_value, $callback ) { |
|
34 | + public function register_feature_from_slug($feature_slug, $default_value, $callback) { |
|
35 | 35 | $this->features[] = new Feature( |
36 | 36 | $feature_slug, |
37 | 37 | $default_value, |
@@ -40,13 +40,13 @@ discard block |
||
40 | 40 | } |
41 | 41 | |
42 | 42 | public function initialize_all_features() { |
43 | - foreach ( $this->features as $feature ) { |
|
43 | + foreach ($this->features as $feature) { |
|
44 | 44 | /** |
45 | 45 | * @var $feature Feature |
46 | 46 | */ |
47 | 47 | $feature_slug = $feature->feature_slug; |
48 | - if ( apply_filters( "wl_feature__enable__${feature_slug}", $feature->default_value ) ) { |
|
49 | - call_user_func( $feature->callback ); |
|
48 | + if (apply_filters("wl_feature__enable__${feature_slug}", $feature->default_value)) { |
|
49 | + call_user_func($feature->callback); |
|
50 | 50 | } |
51 | 51 | } |
52 | 52 | } |
@@ -3,108 +3,108 @@ |
||
3 | 3 | namespace Wordlift\Features; |
4 | 4 | |
5 | 5 | class Response_Adapter { |
6 | - const WL_FEATURES = '_wl_features'; |
|
7 | - const WL_1 = 'wl1'; |
|
8 | - |
|
9 | - public function __construct() { |
|
10 | - |
|
11 | - // Filter responses from the API calls to update the enabled features. |
|
12 | - add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
13 | - |
|
14 | - // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
|
15 | - // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
|
16 | - // $this->init_from_env(); |
|
17 | - |
|
18 | - // Register the `wl_features__enable__{feature-name}` filters. |
|
19 | - $this->register_filters(); |
|
20 | - |
|
21 | - // Hook to the updates to the features setting to refresh the features' filters. |
|
22 | - add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
23 | - |
|
24 | - } |
|
25 | - |
|
26 | - public function response( $response ) { |
|
27 | - |
|
28 | - $headers = wp_remote_retrieve_headers( $response ); |
|
29 | - |
|
30 | - // Bail out if the `wl1` header isn't defined. |
|
31 | - if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
32 | - return $response; |
|
33 | - } |
|
34 | - $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
35 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
36 | - $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
37 | - |
|
38 | - $updated_features = $wl1['features']; |
|
39 | - |
|
40 | - $existing_features = get_option( self::WL_FEATURES, array() ); |
|
41 | - |
|
42 | - // Loop through updated features. |
|
43 | - foreach ( $updated_features as $feature_slug => $new_value ) { |
|
44 | - |
|
45 | - // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
|
46 | - // not set before. |
|
47 | - $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
48 | - |
|
49 | - if ( $old_value !== $new_value ) { |
|
50 | - /** |
|
51 | - * @param $feature_slug string The feature slug. |
|
52 | - * @param $old_value null | boolean Null represents the feature flag was not set before. |
|
53 | - * @param $new_value boolean True or false. |
|
54 | - * |
|
55 | - * @since 3.32.1 |
|
56 | - * Hook : `wl_feature__change__{feature_slug}` |
|
57 | - * Action hook to be fired when there is a change in feature state. |
|
58 | - */ |
|
59 | - do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
60 | - } |
|
61 | - } |
|
62 | - |
|
63 | - if ( isset( $updated_features ) ) { |
|
64 | - |
|
65 | - if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
66 | - $this->register_filters(); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - return $response; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Registers the feature filters. |
|
75 | - */ |
|
76 | - public function register_filters() { |
|
77 | - |
|
78 | - foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
79 | - // Remove previous filters. |
|
80 | - remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
81 | - remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
82 | - |
|
83 | - $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
84 | - add_filter( "wl_feature__enable__${name}", $callback ); |
|
85 | - } |
|
86 | - |
|
87 | - } |
|
88 | - |
|
89 | - private function init_from_env() { |
|
90 | - $features = array_reduce( |
|
91 | - array_filter( |
|
92 | - array_keys( $_ENV ), |
|
93 | - function ( $key ) { |
|
94 | - return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
95 | - } |
|
96 | - ), |
|
97 | - function ( $features, $env ) { |
|
98 | - $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
99 | - $value = wp_validate_boolean( getenv( $env ) ); |
|
100 | - $features[ $name ] = $value; |
|
101 | - |
|
102 | - return $features; |
|
103 | - }, |
|
104 | - array() |
|
105 | - ); |
|
106 | - |
|
107 | - update_option( self::WL_FEATURES, (array) $features, true ); |
|
108 | - } |
|
6 | + const WL_FEATURES = '_wl_features'; |
|
7 | + const WL_1 = 'wl1'; |
|
8 | + |
|
9 | + public function __construct() { |
|
10 | + |
|
11 | + // Filter responses from the API calls to update the enabled features. |
|
12 | + add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
13 | + |
|
14 | + // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
|
15 | + // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
|
16 | + // $this->init_from_env(); |
|
17 | + |
|
18 | + // Register the `wl_features__enable__{feature-name}` filters. |
|
19 | + $this->register_filters(); |
|
20 | + |
|
21 | + // Hook to the updates to the features setting to refresh the features' filters. |
|
22 | + add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
23 | + |
|
24 | + } |
|
25 | + |
|
26 | + public function response( $response ) { |
|
27 | + |
|
28 | + $headers = wp_remote_retrieve_headers( $response ); |
|
29 | + |
|
30 | + // Bail out if the `wl1` header isn't defined. |
|
31 | + if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
32 | + return $response; |
|
33 | + } |
|
34 | + $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
35 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
36 | + $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
37 | + |
|
38 | + $updated_features = $wl1['features']; |
|
39 | + |
|
40 | + $existing_features = get_option( self::WL_FEATURES, array() ); |
|
41 | + |
|
42 | + // Loop through updated features. |
|
43 | + foreach ( $updated_features as $feature_slug => $new_value ) { |
|
44 | + |
|
45 | + // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
|
46 | + // not set before. |
|
47 | + $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
48 | + |
|
49 | + if ( $old_value !== $new_value ) { |
|
50 | + /** |
|
51 | + * @param $feature_slug string The feature slug. |
|
52 | + * @param $old_value null | boolean Null represents the feature flag was not set before. |
|
53 | + * @param $new_value boolean True or false. |
|
54 | + * |
|
55 | + * @since 3.32.1 |
|
56 | + * Hook : `wl_feature__change__{feature_slug}` |
|
57 | + * Action hook to be fired when there is a change in feature state. |
|
58 | + */ |
|
59 | + do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
60 | + } |
|
61 | + } |
|
62 | + |
|
63 | + if ( isset( $updated_features ) ) { |
|
64 | + |
|
65 | + if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
66 | + $this->register_filters(); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + return $response; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Registers the feature filters. |
|
75 | + */ |
|
76 | + public function register_filters() { |
|
77 | + |
|
78 | + foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
79 | + // Remove previous filters. |
|
80 | + remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
81 | + remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
82 | + |
|
83 | + $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
84 | + add_filter( "wl_feature__enable__${name}", $callback ); |
|
85 | + } |
|
86 | + |
|
87 | + } |
|
88 | + |
|
89 | + private function init_from_env() { |
|
90 | + $features = array_reduce( |
|
91 | + array_filter( |
|
92 | + array_keys( $_ENV ), |
|
93 | + function ( $key ) { |
|
94 | + return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
95 | + } |
|
96 | + ), |
|
97 | + function ( $features, $env ) { |
|
98 | + $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
99 | + $value = wp_validate_boolean( getenv( $env ) ); |
|
100 | + $features[ $name ] = $value; |
|
101 | + |
|
102 | + return $features; |
|
103 | + }, |
|
104 | + array() |
|
105 | + ); |
|
106 | + |
|
107 | + update_option( self::WL_FEATURES, (array) $features, true ); |
|
108 | + } |
|
109 | 109 | |
110 | 110 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | public function __construct() { |
10 | 10 | |
11 | 11 | // Filter responses from the API calls to update the enabled features. |
12 | - add_filter( 'wl_api_service__response', array( $this, 'response' ), 10, 1 ); |
|
12 | + add_filter('wl_api_service__response', array($this, 'response'), 10, 1); |
|
13 | 13 | |
14 | 14 | // Initialize from `$_ENV`: this is currently required for Unit Tests, since `tests/bootstrap.php` loads WordLift |
15 | 15 | // before it can actually query the enabled features via HTTP (mock), which would prevent files from being included. |
@@ -19,34 +19,34 @@ discard block |
||
19 | 19 | $this->register_filters(); |
20 | 20 | |
21 | 21 | // Hook to the updates to the features setting to refresh the features' filters. |
22 | - add_action( 'update_option_' . self::WL_FEATURES, array( $this, 'register_filters' ), 10, 0 ); |
|
22 | + add_action('update_option_'.self::WL_FEATURES, array($this, 'register_filters'), 10, 0); |
|
23 | 23 | |
24 | 24 | } |
25 | 25 | |
26 | - public function response( $response ) { |
|
26 | + public function response($response) { |
|
27 | 27 | |
28 | - $headers = wp_remote_retrieve_headers( $response ); |
|
28 | + $headers = wp_remote_retrieve_headers($response); |
|
29 | 29 | |
30 | 30 | // Bail out if the `wl1` header isn't defined. |
31 | - if ( ! isset( $headers[ self::WL_1 ] ) ) { |
|
31 | + if ( ! isset($headers[self::WL_1])) { |
|
32 | 32 | return $response; |
33 | 33 | } |
34 | - $wl1_as_base64_string = $headers[ self::WL_1 ]; |
|
34 | + $wl1_as_base64_string = $headers[self::WL_1]; |
|
35 | 35 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
36 | - $wl1 = json_decode( base64_decode( $wl1_as_base64_string ), true ); |
|
36 | + $wl1 = json_decode(base64_decode($wl1_as_base64_string), true); |
|
37 | 37 | |
38 | 38 | $updated_features = $wl1['features']; |
39 | 39 | |
40 | - $existing_features = get_option( self::WL_FEATURES, array() ); |
|
40 | + $existing_features = get_option(self::WL_FEATURES, array()); |
|
41 | 41 | |
42 | 42 | // Loop through updated features. |
43 | - foreach ( $updated_features as $feature_slug => $new_value ) { |
|
43 | + foreach ($updated_features as $feature_slug => $new_value) { |
|
44 | 44 | |
45 | 45 | // We cant pass false because that indicates if the feature is active or not, null is used to represent the features which are |
46 | 46 | // not set before. |
47 | - $old_value = array_key_exists( $feature_slug, $existing_features ) ? $existing_features[ $feature_slug ] : null; |
|
47 | + $old_value = array_key_exists($feature_slug, $existing_features) ? $existing_features[$feature_slug] : null; |
|
48 | 48 | |
49 | - if ( $old_value !== $new_value ) { |
|
49 | + if ($old_value !== $new_value) { |
|
50 | 50 | /** |
51 | 51 | * @param $feature_slug string The feature slug. |
52 | 52 | * @param $old_value null | boolean Null represents the feature flag was not set before. |
@@ -56,13 +56,13 @@ discard block |
||
56 | 56 | * Hook : `wl_feature__change__{feature_slug}` |
57 | 57 | * Action hook to be fired when there is a change in feature state. |
58 | 58 | */ |
59 | - do_action( "wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug ); |
|
59 | + do_action("wl_feature__change__${feature_slug}", $new_value, $old_value, $feature_slug); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | |
63 | - if ( isset( $updated_features ) ) { |
|
63 | + if (isset($updated_features)) { |
|
64 | 64 | |
65 | - if ( update_option( self::WL_FEATURES, (array) $updated_features, true ) ) { |
|
65 | + if (update_option(self::WL_FEATURES, (array) $updated_features, true)) { |
|
66 | 66 | $this->register_filters(); |
67 | 67 | } |
68 | 68 | } |
@@ -75,13 +75,13 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function register_filters() { |
77 | 77 | |
78 | - foreach ( (array) get_option( self::WL_FEATURES, array() ) as $name => $enabled ) { |
|
78 | + foreach ((array) get_option(self::WL_FEATURES, array()) as $name => $enabled) { |
|
79 | 79 | // Remove previous filters. |
80 | - remove_filter( "wl_feature__enable__${name}", '__return_true' ); |
|
81 | - remove_filter( "wl_feature__enable__${name}", '__return_false' ); |
|
80 | + remove_filter("wl_feature__enable__${name}", '__return_true'); |
|
81 | + remove_filter("wl_feature__enable__${name}", '__return_false'); |
|
82 | 82 | |
83 | - $callback = ( $enabled ? '__return_true' : '__return_false' ); |
|
84 | - add_filter( "wl_feature__enable__${name}", $callback ); |
|
83 | + $callback = ($enabled ? '__return_true' : '__return_false'); |
|
84 | + add_filter("wl_feature__enable__${name}", $callback); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | } |
@@ -89,22 +89,22 @@ discard block |
||
89 | 89 | private function init_from_env() { |
90 | 90 | $features = array_reduce( |
91 | 91 | array_filter( |
92 | - array_keys( $_ENV ), |
|
93 | - function ( $key ) { |
|
94 | - return preg_match( '~^WL_FEATURES__.*~', $key ); |
|
92 | + array_keys($_ENV), |
|
93 | + function($key) { |
|
94 | + return preg_match('~^WL_FEATURES__.*~', $key); |
|
95 | 95 | } |
96 | 96 | ), |
97 | - function ( $features, $env ) { |
|
98 | - $name = strtolower( str_replace( '_', '-', substr( $env, strlen( 'WL_FEATURES__' ) ) ) ); |
|
99 | - $value = wp_validate_boolean( getenv( $env ) ); |
|
100 | - $features[ $name ] = $value; |
|
97 | + function($features, $env) { |
|
98 | + $name = strtolower(str_replace('_', '-', substr($env, strlen('WL_FEATURES__')))); |
|
99 | + $value = wp_validate_boolean(getenv($env)); |
|
100 | + $features[$name] = $value; |
|
101 | 101 | |
102 | 102 | return $features; |
103 | 103 | }, |
104 | 104 | array() |
105 | 105 | ); |
106 | 106 | |
107 | - update_option( self::WL_FEATURES, (array) $features, true ); |
|
107 | + update_option(self::WL_FEATURES, (array) $features, true); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | } |
@@ -17,88 +17,88 @@ |
||
17 | 17 | * @package Wordlift\Term |
18 | 18 | */ |
19 | 19 | class Type_Service extends Singleton { |
20 | - /** |
|
21 | - * @var \Wordlift_Schema_Service |
|
22 | - */ |
|
23 | - private $schema_service; |
|
24 | - |
|
25 | - public function __construct() { |
|
26 | - parent::__construct(); |
|
27 | - $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
|
28 | - } |
|
29 | - |
|
30 | - public function get_entity_types_labels( $term_id ) { |
|
31 | - $entity_types = $this->get_entity_types( $term_id ); |
|
32 | - return array_map( |
|
33 | - function ( $entity_type ) { |
|
34 | - return $entity_type->name; |
|
35 | - }, |
|
36 | - $entity_types |
|
37 | - ); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Returns the entity types selected for the term. |
|
42 | - * |
|
43 | - * @param $term_id int |
|
44 | - * |
|
45 | - * @return \WP_Term[] |
|
46 | - */ |
|
47 | - public function get_entity_types( $term_id ) { |
|
48 | - |
|
49 | - $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
50 | - |
|
51 | - $types = array_filter( |
|
52 | - array_map( |
|
53 | - function ( $type_slug ) { |
|
54 | - $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
55 | - if ( ! $term ) { |
|
56 | - return false; |
|
57 | - } |
|
58 | - |
|
59 | - return $term; |
|
60 | - }, |
|
61 | - $entity_type_slugs |
|
62 | - ) |
|
63 | - ); |
|
64 | - |
|
65 | - $types = array_filter( $types ); |
|
66 | - |
|
67 | - if ( 0 !== count( $types ) ) { |
|
68 | - return $types; |
|
69 | - } |
|
70 | - |
|
71 | - return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @param $term_id int |
|
76 | - * |
|
77 | - * @return array|null |
|
78 | - */ |
|
79 | - public function get_schema( $term_id ) { |
|
80 | - $entity_types = $this->get_entity_types( $term_id ); |
|
81 | - foreach ( $entity_types as $entity_type ) { |
|
82 | - $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
83 | - if ( ! $schema ) { |
|
84 | - break; |
|
85 | - } |
|
86 | - } |
|
87 | - |
|
88 | - return $this->schema_service->get_schema( 'thing' ); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Removes all the existing entity types and set the entity types for the term. |
|
93 | - * |
|
94 | - * @param $term_id int |
|
95 | - * @param $entity_types array |
|
96 | - */ |
|
97 | - public function set_entity_types( $term_id, $entity_types ) { |
|
98 | - delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
99 | - foreach ( $entity_types as $entity_type ) { |
|
100 | - add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $entity_type ); |
|
101 | - } |
|
102 | - } |
|
20 | + /** |
|
21 | + * @var \Wordlift_Schema_Service |
|
22 | + */ |
|
23 | + private $schema_service; |
|
24 | + |
|
25 | + public function __construct() { |
|
26 | + parent::__construct(); |
|
27 | + $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
|
28 | + } |
|
29 | + |
|
30 | + public function get_entity_types_labels( $term_id ) { |
|
31 | + $entity_types = $this->get_entity_types( $term_id ); |
|
32 | + return array_map( |
|
33 | + function ( $entity_type ) { |
|
34 | + return $entity_type->name; |
|
35 | + }, |
|
36 | + $entity_types |
|
37 | + ); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Returns the entity types selected for the term. |
|
42 | + * |
|
43 | + * @param $term_id int |
|
44 | + * |
|
45 | + * @return \WP_Term[] |
|
46 | + */ |
|
47 | + public function get_entity_types( $term_id ) { |
|
48 | + |
|
49 | + $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
50 | + |
|
51 | + $types = array_filter( |
|
52 | + array_map( |
|
53 | + function ( $type_slug ) { |
|
54 | + $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
55 | + if ( ! $term ) { |
|
56 | + return false; |
|
57 | + } |
|
58 | + |
|
59 | + return $term; |
|
60 | + }, |
|
61 | + $entity_type_slugs |
|
62 | + ) |
|
63 | + ); |
|
64 | + |
|
65 | + $types = array_filter( $types ); |
|
66 | + |
|
67 | + if ( 0 !== count( $types ) ) { |
|
68 | + return $types; |
|
69 | + } |
|
70 | + |
|
71 | + return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @param $term_id int |
|
76 | + * |
|
77 | + * @return array|null |
|
78 | + */ |
|
79 | + public function get_schema( $term_id ) { |
|
80 | + $entity_types = $this->get_entity_types( $term_id ); |
|
81 | + foreach ( $entity_types as $entity_type ) { |
|
82 | + $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
83 | + if ( ! $schema ) { |
|
84 | + break; |
|
85 | + } |
|
86 | + } |
|
87 | + |
|
88 | + return $this->schema_service->get_schema( 'thing' ); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Removes all the existing entity types and set the entity types for the term. |
|
93 | + * |
|
94 | + * @param $term_id int |
|
95 | + * @param $entity_types array |
|
96 | + */ |
|
97 | + public function set_entity_types( $term_id, $entity_types ) { |
|
98 | + delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
99 | + foreach ( $entity_types as $entity_type ) { |
|
100 | + add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $entity_type ); |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | 104 | } |
@@ -27,10 +27,10 @@ discard block |
||
27 | 27 | $this->schema_service = \Wordlift_Schema_Service::get_instance(); |
28 | 28 | } |
29 | 29 | |
30 | - public function get_entity_types_labels( $term_id ) { |
|
31 | - $entity_types = $this->get_entity_types( $term_id ); |
|
30 | + public function get_entity_types_labels($term_id) { |
|
31 | + $entity_types = $this->get_entity_types($term_id); |
|
32 | 32 | return array_map( |
33 | - function ( $entity_type ) { |
|
33 | + function($entity_type) { |
|
34 | 34 | return $entity_type->name; |
35 | 35 | }, |
36 | 36 | $entity_types |
@@ -44,15 +44,15 @@ discard block |
||
44 | 44 | * |
45 | 45 | * @return \WP_Term[] |
46 | 46 | */ |
47 | - public function get_entity_types( $term_id ) { |
|
47 | + public function get_entity_types($term_id) { |
|
48 | 48 | |
49 | - $entity_type_slugs = get_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
49 | + $entity_type_slugs = get_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
50 | 50 | |
51 | 51 | $types = array_filter( |
52 | 52 | array_map( |
53 | - function ( $type_slug ) { |
|
54 | - $term = get_term_by( 'slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
55 | - if ( ! $term ) { |
|
53 | + function($type_slug) { |
|
54 | + $term = get_term_by('slug', $type_slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
55 | + if ( ! $term) { |
|
56 | 56 | return false; |
57 | 57 | } |
58 | 58 | |
@@ -62,13 +62,13 @@ discard block |
||
62 | 62 | ) |
63 | 63 | ); |
64 | 64 | |
65 | - $types = array_filter( $types ); |
|
65 | + $types = array_filter($types); |
|
66 | 66 | |
67 | - if ( 0 !== count( $types ) ) { |
|
67 | + if (0 !== count($types)) { |
|
68 | 68 | return $types; |
69 | 69 | } |
70 | 70 | |
71 | - return array( get_term_by( 'slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ) ); |
|
71 | + return array(get_term_by('slug', 'thing', Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME)); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -76,16 +76,16 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @return array|null |
78 | 78 | */ |
79 | - public function get_schema( $term_id ) { |
|
80 | - $entity_types = $this->get_entity_types( $term_id ); |
|
81 | - foreach ( $entity_types as $entity_type ) { |
|
82 | - $schema = $this->schema_service->get_schema( $entity_type->slug ); |
|
83 | - if ( ! $schema ) { |
|
79 | + public function get_schema($term_id) { |
|
80 | + $entity_types = $this->get_entity_types($term_id); |
|
81 | + foreach ($entity_types as $entity_type) { |
|
82 | + $schema = $this->schema_service->get_schema($entity_type->slug); |
|
83 | + if ( ! $schema) { |
|
84 | 84 | break; |
85 | 85 | } |
86 | 86 | } |
87 | 87 | |
88 | - return $this->schema_service->get_schema( 'thing' ); |
|
88 | + return $this->schema_service->get_schema('thing'); |
|
89 | 89 | } |
90 | 90 | |
91 | 91 | /** |
@@ -94,10 +94,10 @@ discard block |
||
94 | 94 | * @param $term_id int |
95 | 95 | * @param $entity_types array |
96 | 96 | */ |
97 | - public function set_entity_types( $term_id, $entity_types ) { |
|
98 | - delete_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
99 | - foreach ( $entity_types as $entity_type ) { |
|
100 | - add_term_meta( $term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $entity_type ); |
|
97 | + public function set_entity_types($term_id, $entity_types) { |
|
98 | + delete_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
99 | + foreach ($entity_types as $entity_type) { |
|
100 | + add_term_meta($term_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $entity_type); |
|
101 | 101 | } |
102 | 102 | } |
103 | 103 |
@@ -11,8 +11,8 @@ |
||
11 | 11 | |
12 | 12 | class Synonyms_Service extends Singleton { |
13 | 13 | |
14 | - public function get_synonyms( $term_id ) { |
|
15 | - return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
16 | - } |
|
14 | + public function get_synonyms( $term_id ) { |
|
15 | + return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
16 | + } |
|
17 | 17 | |
18 | 18 | } |
@@ -11,8 +11,8 @@ |
||
11 | 11 | |
12 | 12 | class Synonyms_Service extends Singleton { |
13 | 13 | |
14 | - public function get_synonyms( $term_id ) { |
|
15 | - return get_term_meta( $term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ); |
|
14 | + public function get_synonyms($term_id) { |
|
15 | + return get_term_meta($term_id, \Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY); |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | } |
@@ -20,378 +20,378 @@ |
||
20 | 20 | |
21 | 21 | class Post_Adapter { |
22 | 22 | |
23 | - /** |
|
24 | - * A {@link Wordlift_Log_Service} logging instance. |
|
25 | - * |
|
26 | - * @access private |
|
27 | - * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
28 | - */ |
|
29 | - private $log; |
|
30 | - |
|
31 | - public function __construct() { |
|
32 | - |
|
33 | - // Bail out if block editor's functions aren't available. |
|
34 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
35 | - return; |
|
36 | - } |
|
37 | - |
|
38 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
39 | - |
|
40 | - add_action( 'init', array( $this, 'init' ) ); |
|
41 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10 ); |
|
42 | - |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
47 | - * correctly. |
|
48 | - */ |
|
49 | - public function init() { |
|
50 | - |
|
51 | - register_block_type( |
|
52 | - 'wordlift/classification', |
|
53 | - array( |
|
54 | - 'editor_script' => 'wl-block-editor', |
|
55 | - 'attributes' => array( |
|
56 | - 'entities' => array( 'type' => 'array' ), |
|
57 | - ), |
|
58 | - ) |
|
59 | - ); |
|
60 | - |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * A sample structure: |
|
65 | - * |
|
66 | - * { |
|
67 | - * "entities": [ |
|
68 | - * { |
|
69 | - * "annotations": { |
|
70 | - * "urn:enhancement-7e8e66fc": { |
|
71 | - * "start": 3480, |
|
72 | - * "end": 3486, |
|
73 | - * "text": "libero" |
|
74 | - * } |
|
75 | - * }, |
|
76 | - * "description": "Le libero ou libéro est un poste défensif du volley-ball. Des règles particulières le concernant ont été introduites à la fin des années 1990. De par sa spécificité, le libéro a un statut à part au sein d’une équipe de volley-ball. Pour être identifié, il doit porter un uniforme qui contraste avec ceux des autres membres de son équipe, titulaires ou remplaçants.", |
|
77 | - * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
78 | - * "label": "Libero (volley-ball)", |
|
79 | - * "mainType": "other", |
|
80 | - * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
81 | - * "sameAs": null, |
|
82 | - * "synonyms": [], |
|
83 | - * "types": ["other"] |
|
84 | - * } |
|
85 | - * ] |
|
86 | - * } |
|
87 | - * |
|
88 | - * @param array $data An array of slashed post data. |
|
89 | - * |
|
90 | - * @return array The data array. |
|
91 | - */ |
|
92 | - public function wp_insert_post_data( $data ) { |
|
93 | - $post_status = $data['post_status']; |
|
94 | - if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
95 | - return $data; |
|
96 | - } |
|
97 | - |
|
98 | - try { |
|
99 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
100 | - |
|
101 | - foreach ( $entities as $entity ) { |
|
102 | - |
|
103 | - $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
104 | - |
|
105 | - if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
106 | - continue; |
|
107 | - } |
|
108 | - |
|
109 | - $entity_uris = $this->get_entity_uris( $entity ); |
|
110 | - |
|
111 | - if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
112 | - Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
113 | - // Skip the entity |
|
114 | - continue; |
|
115 | - } |
|
116 | - |
|
117 | - // If 'entity auto publish' is false, we set the status to `draft` by default. |
|
118 | - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
119 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
120 | - ? $data['post_status'] : 'draft'; |
|
121 | - $this->create_or_update_entity( $entity, $post_status ); |
|
122 | - |
|
123 | - } |
|
124 | - } catch ( \Exception $e ) { |
|
125 | - $this->log->error( $e->getMessage() ); |
|
126 | - } |
|
127 | - |
|
128 | - return $data; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
133 | - * |
|
134 | - * @param string $post_content The post content. |
|
135 | - * |
|
136 | - * @return array An array of entities' structures. |
|
137 | - * @throws \Exception when an error occurs. |
|
138 | - */ |
|
139 | - private function parse_content( $post_content ) { |
|
140 | - |
|
141 | - $all_blocks = parse_blocks( $post_content ); |
|
142 | - |
|
143 | - $blocks = array_filter( |
|
144 | - $all_blocks, |
|
145 | - function ( $item ) { |
|
146 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
147 | - } |
|
148 | - ); |
|
149 | - |
|
150 | - // Bail out if the blocks' array is empty. |
|
151 | - if ( empty( $blocks ) ) { |
|
152 | - return array(); |
|
153 | - } |
|
154 | - |
|
155 | - $block = current( $blocks ); |
|
156 | - |
|
157 | - // Bail out if the entities array is empty. |
|
158 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
159 | - return array(); |
|
160 | - } |
|
161 | - |
|
162 | - return $block['attrs']['entities']; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Collect entity labels from the entity array. |
|
167 | - * |
|
168 | - * This function expects an array with the following keys: |
|
169 | - * |
|
170 | - * array( |
|
171 | - * 'label' => ..., |
|
172 | - * 'synonyms' => array( ... ), |
|
173 | - * 'annotations' => array( |
|
174 | - * ...id... => array( text => ... ), |
|
175 | - * ), |
|
176 | - * 'occurrences' => array( ... ), |
|
177 | - * ) |
|
178 | - * |
|
179 | - * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
180 | - * |
|
181 | - * array( |
|
182 | - * ...label..., |
|
183 | - * ...synonyms..., |
|
184 | - * ...texts..., |
|
185 | - * ) |
|
186 | - * |
|
187 | - * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
188 | - * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
189 | - * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
190 | - * `occurrences`. |
|
191 | - * |
|
192 | - * @param array $entity { |
|
193 | - * The entity data. |
|
194 | - * |
|
195 | - * @type string $label The entity label. |
|
196 | - * @type array $synonyms The entity synonyms. |
|
197 | - * @type array $occurrences The selected occurrences. |
|
198 | - * @type array $annotations The annotations. |
|
199 | - * } |
|
200 | - * |
|
201 | - * @return array An array of labels. |
|
202 | - */ |
|
203 | - public function get_labels( $entity ) { |
|
204 | - |
|
205 | - $args = wp_parse_args( |
|
206 | - $entity, |
|
207 | - array( |
|
208 | - 'label' => array(), |
|
209 | - 'synonyms' => array(), |
|
210 | - 'annotations' => array(), |
|
211 | - 'occurrences' => array(), |
|
212 | - ) |
|
213 | - ); |
|
214 | - |
|
215 | - // We gather all the labels, occurrences texts and synonyms into one array. |
|
216 | - $initial = array_merge( |
|
217 | - (array) $args['label'], |
|
218 | - (array) $args['synonyms'] |
|
219 | - ); |
|
220 | - |
|
221 | - $annotations = $args['annotations']; |
|
222 | - |
|
223 | - return array_reduce( |
|
224 | - $args['occurrences'], |
|
225 | - function ( $carry, $item ) use ( $annotations ) { |
|
226 | - |
|
227 | - // Bail out if occurrences->$item->text isn't set or its contents are already |
|
228 | - // in `$carry`. |
|
229 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
230 | - || in_array( $annotations[ $item ]['text'], $carry, true ) ) { |
|
231 | - return $carry; |
|
232 | - } |
|
233 | - |
|
234 | - // Push the label. |
|
235 | - $carry[] = $annotations[ $item ]['text']; |
|
236 | - |
|
237 | - return $carry; |
|
238 | - }, |
|
239 | - $initial |
|
240 | - ); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Create or update the entity. |
|
245 | - * |
|
246 | - * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
247 | - * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
248 | - * |
|
249 | - * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
250 | - * |
|
251 | - * @param array $entity { |
|
252 | - * The entity parameters. |
|
253 | - * |
|
254 | - * @type string The entity item id URI. |
|
255 | - * @type string|array The entity sameAs URI(s). |
|
256 | - * @type string $description The entity description. |
|
257 | - * } |
|
258 | - * |
|
259 | - * @param $string $post_status The post status, default 'draft'. |
|
260 | - * |
|
261 | - * @return int|\WP_Error |
|
262 | - * @throws \Exception when an error occurs. |
|
263 | - */ |
|
264 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
265 | - |
|
266 | - // Get only valid IDs. |
|
267 | - $uris = $this->get_entity_uris( $entity ); |
|
268 | - |
|
269 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
270 | - |
|
271 | - // Get the labels. |
|
272 | - $labels = $this->get_labels( $entity ); |
|
273 | - |
|
274 | - if ( empty( $post ) ) { |
|
275 | - $entity_description = array_key_exists( 'description', $entity ) ? $entity['description'] : ''; |
|
276 | - // Create the entity if it doesn't exist. |
|
277 | - $post_id = Entity_Store::get_instance()->create( |
|
278 | - array( |
|
279 | - 'labels' => $labels, |
|
280 | - 'description' => $entity_description, |
|
281 | - 'same_as' => $uris, |
|
282 | - ), |
|
283 | - $post_status |
|
284 | - ); |
|
285 | - |
|
286 | - // Return the WP_Error if we got one. |
|
287 | - if ( is_wp_error( $post_id ) ) { |
|
288 | - return $post_id; |
|
289 | - } |
|
290 | - |
|
291 | - // Add the entity type. |
|
292 | - if ( isset( $entity['mainType'] ) ) { |
|
293 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
294 | - } |
|
295 | - |
|
296 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
297 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
298 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
299 | - } |
|
300 | - } else { |
|
301 | - // Update the entity otherwise. |
|
302 | - $post_id = Entity_Store::get_instance()->update( |
|
303 | - array( |
|
304 | - 'ID' => $post->ID, |
|
305 | - 'labels' => $labels, |
|
306 | - 'same_as' => $uris, |
|
307 | - ) |
|
308 | - ); |
|
309 | - |
|
310 | - // Add the entity type. |
|
311 | - if ( isset( $entity['mainType'] ) ) { |
|
312 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
313 | - } |
|
314 | - |
|
315 | - // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
|
316 | - // Set the post status, we need to set that in order to support entities |
|
317 | - // created using rest endpoint on block editor, so that they get published |
|
318 | - // when the post is published. |
|
319 | - // Once the entity is published don't update the post status. |
|
320 | - if ( 'publish' !== $post->post_status ) { |
|
321 | - wp_update_post( |
|
322 | - array( |
|
323 | - 'ID' => $post->ID, |
|
324 | - 'post_status' => $post_status, |
|
325 | - ) |
|
326 | - ); |
|
327 | - } |
|
328 | - } |
|
329 | - |
|
330 | - return $post_id; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Get the first matching entity for the provided URI array. |
|
335 | - * |
|
336 | - * Entities IDs and sameAs are searched. |
|
337 | - * |
|
338 | - * @param array $uris An array of URIs. |
|
339 | - * |
|
340 | - * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
341 | - */ |
|
342 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
343 | - |
|
344 | - foreach ( $uris as $uri ) { |
|
345 | - |
|
346 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
347 | - |
|
348 | - if ( ! $content ) { |
|
349 | - continue; |
|
350 | - } |
|
351 | - |
|
352 | - $existing_entity = $content->get_bag(); |
|
353 | - |
|
354 | - if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
355 | - return $existing_entity; |
|
356 | - } |
|
357 | - } |
|
358 | - |
|
359 | - return null; |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @param $entity |
|
364 | - * |
|
365 | - * @return array |
|
366 | - */ |
|
367 | - private function filter_valid_entity_ids( $entity ) { |
|
368 | - $id = $entity['id']; |
|
369 | - |
|
370 | - return array_filter( |
|
371 | - (array) $id, |
|
372 | - function ( $id ) { |
|
373 | - return preg_match( '|^https?://|', $id ); |
|
374 | - } |
|
375 | - ); |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * @param array $entity |
|
380 | - * |
|
381 | - * @return array |
|
382 | - */ |
|
383 | - private function get_entity_uris( $entity ) { |
|
384 | - $ids = $this->filter_valid_entity_ids( $entity ); |
|
385 | - $same_as = array_key_exists( 'sameAs', $entity ) ? $entity['sameAs'] : array(); |
|
386 | - |
|
387 | - return array_merge( |
|
388 | - (array) $ids, |
|
389 | - (array) $same_as |
|
390 | - ); |
|
391 | - } |
|
392 | - |
|
393 | - private function entity_id_valid( $entity_id ) { |
|
394 | - return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
395 | - } |
|
23 | + /** |
|
24 | + * A {@link Wordlift_Log_Service} logging instance. |
|
25 | + * |
|
26 | + * @access private |
|
27 | + * @var \Wordlift_Log_Service A {@link Wordlift_Log_Service} logging instance. |
|
28 | + */ |
|
29 | + private $log; |
|
30 | + |
|
31 | + public function __construct() { |
|
32 | + |
|
33 | + // Bail out if block editor's functions aren't available. |
|
34 | + if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
35 | + return; |
|
36 | + } |
|
37 | + |
|
38 | + $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
39 | + |
|
40 | + add_action( 'init', array( $this, 'init' ) ); |
|
41 | + add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10 ); |
|
42 | + |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Initialize by registering our block type `wordlift/classification`, required for {@link parse_blocks) to work |
|
47 | + * correctly. |
|
48 | + */ |
|
49 | + public function init() { |
|
50 | + |
|
51 | + register_block_type( |
|
52 | + 'wordlift/classification', |
|
53 | + array( |
|
54 | + 'editor_script' => 'wl-block-editor', |
|
55 | + 'attributes' => array( |
|
56 | + 'entities' => array( 'type' => 'array' ), |
|
57 | + ), |
|
58 | + ) |
|
59 | + ); |
|
60 | + |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * A sample structure: |
|
65 | + * |
|
66 | + * { |
|
67 | + * "entities": [ |
|
68 | + * { |
|
69 | + * "annotations": { |
|
70 | + * "urn:enhancement-7e8e66fc": { |
|
71 | + * "start": 3480, |
|
72 | + * "end": 3486, |
|
73 | + * "text": "libero" |
|
74 | + * } |
|
75 | + * }, |
|
76 | + * "description": "Le libero ou libéro est un poste défensif du volley-ball. Des règles particulières le concernant ont été introduites à la fin des années 1990. De par sa spécificité, le libéro a un statut à part au sein d’une équipe de volley-ball. Pour être identifié, il doit porter un uniforme qui contraste avec ceux des autres membres de son équipe, titulaires ou remplaçants.", |
|
77 | + * "id": "http://fr.dbpedia.org/resource/Libero_(volley-ball)", |
|
78 | + * "label": "Libero (volley-ball)", |
|
79 | + * "mainType": "other", |
|
80 | + * "occurrences": ["urn:enhancement-7e8e66fc"], |
|
81 | + * "sameAs": null, |
|
82 | + * "synonyms": [], |
|
83 | + * "types": ["other"] |
|
84 | + * } |
|
85 | + * ] |
|
86 | + * } |
|
87 | + * |
|
88 | + * @param array $data An array of slashed post data. |
|
89 | + * |
|
90 | + * @return array The data array. |
|
91 | + */ |
|
92 | + public function wp_insert_post_data( $data ) { |
|
93 | + $post_status = $data['post_status']; |
|
94 | + if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
95 | + return $data; |
|
96 | + } |
|
97 | + |
|
98 | + try { |
|
99 | + $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
100 | + |
|
101 | + foreach ( $entities as $entity ) { |
|
102 | + |
|
103 | + $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
104 | + |
|
105 | + if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
106 | + continue; |
|
107 | + } |
|
108 | + |
|
109 | + $entity_uris = $this->get_entity_uris( $entity ); |
|
110 | + |
|
111 | + if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
112 | + Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
113 | + // Skip the entity |
|
114 | + continue; |
|
115 | + } |
|
116 | + |
|
117 | + // If 'entity auto publish' is false, we set the status to `draft` by default. |
|
118 | + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
|
119 | + $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
120 | + ? $data['post_status'] : 'draft'; |
|
121 | + $this->create_or_update_entity( $entity, $post_status ); |
|
122 | + |
|
123 | + } |
|
124 | + } catch ( \Exception $e ) { |
|
125 | + $this->log->error( $e->getMessage() ); |
|
126 | + } |
|
127 | + |
|
128 | + return $data; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Parse the post content to find the `wordlift/classification` block and return the entities' data. |
|
133 | + * |
|
134 | + * @param string $post_content The post content. |
|
135 | + * |
|
136 | + * @return array An array of entities' structures. |
|
137 | + * @throws \Exception when an error occurs. |
|
138 | + */ |
|
139 | + private function parse_content( $post_content ) { |
|
140 | + |
|
141 | + $all_blocks = parse_blocks( $post_content ); |
|
142 | + |
|
143 | + $blocks = array_filter( |
|
144 | + $all_blocks, |
|
145 | + function ( $item ) { |
|
146 | + return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
147 | + } |
|
148 | + ); |
|
149 | + |
|
150 | + // Bail out if the blocks' array is empty. |
|
151 | + if ( empty( $blocks ) ) { |
|
152 | + return array(); |
|
153 | + } |
|
154 | + |
|
155 | + $block = current( $blocks ); |
|
156 | + |
|
157 | + // Bail out if the entities array is empty. |
|
158 | + if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
159 | + return array(); |
|
160 | + } |
|
161 | + |
|
162 | + return $block['attrs']['entities']; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Collect entity labels from the entity array. |
|
167 | + * |
|
168 | + * This function expects an array with the following keys: |
|
169 | + * |
|
170 | + * array( |
|
171 | + * 'label' => ..., |
|
172 | + * 'synonyms' => array( ... ), |
|
173 | + * 'annotations' => array( |
|
174 | + * ...id... => array( text => ... ), |
|
175 | + * ), |
|
176 | + * 'occurrences' => array( ... ), |
|
177 | + * ) |
|
178 | + * |
|
179 | + * and it is going to output an array with all the labels, keeping the `label` at first position: |
|
180 | + * |
|
181 | + * array( |
|
182 | + * ...label..., |
|
183 | + * ...synonyms..., |
|
184 | + * ...texts..., |
|
185 | + * ) |
|
186 | + * |
|
187 | + * This function is going to collect the label from the `label` property, from the `synonyms` property and from |
|
188 | + * `annotations` property. Since the `annotations` property contains all the annotations including those that |
|
189 | + * haven't been selected, this function is going to only get the `text` for the annotations property listed in |
|
190 | + * `occurrences`. |
|
191 | + * |
|
192 | + * @param array $entity { |
|
193 | + * The entity data. |
|
194 | + * |
|
195 | + * @type string $label The entity label. |
|
196 | + * @type array $synonyms The entity synonyms. |
|
197 | + * @type array $occurrences The selected occurrences. |
|
198 | + * @type array $annotations The annotations. |
|
199 | + * } |
|
200 | + * |
|
201 | + * @return array An array of labels. |
|
202 | + */ |
|
203 | + public function get_labels( $entity ) { |
|
204 | + |
|
205 | + $args = wp_parse_args( |
|
206 | + $entity, |
|
207 | + array( |
|
208 | + 'label' => array(), |
|
209 | + 'synonyms' => array(), |
|
210 | + 'annotations' => array(), |
|
211 | + 'occurrences' => array(), |
|
212 | + ) |
|
213 | + ); |
|
214 | + |
|
215 | + // We gather all the labels, occurrences texts and synonyms into one array. |
|
216 | + $initial = array_merge( |
|
217 | + (array) $args['label'], |
|
218 | + (array) $args['synonyms'] |
|
219 | + ); |
|
220 | + |
|
221 | + $annotations = $args['annotations']; |
|
222 | + |
|
223 | + return array_reduce( |
|
224 | + $args['occurrences'], |
|
225 | + function ( $carry, $item ) use ( $annotations ) { |
|
226 | + |
|
227 | + // Bail out if occurrences->$item->text isn't set or its contents are already |
|
228 | + // in `$carry`. |
|
229 | + if ( ! isset( $annotations[ $item ]['text'] ) |
|
230 | + || in_array( $annotations[ $item ]['text'], $carry, true ) ) { |
|
231 | + return $carry; |
|
232 | + } |
|
233 | + |
|
234 | + // Push the label. |
|
235 | + $carry[] = $annotations[ $item ]['text']; |
|
236 | + |
|
237 | + return $carry; |
|
238 | + }, |
|
239 | + $initial |
|
240 | + ); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Create or update the entity. |
|
245 | + * |
|
246 | + * An entity lookup is performed on the local vocabulary using the `id` and `sameAs` URIs. If an entity is found |
|
247 | + * the {@link Entity_Store} update function is called to update the `labels` and the `sameAs` values. |
|
248 | + * |
|
249 | + * If an entity is not found the {@link Entity_Store} create function is called to create a new entity. |
|
250 | + * |
|
251 | + * @param array $entity { |
|
252 | + * The entity parameters. |
|
253 | + * |
|
254 | + * @type string The entity item id URI. |
|
255 | + * @type string|array The entity sameAs URI(s). |
|
256 | + * @type string $description The entity description. |
|
257 | + * } |
|
258 | + * |
|
259 | + * @param $string $post_status The post status, default 'draft'. |
|
260 | + * |
|
261 | + * @return int|\WP_Error |
|
262 | + * @throws \Exception when an error occurs. |
|
263 | + */ |
|
264 | + private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
265 | + |
|
266 | + // Get only valid IDs. |
|
267 | + $uris = $this->get_entity_uris( $entity ); |
|
268 | + |
|
269 | + $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
270 | + |
|
271 | + // Get the labels. |
|
272 | + $labels = $this->get_labels( $entity ); |
|
273 | + |
|
274 | + if ( empty( $post ) ) { |
|
275 | + $entity_description = array_key_exists( 'description', $entity ) ? $entity['description'] : ''; |
|
276 | + // Create the entity if it doesn't exist. |
|
277 | + $post_id = Entity_Store::get_instance()->create( |
|
278 | + array( |
|
279 | + 'labels' => $labels, |
|
280 | + 'description' => $entity_description, |
|
281 | + 'same_as' => $uris, |
|
282 | + ), |
|
283 | + $post_status |
|
284 | + ); |
|
285 | + |
|
286 | + // Return the WP_Error if we got one. |
|
287 | + if ( is_wp_error( $post_id ) ) { |
|
288 | + return $post_id; |
|
289 | + } |
|
290 | + |
|
291 | + // Add the entity type. |
|
292 | + if ( isset( $entity['mainType'] ) ) { |
|
293 | + wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
294 | + } |
|
295 | + |
|
296 | + if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
297 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
298 | + add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
299 | + } |
|
300 | + } else { |
|
301 | + // Update the entity otherwise. |
|
302 | + $post_id = Entity_Store::get_instance()->update( |
|
303 | + array( |
|
304 | + 'ID' => $post->ID, |
|
305 | + 'labels' => $labels, |
|
306 | + 'same_as' => $uris, |
|
307 | + ) |
|
308 | + ); |
|
309 | + |
|
310 | + // Add the entity type. |
|
311 | + if ( isset( $entity['mainType'] ) ) { |
|
312 | + wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
313 | + } |
|
314 | + |
|
315 | + // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
|
316 | + // Set the post status, we need to set that in order to support entities |
|
317 | + // created using rest endpoint on block editor, so that they get published |
|
318 | + // when the post is published. |
|
319 | + // Once the entity is published don't update the post status. |
|
320 | + if ( 'publish' !== $post->post_status ) { |
|
321 | + wp_update_post( |
|
322 | + array( |
|
323 | + 'ID' => $post->ID, |
|
324 | + 'post_status' => $post_status, |
|
325 | + ) |
|
326 | + ); |
|
327 | + } |
|
328 | + } |
|
329 | + |
|
330 | + return $post_id; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Get the first matching entity for the provided URI array. |
|
335 | + * |
|
336 | + * Entities IDs and sameAs are searched. |
|
337 | + * |
|
338 | + * @param array $uris An array of URIs. |
|
339 | + * |
|
340 | + * @return \WP_Post|null The entity WP_Post if found or null if not found. |
|
341 | + */ |
|
342 | + private function get_first_matching_entity_by_uri( $uris ) { |
|
343 | + |
|
344 | + foreach ( $uris as $uri ) { |
|
345 | + |
|
346 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
347 | + |
|
348 | + if ( ! $content ) { |
|
349 | + continue; |
|
350 | + } |
|
351 | + |
|
352 | + $existing_entity = $content->get_bag(); |
|
353 | + |
|
354 | + if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
355 | + return $existing_entity; |
|
356 | + } |
|
357 | + } |
|
358 | + |
|
359 | + return null; |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @param $entity |
|
364 | + * |
|
365 | + * @return array |
|
366 | + */ |
|
367 | + private function filter_valid_entity_ids( $entity ) { |
|
368 | + $id = $entity['id']; |
|
369 | + |
|
370 | + return array_filter( |
|
371 | + (array) $id, |
|
372 | + function ( $id ) { |
|
373 | + return preg_match( '|^https?://|', $id ); |
|
374 | + } |
|
375 | + ); |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * @param array $entity |
|
380 | + * |
|
381 | + * @return array |
|
382 | + */ |
|
383 | + private function get_entity_uris( $entity ) { |
|
384 | + $ids = $this->filter_valid_entity_ids( $entity ); |
|
385 | + $same_as = array_key_exists( 'sameAs', $entity ) ? $entity['sameAs'] : array(); |
|
386 | + |
|
387 | + return array_merge( |
|
388 | + (array) $ids, |
|
389 | + (array) $same_as |
|
390 | + ); |
|
391 | + } |
|
392 | + |
|
393 | + private function entity_id_valid( $entity_id ) { |
|
394 | + return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
395 | + } |
|
396 | 396 | |
397 | 397 | } |
@@ -31,14 +31,14 @@ discard block |
||
31 | 31 | public function __construct() { |
32 | 32 | |
33 | 33 | // Bail out if block editor's functions aren't available. |
34 | - if ( ! function_exists( 'register_block_type' ) || ! function_exists( 'parse_blocks' ) ) { |
|
34 | + if ( ! function_exists('register_block_type') || ! function_exists('parse_blocks')) { |
|
35 | 35 | return; |
36 | 36 | } |
37 | 37 | |
38 | - $this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
38 | + $this->log = \Wordlift_Log_Service::get_logger(get_class()); |
|
39 | 39 | |
40 | - add_action( 'init', array( $this, 'init' ) ); |
|
41 | - add_filter( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10 ); |
|
40 | + add_action('init', array($this, 'init')); |
|
41 | + add_filter('wp_insert_post_data', array($this, 'wp_insert_post_data'), 10); |
|
42 | 42 | |
43 | 43 | } |
44 | 44 | |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | array( |
54 | 54 | 'editor_script' => 'wl-block-editor', |
55 | 55 | 'attributes' => array( |
56 | - 'entities' => array( 'type' => 'array' ), |
|
56 | + 'entities' => array('type' => 'array'), |
|
57 | 57 | ), |
58 | 58 | ) |
59 | 59 | ); |
@@ -89,40 +89,40 @@ discard block |
||
89 | 89 | * |
90 | 90 | * @return array The data array. |
91 | 91 | */ |
92 | - public function wp_insert_post_data( $data ) { |
|
92 | + public function wp_insert_post_data($data) { |
|
93 | 93 | $post_status = $data['post_status']; |
94 | - if ( 'auto-draft' === $post_status || 'inherit' === $post_status ) { |
|
94 | + if ('auto-draft' === $post_status || 'inherit' === $post_status) { |
|
95 | 95 | return $data; |
96 | 96 | } |
97 | 97 | |
98 | 98 | try { |
99 | - $entities = $this->parse_content( wp_unslash( $data['post_content'] ) ); |
|
99 | + $entities = $this->parse_content(wp_unslash($data['post_content'])); |
|
100 | 100 | |
101 | - foreach ( $entities as $entity ) { |
|
101 | + foreach ($entities as $entity) { |
|
102 | 102 | |
103 | - $entity_id = array_key_exists( 'id', $entity ) ? $entity['id'] : ''; |
|
103 | + $entity_id = array_key_exists('id', $entity) ? $entity['id'] : ''; |
|
104 | 104 | |
105 | - if ( ! $this->entity_id_valid( $entity_id ) ) { |
|
105 | + if ( ! $this->entity_id_valid($entity_id)) { |
|
106 | 106 | continue; |
107 | 107 | } |
108 | 108 | |
109 | - $entity_uris = $this->get_entity_uris( $entity ); |
|
109 | + $entity_uris = $this->get_entity_uris($entity); |
|
110 | 110 | |
111 | - if ( $this->get_first_matching_entity_by_uri( $entity_uris ) === null && |
|
112 | - Post_Entities_Validator::is_local_entity_uri_exist( Wordlift_Entity_Uri_Service::get_instance(), $entity_uris ) ) { |
|
111 | + if ($this->get_first_matching_entity_by_uri($entity_uris) === null && |
|
112 | + Post_Entities_Validator::is_local_entity_uri_exist(Wordlift_Entity_Uri_Service::get_instance(), $entity_uris)) { |
|
113 | 113 | // Skip the entity |
114 | 114 | continue; |
115 | 115 | } |
116 | 116 | |
117 | 117 | // If 'entity auto publish' is false, we set the status to `draft` by default. |
118 | 118 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
119 | - $post_status = apply_filters( 'wl_feature__enable__entity-auto-publish', true ) |
|
119 | + $post_status = apply_filters('wl_feature__enable__entity-auto-publish', true) |
|
120 | 120 | ? $data['post_status'] : 'draft'; |
121 | - $this->create_or_update_entity( $entity, $post_status ); |
|
121 | + $this->create_or_update_entity($entity, $post_status); |
|
122 | 122 | |
123 | 123 | } |
124 | - } catch ( \Exception $e ) { |
|
125 | - $this->log->error( $e->getMessage() ); |
|
124 | + } catch (\Exception $e) { |
|
125 | + $this->log->error($e->getMessage()); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | return $data; |
@@ -136,26 +136,26 @@ discard block |
||
136 | 136 | * @return array An array of entities' structures. |
137 | 137 | * @throws \Exception when an error occurs. |
138 | 138 | */ |
139 | - private function parse_content( $post_content ) { |
|
139 | + private function parse_content($post_content) { |
|
140 | 140 | |
141 | - $all_blocks = parse_blocks( $post_content ); |
|
141 | + $all_blocks = parse_blocks($post_content); |
|
142 | 142 | |
143 | 143 | $blocks = array_filter( |
144 | 144 | $all_blocks, |
145 | - function ( $item ) { |
|
146 | - return ! empty( $item['blockName'] ) && 'wordlift/classification' === $item['blockName']; |
|
145 | + function($item) { |
|
146 | + return ! empty($item['blockName']) && 'wordlift/classification' === $item['blockName']; |
|
147 | 147 | } |
148 | 148 | ); |
149 | 149 | |
150 | 150 | // Bail out if the blocks' array is empty. |
151 | - if ( empty( $blocks ) ) { |
|
151 | + if (empty($blocks)) { |
|
152 | 152 | return array(); |
153 | 153 | } |
154 | 154 | |
155 | - $block = current( $blocks ); |
|
155 | + $block = current($blocks); |
|
156 | 156 | |
157 | 157 | // Bail out if the entities array is empty. |
158 | - if ( empty( $block['attrs'] ) && empty( $block['attrs']['entities'] ) ) { |
|
158 | + if (empty($block['attrs']) && empty($block['attrs']['entities'])) { |
|
159 | 159 | return array(); |
160 | 160 | } |
161 | 161 | |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | * |
201 | 201 | * @return array An array of labels. |
202 | 202 | */ |
203 | - public function get_labels( $entity ) { |
|
203 | + public function get_labels($entity) { |
|
204 | 204 | |
205 | 205 | $args = wp_parse_args( |
206 | 206 | $entity, |
@@ -222,17 +222,17 @@ discard block |
||
222 | 222 | |
223 | 223 | return array_reduce( |
224 | 224 | $args['occurrences'], |
225 | - function ( $carry, $item ) use ( $annotations ) { |
|
225 | + function($carry, $item) use ($annotations) { |
|
226 | 226 | |
227 | 227 | // Bail out if occurrences->$item->text isn't set or its contents are already |
228 | 228 | // in `$carry`. |
229 | - if ( ! isset( $annotations[ $item ]['text'] ) |
|
230 | - || in_array( $annotations[ $item ]['text'], $carry, true ) ) { |
|
229 | + if ( ! isset($annotations[$item]['text']) |
|
230 | + || in_array($annotations[$item]['text'], $carry, true)) { |
|
231 | 231 | return $carry; |
232 | 232 | } |
233 | 233 | |
234 | 234 | // Push the label. |
235 | - $carry[] = $annotations[ $item ]['text']; |
|
235 | + $carry[] = $annotations[$item]['text']; |
|
236 | 236 | |
237 | 237 | return $carry; |
238 | 238 | }, |
@@ -261,18 +261,18 @@ discard block |
||
261 | 261 | * @return int|\WP_Error |
262 | 262 | * @throws \Exception when an error occurs. |
263 | 263 | */ |
264 | - private function create_or_update_entity( $entity, $post_status = 'draft' ) { |
|
264 | + private function create_or_update_entity($entity, $post_status = 'draft') { |
|
265 | 265 | |
266 | 266 | // Get only valid IDs. |
267 | - $uris = $this->get_entity_uris( $entity ); |
|
267 | + $uris = $this->get_entity_uris($entity); |
|
268 | 268 | |
269 | - $post = $this->get_first_matching_entity_by_uri( $uris ); |
|
269 | + $post = $this->get_first_matching_entity_by_uri($uris); |
|
270 | 270 | |
271 | 271 | // Get the labels. |
272 | - $labels = $this->get_labels( $entity ); |
|
272 | + $labels = $this->get_labels($entity); |
|
273 | 273 | |
274 | - if ( empty( $post ) ) { |
|
275 | - $entity_description = array_key_exists( 'description', $entity ) ? $entity['description'] : ''; |
|
274 | + if (empty($post)) { |
|
275 | + $entity_description = array_key_exists('description', $entity) ? $entity['description'] : ''; |
|
276 | 276 | // Create the entity if it doesn't exist. |
277 | 277 | $post_id = Entity_Store::get_instance()->create( |
278 | 278 | array( |
@@ -284,18 +284,18 @@ discard block |
||
284 | 284 | ); |
285 | 285 | |
286 | 286 | // Return the WP_Error if we got one. |
287 | - if ( is_wp_error( $post_id ) ) { |
|
287 | + if (is_wp_error($post_id)) { |
|
288 | 288 | return $post_id; |
289 | 289 | } |
290 | 290 | |
291 | 291 | // Add the entity type. |
292 | - if ( isset( $entity['mainType'] ) ) { |
|
293 | - wp_set_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
292 | + if (isset($entity['mainType'])) { |
|
293 | + wp_set_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
294 | 294 | } |
295 | 295 | |
296 | - if ( isset( $entity['properties'] ) && isset( $entity['properties']['latitude'] ) && isset( $entity['properties']['longitude'] ) ) { |
|
297 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude'] ); |
|
298 | - add_post_meta( $post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude'] ); |
|
296 | + if (isset($entity['properties']) && isset($entity['properties']['latitude']) && isset($entity['properties']['longitude'])) { |
|
297 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LATITUDE, $entity['properties']['latitude']); |
|
298 | + add_post_meta($post_id, \Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, $entity['properties']['longitude']); |
|
299 | 299 | } |
300 | 300 | } else { |
301 | 301 | // Update the entity otherwise. |
@@ -308,8 +308,8 @@ discard block |
||
308 | 308 | ); |
309 | 309 | |
310 | 310 | // Add the entity type. |
311 | - if ( isset( $entity['mainType'] ) ) { |
|
312 | - wp_add_object_terms( $post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
311 | + if (isset($entity['mainType'])) { |
|
312 | + wp_add_object_terms($post_id, $entity['mainType'], \Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
313 | 313 | } |
314 | 314 | |
315 | 315 | // see https://github.com/insideout10/wordlift-plugin/issues/1304 |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | // created using rest endpoint on block editor, so that they get published |
318 | 318 | // when the post is published. |
319 | 319 | // Once the entity is published don't update the post status. |
320 | - if ( 'publish' !== $post->post_status ) { |
|
320 | + if ('publish' !== $post->post_status) { |
|
321 | 321 | wp_update_post( |
322 | 322 | array( |
323 | 323 | 'ID' => $post->ID, |
@@ -339,19 +339,19 @@ discard block |
||
339 | 339 | * |
340 | 340 | * @return \WP_Post|null The entity WP_Post if found or null if not found. |
341 | 341 | */ |
342 | - private function get_first_matching_entity_by_uri( $uris ) { |
|
342 | + private function get_first_matching_entity_by_uri($uris) { |
|
343 | 343 | |
344 | - foreach ( $uris as $uri ) { |
|
344 | + foreach ($uris as $uri) { |
|
345 | 345 | |
346 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as( $uri ); |
|
346 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id_or_same_as($uri); |
|
347 | 347 | |
348 | - if ( ! $content ) { |
|
348 | + if ( ! $content) { |
|
349 | 349 | continue; |
350 | 350 | } |
351 | 351 | |
352 | 352 | $existing_entity = $content->get_bag(); |
353 | 353 | |
354 | - if ( is_a( $existing_entity, 'WP_Post' ) ) { |
|
354 | + if (is_a($existing_entity, 'WP_Post')) { |
|
355 | 355 | return $existing_entity; |
356 | 356 | } |
357 | 357 | } |
@@ -364,13 +364,13 @@ discard block |
||
364 | 364 | * |
365 | 365 | * @return array |
366 | 366 | */ |
367 | - private function filter_valid_entity_ids( $entity ) { |
|
367 | + private function filter_valid_entity_ids($entity) { |
|
368 | 368 | $id = $entity['id']; |
369 | 369 | |
370 | 370 | return array_filter( |
371 | 371 | (array) $id, |
372 | - function ( $id ) { |
|
373 | - return preg_match( '|^https?://|', $id ); |
|
372 | + function($id) { |
|
373 | + return preg_match('|^https?://|', $id); |
|
374 | 374 | } |
375 | 375 | ); |
376 | 376 | } |
@@ -380,9 +380,9 @@ discard block |
||
380 | 380 | * |
381 | 381 | * @return array |
382 | 382 | */ |
383 | - private function get_entity_uris( $entity ) { |
|
384 | - $ids = $this->filter_valid_entity_ids( $entity ); |
|
385 | - $same_as = array_key_exists( 'sameAs', $entity ) ? $entity['sameAs'] : array(); |
|
383 | + private function get_entity_uris($entity) { |
|
384 | + $ids = $this->filter_valid_entity_ids($entity); |
|
385 | + $same_as = array_key_exists('sameAs', $entity) ? $entity['sameAs'] : array(); |
|
386 | 386 | |
387 | 387 | return array_merge( |
388 | 388 | (array) $ids, |
@@ -390,8 +390,8 @@ discard block |
||
390 | 390 | ); |
391 | 391 | } |
392 | 392 | |
393 | - private function entity_id_valid( $entity_id ) { |
|
394 | - return preg_match( '#^https?://#i', $entity_id ) === 1; |
|
393 | + private function entity_id_valid($entity_id) { |
|
394 | + return preg_match('#^https?://#i', $entity_id) === 1; |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | } |
@@ -17,183 +17,183 @@ |
||
17 | 17 | // @@todo: add a hook to clear the cached files now and then. |
18 | 18 | class Ttl_Cache { |
19 | 19 | |
20 | - /** |
|
21 | - * The cache name. |
|
22 | - * |
|
23 | - * @var string $name The cache name. |
|
24 | - * @access private |
|
25 | - * @since 3.21.2 |
|
26 | - */ |
|
27 | - private $name; |
|
28 | - |
|
29 | - /** |
|
30 | - * The TTL of cached responses in seconds. |
|
31 | - * |
|
32 | - * @var int $ttl The TTL in seconds. |
|
33 | - * @access private |
|
34 | - * @since 3.21.2 |
|
35 | - */ |
|
36 | - private $ttl; |
|
37 | - |
|
38 | - /** |
|
39 | - * The cache dir where the cached data is written. |
|
40 | - * |
|
41 | - * @since 3.21.2 |
|
42 | - * @access private |
|
43 | - * @var string $cache_dir The cache dir where the cached responses are written. |
|
44 | - */ |
|
45 | - private $cache_dir; |
|
46 | - |
|
47 | - /** |
|
48 | - * A {@link Wordlift_Log_Service} instance. |
|
49 | - * |
|
50 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
51 | - * @access private |
|
52 | - * @since 3.21.2 |
|
53 | - */ |
|
54 | - private $log; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - private static $caches = array(); |
|
60 | - |
|
61 | - /** |
|
62 | - * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs. |
|
63 | - * |
|
64 | - * @param string $name The cache name. |
|
65 | - * @param int $ttl The cache TTL, default 900 secs. |
|
66 | - * |
|
67 | - * @since 3.21.2 |
|
68 | - */ |
|
69 | - public function __construct( $name, $ttl = 900 ) { |
|
70 | - |
|
71 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
72 | - |
|
73 | - $this->name = $name; |
|
74 | - $this->ttl = $ttl; |
|
75 | - |
|
76 | - $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name ); |
|
77 | - |
|
78 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
79 | - wp_mkdir_p( $this->cache_dir ); |
|
80 | - |
|
81 | - self::$caches[ $name ] = $this; |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Get the root cache folder. |
|
87 | - * |
|
88 | - * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files. |
|
89 | - * |
|
90 | - * @return string The root cache folder. |
|
91 | - * @since 3.22.5 |
|
92 | - */ |
|
93 | - public static function get_cache_folder() { |
|
94 | - |
|
95 | - // Get the temp dir and add the directory separator if missing. |
|
96 | - $temp_dir = get_temp_dir(); |
|
97 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
98 | - $temp_dir .= DIRECTORY_SEPARATOR; |
|
99 | - } |
|
100 | - |
|
101 | - return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() ); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Get the cached data for the specified key. |
|
106 | - * |
|
107 | - * @param mixed $key A serializable key. |
|
108 | - * |
|
109 | - * @return mixed|null |
|
110 | - * @since 3.21.2 |
|
111 | - */ |
|
112 | - public function get( $key ) { |
|
113 | - |
|
114 | - $filename = $this->get_filename( $key ); |
|
115 | - |
|
116 | - // If the cache file exists and it's not too old, then return it. |
|
117 | - if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
118 | - $this->log->trace( "Cache HIT.\n" ); |
|
119 | - |
|
120 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
121 | - return json_decode( file_get_contents( $filename ), true ); |
|
122 | - } |
|
123 | - |
|
124 | - $this->log->trace( "Cache MISS, filename $filename." ); |
|
125 | - |
|
126 | - return null; |
|
127 | - } |
|
128 | - |
|
129 | - public function put( $key, $data ) { |
|
130 | - |
|
131 | - $filename = $this->get_filename( $key ); |
|
132 | - |
|
133 | - // Cache. |
|
134 | - if ( file_exists( $filename ) ) { |
|
135 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
136 | - @unlink( $filename ); |
|
137 | - } |
|
138 | - |
|
139 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
140 | - @file_put_contents( $filename, wp_json_encode( $data ) ); |
|
141 | - |
|
142 | - } |
|
20 | + /** |
|
21 | + * The cache name. |
|
22 | + * |
|
23 | + * @var string $name The cache name. |
|
24 | + * @access private |
|
25 | + * @since 3.21.2 |
|
26 | + */ |
|
27 | + private $name; |
|
28 | + |
|
29 | + /** |
|
30 | + * The TTL of cached responses in seconds. |
|
31 | + * |
|
32 | + * @var int $ttl The TTL in seconds. |
|
33 | + * @access private |
|
34 | + * @since 3.21.2 |
|
35 | + */ |
|
36 | + private $ttl; |
|
37 | + |
|
38 | + /** |
|
39 | + * The cache dir where the cached data is written. |
|
40 | + * |
|
41 | + * @since 3.21.2 |
|
42 | + * @access private |
|
43 | + * @var string $cache_dir The cache dir where the cached responses are written. |
|
44 | + */ |
|
45 | + private $cache_dir; |
|
46 | + |
|
47 | + /** |
|
48 | + * A {@link Wordlift_Log_Service} instance. |
|
49 | + * |
|
50 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
51 | + * @access private |
|
52 | + * @since 3.21.2 |
|
53 | + */ |
|
54 | + private $log; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + private static $caches = array(); |
|
60 | + |
|
61 | + /** |
|
62 | + * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs. |
|
63 | + * |
|
64 | + * @param string $name The cache name. |
|
65 | + * @param int $ttl The cache TTL, default 900 secs. |
|
66 | + * |
|
67 | + * @since 3.21.2 |
|
68 | + */ |
|
69 | + public function __construct( $name, $ttl = 900 ) { |
|
70 | + |
|
71 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
72 | + |
|
73 | + $this->name = $name; |
|
74 | + $this->ttl = $ttl; |
|
75 | + |
|
76 | + $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name ); |
|
77 | + |
|
78 | + $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
79 | + wp_mkdir_p( $this->cache_dir ); |
|
80 | + |
|
81 | + self::$caches[ $name ] = $this; |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Get the root cache folder. |
|
87 | + * |
|
88 | + * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files. |
|
89 | + * |
|
90 | + * @return string The root cache folder. |
|
91 | + * @since 3.22.5 |
|
92 | + */ |
|
93 | + public static function get_cache_folder() { |
|
94 | + |
|
95 | + // Get the temp dir and add the directory separator if missing. |
|
96 | + $temp_dir = get_temp_dir(); |
|
97 | + if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
98 | + $temp_dir .= DIRECTORY_SEPARATOR; |
|
99 | + } |
|
100 | + |
|
101 | + return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() ); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Get the cached data for the specified key. |
|
106 | + * |
|
107 | + * @param mixed $key A serializable key. |
|
108 | + * |
|
109 | + * @return mixed|null |
|
110 | + * @since 3.21.2 |
|
111 | + */ |
|
112 | + public function get( $key ) { |
|
113 | + |
|
114 | + $filename = $this->get_filename( $key ); |
|
115 | + |
|
116 | + // If the cache file exists and it's not too old, then return it. |
|
117 | + if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
118 | + $this->log->trace( "Cache HIT.\n" ); |
|
119 | + |
|
120 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
|
121 | + return json_decode( file_get_contents( $filename ), true ); |
|
122 | + } |
|
123 | + |
|
124 | + $this->log->trace( "Cache MISS, filename $filename." ); |
|
125 | + |
|
126 | + return null; |
|
127 | + } |
|
128 | + |
|
129 | + public function put( $key, $data ) { |
|
130 | + |
|
131 | + $filename = $this->get_filename( $key ); |
|
132 | + |
|
133 | + // Cache. |
|
134 | + if ( file_exists( $filename ) ) { |
|
135 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
136 | + @unlink( $filename ); |
|
137 | + } |
|
138 | + |
|
139 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
|
140 | + @file_put_contents( $filename, wp_json_encode( $data ) ); |
|
141 | + |
|
142 | + } |
|
143 | 143 | |
144 | - public function delete( $key ) { |
|
144 | + public function delete( $key ) { |
|
145 | 145 | |
146 | - $filename = $this->get_filename( $key ); |
|
146 | + $filename = $this->get_filename( $key ); |
|
147 | 147 | |
148 | - // Delete. |
|
149 | - if ( file_exists( $filename ) ) { |
|
150 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
151 | - @unlink( $filename ); |
|
152 | - } |
|
148 | + // Delete. |
|
149 | + if ( file_exists( $filename ) ) { |
|
150 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
151 | + @unlink( $filename ); |
|
152 | + } |
|
153 | 153 | |
154 | - } |
|
154 | + } |
|
155 | 155 | |
156 | - public function flush() { |
|
156 | + public function flush() { |
|
157 | 157 | |
158 | - $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' ); |
|
159 | - foreach ( $files as $file ) { // iterate files |
|
160 | - if ( is_file( $file ) ) { |
|
161 | - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
162 | - @unlink( $file ); |
|
163 | - } |
|
164 | - } |
|
158 | + $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' ); |
|
159 | + foreach ( $files as $file ) { // iterate files |
|
160 | + if ( is_file( $file ) ) { |
|
161 | + // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
|
162 | + @unlink( $file ); |
|
163 | + } |
|
164 | + } |
|
165 | 165 | |
166 | - } |
|
166 | + } |
|
167 | 167 | |
168 | - public static function flush_all() { |
|
168 | + public static function flush_all() { |
|
169 | 169 | |
170 | - /** @var Ttl_Cache $cache */ |
|
171 | - foreach ( self::$caches as $cache ) { |
|
172 | - $cache->flush(); |
|
173 | - } |
|
170 | + /** @var Ttl_Cache $cache */ |
|
171 | + foreach ( self::$caches as $cache ) { |
|
172 | + $cache->flush(); |
|
173 | + } |
|
174 | 174 | |
175 | - } |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
179 | - * |
|
180 | - * @param string $hash A file hash. |
|
181 | - * |
|
182 | - * @return string The full path to the file. |
|
183 | - * @since 3.21.2 |
|
184 | - */ |
|
185 | - private function get_path( $hash ) { |
|
177 | + /** |
|
178 | + * Get the full path for the given `$hash`. The file is not checked for its existence. |
|
179 | + * |
|
180 | + * @param string $hash A file hash. |
|
181 | + * |
|
182 | + * @return string The full path to the file. |
|
183 | + * @since 3.21.2 |
|
184 | + */ |
|
185 | + private function get_path( $hash ) { |
|
186 | 186 | |
187 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
188 | - } |
|
187 | + return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
188 | + } |
|
189 | 189 | |
190 | - private function get_filename( $key ) { |
|
190 | + private function get_filename( $key ) { |
|
191 | 191 | |
192 | - // Create a hash and a path to the cache file. |
|
193 | - $hash = md5( wp_json_encode( $key ) ); |
|
194 | - $filename = $this->get_path( $hash ); |
|
192 | + // Create a hash and a path to the cache file. |
|
193 | + $hash = md5( wp_json_encode( $key ) ); |
|
194 | + $filename = $this->get_path( $hash ); |
|
195 | 195 | |
196 | - return $filename; |
|
197 | - } |
|
196 | + return $filename; |
|
197 | + } |
|
198 | 198 | |
199 | 199 | } |
@@ -66,19 +66,19 @@ discard block |
||
66 | 66 | * |
67 | 67 | * @since 3.21.2 |
68 | 68 | */ |
69 | - public function __construct( $name, $ttl = 900 ) { |
|
69 | + public function __construct($name, $ttl = 900) { |
|
70 | 70 | |
71 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
71 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
72 | 72 | |
73 | 73 | $this->name = $name; |
74 | 74 | $this->ttl = $ttl; |
75 | 75 | |
76 | - $this->cache_dir = self::get_cache_folder() . DIRECTORY_SEPARATOR . md5( $name ); |
|
76 | + $this->cache_dir = self::get_cache_folder().DIRECTORY_SEPARATOR.md5($name); |
|
77 | 77 | |
78 | - $this->log->trace( "Creating the cache folder {$this->cache_dir}..." ); |
|
79 | - wp_mkdir_p( $this->cache_dir ); |
|
78 | + $this->log->trace("Creating the cache folder {$this->cache_dir}..."); |
|
79 | + wp_mkdir_p($this->cache_dir); |
|
80 | 80 | |
81 | - self::$caches[ $name ] = $this; |
|
81 | + self::$caches[$name] = $this; |
|
82 | 82 | |
83 | 83 | } |
84 | 84 | |
@@ -94,11 +94,11 @@ discard block |
||
94 | 94 | |
95 | 95 | // Get the temp dir and add the directory separator if missing. |
96 | 96 | $temp_dir = get_temp_dir(); |
97 | - if ( DIRECTORY_SEPARATOR !== substr( $temp_dir, - strlen( DIRECTORY_SEPARATOR ) ) ) { |
|
97 | + if (DIRECTORY_SEPARATOR !== substr($temp_dir, - strlen(DIRECTORY_SEPARATOR))) { |
|
98 | 98 | $temp_dir .= DIRECTORY_SEPARATOR; |
99 | 99 | } |
100 | 100 | |
101 | - return $temp_dir . 'wl.cache' . DIRECTORY_SEPARATOR . md5( home_url() ); |
|
101 | + return $temp_dir.'wl.cache'.DIRECTORY_SEPARATOR.md5(home_url()); |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | /** |
@@ -109,57 +109,57 @@ discard block |
||
109 | 109 | * @return mixed|null |
110 | 110 | * @since 3.21.2 |
111 | 111 | */ |
112 | - public function get( $key ) { |
|
112 | + public function get($key) { |
|
113 | 113 | |
114 | - $filename = $this->get_filename( $key ); |
|
114 | + $filename = $this->get_filename($key); |
|
115 | 115 | |
116 | 116 | // If the cache file exists and it's not too old, then return it. |
117 | - if ( file_exists( $filename ) && $this->ttl >= time() - filemtime( $filename ) ) { |
|
118 | - $this->log->trace( "Cache HIT.\n" ); |
|
117 | + if (file_exists($filename) && $this->ttl >= time() - filemtime($filename)) { |
|
118 | + $this->log->trace("Cache HIT.\n"); |
|
119 | 119 | |
120 | 120 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
121 | - return json_decode( file_get_contents( $filename ), true ); |
|
121 | + return json_decode(file_get_contents($filename), true); |
|
122 | 122 | } |
123 | 123 | |
124 | - $this->log->trace( "Cache MISS, filename $filename." ); |
|
124 | + $this->log->trace("Cache MISS, filename $filename."); |
|
125 | 125 | |
126 | 126 | return null; |
127 | 127 | } |
128 | 128 | |
129 | - public function put( $key, $data ) { |
|
129 | + public function put($key, $data) { |
|
130 | 130 | |
131 | - $filename = $this->get_filename( $key ); |
|
131 | + $filename = $this->get_filename($key); |
|
132 | 132 | |
133 | 133 | // Cache. |
134 | - if ( file_exists( $filename ) ) { |
|
134 | + if (file_exists($filename)) { |
|
135 | 135 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
136 | - @unlink( $filename ); |
|
136 | + @unlink($filename); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
140 | - @file_put_contents( $filename, wp_json_encode( $data ) ); |
|
140 | + @file_put_contents($filename, wp_json_encode($data)); |
|
141 | 141 | |
142 | 142 | } |
143 | 143 | |
144 | - public function delete( $key ) { |
|
144 | + public function delete($key) { |
|
145 | 145 | |
146 | - $filename = $this->get_filename( $key ); |
|
146 | + $filename = $this->get_filename($key); |
|
147 | 147 | |
148 | 148 | // Delete. |
149 | - if ( file_exists( $filename ) ) { |
|
149 | + if (file_exists($filename)) { |
|
150 | 150 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
151 | - @unlink( $filename ); |
|
151 | + @unlink($filename); |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | } |
155 | 155 | |
156 | 156 | public function flush() { |
157 | 157 | |
158 | - $files = glob( $this->cache_dir . DIRECTORY_SEPARATOR . '*' ); |
|
159 | - foreach ( $files as $file ) { // iterate files |
|
160 | - if ( is_file( $file ) ) { |
|
158 | + $files = glob($this->cache_dir.DIRECTORY_SEPARATOR.'*'); |
|
159 | + foreach ($files as $file) { // iterate files |
|
160 | + if (is_file($file)) { |
|
161 | 161 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
162 | - @unlink( $file ); |
|
162 | + @unlink($file); |
|
163 | 163 | } |
164 | 164 | } |
165 | 165 | |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | public static function flush_all() { |
169 | 169 | |
170 | 170 | /** @var Ttl_Cache $cache */ |
171 | - foreach ( self::$caches as $cache ) { |
|
171 | + foreach (self::$caches as $cache) { |
|
172 | 172 | $cache->flush(); |
173 | 173 | } |
174 | 174 | |
@@ -182,16 +182,16 @@ discard block |
||
182 | 182 | * @return string The full path to the file. |
183 | 183 | * @since 3.21.2 |
184 | 184 | */ |
185 | - private function get_path( $hash ) { |
|
185 | + private function get_path($hash) { |
|
186 | 186 | |
187 | - return $this->cache_dir . DIRECTORY_SEPARATOR . $hash; |
|
187 | + return $this->cache_dir.DIRECTORY_SEPARATOR.$hash; |
|
188 | 188 | } |
189 | 189 | |
190 | - private function get_filename( $key ) { |
|
190 | + private function get_filename($key) { |
|
191 | 191 | |
192 | 192 | // Create a hash and a path to the cache file. |
193 | - $hash = md5( wp_json_encode( $key ) ); |
|
194 | - $filename = $this->get_path( $hash ); |
|
193 | + $hash = md5(wp_json_encode($key)); |
|
194 | + $filename = $this->get_path($hash); |
|
195 | 195 | |
196 | 196 | return $filename; |
197 | 197 | } |
@@ -15,42 +15,42 @@ |
||
15 | 15 | */ |
16 | 16 | interface Task { |
17 | 17 | |
18 | - /** |
|
19 | - * Define the task ID. |
|
20 | - * |
|
21 | - * @return string The task id. |
|
22 | - * @since 1.0.0 |
|
23 | - */ |
|
24 | - public function get_id(); |
|
25 | - |
|
26 | - public function get_label(); |
|
27 | - |
|
28 | - /** |
|
29 | - * List the items to process. |
|
30 | - * |
|
31 | - * @param int $limit |
|
32 | - * @param int $offset |
|
33 | - * |
|
34 | - * @return array An array of items. |
|
35 | - * @since 1.0.0 |
|
36 | - */ |
|
37 | - public function list_items( $limit = 10, $offset = 0 ); |
|
38 | - |
|
39 | - /** |
|
40 | - * Count the total number of items to process. |
|
41 | - * |
|
42 | - * @return int Total number of items to process. |
|
43 | - * @since 1.0.0 |
|
44 | - */ |
|
45 | - public function count_items(); |
|
46 | - |
|
47 | - /** |
|
48 | - * Process the provided item. |
|
49 | - * |
|
50 | - * @param mixed $item Process the provided item. |
|
51 | - * |
|
52 | - * @since 1.0.0 |
|
53 | - */ |
|
54 | - public function process_item( $item ); |
|
18 | + /** |
|
19 | + * Define the task ID. |
|
20 | + * |
|
21 | + * @return string The task id. |
|
22 | + * @since 1.0.0 |
|
23 | + */ |
|
24 | + public function get_id(); |
|
25 | + |
|
26 | + public function get_label(); |
|
27 | + |
|
28 | + /** |
|
29 | + * List the items to process. |
|
30 | + * |
|
31 | + * @param int $limit |
|
32 | + * @param int $offset |
|
33 | + * |
|
34 | + * @return array An array of items. |
|
35 | + * @since 1.0.0 |
|
36 | + */ |
|
37 | + public function list_items( $limit = 10, $offset = 0 ); |
|
38 | + |
|
39 | + /** |
|
40 | + * Count the total number of items to process. |
|
41 | + * |
|
42 | + * @return int Total number of items to process. |
|
43 | + * @since 1.0.0 |
|
44 | + */ |
|
45 | + public function count_items(); |
|
46 | + |
|
47 | + /** |
|
48 | + * Process the provided item. |
|
49 | + * |
|
50 | + * @param mixed $item Process the provided item. |
|
51 | + * |
|
52 | + * @since 1.0.0 |
|
53 | + */ |
|
54 | + public function process_item( $item ); |
|
55 | 55 | |
56 | 56 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * @return array An array of items. |
35 | 35 | * @since 1.0.0 |
36 | 36 | */ |
37 | - public function list_items( $limit = 10, $offset = 0 ); |
|
37 | + public function list_items($limit = 10, $offset = 0); |
|
38 | 38 | |
39 | 39 | /** |
40 | 40 | * Count the total number of items to process. |
@@ -51,6 +51,6 @@ discard block |
||
51 | 51 | * |
52 | 52 | * @since 1.0.0 |
53 | 53 | */ |
54 | - public function process_item( $item ); |
|
54 | + public function process_item($item); |
|
55 | 55 | |
56 | 56 | } |
@@ -24,196 +24,196 @@ |
||
24 | 24 | */ |
25 | 25 | class Task_Single_Instance_Task_Runner { |
26 | 26 | |
27 | - /** |
|
28 | - * Define the transient prefix. |
|
29 | - * |
|
30 | - * @since 1.0.0 |
|
31 | - */ |
|
32 | - const IS_RUNNING_PREFIX = '_wf_task_runner__'; |
|
33 | - |
|
34 | - /** |
|
35 | - * A {@link Wordlift_Log_Service} instance. |
|
36 | - * |
|
37 | - * @since 1.0.0 |
|
38 | - * @var Wordlift_Log_Service A {@link Wordlift_Log_Service} instance. |
|
39 | - * @access private |
|
40 | - */ |
|
41 | - private $log; |
|
42 | - |
|
43 | - /** |
|
44 | - * The {@link Task} to execute. |
|
45 | - * |
|
46 | - * @since 1.0.0 |
|
47 | - * @var Task $task The {@link Task} to execute. |
|
48 | - * @access private |
|
49 | - */ |
|
50 | - private $task; |
|
51 | - |
|
52 | - /** |
|
53 | - * One or more callbacks to call to update about the task progress. |
|
54 | - * |
|
55 | - * @since 1.0.0 |
|
56 | - * @var Task_Progress[] $callbacks An array of {@link Wordlift_For_Bungalowparkoverzicht_Progress}. |
|
57 | - * @access private |
|
58 | - */ |
|
59 | - private $callbacks; |
|
60 | - |
|
61 | - /** |
|
62 | - * Whether to force starting a task even if another instance of the task is already running. |
|
63 | - * |
|
64 | - * @since 1.0.0 |
|
65 | - * @var bool $force Whether to force starting a task even if another instance of the task is already running. |
|
66 | - * @access private |
|
67 | - */ |
|
68 | - private $force; |
|
69 | - |
|
70 | - /** |
|
71 | - * Create a {@link Task_Single_Instance_Task_Runner} instance. |
|
72 | - * |
|
73 | - * @param Task $task The {@link Task} instance. |
|
74 | - * @param bool $force Whether to force starting a task even if another instance of the task is already running, default `false`. |
|
75 | - * @param array $callbacks An array of {@link Wordlift_For_Bungalowparkoverzicht_Progress}. |
|
76 | - * |
|
77 | - * @since 1.0.0 |
|
78 | - */ |
|
79 | - public function __construct( $task, $force = false, $callbacks = array() ) { |
|
80 | - |
|
81 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
82 | - |
|
83 | - $this->task = $task; |
|
84 | - $this->force = $force; |
|
85 | - $this->callbacks = $callbacks; |
|
86 | - |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Get the transient name for running flag. |
|
91 | - * |
|
92 | - * @return string The transient name. |
|
93 | - * @since 1.0.0 |
|
94 | - */ |
|
95 | - private function get_running_transient() { |
|
96 | - |
|
97 | - return self::IS_RUNNING_PREFIX . $this->task->get_id(); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Check whether a task is running. |
|
102 | - * |
|
103 | - * @return bool |
|
104 | - * @since 1.0.0 |
|
105 | - */ |
|
106 | - public function is_running() { |
|
107 | - return 'yes' === get_transient( $this->get_running_transient() ); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Set whether the task is running or not. |
|
112 | - * |
|
113 | - * @param bool $value Whether the task is running or not. |
|
114 | - * |
|
115 | - * @since 1.0.0 |
|
116 | - */ |
|
117 | - public function set_running( $value ) { |
|
118 | - set_transient( $this->get_running_transient(), $value ? 'yes' : 'no' ); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Start the task. |
|
123 | - * |
|
124 | - * @param int $limit The maximum number of items to process. |
|
125 | - * @param int $offset The starting offset. |
|
126 | - * |
|
127 | - * @throws Task_Another_Instance_Is_Running_Exception if the task is already running. |
|
128 | - * @since 1.0.0 |
|
129 | - */ |
|
130 | - public function start( $limit = 0, $offset = 0 ) { |
|
131 | - |
|
132 | - // Bail out if the task is already running. |
|
133 | - if ( ! $this->force && $this->is_running() ) { |
|
134 | - throw new Task_Another_Instance_Is_Running_Exception(); |
|
135 | - } |
|
136 | - |
|
137 | - // Set the task as running. |
|
138 | - $this->set_running( true ); |
|
139 | - |
|
140 | - // List the chunk of elements to process. |
|
141 | - $items = $this->task->list_items( $limit, $offset ); |
|
142 | - |
|
143 | - $count = count( $items ); |
|
144 | - for ( $i = 0; $i < $count; $i ++ ) { |
|
145 | - // Process the item. |
|
146 | - $this->task->process_item( $items[ $i ] ); |
|
147 | - |
|
148 | - // Update the progress. |
|
149 | - $this->set_progress( $offset + $i, $items[ $i ] ); |
|
150 | - } |
|
151 | - |
|
152 | - // Set the total number of elements to process. |
|
153 | - $this->set_count( $this->task->count_items() ); |
|
154 | - |
|
155 | - // Unset the running flag. |
|
156 | - $this->set_running( false ); |
|
157 | - |
|
158 | - // Set the task to complete. |
|
159 | - $this->finish(); |
|
160 | - |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Set the total number of items to process. |
|
165 | - * |
|
166 | - * @param int $value The total number of items to process. |
|
167 | - * |
|
168 | - * @since 1.0.0 |
|
169 | - */ |
|
170 | - private function set_count( $value ) { |
|
171 | - |
|
172 | - if ( empty( $this->callbacks ) ) { |
|
173 | - return; |
|
174 | - } |
|
175 | - |
|
176 | - foreach ( $this->callbacks as $callback ) { |
|
177 | - call_user_func( array( $callback, 'set_count' ), $value ); |
|
178 | - } |
|
179 | - |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Set the task progress. |
|
184 | - * |
|
185 | - * @param int $index The current item index. |
|
186 | - * @param mixed $item The current item. |
|
187 | - * |
|
188 | - * @since 1.0.0 |
|
189 | - */ |
|
190 | - private function set_progress( $index, $item ) { |
|
191 | - |
|
192 | - if ( empty( $this->callbacks ) ) { |
|
193 | - return; |
|
194 | - } |
|
195 | - |
|
196 | - foreach ( $this->callbacks as $callback ) { |
|
197 | - call_user_func( array( $callback, 'set_progress' ), $index, $item ); |
|
198 | - } |
|
199 | - |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Inform the callbacks that the task completed. |
|
204 | - * |
|
205 | - * @since 1.0.0 |
|
206 | - */ |
|
207 | - private function finish() { |
|
208 | - |
|
209 | - if ( empty( $this->callbacks ) ) { |
|
210 | - return; |
|
211 | - } |
|
212 | - |
|
213 | - foreach ( $this->callbacks as $callback ) { |
|
214 | - call_user_func( array( $callback, 'finish' ) ); |
|
215 | - } |
|
216 | - |
|
217 | - } |
|
27 | + /** |
|
28 | + * Define the transient prefix. |
|
29 | + * |
|
30 | + * @since 1.0.0 |
|
31 | + */ |
|
32 | + const IS_RUNNING_PREFIX = '_wf_task_runner__'; |
|
33 | + |
|
34 | + /** |
|
35 | + * A {@link Wordlift_Log_Service} instance. |
|
36 | + * |
|
37 | + * @since 1.0.0 |
|
38 | + * @var Wordlift_Log_Service A {@link Wordlift_Log_Service} instance. |
|
39 | + * @access private |
|
40 | + */ |
|
41 | + private $log; |
|
42 | + |
|
43 | + /** |
|
44 | + * The {@link Task} to execute. |
|
45 | + * |
|
46 | + * @since 1.0.0 |
|
47 | + * @var Task $task The {@link Task} to execute. |
|
48 | + * @access private |
|
49 | + */ |
|
50 | + private $task; |
|
51 | + |
|
52 | + /** |
|
53 | + * One or more callbacks to call to update about the task progress. |
|
54 | + * |
|
55 | + * @since 1.0.0 |
|
56 | + * @var Task_Progress[] $callbacks An array of {@link Wordlift_For_Bungalowparkoverzicht_Progress}. |
|
57 | + * @access private |
|
58 | + */ |
|
59 | + private $callbacks; |
|
60 | + |
|
61 | + /** |
|
62 | + * Whether to force starting a task even if another instance of the task is already running. |
|
63 | + * |
|
64 | + * @since 1.0.0 |
|
65 | + * @var bool $force Whether to force starting a task even if another instance of the task is already running. |
|
66 | + * @access private |
|
67 | + */ |
|
68 | + private $force; |
|
69 | + |
|
70 | + /** |
|
71 | + * Create a {@link Task_Single_Instance_Task_Runner} instance. |
|
72 | + * |
|
73 | + * @param Task $task The {@link Task} instance. |
|
74 | + * @param bool $force Whether to force starting a task even if another instance of the task is already running, default `false`. |
|
75 | + * @param array $callbacks An array of {@link Wordlift_For_Bungalowparkoverzicht_Progress}. |
|
76 | + * |
|
77 | + * @since 1.0.0 |
|
78 | + */ |
|
79 | + public function __construct( $task, $force = false, $callbacks = array() ) { |
|
80 | + |
|
81 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
82 | + |
|
83 | + $this->task = $task; |
|
84 | + $this->force = $force; |
|
85 | + $this->callbacks = $callbacks; |
|
86 | + |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Get the transient name for running flag. |
|
91 | + * |
|
92 | + * @return string The transient name. |
|
93 | + * @since 1.0.0 |
|
94 | + */ |
|
95 | + private function get_running_transient() { |
|
96 | + |
|
97 | + return self::IS_RUNNING_PREFIX . $this->task->get_id(); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Check whether a task is running. |
|
102 | + * |
|
103 | + * @return bool |
|
104 | + * @since 1.0.0 |
|
105 | + */ |
|
106 | + public function is_running() { |
|
107 | + return 'yes' === get_transient( $this->get_running_transient() ); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Set whether the task is running or not. |
|
112 | + * |
|
113 | + * @param bool $value Whether the task is running or not. |
|
114 | + * |
|
115 | + * @since 1.0.0 |
|
116 | + */ |
|
117 | + public function set_running( $value ) { |
|
118 | + set_transient( $this->get_running_transient(), $value ? 'yes' : 'no' ); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Start the task. |
|
123 | + * |
|
124 | + * @param int $limit The maximum number of items to process. |
|
125 | + * @param int $offset The starting offset. |
|
126 | + * |
|
127 | + * @throws Task_Another_Instance_Is_Running_Exception if the task is already running. |
|
128 | + * @since 1.0.0 |
|
129 | + */ |
|
130 | + public function start( $limit = 0, $offset = 0 ) { |
|
131 | + |
|
132 | + // Bail out if the task is already running. |
|
133 | + if ( ! $this->force && $this->is_running() ) { |
|
134 | + throw new Task_Another_Instance_Is_Running_Exception(); |
|
135 | + } |
|
136 | + |
|
137 | + // Set the task as running. |
|
138 | + $this->set_running( true ); |
|
139 | + |
|
140 | + // List the chunk of elements to process. |
|
141 | + $items = $this->task->list_items( $limit, $offset ); |
|
142 | + |
|
143 | + $count = count( $items ); |
|
144 | + for ( $i = 0; $i < $count; $i ++ ) { |
|
145 | + // Process the item. |
|
146 | + $this->task->process_item( $items[ $i ] ); |
|
147 | + |
|
148 | + // Update the progress. |
|
149 | + $this->set_progress( $offset + $i, $items[ $i ] ); |
|
150 | + } |
|
151 | + |
|
152 | + // Set the total number of elements to process. |
|
153 | + $this->set_count( $this->task->count_items() ); |
|
154 | + |
|
155 | + // Unset the running flag. |
|
156 | + $this->set_running( false ); |
|
157 | + |
|
158 | + // Set the task to complete. |
|
159 | + $this->finish(); |
|
160 | + |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Set the total number of items to process. |
|
165 | + * |
|
166 | + * @param int $value The total number of items to process. |
|
167 | + * |
|
168 | + * @since 1.0.0 |
|
169 | + */ |
|
170 | + private function set_count( $value ) { |
|
171 | + |
|
172 | + if ( empty( $this->callbacks ) ) { |
|
173 | + return; |
|
174 | + } |
|
175 | + |
|
176 | + foreach ( $this->callbacks as $callback ) { |
|
177 | + call_user_func( array( $callback, 'set_count' ), $value ); |
|
178 | + } |
|
179 | + |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Set the task progress. |
|
184 | + * |
|
185 | + * @param int $index The current item index. |
|
186 | + * @param mixed $item The current item. |
|
187 | + * |
|
188 | + * @since 1.0.0 |
|
189 | + */ |
|
190 | + private function set_progress( $index, $item ) { |
|
191 | + |
|
192 | + if ( empty( $this->callbacks ) ) { |
|
193 | + return; |
|
194 | + } |
|
195 | + |
|
196 | + foreach ( $this->callbacks as $callback ) { |
|
197 | + call_user_func( array( $callback, 'set_progress' ), $index, $item ); |
|
198 | + } |
|
199 | + |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Inform the callbacks that the task completed. |
|
204 | + * |
|
205 | + * @since 1.0.0 |
|
206 | + */ |
|
207 | + private function finish() { |
|
208 | + |
|
209 | + if ( empty( $this->callbacks ) ) { |
|
210 | + return; |
|
211 | + } |
|
212 | + |
|
213 | + foreach ( $this->callbacks as $callback ) { |
|
214 | + call_user_func( array( $callback, 'finish' ) ); |
|
215 | + } |
|
216 | + |
|
217 | + } |
|
218 | 218 | |
219 | 219 | } |
@@ -76,9 +76,9 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @since 1.0.0 |
78 | 78 | */ |
79 | - public function __construct( $task, $force = false, $callbacks = array() ) { |
|
79 | + public function __construct($task, $force = false, $callbacks = array()) { |
|
80 | 80 | |
81 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
81 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
82 | 82 | |
83 | 83 | $this->task = $task; |
84 | 84 | $this->force = $force; |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | */ |
95 | 95 | private function get_running_transient() { |
96 | 96 | |
97 | - return self::IS_RUNNING_PREFIX . $this->task->get_id(); |
|
97 | + return self::IS_RUNNING_PREFIX.$this->task->get_id(); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | * @since 1.0.0 |
105 | 105 | */ |
106 | 106 | public function is_running() { |
107 | - return 'yes' === get_transient( $this->get_running_transient() ); |
|
107 | + return 'yes' === get_transient($this->get_running_transient()); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | /** |
@@ -114,8 +114,8 @@ discard block |
||
114 | 114 | * |
115 | 115 | * @since 1.0.0 |
116 | 116 | */ |
117 | - public function set_running( $value ) { |
|
118 | - set_transient( $this->get_running_transient(), $value ? 'yes' : 'no' ); |
|
117 | + public function set_running($value) { |
|
118 | + set_transient($this->get_running_transient(), $value ? 'yes' : 'no'); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | /** |
@@ -127,33 +127,33 @@ discard block |
||
127 | 127 | * @throws Task_Another_Instance_Is_Running_Exception if the task is already running. |
128 | 128 | * @since 1.0.0 |
129 | 129 | */ |
130 | - public function start( $limit = 0, $offset = 0 ) { |
|
130 | + public function start($limit = 0, $offset = 0) { |
|
131 | 131 | |
132 | 132 | // Bail out if the task is already running. |
133 | - if ( ! $this->force && $this->is_running() ) { |
|
133 | + if ( ! $this->force && $this->is_running()) { |
|
134 | 134 | throw new Task_Another_Instance_Is_Running_Exception(); |
135 | 135 | } |
136 | 136 | |
137 | 137 | // Set the task as running. |
138 | - $this->set_running( true ); |
|
138 | + $this->set_running(true); |
|
139 | 139 | |
140 | 140 | // List the chunk of elements to process. |
141 | - $items = $this->task->list_items( $limit, $offset ); |
|
141 | + $items = $this->task->list_items($limit, $offset); |
|
142 | 142 | |
143 | - $count = count( $items ); |
|
144 | - for ( $i = 0; $i < $count; $i ++ ) { |
|
143 | + $count = count($items); |
|
144 | + for ($i = 0; $i < $count; $i++) { |
|
145 | 145 | // Process the item. |
146 | - $this->task->process_item( $items[ $i ] ); |
|
146 | + $this->task->process_item($items[$i]); |
|
147 | 147 | |
148 | 148 | // Update the progress. |
149 | - $this->set_progress( $offset + $i, $items[ $i ] ); |
|
149 | + $this->set_progress($offset + $i, $items[$i]); |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | // Set the total number of elements to process. |
153 | - $this->set_count( $this->task->count_items() ); |
|
153 | + $this->set_count($this->task->count_items()); |
|
154 | 154 | |
155 | 155 | // Unset the running flag. |
156 | - $this->set_running( false ); |
|
156 | + $this->set_running(false); |
|
157 | 157 | |
158 | 158 | // Set the task to complete. |
159 | 159 | $this->finish(); |
@@ -167,14 +167,14 @@ discard block |
||
167 | 167 | * |
168 | 168 | * @since 1.0.0 |
169 | 169 | */ |
170 | - private function set_count( $value ) { |
|
170 | + private function set_count($value) { |
|
171 | 171 | |
172 | - if ( empty( $this->callbacks ) ) { |
|
172 | + if (empty($this->callbacks)) { |
|
173 | 173 | return; |
174 | 174 | } |
175 | 175 | |
176 | - foreach ( $this->callbacks as $callback ) { |
|
177 | - call_user_func( array( $callback, 'set_count' ), $value ); |
|
176 | + foreach ($this->callbacks as $callback) { |
|
177 | + call_user_func(array($callback, 'set_count'), $value); |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | } |
@@ -187,14 +187,14 @@ discard block |
||
187 | 187 | * |
188 | 188 | * @since 1.0.0 |
189 | 189 | */ |
190 | - private function set_progress( $index, $item ) { |
|
190 | + private function set_progress($index, $item) { |
|
191 | 191 | |
192 | - if ( empty( $this->callbacks ) ) { |
|
192 | + if (empty($this->callbacks)) { |
|
193 | 193 | return; |
194 | 194 | } |
195 | 195 | |
196 | - foreach ( $this->callbacks as $callback ) { |
|
197 | - call_user_func( array( $callback, 'set_progress' ), $index, $item ); |
|
196 | + foreach ($this->callbacks as $callback) { |
|
197 | + call_user_func(array($callback, 'set_progress'), $index, $item); |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | } |
@@ -206,12 +206,12 @@ discard block |
||
206 | 206 | */ |
207 | 207 | private function finish() { |
208 | 208 | |
209 | - if ( empty( $this->callbacks ) ) { |
|
209 | + if (empty($this->callbacks)) { |
|
210 | 210 | return; |
211 | 211 | } |
212 | 212 | |
213 | - foreach ( $this->callbacks as $callback ) { |
|
214 | - call_user_func( array( $callback, 'finish' ) ); |
|
213 | + foreach ($this->callbacks as $callback) { |
|
214 | + call_user_func(array($callback, 'finish')); |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | } |