@@ -4,47 +4,47 @@ |
||
4 | 4 | |
5 | 5 | class Remote_Entity_Factory { |
6 | 6 | |
7 | - /** |
|
8 | - * |
|
9 | - * @param $entity_url string |
|
10 | - * @param $response \Wordlift\Api\Response |
|
11 | - * |
|
12 | - * @return Remote_Entity |
|
13 | - */ |
|
14 | - public static function from_response( $entity_url, $response ) { |
|
15 | - |
|
16 | - if ( ! $response->is_success() ) { |
|
17 | - return new Invalid_Remote_Entity(); |
|
18 | - } |
|
19 | - |
|
20 | - $entity_data = json_decode( $response->get_body(), true ); |
|
21 | - |
|
22 | - if ( ! $entity_data ) { |
|
23 | - return new Invalid_Remote_Entity(); |
|
24 | - } |
|
25 | - |
|
26 | - if ( ! array_key_exists( '@type', $entity_data ) |
|
27 | - || ! array_key_exists( 'name', $entity_data ) |
|
28 | - || ! array_key_exists( 'description', $entity_data ) ) { |
|
29 | - |
|
30 | - return new Invalid_Remote_Entity(); |
|
31 | - } |
|
32 | - |
|
33 | - return new Valid_Remote_Entity( |
|
34 | - self::may_be_wrap_array( $entity_data['@type'] ), |
|
35 | - $entity_data['name'], |
|
36 | - $entity_data['description'], |
|
37 | - array_merge( array( $entity_url ), self::may_be_wrap_array( $entity_data['sameAs'] ) ) |
|
38 | - ); |
|
39 | - |
|
40 | - } |
|
41 | - |
|
42 | - private static function may_be_wrap_array( $el ) { |
|
43 | - if ( is_array( $el ) ) { |
|
44 | - return $el; |
|
45 | - } |
|
46 | - |
|
47 | - return array( $el ); |
|
48 | - } |
|
7 | + /** |
|
8 | + * |
|
9 | + * @param $entity_url string |
|
10 | + * @param $response \Wordlift\Api\Response |
|
11 | + * |
|
12 | + * @return Remote_Entity |
|
13 | + */ |
|
14 | + public static function from_response( $entity_url, $response ) { |
|
15 | + |
|
16 | + if ( ! $response->is_success() ) { |
|
17 | + return new Invalid_Remote_Entity(); |
|
18 | + } |
|
19 | + |
|
20 | + $entity_data = json_decode( $response->get_body(), true ); |
|
21 | + |
|
22 | + if ( ! $entity_data ) { |
|
23 | + return new Invalid_Remote_Entity(); |
|
24 | + } |
|
25 | + |
|
26 | + if ( ! array_key_exists( '@type', $entity_data ) |
|
27 | + || ! array_key_exists( 'name', $entity_data ) |
|
28 | + || ! array_key_exists( 'description', $entity_data ) ) { |
|
29 | + |
|
30 | + return new Invalid_Remote_Entity(); |
|
31 | + } |
|
32 | + |
|
33 | + return new Valid_Remote_Entity( |
|
34 | + self::may_be_wrap_array( $entity_data['@type'] ), |
|
35 | + $entity_data['name'], |
|
36 | + $entity_data['description'], |
|
37 | + array_merge( array( $entity_url ), self::may_be_wrap_array( $entity_data['sameAs'] ) ) |
|
38 | + ); |
|
39 | + |
|
40 | + } |
|
41 | + |
|
42 | + private static function may_be_wrap_array( $el ) { |
|
43 | + if ( is_array( $el ) ) { |
|
44 | + return $el; |
|
45 | + } |
|
46 | + |
|
47 | + return array( $el ); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | } |
@@ -11,40 +11,40 @@ |
||
11 | 11 | * |
12 | 12 | * @return Remote_Entity |
13 | 13 | */ |
14 | - public static function from_response( $entity_url, $response ) { |
|
14 | + public static function from_response($entity_url, $response) { |
|
15 | 15 | |
16 | - if ( ! $response->is_success() ) { |
|
16 | + if ( ! $response->is_success()) { |
|
17 | 17 | return new Invalid_Remote_Entity(); |
18 | 18 | } |
19 | 19 | |
20 | - $entity_data = json_decode( $response->get_body(), true ); |
|
20 | + $entity_data = json_decode($response->get_body(), true); |
|
21 | 21 | |
22 | - if ( ! $entity_data ) { |
|
22 | + if ( ! $entity_data) { |
|
23 | 23 | return new Invalid_Remote_Entity(); |
24 | 24 | } |
25 | 25 | |
26 | - if ( ! array_key_exists( '@type', $entity_data ) |
|
27 | - || ! array_key_exists( 'name', $entity_data ) |
|
28 | - || ! array_key_exists( 'description', $entity_data ) ) { |
|
26 | + if ( ! array_key_exists('@type', $entity_data) |
|
27 | + || ! array_key_exists('name', $entity_data) |
|
28 | + || ! array_key_exists('description', $entity_data)) { |
|
29 | 29 | |
30 | 30 | return new Invalid_Remote_Entity(); |
31 | 31 | } |
32 | 32 | |
33 | 33 | return new Valid_Remote_Entity( |
34 | - self::may_be_wrap_array( $entity_data['@type'] ), |
|
34 | + self::may_be_wrap_array($entity_data['@type']), |
|
35 | 35 | $entity_data['name'], |
36 | 36 | $entity_data['description'], |
37 | - array_merge( array( $entity_url ), self::may_be_wrap_array( $entity_data['sameAs'] ) ) |
|
37 | + array_merge(array($entity_url), self::may_be_wrap_array($entity_data['sameAs'])) |
|
38 | 38 | ); |
39 | 39 | |
40 | 40 | } |
41 | 41 | |
42 | - private static function may_be_wrap_array( $el ) { |
|
43 | - if ( is_array( $el ) ) { |
|
42 | + private static function may_be_wrap_array($el) { |
|
43 | + if (is_array($el)) { |
|
44 | 44 | return $el; |
45 | 45 | } |
46 | 46 | |
47 | - return array( $el ); |
|
47 | + return array($el); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | } |
@@ -4,50 +4,50 @@ |
||
4 | 4 | |
5 | 5 | class Valid_Remote_Entity implements Remote_Entity { |
6 | 6 | |
7 | - /** |
|
8 | - * @var array<string> |
|
9 | - */ |
|
10 | - private $types; |
|
11 | - |
|
12 | - /** |
|
13 | - * Title or name of the entity. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - private $name; |
|
18 | - /** |
|
19 | - * The entity description. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - private $description; |
|
24 | - /** |
|
25 | - * An array of sameAs urls. |
|
26 | - * |
|
27 | - * @var array<string> |
|
28 | - */ |
|
29 | - private $same_as; |
|
30 | - |
|
31 | - public function __construct( $types, $name, $description, $same_as ) { |
|
32 | - $this->types = $types; |
|
33 | - $this->name = $name; |
|
34 | - $this->description = $description; |
|
35 | - $this->same_as = $same_as; |
|
36 | - } |
|
37 | - |
|
38 | - public function get_name() { |
|
39 | - return $this->name; |
|
40 | - } |
|
41 | - |
|
42 | - public function get_description() { |
|
43 | - return $this->description; |
|
44 | - } |
|
45 | - |
|
46 | - public function get_same_as() { |
|
47 | - return $this->same_as; |
|
48 | - } |
|
49 | - |
|
50 | - public function get_types() { |
|
51 | - return $this->types; |
|
52 | - } |
|
7 | + /** |
|
8 | + * @var array<string> |
|
9 | + */ |
|
10 | + private $types; |
|
11 | + |
|
12 | + /** |
|
13 | + * Title or name of the entity. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + private $name; |
|
18 | + /** |
|
19 | + * The entity description. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + private $description; |
|
24 | + /** |
|
25 | + * An array of sameAs urls. |
|
26 | + * |
|
27 | + * @var array<string> |
|
28 | + */ |
|
29 | + private $same_as; |
|
30 | + |
|
31 | + public function __construct( $types, $name, $description, $same_as ) { |
|
32 | + $this->types = $types; |
|
33 | + $this->name = $name; |
|
34 | + $this->description = $description; |
|
35 | + $this->same_as = $same_as; |
|
36 | + } |
|
37 | + |
|
38 | + public function get_name() { |
|
39 | + return $this->name; |
|
40 | + } |
|
41 | + |
|
42 | + public function get_description() { |
|
43 | + return $this->description; |
|
44 | + } |
|
45 | + |
|
46 | + public function get_same_as() { |
|
47 | + return $this->same_as; |
|
48 | + } |
|
49 | + |
|
50 | + public function get_types() { |
|
51 | + return $this->types; |
|
52 | + } |
|
53 | 53 | } |
@@ -28,7 +28,7 @@ |
||
28 | 28 | */ |
29 | 29 | private $same_as; |
30 | 30 | |
31 | - public function __construct( $types, $name, $description, $same_as ) { |
|
31 | + public function __construct($types, $name, $description, $same_as) { |
|
32 | 32 | $this->types = $types; |
33 | 33 | $this->name = $name; |
34 | 34 | $this->description = $description; |
@@ -4,24 +4,24 @@ |
||
4 | 4 | |
5 | 5 | interface Remote_Entity { |
6 | 6 | |
7 | - /** |
|
8 | - * @return string |
|
9 | - */ |
|
10 | - public function get_name(); |
|
7 | + /** |
|
8 | + * @return string |
|
9 | + */ |
|
10 | + public function get_name(); |
|
11 | 11 | |
12 | - /** |
|
13 | - * @return string |
|
14 | - */ |
|
15 | - public function get_description(); |
|
12 | + /** |
|
13 | + * @return string |
|
14 | + */ |
|
15 | + public function get_description(); |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return array<string> |
|
19 | - */ |
|
20 | - public function get_same_as(); |
|
17 | + /** |
|
18 | + * @return array<string> |
|
19 | + */ |
|
20 | + public function get_same_as(); |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return array<string> |
|
24 | - */ |
|
25 | - public function get_types(); |
|
22 | + /** |
|
23 | + * @return array<string> |
|
24 | + */ |
|
25 | + public function get_types(); |
|
26 | 26 | |
27 | 27 | } |
@@ -14,75 +14,75 @@ |
||
14 | 14 | |
15 | 15 | class Entity_Helper { |
16 | 16 | |
17 | - private $entity_uri_service; |
|
18 | - |
|
19 | - private $entity_service; |
|
20 | - |
|
21 | - /** |
|
22 | - * Entity_Helper constructor. |
|
23 | - * |
|
24 | - * @param Wordlift_Entity_Uri_Service $entity_uri_service |
|
25 | - * @param Wordlift_Entity_Service $entity_service |
|
26 | - */ |
|
27 | - protected function __construct( $entity_uri_service, $entity_service ) { |
|
28 | - |
|
29 | - $this->entity_uri_service = $entity_uri_service; |
|
30 | - $this->entity_service = $entity_service; |
|
31 | - |
|
32 | - } |
|
33 | - |
|
34 | - private static $instance; |
|
35 | - |
|
36 | - public static function get_instance() { |
|
37 | - if ( ! isset( self::$instance ) ) { |
|
38 | - self::$instance = new self( Wordlift_Entity_Uri_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
39 | - } |
|
40 | - |
|
41 | - return self::$instance; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Maps the provided URIs to local URIs. |
|
46 | - * |
|
47 | - * The input array is filtered out of the local URIs. Then each URI is checked with itemid and sameAs in the local |
|
48 | - * database. If found a mapping is added from the external URI to the local URI. |
|
49 | - * |
|
50 | - * An array of mappings is returned, where the key is the external URI and the value is the local URI. |
|
51 | - * |
|
52 | - * @param array $uris An array of URIs. |
|
53 | - * |
|
54 | - * @return array The mappings array. |
|
55 | - */ |
|
56 | - public function map_many_to_local( $uris ) { |
|
57 | - |
|
58 | - // Filter only the external URIs. |
|
59 | - $entity_uri_service = $this->entity_uri_service; |
|
60 | - $external_uris = array_filter( |
|
61 | - $uris, |
|
62 | - function ( $item ) use ( $entity_uri_service ) { |
|
63 | - return ! $entity_uri_service->is_internal( $item ); |
|
64 | - } |
|
65 | - ); |
|
66 | - |
|
67 | - // Preload the URIs. |
|
68 | - $entity_uri_service->preload_uris( $external_uris ); |
|
69 | - |
|
70 | - $mappings = array(); |
|
71 | - foreach ( $external_uris as $external_uri ) { |
|
72 | - if ( empty( $external_uri ) ) { |
|
73 | - continue; |
|
74 | - } |
|
75 | - |
|
76 | - $entity = $entity_uri_service->get_entity( $external_uri ); |
|
77 | - if ( null !== $entity ) { |
|
78 | - |
|
79 | - // Get the internal URI. |
|
80 | - $internal_uri = $this->entity_service->get_uri( $entity->ID ); |
|
81 | - $mappings[ $external_uri ] = $internal_uri; |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - return $mappings; |
|
86 | - } |
|
17 | + private $entity_uri_service; |
|
18 | + |
|
19 | + private $entity_service; |
|
20 | + |
|
21 | + /** |
|
22 | + * Entity_Helper constructor. |
|
23 | + * |
|
24 | + * @param Wordlift_Entity_Uri_Service $entity_uri_service |
|
25 | + * @param Wordlift_Entity_Service $entity_service |
|
26 | + */ |
|
27 | + protected function __construct( $entity_uri_service, $entity_service ) { |
|
28 | + |
|
29 | + $this->entity_uri_service = $entity_uri_service; |
|
30 | + $this->entity_service = $entity_service; |
|
31 | + |
|
32 | + } |
|
33 | + |
|
34 | + private static $instance; |
|
35 | + |
|
36 | + public static function get_instance() { |
|
37 | + if ( ! isset( self::$instance ) ) { |
|
38 | + self::$instance = new self( Wordlift_Entity_Uri_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
39 | + } |
|
40 | + |
|
41 | + return self::$instance; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Maps the provided URIs to local URIs. |
|
46 | + * |
|
47 | + * The input array is filtered out of the local URIs. Then each URI is checked with itemid and sameAs in the local |
|
48 | + * database. If found a mapping is added from the external URI to the local URI. |
|
49 | + * |
|
50 | + * An array of mappings is returned, where the key is the external URI and the value is the local URI. |
|
51 | + * |
|
52 | + * @param array $uris An array of URIs. |
|
53 | + * |
|
54 | + * @return array The mappings array. |
|
55 | + */ |
|
56 | + public function map_many_to_local( $uris ) { |
|
57 | + |
|
58 | + // Filter only the external URIs. |
|
59 | + $entity_uri_service = $this->entity_uri_service; |
|
60 | + $external_uris = array_filter( |
|
61 | + $uris, |
|
62 | + function ( $item ) use ( $entity_uri_service ) { |
|
63 | + return ! $entity_uri_service->is_internal( $item ); |
|
64 | + } |
|
65 | + ); |
|
66 | + |
|
67 | + // Preload the URIs. |
|
68 | + $entity_uri_service->preload_uris( $external_uris ); |
|
69 | + |
|
70 | + $mappings = array(); |
|
71 | + foreach ( $external_uris as $external_uri ) { |
|
72 | + if ( empty( $external_uri ) ) { |
|
73 | + continue; |
|
74 | + } |
|
75 | + |
|
76 | + $entity = $entity_uri_service->get_entity( $external_uri ); |
|
77 | + if ( null !== $entity ) { |
|
78 | + |
|
79 | + // Get the internal URI. |
|
80 | + $internal_uri = $this->entity_service->get_uri( $entity->ID ); |
|
81 | + $mappings[ $external_uri ] = $internal_uri; |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + return $mappings; |
|
86 | + } |
|
87 | 87 | |
88 | 88 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * @param Wordlift_Entity_Uri_Service $entity_uri_service |
25 | 25 | * @param Wordlift_Entity_Service $entity_service |
26 | 26 | */ |
27 | - protected function __construct( $entity_uri_service, $entity_service ) { |
|
27 | + protected function __construct($entity_uri_service, $entity_service) { |
|
28 | 28 | |
29 | 29 | $this->entity_uri_service = $entity_uri_service; |
30 | 30 | $this->entity_service = $entity_service; |
@@ -34,8 +34,8 @@ discard block |
||
34 | 34 | private static $instance; |
35 | 35 | |
36 | 36 | public static function get_instance() { |
37 | - if ( ! isset( self::$instance ) ) { |
|
38 | - self::$instance = new self( Wordlift_Entity_Uri_Service::get_instance(), Wordlift_Entity_Service::get_instance() ); |
|
37 | + if ( ! isset(self::$instance)) { |
|
38 | + self::$instance = new self(Wordlift_Entity_Uri_Service::get_instance(), Wordlift_Entity_Service::get_instance()); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | return self::$instance; |
@@ -53,32 +53,32 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @return array The mappings array. |
55 | 55 | */ |
56 | - public function map_many_to_local( $uris ) { |
|
56 | + public function map_many_to_local($uris) { |
|
57 | 57 | |
58 | 58 | // Filter only the external URIs. |
59 | 59 | $entity_uri_service = $this->entity_uri_service; |
60 | 60 | $external_uris = array_filter( |
61 | 61 | $uris, |
62 | - function ( $item ) use ( $entity_uri_service ) { |
|
63 | - return ! $entity_uri_service->is_internal( $item ); |
|
62 | + function($item) use ($entity_uri_service) { |
|
63 | + return ! $entity_uri_service->is_internal($item); |
|
64 | 64 | } |
65 | 65 | ); |
66 | 66 | |
67 | 67 | // Preload the URIs. |
68 | - $entity_uri_service->preload_uris( $external_uris ); |
|
68 | + $entity_uri_service->preload_uris($external_uris); |
|
69 | 69 | |
70 | 70 | $mappings = array(); |
71 | - foreach ( $external_uris as $external_uri ) { |
|
72 | - if ( empty( $external_uri ) ) { |
|
71 | + foreach ($external_uris as $external_uri) { |
|
72 | + if (empty($external_uri)) { |
|
73 | 73 | continue; |
74 | 74 | } |
75 | 75 | |
76 | - $entity = $entity_uri_service->get_entity( $external_uri ); |
|
77 | - if ( null !== $entity ) { |
|
76 | + $entity = $entity_uri_service->get_entity($external_uri); |
|
77 | + if (null !== $entity) { |
|
78 | 78 | |
79 | 79 | // Get the internal URI. |
80 | - $internal_uri = $this->entity_service->get_uri( $entity->ID ); |
|
81 | - $mappings[ $external_uri ] = $internal_uri; |
|
80 | + $internal_uri = $this->entity_service->get_uri($entity->ID); |
|
81 | + $mappings[$external_uri] = $internal_uri; |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 |
@@ -8,57 +8,57 @@ |
||
8 | 8 | |
9 | 9 | class Entity_Uri_Generator { |
10 | 10 | |
11 | - public static function create_uri( $object_type_enum, $id ) { |
|
11 | + public static function create_uri( $object_type_enum, $id ) { |
|
12 | 12 | |
13 | - switch ( $object_type_enum ) { |
|
14 | - case Object_Type_Enum::POST: |
|
15 | - $post = get_post( $id ); |
|
16 | - if ( ! isset( $post ) || in_array( $post->post_status, array( 'auto-draft', 'inherit' ), true ) ) { |
|
17 | - return null; |
|
18 | - } |
|
13 | + switch ( $object_type_enum ) { |
|
14 | + case Object_Type_Enum::POST: |
|
15 | + $post = get_post( $id ); |
|
16 | + if ( ! isset( $post ) || in_array( $post->post_status, array( 'auto-draft', 'inherit' ), true ) ) { |
|
17 | + return null; |
|
18 | + } |
|
19 | 19 | |
20 | - $slug = $post->post_name ? $post->post_name : sanitize_title( $post->post_title ) . '-' . $post->ID; |
|
20 | + $slug = $post->post_name ? $post->post_name : sanitize_title( $post->post_title ) . '-' . $post->ID; |
|
21 | 21 | |
22 | - return self::ensure_unique( $post->post_type . '/' . $slug ); |
|
22 | + return self::ensure_unique( $post->post_type . '/' . $slug ); |
|
23 | 23 | |
24 | - case Object_Type_Enum::TERM: |
|
25 | - $term = get_term( $id ); |
|
26 | - if ( ! is_a( $term, 'WP_Term' ) ) { |
|
27 | - return null; |
|
28 | - } |
|
24 | + case Object_Type_Enum::TERM: |
|
25 | + $term = get_term( $id ); |
|
26 | + if ( ! is_a( $term, 'WP_Term' ) ) { |
|
27 | + return null; |
|
28 | + } |
|
29 | 29 | |
30 | - return self::ensure_unique( $term->taxonomy . '/' . $term->slug ); |
|
30 | + return self::ensure_unique( $term->taxonomy . '/' . $term->slug ); |
|
31 | 31 | |
32 | - case Object_Type_Enum::USER: |
|
33 | - $user = get_user_by( 'id', $id ); |
|
32 | + case Object_Type_Enum::USER: |
|
33 | + $user = get_user_by( 'id', $id ); |
|
34 | 34 | |
35 | - if ( ! is_a( $user, 'WP_User' ) ) { |
|
36 | - return null; |
|
37 | - } |
|
35 | + if ( ! is_a( $user, 'WP_User' ) ) { |
|
36 | + return null; |
|
37 | + } |
|
38 | 38 | |
39 | - return self::ensure_unique( 'user/' . $user->user_nicename ); |
|
39 | + return self::ensure_unique( 'user/' . $user->user_nicename ); |
|
40 | 40 | |
41 | - default: |
|
42 | - } |
|
41 | + default: |
|
42 | + } |
|
43 | 43 | |
44 | - return null; |
|
45 | - } |
|
44 | + return null; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @throws Exception when an error occurs. |
|
49 | - */ |
|
50 | - private static function ensure_unique( $rel_uri ) { |
|
51 | - for ( $try_rel_uri = $rel_uri, $i = 2; $i < 100; $i ++ ) { |
|
52 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $try_rel_uri ); |
|
53 | - if ( ! isset( $content ) ) { |
|
54 | - return $try_rel_uri; |
|
55 | - } |
|
47 | + /** |
|
48 | + * @throws Exception when an error occurs. |
|
49 | + */ |
|
50 | + private static function ensure_unique( $rel_uri ) { |
|
51 | + for ( $try_rel_uri = $rel_uri, $i = 2; $i < 100; $i ++ ) { |
|
52 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $try_rel_uri ); |
|
53 | + if ( ! isset( $content ) ) { |
|
54 | + return $try_rel_uri; |
|
55 | + } |
|
56 | 56 | |
57 | - $try_rel_uri = $rel_uri . '-' . $i; |
|
58 | - } |
|
57 | + $try_rel_uri = $rel_uri . '-' . $i; |
|
58 | + } |
|
59 | 59 | |
60 | - // Giving up. |
|
61 | - return $rel_uri . '-' . uniqid(); |
|
62 | - } |
|
60 | + // Giving up. |
|
61 | + return $rel_uri . '-' . uniqid(); |
|
62 | + } |
|
63 | 63 | |
64 | 64 | } |
@@ -8,35 +8,35 @@ discard block |
||
8 | 8 | |
9 | 9 | class Entity_Uri_Generator { |
10 | 10 | |
11 | - public static function create_uri( $object_type_enum, $id ) { |
|
11 | + public static function create_uri($object_type_enum, $id) { |
|
12 | 12 | |
13 | - switch ( $object_type_enum ) { |
|
13 | + switch ($object_type_enum) { |
|
14 | 14 | case Object_Type_Enum::POST: |
15 | - $post = get_post( $id ); |
|
16 | - if ( ! isset( $post ) || in_array( $post->post_status, array( 'auto-draft', 'inherit' ), true ) ) { |
|
15 | + $post = get_post($id); |
|
16 | + if ( ! isset($post) || in_array($post->post_status, array('auto-draft', 'inherit'), true)) { |
|
17 | 17 | return null; |
18 | 18 | } |
19 | 19 | |
20 | - $slug = $post->post_name ? $post->post_name : sanitize_title( $post->post_title ) . '-' . $post->ID; |
|
20 | + $slug = $post->post_name ? $post->post_name : sanitize_title($post->post_title).'-'.$post->ID; |
|
21 | 21 | |
22 | - return self::ensure_unique( $post->post_type . '/' . $slug ); |
|
22 | + return self::ensure_unique($post->post_type.'/'.$slug); |
|
23 | 23 | |
24 | 24 | case Object_Type_Enum::TERM: |
25 | - $term = get_term( $id ); |
|
26 | - if ( ! is_a( $term, 'WP_Term' ) ) { |
|
25 | + $term = get_term($id); |
|
26 | + if ( ! is_a($term, 'WP_Term')) { |
|
27 | 27 | return null; |
28 | 28 | } |
29 | 29 | |
30 | - return self::ensure_unique( $term->taxonomy . '/' . $term->slug ); |
|
30 | + return self::ensure_unique($term->taxonomy.'/'.$term->slug); |
|
31 | 31 | |
32 | 32 | case Object_Type_Enum::USER: |
33 | - $user = get_user_by( 'id', $id ); |
|
33 | + $user = get_user_by('id', $id); |
|
34 | 34 | |
35 | - if ( ! is_a( $user, 'WP_User' ) ) { |
|
35 | + if ( ! is_a($user, 'WP_User')) { |
|
36 | 36 | return null; |
37 | 37 | } |
38 | 38 | |
39 | - return self::ensure_unique( 'user/' . $user->user_nicename ); |
|
39 | + return self::ensure_unique('user/'.$user->user_nicename); |
|
40 | 40 | |
41 | 41 | default: |
42 | 42 | } |
@@ -47,18 +47,18 @@ discard block |
||
47 | 47 | /** |
48 | 48 | * @throws Exception when an error occurs. |
49 | 49 | */ |
50 | - private static function ensure_unique( $rel_uri ) { |
|
51 | - for ( $try_rel_uri = $rel_uri, $i = 2; $i < 100; $i ++ ) { |
|
52 | - $content = Wordpress_Content_Service::get_instance()->get_by_entity_id( $try_rel_uri ); |
|
53 | - if ( ! isset( $content ) ) { |
|
50 | + private static function ensure_unique($rel_uri) { |
|
51 | + for ($try_rel_uri = $rel_uri, $i = 2; $i < 100; $i++) { |
|
52 | + $content = Wordpress_Content_Service::get_instance()->get_by_entity_id($try_rel_uri); |
|
53 | + if ( ! isset($content)) { |
|
54 | 54 | return $try_rel_uri; |
55 | 55 | } |
56 | 56 | |
57 | - $try_rel_uri = $rel_uri . '-' . $i; |
|
57 | + $try_rel_uri = $rel_uri.'-'.$i; |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | // Giving up. |
61 | - return $rel_uri . '-' . uniqid(); |
|
61 | + return $rel_uri.'-'.uniqid(); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | } |
@@ -11,17 +11,17 @@ |
||
11 | 11 | */ |
12 | 12 | class Loader extends Default_Loader { |
13 | 13 | |
14 | - public function init_all_dependencies() { |
|
15 | - new Import_Page(); |
|
16 | - $rest_endpoint = new Rest_Endpoint(); |
|
17 | - $rest_endpoint->init(); |
|
18 | - } |
|
14 | + public function init_all_dependencies() { |
|
15 | + new Import_Page(); |
|
16 | + $rest_endpoint = new Rest_Endpoint(); |
|
17 | + $rest_endpoint->init(); |
|
18 | + } |
|
19 | 19 | |
20 | - public function get_feature_slug() { |
|
21 | - return 'google-addon-integration'; |
|
22 | - } |
|
20 | + public function get_feature_slug() { |
|
21 | + return 'google-addon-integration'; |
|
22 | + } |
|
23 | 23 | |
24 | - public function get_feature_default_value() { |
|
25 | - return true; |
|
26 | - } |
|
24 | + public function get_feature_default_value() { |
|
25 | + return true; |
|
26 | + } |
|
27 | 27 | } |
@@ -8,61 +8,61 @@ |
||
8 | 8 | |
9 | 9 | class Rest_Endpoint { |
10 | 10 | |
11 | - public function init() { |
|
12 | - // PHP 5.3 compatibility. |
|
13 | - $that = $this; |
|
11 | + public function init() { |
|
12 | + // PHP 5.3 compatibility. |
|
13 | + $that = $this; |
|
14 | 14 | |
15 | - add_action( |
|
16 | - 'rest_api_init', |
|
17 | - function () use ( $that ) { |
|
18 | - register_rest_route( |
|
19 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
20 | - '/gaddon/import-entity', |
|
21 | - array( |
|
22 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
23 | - 'callback' => array( $that, 'import_entity' ), |
|
24 | - 'permission_callback' => function () { |
|
25 | - return current_user_can( 'manage_options' ); |
|
26 | - }, |
|
27 | - 'args' => array( |
|
28 | - 'id' => array( |
|
29 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
30 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
31 | - return is_string( $param ); |
|
32 | - }, |
|
33 | - ), |
|
34 | - ), |
|
35 | - ) |
|
36 | - ); |
|
37 | - } |
|
38 | - ); |
|
39 | - } |
|
15 | + add_action( |
|
16 | + 'rest_api_init', |
|
17 | + function () use ( $that ) { |
|
18 | + register_rest_route( |
|
19 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
20 | + '/gaddon/import-entity', |
|
21 | + array( |
|
22 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
23 | + 'callback' => array( $that, 'import_entity' ), |
|
24 | + 'permission_callback' => function () { |
|
25 | + return current_user_can( 'manage_options' ); |
|
26 | + }, |
|
27 | + 'args' => array( |
|
28 | + 'id' => array( |
|
29 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
30 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
31 | + return is_string( $param ); |
|
32 | + }, |
|
33 | + ), |
|
34 | + ), |
|
35 | + ) |
|
36 | + ); |
|
37 | + } |
|
38 | + ); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param $request \WP_REST_Request |
|
43 | - * |
|
44 | - * @return bool[] |
|
45 | - */ |
|
46 | - public function import_entity( $request ) { |
|
41 | + /** |
|
42 | + * @param $request \WP_REST_Request |
|
43 | + * |
|
44 | + * @return bool[] |
|
45 | + */ |
|
46 | + public function import_entity( $request ) { |
|
47 | 47 | |
48 | - $body = $request->get_body(); |
|
49 | - $data = json_decode( $body, true ); |
|
50 | - $entity_id = $data['entity_id']; |
|
48 | + $body = $request->get_body(); |
|
49 | + $data = json_decode( $body, true ); |
|
50 | + $entity_id = $data['entity_id']; |
|
51 | 51 | |
52 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
52 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
53 | 53 | |
54 | - // Do not create/update an existing entity. |
|
55 | - if ( $content_service->get_by_entity_id_or_same_as( $entity_id ) ) { |
|
56 | - return array( 'import_status' => false ); |
|
57 | - } |
|
54 | + // Do not create/update an existing entity. |
|
55 | + if ( $content_service->get_by_entity_id_or_same_as( $entity_id ) ) { |
|
56 | + return array( 'import_status' => false ); |
|
57 | + } |
|
58 | 58 | |
59 | - $remote_entity = Url_To_Remote_Entity_Converter::convert( $entity_id ); |
|
60 | - $importer = Remote_Entity_Importer_Factory::from_entity( $remote_entity ); |
|
61 | - $result = $importer->import(); |
|
59 | + $remote_entity = Url_To_Remote_Entity_Converter::convert( $entity_id ); |
|
60 | + $importer = Remote_Entity_Importer_Factory::from_entity( $remote_entity ); |
|
61 | + $result = $importer->import(); |
|
62 | 62 | |
63 | - return array( |
|
64 | - 'import_status' => $result ? true : false, |
|
65 | - ); |
|
66 | - } |
|
63 | + return array( |
|
64 | + 'import_status' => $result ? true : false, |
|
65 | + ); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | } |
@@ -14,21 +14,21 @@ discard block |
||
14 | 14 | |
15 | 15 | add_action( |
16 | 16 | 'rest_api_init', |
17 | - function () use ( $that ) { |
|
17 | + function() use ($that) { |
|
18 | 18 | register_rest_route( |
19 | 19 | WL_REST_ROUTE_DEFAULT_NAMESPACE, |
20 | 20 | '/gaddon/import-entity', |
21 | 21 | array( |
22 | 22 | 'methods' => \WP_REST_Server::CREATABLE, |
23 | - 'callback' => array( $that, 'import_entity' ), |
|
24 | - 'permission_callback' => function () { |
|
25 | - return current_user_can( 'manage_options' ); |
|
23 | + 'callback' => array($that, 'import_entity'), |
|
24 | + 'permission_callback' => function() { |
|
25 | + return current_user_can('manage_options'); |
|
26 | 26 | }, |
27 | 27 | 'args' => array( |
28 | 28 | 'id' => array( |
29 | 29 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
30 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
31 | - return is_string( $param ); |
|
30 | + 'validate_callback' => function($param, $request, $key) { |
|
31 | + return is_string($param); |
|
32 | 32 | }, |
33 | 33 | ), |
34 | 34 | ), |
@@ -43,21 +43,21 @@ discard block |
||
43 | 43 | * |
44 | 44 | * @return bool[] |
45 | 45 | */ |
46 | - public function import_entity( $request ) { |
|
46 | + public function import_entity($request) { |
|
47 | 47 | |
48 | 48 | $body = $request->get_body(); |
49 | - $data = json_decode( $body, true ); |
|
49 | + $data = json_decode($body, true); |
|
50 | 50 | $entity_id = $data['entity_id']; |
51 | 51 | |
52 | 52 | $content_service = Wordpress_Content_Service::get_instance(); |
53 | 53 | |
54 | 54 | // Do not create/update an existing entity. |
55 | - if ( $content_service->get_by_entity_id_or_same_as( $entity_id ) ) { |
|
56 | - return array( 'import_status' => false ); |
|
55 | + if ($content_service->get_by_entity_id_or_same_as($entity_id)) { |
|
56 | + return array('import_status' => false); |
|
57 | 57 | } |
58 | 58 | |
59 | - $remote_entity = Url_To_Remote_Entity_Converter::convert( $entity_id ); |
|
60 | - $importer = Remote_Entity_Importer_Factory::from_entity( $remote_entity ); |
|
59 | + $remote_entity = Url_To_Remote_Entity_Converter::convert($entity_id); |
|
60 | + $importer = Remote_Entity_Importer_Factory::from_entity($remote_entity); |
|
61 | 61 | $result = $importer->import(); |
62 | 62 | |
63 | 63 | return array( |
@@ -3,82 +3,82 @@ |
||
3 | 3 | namespace Wordlift\Wordpress; |
4 | 4 | |
5 | 5 | abstract class Submenu_Page_Base implements Page { |
6 | - /** |
|
7 | - * @var string |
|
8 | - */ |
|
9 | - private $menu_slug; |
|
10 | - /** |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - private $page_title; |
|
14 | - /** |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - private $capability; |
|
18 | - /** |
|
19 | - * @var string|null |
|
20 | - */ |
|
21 | - private $parent_slug; |
|
22 | - /** |
|
23 | - * @var string|null |
|
24 | - */ |
|
25 | - private $menu_title; |
|
6 | + /** |
|
7 | + * @var string |
|
8 | + */ |
|
9 | + private $menu_slug; |
|
10 | + /** |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + private $page_title; |
|
14 | + /** |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + private $capability; |
|
18 | + /** |
|
19 | + * @var string|null |
|
20 | + */ |
|
21 | + private $parent_slug; |
|
22 | + /** |
|
23 | + * @var string|null |
|
24 | + */ |
|
25 | + private $menu_title; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Abstract_Submenu_Page constructor. |
|
29 | - * |
|
30 | - * @param string $menu_slug |
|
31 | - * @param string $page_title |
|
32 | - * @param string $capability |
|
33 | - * @param string|null $parent_slug |
|
34 | - * @param string|null $menu_title |
|
35 | - */ |
|
36 | - public function __construct( $menu_slug, $page_title, $capability = 'manage_options', $parent_slug = null, $menu_title = null ) { |
|
27 | + /** |
|
28 | + * Abstract_Submenu_Page constructor. |
|
29 | + * |
|
30 | + * @param string $menu_slug |
|
31 | + * @param string $page_title |
|
32 | + * @param string $capability |
|
33 | + * @param string|null $parent_slug |
|
34 | + * @param string|null $menu_title |
|
35 | + */ |
|
36 | + public function __construct( $menu_slug, $page_title, $capability = 'manage_options', $parent_slug = null, $menu_title = null ) { |
|
37 | 37 | |
38 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
38 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
39 | 39 | |
40 | - $this->menu_slug = $menu_slug; |
|
41 | - $this->page_title = $page_title; |
|
42 | - $this->capability = $capability; |
|
43 | - $this->parent_slug = $parent_slug; |
|
44 | - $this->menu_title = isset( $menu_title ) ? $menu_title : $page_title; |
|
45 | - } |
|
40 | + $this->menu_slug = $menu_slug; |
|
41 | + $this->page_title = $page_title; |
|
42 | + $this->capability = $capability; |
|
43 | + $this->parent_slug = $parent_slug; |
|
44 | + $this->menu_title = isset( $menu_title ) ? $menu_title : $page_title; |
|
45 | + } |
|
46 | 46 | |
47 | - public function get_menu_slug() { |
|
47 | + public function get_menu_slug() { |
|
48 | 48 | |
49 | - return $this->menu_slug; |
|
50 | - } |
|
49 | + return $this->menu_slug; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * The `admin_menu` callback. Will call {@link add_submenu_page} to add the |
|
54 | - * page to the admin menu. |
|
55 | - * |
|
56 | - * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
57 | - * @since 1.0.0 |
|
58 | - */ |
|
59 | - public function admin_menu() { |
|
52 | + /** |
|
53 | + * The `admin_menu` callback. Will call {@link add_submenu_page} to add the |
|
54 | + * page to the admin menu. |
|
55 | + * |
|
56 | + * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required. |
|
57 | + * @since 1.0.0 |
|
58 | + */ |
|
59 | + public function admin_menu() { |
|
60 | 60 | |
61 | - // Add the sub-menu page. |
|
62 | - // |
|
63 | - // See http://codex.wordpress.org/Function_Reference/add_submenu_page |
|
64 | - $page = add_submenu_page( |
|
65 | - $this->parent_slug, |
|
66 | - $this->page_title, |
|
67 | - $this->menu_title, |
|
68 | - $this->capability, |
|
69 | - $this->menu_slug, |
|
70 | - array( $this, 'render' ) |
|
71 | - ); |
|
61 | + // Add the sub-menu page. |
|
62 | + // |
|
63 | + // See http://codex.wordpress.org/Function_Reference/add_submenu_page |
|
64 | + $page = add_submenu_page( |
|
65 | + $this->parent_slug, |
|
66 | + $this->page_title, |
|
67 | + $this->menu_title, |
|
68 | + $this->capability, |
|
69 | + $this->menu_slug, |
|
70 | + array( $this, 'render' ) |
|
71 | + ); |
|
72 | 72 | |
73 | - // Set a hook to enqueue scripts only when the settings page is displayed. |
|
74 | - add_action( 'admin_print_scripts-' . $page, array( $this, 'enqueue_scripts' ) ); |
|
73 | + // Set a hook to enqueue scripts only when the settings page is displayed. |
|
74 | + add_action( 'admin_print_scripts-' . $page, array( $this, 'enqueue_scripts' ) ); |
|
75 | 75 | |
76 | - // Finally return the page hook_suffix. |
|
77 | - return $page; |
|
78 | - } |
|
76 | + // Finally return the page hook_suffix. |
|
77 | + return $page; |
|
78 | + } |
|
79 | 79 | |
80 | - abstract public function enqueue_scripts(); |
|
80 | + abstract public function enqueue_scripts(); |
|
81 | 81 | |
82 | - abstract public function render(); |
|
82 | + abstract public function render(); |
|
83 | 83 | |
84 | 84 | } |
@@ -33,15 +33,15 @@ discard block |
||
33 | 33 | * @param string|null $parent_slug |
34 | 34 | * @param string|null $menu_title |
35 | 35 | */ |
36 | - public function __construct( $menu_slug, $page_title, $capability = 'manage_options', $parent_slug = null, $menu_title = null ) { |
|
36 | + public function __construct($menu_slug, $page_title, $capability = 'manage_options', $parent_slug = null, $menu_title = null) { |
|
37 | 37 | |
38 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
38 | + add_action('admin_menu', array($this, 'admin_menu')); |
|
39 | 39 | |
40 | 40 | $this->menu_slug = $menu_slug; |
41 | 41 | $this->page_title = $page_title; |
42 | 42 | $this->capability = $capability; |
43 | 43 | $this->parent_slug = $parent_slug; |
44 | - $this->menu_title = isset( $menu_title ) ? $menu_title : $page_title; |
|
44 | + $this->menu_title = isset($menu_title) ? $menu_title : $page_title; |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | public function get_menu_slug() { |
@@ -67,11 +67,11 @@ discard block |
||
67 | 67 | $this->menu_title, |
68 | 68 | $this->capability, |
69 | 69 | $this->menu_slug, |
70 | - array( $this, 'render' ) |
|
70 | + array($this, 'render') |
|
71 | 71 | ); |
72 | 72 | |
73 | 73 | // Set a hook to enqueue scripts only when the settings page is displayed. |
74 | - add_action( 'admin_print_scripts-' . $page, array( $this, 'enqueue_scripts' ) ); |
|
74 | + add_action('admin_print_scripts-'.$page, array($this, 'enqueue_scripts')); |
|
75 | 75 | |
76 | 76 | // Finally return the page hook_suffix. |
77 | 77 | return $page; |
@@ -8,78 +8,78 @@ |
||
8 | 8 | */ |
9 | 9 | class Async_Template_Decorator { |
10 | 10 | |
11 | - /** |
|
12 | - * @var $rest_url_name string |
|
13 | - */ |
|
14 | - private $rest_url_name; |
|
11 | + /** |
|
12 | + * @var $rest_url_name string |
|
13 | + */ |
|
14 | + private $rest_url_name; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @var $filter_name string |
|
18 | - */ |
|
19 | - private $filter_name; |
|
16 | + /** |
|
17 | + * @var $filter_name string |
|
18 | + */ |
|
19 | + private $filter_name; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Async_Template_Decorator constructor. |
|
23 | - * |
|
24 | - * @param $shortcode_instance \Wordlift_Shortcode |
|
25 | - */ |
|
26 | - public function __construct( $shortcode_instance ) { |
|
27 | - $this->rest_url_name = $this->get_widget_name( $shortcode_instance ); |
|
28 | - $this->filter_name = str_replace( '-', '_', $this->rest_url_name ); |
|
29 | - add_action( 'rest_api_init', array( $this, 'register_template_route' ) ); |
|
30 | - } |
|
21 | + /** |
|
22 | + * Async_Template_Decorator constructor. |
|
23 | + * |
|
24 | + * @param $shortcode_instance \Wordlift_Shortcode |
|
25 | + */ |
|
26 | + public function __construct( $shortcode_instance ) { |
|
27 | + $this->rest_url_name = $this->get_widget_name( $shortcode_instance ); |
|
28 | + $this->filter_name = str_replace( '-', '_', $this->rest_url_name ); |
|
29 | + add_action( 'rest_api_init', array( $this, 'register_template_route' ) ); |
|
30 | + } |
|
31 | 31 | |
32 | - public function register_template_route() { |
|
32 | + public function register_template_route() { |
|
33 | 33 | |
34 | - register_rest_route( |
|
35 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
36 | - "/{$this->rest_url_name}/template/", |
|
37 | - array( |
|
38 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
39 | - 'callback' => array( $this, 'get_template' ), |
|
40 | - /** |
|
41 | - * We want this endpoint to be publicly accessible |
|
42 | - */ |
|
43 | - 'permission_callback' => '__return_true', |
|
44 | - 'args' => array( |
|
45 | - 'template_id' => array( |
|
46 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
47 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
48 | - return is_string( $param ) && $param; |
|
49 | - }, |
|
50 | - ), |
|
51 | - ), |
|
52 | - ) |
|
53 | - ); |
|
54 | - } |
|
34 | + register_rest_route( |
|
35 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
36 | + "/{$this->rest_url_name}/template/", |
|
37 | + array( |
|
38 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
39 | + 'callback' => array( $this, 'get_template' ), |
|
40 | + /** |
|
41 | + * We want this endpoint to be publicly accessible |
|
42 | + */ |
|
43 | + 'permission_callback' => '__return_true', |
|
44 | + 'args' => array( |
|
45 | + 'template_id' => array( |
|
46 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
47 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
48 | + return is_string( $param ) && $param; |
|
49 | + }, |
|
50 | + ), |
|
51 | + ), |
|
52 | + ) |
|
53 | + ); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Shortcode widget makes call to this endpoint to get the template. |
|
58 | - * Takes the request, checks if template id is registered via filter, |
|
59 | - * if not it returns empty. |
|
60 | - * |
|
61 | - * @param $request \WP_REST_Request |
|
62 | - * |
|
63 | - * @return string Returns the template string. |
|
64 | - */ |
|
65 | - public function get_template( $request ) { |
|
66 | - $data = $request->get_params(); |
|
67 | - $template_id = (string) $data['template_id']; |
|
68 | - $templates = apply_filters( "wordlift_{$this->filter_name}_templates", array() ); |
|
69 | - $template = array_key_exists( $template_id, $templates ) ? $templates[ $template_id ] : ''; |
|
56 | + /** |
|
57 | + * Shortcode widget makes call to this endpoint to get the template. |
|
58 | + * Takes the request, checks if template id is registered via filter, |
|
59 | + * if not it returns empty. |
|
60 | + * |
|
61 | + * @param $request \WP_REST_Request |
|
62 | + * |
|
63 | + * @return string Returns the template string. |
|
64 | + */ |
|
65 | + public function get_template( $request ) { |
|
66 | + $data = $request->get_params(); |
|
67 | + $template_id = (string) $data['template_id']; |
|
68 | + $templates = apply_filters( "wordlift_{$this->filter_name}_templates", array() ); |
|
69 | + $template = array_key_exists( $template_id, $templates ) ? $templates[ $template_id ] : ''; |
|
70 | 70 | |
71 | - return array( 'template' => $template ); |
|
72 | - } |
|
71 | + return array( 'template' => $template ); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @param $shortcode_instance \Wordlift_Shortcode |
|
76 | - * |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - private static function get_widget_name( $shortcode_instance ) { |
|
80 | - $name = str_replace( 'wl_', '', $shortcode_instance::SHORTCODE ); |
|
74 | + /** |
|
75 | + * @param $shortcode_instance \Wordlift_Shortcode |
|
76 | + * |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + private static function get_widget_name( $shortcode_instance ) { |
|
80 | + $name = str_replace( 'wl_', '', $shortcode_instance::SHORTCODE ); |
|
81 | 81 | |
82 | - return str_replace( '_', '-', $name ); |
|
83 | - } |
|
82 | + return str_replace( '_', '-', $name ); |
|
83 | + } |
|
84 | 84 | |
85 | 85 | } |
@@ -23,10 +23,10 @@ discard block |
||
23 | 23 | * |
24 | 24 | * @param $shortcode_instance \Wordlift_Shortcode |
25 | 25 | */ |
26 | - public function __construct( $shortcode_instance ) { |
|
27 | - $this->rest_url_name = $this->get_widget_name( $shortcode_instance ); |
|
28 | - $this->filter_name = str_replace( '-', '_', $this->rest_url_name ); |
|
29 | - add_action( 'rest_api_init', array( $this, 'register_template_route' ) ); |
|
26 | + public function __construct($shortcode_instance) { |
|
27 | + $this->rest_url_name = $this->get_widget_name($shortcode_instance); |
|
28 | + $this->filter_name = str_replace('-', '_', $this->rest_url_name); |
|
29 | + add_action('rest_api_init', array($this, 'register_template_route')); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | public function register_template_route() { |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | "/{$this->rest_url_name}/template/", |
37 | 37 | array( |
38 | 38 | 'methods' => \WP_REST_Server::CREATABLE, |
39 | - 'callback' => array( $this, 'get_template' ), |
|
39 | + 'callback' => array($this, 'get_template'), |
|
40 | 40 | /** |
41 | 41 | * We want this endpoint to be publicly accessible |
42 | 42 | */ |
@@ -44,8 +44,8 @@ discard block |
||
44 | 44 | 'args' => array( |
45 | 45 | 'template_id' => array( |
46 | 46 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
47 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
48 | - return is_string( $param ) && $param; |
|
47 | + 'validate_callback' => function($param, $request, $key) { |
|
48 | + return is_string($param) && $param; |
|
49 | 49 | }, |
50 | 50 | ), |
51 | 51 | ), |
@@ -62,13 +62,13 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @return string Returns the template string. |
64 | 64 | */ |
65 | - public function get_template( $request ) { |
|
65 | + public function get_template($request) { |
|
66 | 66 | $data = $request->get_params(); |
67 | 67 | $template_id = (string) $data['template_id']; |
68 | - $templates = apply_filters( "wordlift_{$this->filter_name}_templates", array() ); |
|
69 | - $template = array_key_exists( $template_id, $templates ) ? $templates[ $template_id ] : ''; |
|
68 | + $templates = apply_filters("wordlift_{$this->filter_name}_templates", array()); |
|
69 | + $template = array_key_exists($template_id, $templates) ? $templates[$template_id] : ''; |
|
70 | 70 | |
71 | - return array( 'template' => $template ); |
|
71 | + return array('template' => $template); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @return string |
78 | 78 | */ |
79 | - private static function get_widget_name( $shortcode_instance ) { |
|
80 | - $name = str_replace( 'wl_', '', $shortcode_instance::SHORTCODE ); |
|
79 | + private static function get_widget_name($shortcode_instance) { |
|
80 | + $name = str_replace('wl_', '', $shortcode_instance::SHORTCODE); |
|
81 | 81 | |
82 | - return str_replace( '_', '-', $name ); |
|
82 | + return str_replace('_', '-', $name); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | } |