@@ -16,130 +16,130 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Push_Reference_Data_Command { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.18.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 25 | - */ |
|
| 26 | - private $relation_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.18.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Sparql_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.18.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $sparql_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.18.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 52 | - */ |
|
| 53 | - private $configuration_service; |
|
| 54 | - /** |
|
| 55 | - * @var Wordlift_Entity_Type_Service |
|
| 56 | - */ |
|
| 57 | - private $entity_type_service; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Wordlift_Push_Reference_Data_Command constructor. |
|
| 61 | - * |
|
| 62 | - * @since 3.18.0 |
|
| 63 | - * |
|
| 64 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 65 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 66 | - * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 67 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 68 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 69 | - */ |
|
| 70 | - public function __construct( $relation_service, $entity_service, $sparql_service, $configuration_service, $entity_type_service ) { |
|
| 71 | - |
|
| 72 | - $this->relation_service = $relation_service; |
|
| 73 | - $this->entity_service = $entity_service; |
|
| 74 | - $this->sparql_service = $sparql_service; |
|
| 75 | - $this->configuration_service = $configuration_service; |
|
| 76 | - $this->entity_type_service = $entity_type_service; |
|
| 77 | - |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - public function __invoke( $args ) { |
|
| 81 | - |
|
| 82 | - $relations = $this->relation_service->find_all_grouped_by_subject_id(); |
|
| 83 | - $progress_bar = \WP_CLI\Utils\make_progress_bar( 'Processing...', count( $relations ) ); |
|
| 84 | - |
|
| 85 | - foreach ( $relations as $relation ) { |
|
| 86 | - $progress_bar->tick(); |
|
| 87 | - |
|
| 88 | - // Get the post. |
|
| 89 | - $post = get_post( $relation->subject_id ); |
|
| 90 | - |
|
| 91 | - // Bail out if the post isn't found. |
|
| 92 | - if ( null == $post ) { |
|
| 93 | - continue; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // Bail out if it's an entity: we're only interested in articles |
|
| 97 | - // *referencing* entities. |
|
| 98 | - if ( $this->entity_service->is_entity( $post->ID ) ) { |
|
| 99 | - continue; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Get the article URI. |
|
| 103 | - $uri = $this->entity_service->get_uri( $post->ID ); |
|
| 104 | - |
|
| 105 | - // Prepare the DELETE query to delete existing data. |
|
| 106 | - $query = self::get_delete_query( $uri ) |
|
| 107 | - . $this->get_insert_query( $post, $uri, explode( ',', $relation->object_ids ) ); |
|
| 108 | - |
|
| 109 | - $this->sparql_service->execute( $query, false ); |
|
| 110 | - |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $progress_bar->finish(); |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - private static function get_delete_query( $uri ) { |
|
| 118 | - |
|
| 119 | - return Wordlift_Query_Builder |
|
| 120 | - ::new_instance() |
|
| 121 | - ->delete()->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, '?o' ) |
|
| 122 | - ->build() |
|
| 123 | - . Wordlift_Query_Builder |
|
| 124 | - ::new_instance() |
|
| 125 | - ->delete()->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 126 | - ->build() |
|
| 127 | - . Wordlift_Query_Builder |
|
| 128 | - ::new_instance() |
|
| 129 | - ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, '?o' ) |
|
| 130 | - ->build() |
|
| 131 | - . Wordlift_Query_Builder |
|
| 132 | - ::new_instance() |
|
| 133 | - ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 134 | - ->build(); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - private function get_insert_query( $post, $uri, $object_ids ) { |
|
| 138 | - |
|
| 139 | - $language_code = $this->configuration_service->get_language_code(); |
|
| 140 | - $type = $this->entity_type_service->get( $post->ID ); |
|
| 141 | - |
|
| 142 | - /* |
|
| 19 | + /** |
|
| 20 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.18.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 25 | + */ |
|
| 26 | + private $relation_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.18.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Sparql_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.18.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $sparql_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.18.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 52 | + */ |
|
| 53 | + private $configuration_service; |
|
| 54 | + /** |
|
| 55 | + * @var Wordlift_Entity_Type_Service |
|
| 56 | + */ |
|
| 57 | + private $entity_type_service; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Wordlift_Push_Reference_Data_Command constructor. |
|
| 61 | + * |
|
| 62 | + * @since 3.18.0 |
|
| 63 | + * |
|
| 64 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 65 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 66 | + * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 67 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 68 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 69 | + */ |
|
| 70 | + public function __construct( $relation_service, $entity_service, $sparql_service, $configuration_service, $entity_type_service ) { |
|
| 71 | + |
|
| 72 | + $this->relation_service = $relation_service; |
|
| 73 | + $this->entity_service = $entity_service; |
|
| 74 | + $this->sparql_service = $sparql_service; |
|
| 75 | + $this->configuration_service = $configuration_service; |
|
| 76 | + $this->entity_type_service = $entity_type_service; |
|
| 77 | + |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + public function __invoke( $args ) { |
|
| 81 | + |
|
| 82 | + $relations = $this->relation_service->find_all_grouped_by_subject_id(); |
|
| 83 | + $progress_bar = \WP_CLI\Utils\make_progress_bar( 'Processing...', count( $relations ) ); |
|
| 84 | + |
|
| 85 | + foreach ( $relations as $relation ) { |
|
| 86 | + $progress_bar->tick(); |
|
| 87 | + |
|
| 88 | + // Get the post. |
|
| 89 | + $post = get_post( $relation->subject_id ); |
|
| 90 | + |
|
| 91 | + // Bail out if the post isn't found. |
|
| 92 | + if ( null == $post ) { |
|
| 93 | + continue; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // Bail out if it's an entity: we're only interested in articles |
|
| 97 | + // *referencing* entities. |
|
| 98 | + if ( $this->entity_service->is_entity( $post->ID ) ) { |
|
| 99 | + continue; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Get the article URI. |
|
| 103 | + $uri = $this->entity_service->get_uri( $post->ID ); |
|
| 104 | + |
|
| 105 | + // Prepare the DELETE query to delete existing data. |
|
| 106 | + $query = self::get_delete_query( $uri ) |
|
| 107 | + . $this->get_insert_query( $post, $uri, explode( ',', $relation->object_ids ) ); |
|
| 108 | + |
|
| 109 | + $this->sparql_service->execute( $query, false ); |
|
| 110 | + |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $progress_bar->finish(); |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + private static function get_delete_query( $uri ) { |
|
| 118 | + |
|
| 119 | + return Wordlift_Query_Builder |
|
| 120 | + ::new_instance() |
|
| 121 | + ->delete()->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, '?o' ) |
|
| 122 | + ->build() |
|
| 123 | + . Wordlift_Query_Builder |
|
| 124 | + ::new_instance() |
|
| 125 | + ->delete()->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 126 | + ->build() |
|
| 127 | + . Wordlift_Query_Builder |
|
| 128 | + ::new_instance() |
|
| 129 | + ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, '?o' ) |
|
| 130 | + ->build() |
|
| 131 | + . Wordlift_Query_Builder |
|
| 132 | + ::new_instance() |
|
| 133 | + ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 134 | + ->build(); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + private function get_insert_query( $post, $uri, $object_ids ) { |
|
| 138 | + |
|
| 139 | + $language_code = $this->configuration_service->get_language_code(); |
|
| 140 | + $type = $this->entity_type_service->get( $post->ID ); |
|
| 141 | + |
|
| 142 | + /* |
|
| 143 | 143 | * When inserting the schema:url property in the triple store, we want to use the production |
| 144 | 144 | * URL, i.e. we must take into consideration that the current URL is a staging one and that |
| 145 | 145 | * 3rd parties may want to update the URL with a production one. |
@@ -148,21 +148,21 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 150 | 150 | */ |
| 151 | - $permalink = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 152 | - $builder = Wordlift_Query_Builder |
|
| 153 | - ::new_instance() |
|
| 154 | - ->insert() |
|
| 155 | - ->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, $post->post_title, Wordlift_Query_Builder::OBJECT_VALUE, null, $language_code ) |
|
| 156 | - ->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, $permalink ) |
|
| 157 | - ->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, $type['uri'] ); |
|
| 158 | - |
|
| 159 | - $entity_service = $this->entity_service; |
|
| 160 | - array_walk( $object_ids, function ( $item ) use ( $entity_service, $builder, $uri ) { |
|
| 161 | - $object_uri = $entity_service->get_uri( $item ); |
|
| 162 | - $builder->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, $object_uri ); |
|
| 163 | - } ); |
|
| 164 | - |
|
| 165 | - return $builder->build(); |
|
| 166 | - } |
|
| 151 | + $permalink = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 152 | + $builder = Wordlift_Query_Builder |
|
| 153 | + ::new_instance() |
|
| 154 | + ->insert() |
|
| 155 | + ->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, $post->post_title, Wordlift_Query_Builder::OBJECT_VALUE, null, $language_code ) |
|
| 156 | + ->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, $permalink ) |
|
| 157 | + ->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, $type['uri'] ); |
|
| 158 | + |
|
| 159 | + $entity_service = $this->entity_service; |
|
| 160 | + array_walk( $object_ids, function ( $item ) use ( $entity_service, $builder, $uri ) { |
|
| 161 | + $object_uri = $entity_service->get_uri( $item ); |
|
| 162 | + $builder->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, $object_uri ); |
|
| 163 | + } ); |
|
| 164 | + |
|
| 165 | + return $builder->build(); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | 168 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 68 | 68 | * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
| 69 | 69 | */ |
| 70 | - public function __construct( $relation_service, $entity_service, $sparql_service, $configuration_service, $entity_type_service ) { |
|
| 70 | + public function __construct($relation_service, $entity_service, $sparql_service, $configuration_service, $entity_type_service) { |
|
| 71 | 71 | |
| 72 | 72 | $this->relation_service = $relation_service; |
| 73 | 73 | $this->entity_service = $entity_service; |
@@ -77,36 +77,36 @@ discard block |
||
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - public function __invoke( $args ) { |
|
| 80 | + public function __invoke($args) { |
|
| 81 | 81 | |
| 82 | 82 | $relations = $this->relation_service->find_all_grouped_by_subject_id(); |
| 83 | - $progress_bar = \WP_CLI\Utils\make_progress_bar( 'Processing...', count( $relations ) ); |
|
| 83 | + $progress_bar = \WP_CLI\Utils\make_progress_bar('Processing...', count($relations)); |
|
| 84 | 84 | |
| 85 | - foreach ( $relations as $relation ) { |
|
| 85 | + foreach ($relations as $relation) { |
|
| 86 | 86 | $progress_bar->tick(); |
| 87 | 87 | |
| 88 | 88 | // Get the post. |
| 89 | - $post = get_post( $relation->subject_id ); |
|
| 89 | + $post = get_post($relation->subject_id); |
|
| 90 | 90 | |
| 91 | 91 | // Bail out if the post isn't found. |
| 92 | - if ( null == $post ) { |
|
| 92 | + if (null == $post) { |
|
| 93 | 93 | continue; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | // Bail out if it's an entity: we're only interested in articles |
| 97 | 97 | // *referencing* entities. |
| 98 | - if ( $this->entity_service->is_entity( $post->ID ) ) { |
|
| 98 | + if ($this->entity_service->is_entity($post->ID)) { |
|
| 99 | 99 | continue; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | // Get the article URI. |
| 103 | - $uri = $this->entity_service->get_uri( $post->ID ); |
|
| 103 | + $uri = $this->entity_service->get_uri($post->ID); |
|
| 104 | 104 | |
| 105 | 105 | // Prepare the DELETE query to delete existing data. |
| 106 | - $query = self::get_delete_query( $uri ) |
|
| 107 | - . $this->get_insert_query( $post, $uri, explode( ',', $relation->object_ids ) ); |
|
| 106 | + $query = self::get_delete_query($uri) |
|
| 107 | + . $this->get_insert_query($post, $uri, explode(',', $relation->object_ids)); |
|
| 108 | 108 | |
| 109 | - $this->sparql_service->execute( $query, false ); |
|
| 109 | + $this->sparql_service->execute($query, false); |
|
| 110 | 110 | |
| 111 | 111 | } |
| 112 | 112 | |
@@ -114,30 +114,30 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - private static function get_delete_query( $uri ) { |
|
| 117 | + private static function get_delete_query($uri) { |
|
| 118 | 118 | |
| 119 | 119 | return Wordlift_Query_Builder |
| 120 | 120 | ::new_instance() |
| 121 | - ->delete()->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, '?o' ) |
|
| 121 | + ->delete()->statement($uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, '?o') |
|
| 122 | 122 | ->build() |
| 123 | 123 | . Wordlift_Query_Builder |
| 124 | 124 | ::new_instance() |
| 125 | - ->delete()->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 125 | + ->delete()->statement($uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o') |
|
| 126 | 126 | ->build() |
| 127 | 127 | . Wordlift_Query_Builder |
| 128 | 128 | ::new_instance() |
| 129 | - ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, '?o' ) |
|
| 129 | + ->delete()->statement($uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, '?o') |
|
| 130 | 130 | ->build() |
| 131 | 131 | . Wordlift_Query_Builder |
| 132 | 132 | ::new_instance() |
| 133 | - ->delete()->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 133 | + ->delete()->statement($uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o') |
|
| 134 | 134 | ->build(); |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - private function get_insert_query( $post, $uri, $object_ids ) { |
|
| 137 | + private function get_insert_query($post, $uri, $object_ids) { |
|
| 138 | 138 | |
| 139 | 139 | $language_code = $this->configuration_service->get_language_code(); |
| 140 | - $type = $this->entity_type_service->get( $post->ID ); |
|
| 140 | + $type = $this->entity_type_service->get($post->ID); |
|
| 141 | 141 | |
| 142 | 142 | /* |
| 143 | 143 | * When inserting the schema:url property in the triple store, we want to use the production |
@@ -148,18 +148,18 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 150 | 150 | */ |
| 151 | - $permalink = Wordlift_Post_Adapter::get_production_permalink( $post->ID ); |
|
| 151 | + $permalink = Wordlift_Post_Adapter::get_production_permalink($post->ID); |
|
| 152 | 152 | $builder = Wordlift_Query_Builder |
| 153 | 153 | ::new_instance() |
| 154 | 154 | ->insert() |
| 155 | - ->statement( $uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, $post->post_title, Wordlift_Query_Builder::OBJECT_VALUE, null, $language_code ) |
|
| 156 | - ->statement( $uri, Wordlift_Query_Builder::SCHEMA_URL_URI, $permalink ) |
|
| 157 | - ->statement( $uri, Wordlift_Query_Builder::RDFS_TYPE_URI, $type['uri'] ); |
|
| 155 | + ->statement($uri, Wordlift_Query_Builder::SCHEMA_HEADLINE_URI, $post->post_title, Wordlift_Query_Builder::OBJECT_VALUE, null, $language_code) |
|
| 156 | + ->statement($uri, Wordlift_Query_Builder::SCHEMA_URL_URI, $permalink) |
|
| 157 | + ->statement($uri, Wordlift_Query_Builder::RDFS_TYPE_URI, $type['uri']); |
|
| 158 | 158 | |
| 159 | 159 | $entity_service = $this->entity_service; |
| 160 | - array_walk( $object_ids, function ( $item ) use ( $entity_service, $builder, $uri ) { |
|
| 161 | - $object_uri = $entity_service->get_uri( $item ); |
|
| 162 | - $builder->statement( $uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, $object_uri ); |
|
| 160 | + array_walk($object_ids, function($item) use ($entity_service, $builder, $uri) { |
|
| 161 | + $object_uri = $entity_service->get_uri($item); |
|
| 162 | + $builder->statement($uri, Wordlift_Query_Builder::DCTERMS_REFERENCES_URI, $object_uri); |
|
| 163 | 163 | } ); |
| 164 | 164 | |
| 165 | 165 | return $builder->build(); |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | - exit; |
|
| 14 | + exit; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -21,533 +21,533 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Configuration_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The entity base path option name. |
|
| 26 | - * |
|
| 27 | - * @since 3.6.0 |
|
| 28 | - */ |
|
| 29 | - const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The skip wizard (admin installation wizard) option name. |
|
| 33 | - * |
|
| 34 | - * @since 3.9.0 |
|
| 35 | - */ |
|
| 36 | - const SKIP_WIZARD = 'wl_skip_wizard'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * WordLift's key option name. |
|
| 40 | - * |
|
| 41 | - * @since 3.9.0 |
|
| 42 | - */ |
|
| 43 | - const KEY = 'key'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * WordLift's configured language option name. |
|
| 47 | - * |
|
| 48 | - * @since 3.9.0 |
|
| 49 | - */ |
|
| 50 | - const LANGUAGE = 'site_language'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The publisher entity post ID option name. |
|
| 54 | - * |
|
| 55 | - * @since 3.9.0 |
|
| 56 | - */ |
|
| 57 | - const PUBLISHER_ID = 'publisher_id'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The dataset URI option name |
|
| 61 | - * |
|
| 62 | - * @since 3.10.0 |
|
| 63 | - */ |
|
| 64 | - const DATASET_URI = 'redlink_dataset_uri'; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The link by default option name. |
|
| 68 | - * |
|
| 69 | - * @since 3.11.0 |
|
| 70 | - */ |
|
| 71 | - const LINK_BY_DEFAULT = 'link_by_default'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The user preferences about sharing data option. |
|
| 75 | - * |
|
| 76 | - * @since 3.19.0 |
|
| 77 | - */ |
|
| 78 | - const SEND_DIAGNOSTIC = 'send_diagnostic'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * The {@link Wordlift_Log_Service} instance. |
|
| 82 | - * |
|
| 83 | - * @since 3.16.0 |
|
| 84 | - * |
|
| 85 | - * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
|
| 86 | - */ |
|
| 87 | - private $log; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * The Wordlift_Configuration_Service's singleton instance. |
|
| 91 | - * |
|
| 92 | - * @since 3.6.0 |
|
| 93 | - * |
|
| 94 | - * @access private |
|
| 95 | - * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
|
| 96 | - */ |
|
| 97 | - private static $instance; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Create a Wordlift_Configuration_Service's instance. |
|
| 101 | - * |
|
| 102 | - * @since 3.6.0 |
|
| 103 | - */ |
|
| 104 | - public function __construct() { |
|
| 105 | - |
|
| 106 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 107 | - |
|
| 108 | - self::$instance = $this; |
|
| 109 | - |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Get the singleton instance. |
|
| 114 | - * |
|
| 115 | - * @since 3.6.0 |
|
| 116 | - * |
|
| 117 | - * @return \Wordlift_Configuration_Service |
|
| 118 | - */ |
|
| 119 | - public static function get_instance() { |
|
| 120 | - |
|
| 121 | - return self::$instance; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get a configuration given the option name and a key. The option value is |
|
| 126 | - * expected to be an array. |
|
| 127 | - * |
|
| 128 | - * @since 3.6.0 |
|
| 129 | - * |
|
| 130 | - * @param string $option The option name. |
|
| 131 | - * @param string $key A key in the option value array. |
|
| 132 | - * @param string $default The default value in case the key is not found (by default an empty string). |
|
| 133 | - * |
|
| 134 | - * @return mixed The configuration value or the default value if not found. |
|
| 135 | - */ |
|
| 136 | - private function get( $option, $key, $default = '' ) { |
|
| 137 | - |
|
| 138 | - $options = get_option( $option, array() ); |
|
| 139 | - |
|
| 140 | - return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Set a configuration parameter. |
|
| 145 | - * |
|
| 146 | - * @since 3.9.0 |
|
| 147 | - * |
|
| 148 | - * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
|
| 149 | - * @param string $key The value key. |
|
| 150 | - * @param mixed $value The value. |
|
| 151 | - */ |
|
| 152 | - private function set( $option, $key, $value ) { |
|
| 153 | - |
|
| 154 | - $values = get_option( $option ); |
|
| 155 | - $values = isset( $values ) ? $values : array(); |
|
| 156 | - $values[ $key ] = $value; |
|
| 157 | - update_option( $option, $values ); |
|
| 158 | - |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Get the entity base path, by default 'entity'. |
|
| 163 | - * |
|
| 164 | - * @since 3.6.0 |
|
| 165 | - * |
|
| 166 | - * @return string The entity base path. |
|
| 167 | - */ |
|
| 168 | - public function get_entity_base_path() { |
|
| 169 | - |
|
| 170 | - return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Get the entity base path. |
|
| 175 | - * |
|
| 176 | - * @since 3.9.0 |
|
| 177 | - * |
|
| 178 | - * @param string $value The entity base path. |
|
| 179 | - */ |
|
| 180 | - public function set_entity_base_path( $value ) { |
|
| 181 | - |
|
| 182 | - $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 183 | - |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Whether the installation skip wizard should be skipped. |
|
| 188 | - * |
|
| 189 | - * @since 3.9.0 |
|
| 190 | - * |
|
| 191 | - * @return bool True if it should be skipped otherwise false. |
|
| 192 | - */ |
|
| 193 | - public function is_skip_wizard() { |
|
| 194 | - |
|
| 195 | - return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Set the skip wizard parameter. |
|
| 200 | - * |
|
| 201 | - * @since 3.9.0 |
|
| 202 | - * |
|
| 203 | - * @param bool $value True to skip the wizard. We expect a boolean value. |
|
| 204 | - */ |
|
| 205 | - public function set_skip_wizard( $value ) { |
|
| 206 | - |
|
| 207 | - $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 208 | - |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Get WordLift's key. |
|
| 213 | - * |
|
| 214 | - * @since 3.9.0 |
|
| 215 | - * |
|
| 216 | - * @return string WordLift's key or an empty string if not set. |
|
| 217 | - */ |
|
| 218 | - public function get_key() { |
|
| 219 | - |
|
| 220 | - return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Set WordLift's key. |
|
| 225 | - * |
|
| 226 | - * @since 3.9.0 |
|
| 227 | - * |
|
| 228 | - * @param string $value WordLift's key. |
|
| 229 | - */ |
|
| 230 | - public function set_key( $value ) { |
|
| 231 | - |
|
| 232 | - $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Get WordLift's configured language, by default 'en'. |
|
| 237 | - * |
|
| 238 | - * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
|
| 239 | - * |
|
| 240 | - * @since 3.9.0 |
|
| 241 | - * |
|
| 242 | - * @return string WordLift's configured language code ('en' by default). |
|
| 243 | - */ |
|
| 244 | - public function get_language_code() { |
|
| 245 | - |
|
| 246 | - return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
|
| 251 | - * |
|
| 252 | - * @since 3.9.0 |
|
| 253 | - * |
|
| 254 | - * @param string $value WordLift's language code. |
|
| 255 | - */ |
|
| 256 | - public function set_language_code( $value ) { |
|
| 257 | - |
|
| 258 | - $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 259 | - |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Set the user preferences about sharing diagnostic with us. |
|
| 264 | - * |
|
| 265 | - * @since 3.19.0 |
|
| 266 | - * |
|
| 267 | - * @param string $value The user preferences(yes/no). |
|
| 268 | - */ |
|
| 269 | - public function set_diagnostic_preferences( $value ) { |
|
| 270 | - |
|
| 271 | - $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 272 | - |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Get the user preferences about sharing diagnostic. |
|
| 277 | - * |
|
| 278 | - * @since 3.19.0 |
|
| 279 | - */ |
|
| 280 | - public function get_diagnostic_preferences() { |
|
| 281 | - |
|
| 282 | - return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 283 | - |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Get the publisher entity post id. |
|
| 288 | - * |
|
| 289 | - * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
|
| 290 | - * Article markup. |
|
| 291 | - * |
|
| 292 | - * @since 3.9.0 |
|
| 293 | - * |
|
| 294 | - * @return int|NULL The publisher entity post id or NULL if not set. |
|
| 295 | - */ |
|
| 296 | - public function get_publisher_id() { |
|
| 297 | - |
|
| 298 | - return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Set the publisher entity post id. |
|
| 303 | - * |
|
| 304 | - * @since 3.9.0 |
|
| 305 | - * |
|
| 306 | - * @param int $value The publisher entity post id. |
|
| 307 | - */ |
|
| 308 | - public function set_publisher_id( $value ) { |
|
| 309 | - |
|
| 310 | - $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 311 | - |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Get the dataset URI. |
|
| 316 | - * |
|
| 317 | - * @since 3.10.0 |
|
| 318 | - * |
|
| 319 | - * @return string The dataset URI or an empty string if not set. |
|
| 320 | - */ |
|
| 321 | - public function get_dataset_uri() { |
|
| 322 | - |
|
| 323 | - return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Set the dataset URI. |
|
| 328 | - * |
|
| 329 | - * @since 3.10.0 |
|
| 330 | - * |
|
| 331 | - * @param string $value The dataset URI. |
|
| 332 | - */ |
|
| 333 | - public function set_dataset_uri( $value ) { |
|
| 334 | - |
|
| 335 | - $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Intercept the change of the WordLift key in order to set the dataset URI. |
|
| 340 | - * |
|
| 341 | - * @since 3.11.0 |
|
| 342 | - * |
|
| 343 | - * @param array $old_value The old settings. |
|
| 344 | - * @param array $new_value The new settings. |
|
| 345 | - */ |
|
| 346 | - public function update_key( $old_value, $new_value ) { |
|
| 347 | - |
|
| 348 | - // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
|
| 349 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 350 | - $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 351 | - |
|
| 352 | - // If the key hasn't changed, don't do anything. |
|
| 353 | - // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
|
| 354 | - if ( $old_key === $new_key ) { |
|
| 355 | - return; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - // If the key is empty, empty the dataset URI. |
|
| 359 | - if ( '' === $new_key ) { |
|
| 360 | - $this->set_dataset_uri( '' ); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // make the request to the remote server. |
|
| 364 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Handle retrieving the dataset uri from the remote server. |
|
| 369 | - * |
|
| 370 | - * If a valid dataset uri is returned it is stored in the appropriate option, |
|
| 371 | - * otherwise the option is set to empty string. |
|
| 372 | - * |
|
| 373 | - * @since 3.17.0 send the site URL and get the dataset URI. |
|
| 374 | - * @since 3.12.0 |
|
| 375 | - * |
|
| 376 | - * @param string $key The key to be used. |
|
| 377 | - */ |
|
| 378 | - public function get_remote_dataset_uri( $key ) { |
|
| 379 | - |
|
| 380 | - $this->log->trace( 'Getting the remote dataset URI...' ); |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Allow 3rd parties to change the site_url. |
|
| 384 | - * |
|
| 385 | - * @since 3.20.0 |
|
| 386 | - * |
|
| 387 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 388 | - * |
|
| 389 | - * @param string $site_url The site url. |
|
| 390 | - */ |
|
| 391 | - $site_url = apply_filters( 'wl_production_site_url', site_url() ); |
|
| 392 | - |
|
| 393 | - // Build the URL. |
|
| 394 | - $url = $this->get_accounts() |
|
| 395 | - . '?key=' . rawurlencode( $key ) |
|
| 396 | - . '&url=' . rawurlencode( $site_url ); |
|
| 397 | - |
|
| 398 | - $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 399 | - 'method' => 'PUT', |
|
| 400 | - ) ); |
|
| 401 | - $response = wp_remote_request( $url, $args ); |
|
| 402 | - |
|
| 403 | - // The response is an error. |
|
| 404 | - if ( is_wp_error( $response ) ) { |
|
| 405 | - $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 406 | - |
|
| 407 | - $this->set_dataset_uri( '' ); |
|
| 408 | - |
|
| 409 | - return; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - // The response is not OK. |
|
| 413 | - if ( 200 !== (int) $response['response']['code'] ) { |
|
| 414 | - $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 415 | - |
|
| 416 | - $this->set_dataset_uri( '' ); |
|
| 417 | - |
|
| 418 | - return; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - $json = json_decode( $response['body'] ); |
|
| 422 | - $dataset_uri = $json->datasetURI; |
|
| 423 | - |
|
| 424 | - $this->log->info( "Setting the dataset URI to $dataset_uri..." ); |
|
| 425 | - |
|
| 426 | - $this->set_dataset_uri( $dataset_uri ); |
|
| 427 | - |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Handle the edge case where a user submits the same key again |
|
| 432 | - * when he does not have the dataset uri to regain it. |
|
| 433 | - * |
|
| 434 | - * This can not be handled in the normal option update hook because |
|
| 435 | - * it is not being triggered when the save value equals to the one already |
|
| 436 | - * in the DB. |
|
| 437 | - * |
|
| 438 | - * @since 3.12.0 |
|
| 439 | - * |
|
| 440 | - * @param mixed $value The new, unserialized option value. |
|
| 441 | - * @param mixed $old_value The old option value. |
|
| 442 | - * |
|
| 443 | - * @return mixed The same value in the $value parameter |
|
| 444 | - */ |
|
| 445 | - function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 446 | - |
|
| 447 | - // Check the old key value and the new one. Here we're only handling the |
|
| 448 | - // case where the key hasn't changed and the dataset URI isn't set. The |
|
| 449 | - // other case, i.e. a new key is inserted, is handled at `update_key`. |
|
| 450 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 451 | - $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 452 | - |
|
| 453 | - $dataset_uri = $this->get_dataset_uri(); |
|
| 454 | - |
|
| 455 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 456 | - |
|
| 457 | - // make the request to the remote server to try to get the dataset uri. |
|
| 458 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - return $value; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
| 466 | - * |
|
| 467 | - * @since 3.11.0 |
|
| 468 | - * |
|
| 469 | - * @param string $key The WordLift key to use. |
|
| 470 | - * |
|
| 471 | - * @return string The API URI. |
|
| 472 | - */ |
|
| 473 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 474 | - |
|
| 475 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Get the `accounts` end point. |
|
| 480 | - * |
|
| 481 | - * @since 3.16.0 |
|
| 482 | - * |
|
| 483 | - * @return string The `accounts` end point. |
|
| 484 | - */ |
|
| 485 | - public function get_accounts() { |
|
| 486 | - |
|
| 487 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Get the `link by default` option. |
|
| 492 | - * |
|
| 493 | - * @since 3.13.0 |
|
| 494 | - * |
|
| 495 | - * @return bool True if entities must be linked by default otherwise false. |
|
| 496 | - */ |
|
| 497 | - public function is_link_by_default() { |
|
| 498 | - |
|
| 499 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * Set the `link by default` option. |
|
| 504 | - * |
|
| 505 | - * @since 3.13.0 |
|
| 506 | - * |
|
| 507 | - * @param bool $value True to enabling linking by default, otherwise false. |
|
| 508 | - */ |
|
| 509 | - public function set_link_by_default( $value ) { |
|
| 510 | - |
|
| 511 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Get the URL to perform batch analyses. |
|
| 516 | - * |
|
| 517 | - * @since 3.14.0 |
|
| 518 | - * |
|
| 519 | - * @return string The URL to call to perform the batch analyzes. |
|
| 520 | - */ |
|
| 521 | - public function get_batch_analysis_url() { |
|
| 522 | - |
|
| 523 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'batch-analyses'; |
|
| 524 | - |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Get the URL to perform autocomplete request. |
|
| 529 | - * |
|
| 530 | - * @since 3.15.0 |
|
| 531 | - * |
|
| 532 | - * @return string The URL to call to perform the batch analyzes. |
|
| 533 | - */ |
|
| 534 | - public function get_autocomplete_url() { |
|
| 535 | - |
|
| 536 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 537 | - |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * Get the URL to perform feedback deactivation request. |
|
| 542 | - * |
|
| 543 | - * @since 3.19.0 |
|
| 544 | - * |
|
| 545 | - * @return string The URL to call to perform the feedback deactivation request. |
|
| 546 | - */ |
|
| 547 | - public function get_deactivation_feedback_url() { |
|
| 548 | - |
|
| 549 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 550 | - |
|
| 551 | - } |
|
| 24 | + /** |
|
| 25 | + * The entity base path option name. |
|
| 26 | + * |
|
| 27 | + * @since 3.6.0 |
|
| 28 | + */ |
|
| 29 | + const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The skip wizard (admin installation wizard) option name. |
|
| 33 | + * |
|
| 34 | + * @since 3.9.0 |
|
| 35 | + */ |
|
| 36 | + const SKIP_WIZARD = 'wl_skip_wizard'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * WordLift's key option name. |
|
| 40 | + * |
|
| 41 | + * @since 3.9.0 |
|
| 42 | + */ |
|
| 43 | + const KEY = 'key'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * WordLift's configured language option name. |
|
| 47 | + * |
|
| 48 | + * @since 3.9.0 |
|
| 49 | + */ |
|
| 50 | + const LANGUAGE = 'site_language'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The publisher entity post ID option name. |
|
| 54 | + * |
|
| 55 | + * @since 3.9.0 |
|
| 56 | + */ |
|
| 57 | + const PUBLISHER_ID = 'publisher_id'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The dataset URI option name |
|
| 61 | + * |
|
| 62 | + * @since 3.10.0 |
|
| 63 | + */ |
|
| 64 | + const DATASET_URI = 'redlink_dataset_uri'; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The link by default option name. |
|
| 68 | + * |
|
| 69 | + * @since 3.11.0 |
|
| 70 | + */ |
|
| 71 | + const LINK_BY_DEFAULT = 'link_by_default'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The user preferences about sharing data option. |
|
| 75 | + * |
|
| 76 | + * @since 3.19.0 |
|
| 77 | + */ |
|
| 78 | + const SEND_DIAGNOSTIC = 'send_diagnostic'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * The {@link Wordlift_Log_Service} instance. |
|
| 82 | + * |
|
| 83 | + * @since 3.16.0 |
|
| 84 | + * |
|
| 85 | + * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
|
| 86 | + */ |
|
| 87 | + private $log; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * The Wordlift_Configuration_Service's singleton instance. |
|
| 91 | + * |
|
| 92 | + * @since 3.6.0 |
|
| 93 | + * |
|
| 94 | + * @access private |
|
| 95 | + * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
|
| 96 | + */ |
|
| 97 | + private static $instance; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Create a Wordlift_Configuration_Service's instance. |
|
| 101 | + * |
|
| 102 | + * @since 3.6.0 |
|
| 103 | + */ |
|
| 104 | + public function __construct() { |
|
| 105 | + |
|
| 106 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 107 | + |
|
| 108 | + self::$instance = $this; |
|
| 109 | + |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Get the singleton instance. |
|
| 114 | + * |
|
| 115 | + * @since 3.6.0 |
|
| 116 | + * |
|
| 117 | + * @return \Wordlift_Configuration_Service |
|
| 118 | + */ |
|
| 119 | + public static function get_instance() { |
|
| 120 | + |
|
| 121 | + return self::$instance; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get a configuration given the option name and a key. The option value is |
|
| 126 | + * expected to be an array. |
|
| 127 | + * |
|
| 128 | + * @since 3.6.0 |
|
| 129 | + * |
|
| 130 | + * @param string $option The option name. |
|
| 131 | + * @param string $key A key in the option value array. |
|
| 132 | + * @param string $default The default value in case the key is not found (by default an empty string). |
|
| 133 | + * |
|
| 134 | + * @return mixed The configuration value or the default value if not found. |
|
| 135 | + */ |
|
| 136 | + private function get( $option, $key, $default = '' ) { |
|
| 137 | + |
|
| 138 | + $options = get_option( $option, array() ); |
|
| 139 | + |
|
| 140 | + return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Set a configuration parameter. |
|
| 145 | + * |
|
| 146 | + * @since 3.9.0 |
|
| 147 | + * |
|
| 148 | + * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
|
| 149 | + * @param string $key The value key. |
|
| 150 | + * @param mixed $value The value. |
|
| 151 | + */ |
|
| 152 | + private function set( $option, $key, $value ) { |
|
| 153 | + |
|
| 154 | + $values = get_option( $option ); |
|
| 155 | + $values = isset( $values ) ? $values : array(); |
|
| 156 | + $values[ $key ] = $value; |
|
| 157 | + update_option( $option, $values ); |
|
| 158 | + |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Get the entity base path, by default 'entity'. |
|
| 163 | + * |
|
| 164 | + * @since 3.6.0 |
|
| 165 | + * |
|
| 166 | + * @return string The entity base path. |
|
| 167 | + */ |
|
| 168 | + public function get_entity_base_path() { |
|
| 169 | + |
|
| 170 | + return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Get the entity base path. |
|
| 175 | + * |
|
| 176 | + * @since 3.9.0 |
|
| 177 | + * |
|
| 178 | + * @param string $value The entity base path. |
|
| 179 | + */ |
|
| 180 | + public function set_entity_base_path( $value ) { |
|
| 181 | + |
|
| 182 | + $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 183 | + |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Whether the installation skip wizard should be skipped. |
|
| 188 | + * |
|
| 189 | + * @since 3.9.0 |
|
| 190 | + * |
|
| 191 | + * @return bool True if it should be skipped otherwise false. |
|
| 192 | + */ |
|
| 193 | + public function is_skip_wizard() { |
|
| 194 | + |
|
| 195 | + return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Set the skip wizard parameter. |
|
| 200 | + * |
|
| 201 | + * @since 3.9.0 |
|
| 202 | + * |
|
| 203 | + * @param bool $value True to skip the wizard. We expect a boolean value. |
|
| 204 | + */ |
|
| 205 | + public function set_skip_wizard( $value ) { |
|
| 206 | + |
|
| 207 | + $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 208 | + |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Get WordLift's key. |
|
| 213 | + * |
|
| 214 | + * @since 3.9.0 |
|
| 215 | + * |
|
| 216 | + * @return string WordLift's key or an empty string if not set. |
|
| 217 | + */ |
|
| 218 | + public function get_key() { |
|
| 219 | + |
|
| 220 | + return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Set WordLift's key. |
|
| 225 | + * |
|
| 226 | + * @since 3.9.0 |
|
| 227 | + * |
|
| 228 | + * @param string $value WordLift's key. |
|
| 229 | + */ |
|
| 230 | + public function set_key( $value ) { |
|
| 231 | + |
|
| 232 | + $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Get WordLift's configured language, by default 'en'. |
|
| 237 | + * |
|
| 238 | + * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
|
| 239 | + * |
|
| 240 | + * @since 3.9.0 |
|
| 241 | + * |
|
| 242 | + * @return string WordLift's configured language code ('en' by default). |
|
| 243 | + */ |
|
| 244 | + public function get_language_code() { |
|
| 245 | + |
|
| 246 | + return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
|
| 251 | + * |
|
| 252 | + * @since 3.9.0 |
|
| 253 | + * |
|
| 254 | + * @param string $value WordLift's language code. |
|
| 255 | + */ |
|
| 256 | + public function set_language_code( $value ) { |
|
| 257 | + |
|
| 258 | + $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 259 | + |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Set the user preferences about sharing diagnostic with us. |
|
| 264 | + * |
|
| 265 | + * @since 3.19.0 |
|
| 266 | + * |
|
| 267 | + * @param string $value The user preferences(yes/no). |
|
| 268 | + */ |
|
| 269 | + public function set_diagnostic_preferences( $value ) { |
|
| 270 | + |
|
| 271 | + $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 272 | + |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Get the user preferences about sharing diagnostic. |
|
| 277 | + * |
|
| 278 | + * @since 3.19.0 |
|
| 279 | + */ |
|
| 280 | + public function get_diagnostic_preferences() { |
|
| 281 | + |
|
| 282 | + return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 283 | + |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Get the publisher entity post id. |
|
| 288 | + * |
|
| 289 | + * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
|
| 290 | + * Article markup. |
|
| 291 | + * |
|
| 292 | + * @since 3.9.0 |
|
| 293 | + * |
|
| 294 | + * @return int|NULL The publisher entity post id or NULL if not set. |
|
| 295 | + */ |
|
| 296 | + public function get_publisher_id() { |
|
| 297 | + |
|
| 298 | + return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Set the publisher entity post id. |
|
| 303 | + * |
|
| 304 | + * @since 3.9.0 |
|
| 305 | + * |
|
| 306 | + * @param int $value The publisher entity post id. |
|
| 307 | + */ |
|
| 308 | + public function set_publisher_id( $value ) { |
|
| 309 | + |
|
| 310 | + $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 311 | + |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Get the dataset URI. |
|
| 316 | + * |
|
| 317 | + * @since 3.10.0 |
|
| 318 | + * |
|
| 319 | + * @return string The dataset URI or an empty string if not set. |
|
| 320 | + */ |
|
| 321 | + public function get_dataset_uri() { |
|
| 322 | + |
|
| 323 | + return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Set the dataset URI. |
|
| 328 | + * |
|
| 329 | + * @since 3.10.0 |
|
| 330 | + * |
|
| 331 | + * @param string $value The dataset URI. |
|
| 332 | + */ |
|
| 333 | + public function set_dataset_uri( $value ) { |
|
| 334 | + |
|
| 335 | + $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Intercept the change of the WordLift key in order to set the dataset URI. |
|
| 340 | + * |
|
| 341 | + * @since 3.11.0 |
|
| 342 | + * |
|
| 343 | + * @param array $old_value The old settings. |
|
| 344 | + * @param array $new_value The new settings. |
|
| 345 | + */ |
|
| 346 | + public function update_key( $old_value, $new_value ) { |
|
| 347 | + |
|
| 348 | + // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
|
| 349 | + $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 350 | + $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 351 | + |
|
| 352 | + // If the key hasn't changed, don't do anything. |
|
| 353 | + // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
|
| 354 | + if ( $old_key === $new_key ) { |
|
| 355 | + return; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + // If the key is empty, empty the dataset URI. |
|
| 359 | + if ( '' === $new_key ) { |
|
| 360 | + $this->set_dataset_uri( '' ); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // make the request to the remote server. |
|
| 364 | + $this->get_remote_dataset_uri( $new_key ); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Handle retrieving the dataset uri from the remote server. |
|
| 369 | + * |
|
| 370 | + * If a valid dataset uri is returned it is stored in the appropriate option, |
|
| 371 | + * otherwise the option is set to empty string. |
|
| 372 | + * |
|
| 373 | + * @since 3.17.0 send the site URL and get the dataset URI. |
|
| 374 | + * @since 3.12.0 |
|
| 375 | + * |
|
| 376 | + * @param string $key The key to be used. |
|
| 377 | + */ |
|
| 378 | + public function get_remote_dataset_uri( $key ) { |
|
| 379 | + |
|
| 380 | + $this->log->trace( 'Getting the remote dataset URI...' ); |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Allow 3rd parties to change the site_url. |
|
| 384 | + * |
|
| 385 | + * @since 3.20.0 |
|
| 386 | + * |
|
| 387 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 388 | + * |
|
| 389 | + * @param string $site_url The site url. |
|
| 390 | + */ |
|
| 391 | + $site_url = apply_filters( 'wl_production_site_url', site_url() ); |
|
| 392 | + |
|
| 393 | + // Build the URL. |
|
| 394 | + $url = $this->get_accounts() |
|
| 395 | + . '?key=' . rawurlencode( $key ) |
|
| 396 | + . '&url=' . rawurlencode( $site_url ); |
|
| 397 | + |
|
| 398 | + $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 399 | + 'method' => 'PUT', |
|
| 400 | + ) ); |
|
| 401 | + $response = wp_remote_request( $url, $args ); |
|
| 402 | + |
|
| 403 | + // The response is an error. |
|
| 404 | + if ( is_wp_error( $response ) ) { |
|
| 405 | + $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 406 | + |
|
| 407 | + $this->set_dataset_uri( '' ); |
|
| 408 | + |
|
| 409 | + return; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + // The response is not OK. |
|
| 413 | + if ( 200 !== (int) $response['response']['code'] ) { |
|
| 414 | + $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 415 | + |
|
| 416 | + $this->set_dataset_uri( '' ); |
|
| 417 | + |
|
| 418 | + return; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + $json = json_decode( $response['body'] ); |
|
| 422 | + $dataset_uri = $json->datasetURI; |
|
| 423 | + |
|
| 424 | + $this->log->info( "Setting the dataset URI to $dataset_uri..." ); |
|
| 425 | + |
|
| 426 | + $this->set_dataset_uri( $dataset_uri ); |
|
| 427 | + |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Handle the edge case where a user submits the same key again |
|
| 432 | + * when he does not have the dataset uri to regain it. |
|
| 433 | + * |
|
| 434 | + * This can not be handled in the normal option update hook because |
|
| 435 | + * it is not being triggered when the save value equals to the one already |
|
| 436 | + * in the DB. |
|
| 437 | + * |
|
| 438 | + * @since 3.12.0 |
|
| 439 | + * |
|
| 440 | + * @param mixed $value The new, unserialized option value. |
|
| 441 | + * @param mixed $old_value The old option value. |
|
| 442 | + * |
|
| 443 | + * @return mixed The same value in the $value parameter |
|
| 444 | + */ |
|
| 445 | + function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 446 | + |
|
| 447 | + // Check the old key value and the new one. Here we're only handling the |
|
| 448 | + // case where the key hasn't changed and the dataset URI isn't set. The |
|
| 449 | + // other case, i.e. a new key is inserted, is handled at `update_key`. |
|
| 450 | + $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 451 | + $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 452 | + |
|
| 453 | + $dataset_uri = $this->get_dataset_uri(); |
|
| 454 | + |
|
| 455 | + if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 456 | + |
|
| 457 | + // make the request to the remote server to try to get the dataset uri. |
|
| 458 | + $this->get_remote_dataset_uri( $new_key ); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + return $value; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Get the API URI to retrieve the dataset URI using the WordLift Key. |
|
| 466 | + * |
|
| 467 | + * @since 3.11.0 |
|
| 468 | + * |
|
| 469 | + * @param string $key The WordLift key to use. |
|
| 470 | + * |
|
| 471 | + * @return string The API URI. |
|
| 472 | + */ |
|
| 473 | + public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 474 | + |
|
| 475 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Get the `accounts` end point. |
|
| 480 | + * |
|
| 481 | + * @since 3.16.0 |
|
| 482 | + * |
|
| 483 | + * @return string The `accounts` end point. |
|
| 484 | + */ |
|
| 485 | + public function get_accounts() { |
|
| 486 | + |
|
| 487 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Get the `link by default` option. |
|
| 492 | + * |
|
| 493 | + * @since 3.13.0 |
|
| 494 | + * |
|
| 495 | + * @return bool True if entities must be linked by default otherwise false. |
|
| 496 | + */ |
|
| 497 | + public function is_link_by_default() { |
|
| 498 | + |
|
| 499 | + return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * Set the `link by default` option. |
|
| 504 | + * |
|
| 505 | + * @since 3.13.0 |
|
| 506 | + * |
|
| 507 | + * @param bool $value True to enabling linking by default, otherwise false. |
|
| 508 | + */ |
|
| 509 | + public function set_link_by_default( $value ) { |
|
| 510 | + |
|
| 511 | + $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Get the URL to perform batch analyses. |
|
| 516 | + * |
|
| 517 | + * @since 3.14.0 |
|
| 518 | + * |
|
| 519 | + * @return string The URL to call to perform the batch analyzes. |
|
| 520 | + */ |
|
| 521 | + public function get_batch_analysis_url() { |
|
| 522 | + |
|
| 523 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'batch-analyses'; |
|
| 524 | + |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Get the URL to perform autocomplete request. |
|
| 529 | + * |
|
| 530 | + * @since 3.15.0 |
|
| 531 | + * |
|
| 532 | + * @return string The URL to call to perform the batch analyzes. |
|
| 533 | + */ |
|
| 534 | + public function get_autocomplete_url() { |
|
| 535 | + |
|
| 536 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 537 | + |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * Get the URL to perform feedback deactivation request. |
|
| 542 | + * |
|
| 543 | + * @since 3.19.0 |
|
| 544 | + * |
|
| 545 | + * @return string The URL to call to perform the feedback deactivation request. |
|
| 546 | + */ |
|
| 547 | + public function get_deactivation_feedback_url() { |
|
| 548 | + |
|
| 549 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 550 | + |
|
| 551 | + } |
|
| 552 | 552 | |
| 553 | 553 | } |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * @since 3.6.0 |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 13 | +if ( ! defined('ABSPATH')) { |
|
| 14 | 14 | exit; |
| 15 | 15 | } |
| 16 | 16 | |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | public function __construct() { |
| 105 | 105 | |
| 106 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 106 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 107 | 107 | |
| 108 | 108 | self::$instance = $this; |
| 109 | 109 | |
@@ -133,11 +133,11 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @return mixed The configuration value or the default value if not found. |
| 135 | 135 | */ |
| 136 | - private function get( $option, $key, $default = '' ) { |
|
| 136 | + private function get($option, $key, $default = '') { |
|
| 137 | 137 | |
| 138 | - $options = get_option( $option, array() ); |
|
| 138 | + $options = get_option($option, array()); |
|
| 139 | 139 | |
| 140 | - return isset( $options[ $key ] ) ? $options[ $key ] : $default; |
|
| 140 | + return isset($options[$key]) ? $options[$key] : $default; |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
@@ -149,12 +149,12 @@ discard block |
||
| 149 | 149 | * @param string $key The value key. |
| 150 | 150 | * @param mixed $value The value. |
| 151 | 151 | */ |
| 152 | - private function set( $option, $key, $value ) { |
|
| 152 | + private function set($option, $key, $value) { |
|
| 153 | 153 | |
| 154 | - $values = get_option( $option ); |
|
| 155 | - $values = isset( $values ) ? $values : array(); |
|
| 156 | - $values[ $key ] = $value; |
|
| 157 | - update_option( $option, $values ); |
|
| 154 | + $values = get_option($option); |
|
| 155 | + $values = isset($values) ? $values : array(); |
|
| 156 | + $values[$key] = $value; |
|
| 157 | + update_option($option, $values); |
|
| 158 | 158 | |
| 159 | 159 | } |
| 160 | 160 | |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | */ |
| 168 | 168 | public function get_entity_base_path() { |
| 169 | 169 | |
| 170 | - return $this->get( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity' ); |
|
| 170 | + return $this->get('wl_general_settings', self::ENTITY_BASE_PATH_KEY, 'entity'); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -177,9 +177,9 @@ discard block |
||
| 177 | 177 | * |
| 178 | 178 | * @param string $value The entity base path. |
| 179 | 179 | */ |
| 180 | - public function set_entity_base_path( $value ) { |
|
| 180 | + public function set_entity_base_path($value) { |
|
| 181 | 181 | |
| 182 | - $this->set( 'wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value ); |
|
| 182 | + $this->set('wl_general_settings', self::ENTITY_BASE_PATH_KEY, $value); |
|
| 183 | 183 | |
| 184 | 184 | } |
| 185 | 185 | |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | */ |
| 193 | 193 | public function is_skip_wizard() { |
| 194 | 194 | |
| 195 | - return $this->get( 'wl_general_settings', self::SKIP_WIZARD, false ); |
|
| 195 | + return $this->get('wl_general_settings', self::SKIP_WIZARD, false); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -202,9 +202,9 @@ discard block |
||
| 202 | 202 | * |
| 203 | 203 | * @param bool $value True to skip the wizard. We expect a boolean value. |
| 204 | 204 | */ |
| 205 | - public function set_skip_wizard( $value ) { |
|
| 205 | + public function set_skip_wizard($value) { |
|
| 206 | 206 | |
| 207 | - $this->set( 'wl_general_settings', self::SKIP_WIZARD, true === $value ); |
|
| 207 | + $this->set('wl_general_settings', self::SKIP_WIZARD, true === $value); |
|
| 208 | 208 | |
| 209 | 209 | } |
| 210 | 210 | |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | */ |
| 218 | 218 | public function get_key() { |
| 219 | 219 | |
| 220 | - return $this->get( 'wl_general_settings', self::KEY, '' ); |
|
| 220 | + return $this->get('wl_general_settings', self::KEY, ''); |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /** |
@@ -227,9 +227,9 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @param string $value WordLift's key. |
| 229 | 229 | */ |
| 230 | - public function set_key( $value ) { |
|
| 230 | + public function set_key($value) { |
|
| 231 | 231 | |
| 232 | - $this->set( 'wl_general_settings', self::KEY, $value ); |
|
| 232 | + $this->set('wl_general_settings', self::KEY, $value); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | /** |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | */ |
| 244 | 244 | public function get_language_code() { |
| 245 | 245 | |
| 246 | - return $this->get( 'wl_general_settings', self::LANGUAGE, 'en' ); |
|
| 246 | + return $this->get('wl_general_settings', self::LANGUAGE, 'en'); |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | /** |
@@ -253,9 +253,9 @@ discard block |
||
| 253 | 253 | * |
| 254 | 254 | * @param string $value WordLift's language code. |
| 255 | 255 | */ |
| 256 | - public function set_language_code( $value ) { |
|
| 256 | + public function set_language_code($value) { |
|
| 257 | 257 | |
| 258 | - $this->set( 'wl_general_settings', self::LANGUAGE, $value ); |
|
| 258 | + $this->set('wl_general_settings', self::LANGUAGE, $value); |
|
| 259 | 259 | |
| 260 | 260 | } |
| 261 | 261 | |
@@ -266,9 +266,9 @@ discard block |
||
| 266 | 266 | * |
| 267 | 267 | * @param string $value The user preferences(yes/no). |
| 268 | 268 | */ |
| 269 | - public function set_diagnostic_preferences( $value ) { |
|
| 269 | + public function set_diagnostic_preferences($value) { |
|
| 270 | 270 | |
| 271 | - $this->set( 'wl_general_settings', self::SEND_DIAGNOSTIC, $value ); |
|
| 271 | + $this->set('wl_general_settings', self::SEND_DIAGNOSTIC, $value); |
|
| 272 | 272 | |
| 273 | 273 | } |
| 274 | 274 | |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | */ |
| 280 | 280 | public function get_diagnostic_preferences() { |
| 281 | 281 | |
| 282 | - return $this->get( 'wl_general_settings', self::SEND_DIAGNOSTIC, 'no' ); |
|
| 282 | + return $this->get('wl_general_settings', self::SEND_DIAGNOSTIC, 'no'); |
|
| 283 | 283 | |
| 284 | 284 | } |
| 285 | 285 | |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | */ |
| 296 | 296 | public function get_publisher_id() { |
| 297 | 297 | |
| 298 | - return $this->get( 'wl_general_settings', self::PUBLISHER_ID, null ); |
|
| 298 | + return $this->get('wl_general_settings', self::PUBLISHER_ID, null); |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | /** |
@@ -305,9 +305,9 @@ discard block |
||
| 305 | 305 | * |
| 306 | 306 | * @param int $value The publisher entity post id. |
| 307 | 307 | */ |
| 308 | - public function set_publisher_id( $value ) { |
|
| 308 | + public function set_publisher_id($value) { |
|
| 309 | 309 | |
| 310 | - $this->set( 'wl_general_settings', self::PUBLISHER_ID, $value ); |
|
| 310 | + $this->set('wl_general_settings', self::PUBLISHER_ID, $value); |
|
| 311 | 311 | |
| 312 | 312 | } |
| 313 | 313 | |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | */ |
| 321 | 321 | public function get_dataset_uri() { |
| 322 | 322 | |
| 323 | - return $this->get( 'wl_advanced_settings', self::DATASET_URI, null ); |
|
| 323 | + return $this->get('wl_advanced_settings', self::DATASET_URI, null); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | /** |
@@ -330,9 +330,9 @@ discard block |
||
| 330 | 330 | * |
| 331 | 331 | * @param string $value The dataset URI. |
| 332 | 332 | */ |
| 333 | - public function set_dataset_uri( $value ) { |
|
| 333 | + public function set_dataset_uri($value) { |
|
| 334 | 334 | |
| 335 | - $this->set( 'wl_advanced_settings', self::DATASET_URI, $value ); |
|
| 335 | + $this->set('wl_advanced_settings', self::DATASET_URI, $value); |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | /** |
@@ -343,25 +343,25 @@ discard block |
||
| 343 | 343 | * @param array $old_value The old settings. |
| 344 | 344 | * @param array $new_value The new settings. |
| 345 | 345 | */ |
| 346 | - public function update_key( $old_value, $new_value ) { |
|
| 346 | + public function update_key($old_value, $new_value) { |
|
| 347 | 347 | |
| 348 | 348 | // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed. |
| 349 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 350 | - $new_key = isset( $new_value['key'] ) ? $new_value['key'] : ''; |
|
| 349 | + $old_key = isset($old_value['key']) ? $old_value['key'] : ''; |
|
| 350 | + $new_key = isset($new_value['key']) ? $new_value['key'] : ''; |
|
| 351 | 351 | |
| 352 | 352 | // If the key hasn't changed, don't do anything. |
| 353 | 353 | // WARN The 'update_option' hook is fired only if the new and old value are not equal. |
| 354 | - if ( $old_key === $new_key ) { |
|
| 354 | + if ($old_key === $new_key) { |
|
| 355 | 355 | return; |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | // If the key is empty, empty the dataset URI. |
| 359 | - if ( '' === $new_key ) { |
|
| 360 | - $this->set_dataset_uri( '' ); |
|
| 359 | + if ('' === $new_key) { |
|
| 360 | + $this->set_dataset_uri(''); |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | // make the request to the remote server. |
| 364 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 364 | + $this->get_remote_dataset_uri($new_key); |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | /** |
@@ -375,9 +375,9 @@ discard block |
||
| 375 | 375 | * |
| 376 | 376 | * @param string $key The key to be used. |
| 377 | 377 | */ |
| 378 | - public function get_remote_dataset_uri( $key ) { |
|
| 378 | + public function get_remote_dataset_uri($key) { |
|
| 379 | 379 | |
| 380 | - $this->log->trace( 'Getting the remote dataset URI...' ); |
|
| 380 | + $this->log->trace('Getting the remote dataset URI...'); |
|
| 381 | 381 | |
| 382 | 382 | /** |
| 383 | 383 | * Allow 3rd parties to change the site_url. |
@@ -388,42 +388,42 @@ discard block |
||
| 388 | 388 | * |
| 389 | 389 | * @param string $site_url The site url. |
| 390 | 390 | */ |
| 391 | - $site_url = apply_filters( 'wl_production_site_url', site_url() ); |
|
| 391 | + $site_url = apply_filters('wl_production_site_url', site_url()); |
|
| 392 | 392 | |
| 393 | 393 | // Build the URL. |
| 394 | 394 | $url = $this->get_accounts() |
| 395 | - . '?key=' . rawurlencode( $key ) |
|
| 396 | - . '&url=' . rawurlencode( $site_url ); |
|
| 395 | + . '?key='.rawurlencode($key) |
|
| 396 | + . '&url='.rawurlencode($site_url); |
|
| 397 | 397 | |
| 398 | - $args = wp_parse_args( unserialize( WL_REDLINK_API_HTTP_OPTIONS ), array( |
|
| 398 | + $args = wp_parse_args(unserialize(WL_REDLINK_API_HTTP_OPTIONS), array( |
|
| 399 | 399 | 'method' => 'PUT', |
| 400 | - ) ); |
|
| 401 | - $response = wp_remote_request( $url, $args ); |
|
| 400 | + )); |
|
| 401 | + $response = wp_remote_request($url, $args); |
|
| 402 | 402 | |
| 403 | 403 | // The response is an error. |
| 404 | - if ( is_wp_error( $response ) ) { |
|
| 405 | - $this->log->error( 'An error occurred setting the dataset URI: ' . $response->get_error_message() ); |
|
| 404 | + if (is_wp_error($response)) { |
|
| 405 | + $this->log->error('An error occurred setting the dataset URI: '.$response->get_error_message()); |
|
| 406 | 406 | |
| 407 | - $this->set_dataset_uri( '' ); |
|
| 407 | + $this->set_dataset_uri(''); |
|
| 408 | 408 | |
| 409 | 409 | return; |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | // The response is not OK. |
| 413 | - if ( 200 !== (int) $response['response']['code'] ) { |
|
| 414 | - $this->log->error( "Unexpected status code when opening URL $url: " . $response['response']['code'] ); |
|
| 413 | + if (200 !== (int) $response['response']['code']) { |
|
| 414 | + $this->log->error("Unexpected status code when opening URL $url: ".$response['response']['code']); |
|
| 415 | 415 | |
| 416 | - $this->set_dataset_uri( '' ); |
|
| 416 | + $this->set_dataset_uri(''); |
|
| 417 | 417 | |
| 418 | 418 | return; |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | - $json = json_decode( $response['body'] ); |
|
| 421 | + $json = json_decode($response['body']); |
|
| 422 | 422 | $dataset_uri = $json->datasetURI; |
| 423 | 423 | |
| 424 | - $this->log->info( "Setting the dataset URI to $dataset_uri..." ); |
|
| 424 | + $this->log->info("Setting the dataset URI to $dataset_uri..."); |
|
| 425 | 425 | |
| 426 | - $this->set_dataset_uri( $dataset_uri ); |
|
| 426 | + $this->set_dataset_uri($dataset_uri); |
|
| 427 | 427 | |
| 428 | 428 | } |
| 429 | 429 | |
@@ -442,20 +442,20 @@ discard block |
||
| 442 | 442 | * |
| 443 | 443 | * @return mixed The same value in the $value parameter |
| 444 | 444 | */ |
| 445 | - function maybe_update_dataset_uri( $value, $old_value ) { |
|
| 445 | + function maybe_update_dataset_uri($value, $old_value) { |
|
| 446 | 446 | |
| 447 | 447 | // Check the old key value and the new one. Here we're only handling the |
| 448 | 448 | // case where the key hasn't changed and the dataset URI isn't set. The |
| 449 | 449 | // other case, i.e. a new key is inserted, is handled at `update_key`. |
| 450 | - $old_key = isset( $old_value['key'] ) ? $old_value['key'] : ''; |
|
| 451 | - $new_key = isset( $value['key'] ) ? $value['key'] : ''; |
|
| 450 | + $old_key = isset($old_value['key']) ? $old_value['key'] : ''; |
|
| 451 | + $new_key = isset($value['key']) ? $value['key'] : ''; |
|
| 452 | 452 | |
| 453 | 453 | $dataset_uri = $this->get_dataset_uri(); |
| 454 | 454 | |
| 455 | - if ( ! empty( $new_key ) && $new_key === $old_key && empty( $dataset_uri ) ) { |
|
| 455 | + if ( ! empty($new_key) && $new_key === $old_key && empty($dataset_uri)) { |
|
| 456 | 456 | |
| 457 | 457 | // make the request to the remote server to try to get the dataset uri. |
| 458 | - $this->get_remote_dataset_uri( $new_key ); |
|
| 458 | + $this->get_remote_dataset_uri($new_key); |
|
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | return $value; |
@@ -470,9 +470,9 @@ discard block |
||
| 470 | 470 | * |
| 471 | 471 | * @return string The API URI. |
| 472 | 472 | */ |
| 473 | - public function get_accounts_by_key_dataset_uri( $key ) { |
|
| 473 | + public function get_accounts_by_key_dataset_uri($key) { |
|
| 474 | 474 | |
| 475 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . "accounts/key=$key/dataset_uri"; |
|
| 475 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE."accounts/key=$key/dataset_uri"; |
|
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | /** |
@@ -484,7 +484,7 @@ discard block |
||
| 484 | 484 | */ |
| 485 | 485 | public function get_accounts() { |
| 486 | 486 | |
| 487 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'accounts'; |
|
| 487 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'accounts'; |
|
| 488 | 488 | } |
| 489 | 489 | |
| 490 | 490 | /** |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | */ |
| 497 | 497 | public function is_link_by_default() { |
| 498 | 498 | |
| 499 | - return 'yes' === $this->get( 'wl_general_settings', self::LINK_BY_DEFAULT, 'yes' ); |
|
| 499 | + return 'yes' === $this->get('wl_general_settings', self::LINK_BY_DEFAULT, 'yes'); |
|
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | /** |
@@ -506,9 +506,9 @@ discard block |
||
| 506 | 506 | * |
| 507 | 507 | * @param bool $value True to enabling linking by default, otherwise false. |
| 508 | 508 | */ |
| 509 | - public function set_link_by_default( $value ) { |
|
| 509 | + public function set_link_by_default($value) { |
|
| 510 | 510 | |
| 511 | - $this->set( 'wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no' ); |
|
| 511 | + $this->set('wl_general_settings', self::LINK_BY_DEFAULT, true === $value ? 'yes' : 'no'); |
|
| 512 | 512 | } |
| 513 | 513 | |
| 514 | 514 | /** |
@@ -520,7 +520,7 @@ discard block |
||
| 520 | 520 | */ |
| 521 | 521 | public function get_batch_analysis_url() { |
| 522 | 522 | |
| 523 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'batch-analyses'; |
|
| 523 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'batch-analyses'; |
|
| 524 | 524 | |
| 525 | 525 | } |
| 526 | 526 | |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | */ |
| 534 | 534 | public function get_autocomplete_url() { |
| 535 | 535 | |
| 536 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'autocomplete'; |
|
| 536 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'autocomplete'; |
|
| 537 | 537 | |
| 538 | 538 | } |
| 539 | 539 | |
@@ -546,7 +546,7 @@ discard block |
||
| 546 | 546 | */ |
| 547 | 547 | public function get_deactivation_feedback_url() { |
| 548 | 548 | |
| 549 | - return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . 'feedbacks'; |
|
| 549 | + return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE.'feedbacks'; |
|
| 550 | 550 | |
| 551 | 551 | } |
| 552 | 552 | |
@@ -16,68 +16,68 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Post_Adapter { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The post id to which the adopter relates. |
|
| 21 | - * |
|
| 22 | - * @since 3.14.0 |
|
| 23 | - * |
|
| 24 | - * @var integer $post_id . |
|
| 25 | - */ |
|
| 26 | - private $post_id; |
|
| 19 | + /** |
|
| 20 | + * The post id to which the adopter relates. |
|
| 21 | + * |
|
| 22 | + * @since 3.14.0 |
|
| 23 | + * |
|
| 24 | + * @var integer $post_id . |
|
| 25 | + */ |
|
| 26 | + private $post_id; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Create the {@link Wordlift_Post_Adatpter} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.14.0 |
|
| 32 | - * |
|
| 33 | - * @param integer $post_id the post ID of the post the adopter relates to. |
|
| 34 | - */ |
|
| 35 | - public function __construct( $post_id ) { |
|
| 28 | + /** |
|
| 29 | + * Create the {@link Wordlift_Post_Adatpter} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.14.0 |
|
| 32 | + * |
|
| 33 | + * @param integer $post_id the post ID of the post the adopter relates to. |
|
| 34 | + */ |
|
| 35 | + public function __construct( $post_id ) { |
|
| 36 | 36 | |
| 37 | - $this->post_id = $post_id; |
|
| 37 | + $this->post_id = $post_id; |
|
| 38 | 38 | |
| 39 | - } |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get the word count of the post content. |
|
| 43 | - * |
|
| 44 | - * The count is calculated over the post content after stripping shortcodes and html tags. |
|
| 45 | - * |
|
| 46 | - * @since 3.14.0 |
|
| 47 | - * |
|
| 48 | - * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
| 49 | - */ |
|
| 50 | - public function word_count() { |
|
| 41 | + /** |
|
| 42 | + * Get the word count of the post content. |
|
| 43 | + * |
|
| 44 | + * The count is calculated over the post content after stripping shortcodes and html tags. |
|
| 45 | + * |
|
| 46 | + * @since 3.14.0 |
|
| 47 | + * |
|
| 48 | + * @return integer the number of words in the content after stripping shortcodes and html tags.. |
|
| 49 | + */ |
|
| 50 | + public function word_count() { |
|
| 51 | 51 | |
| 52 | - $post = get_post( $this->post_id ); |
|
| 52 | + $post = get_post( $this->post_id ); |
|
| 53 | 53 | |
| 54 | - return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) ); |
|
| 55 | - } |
|
| 54 | + return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) ); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
| 59 | - * |
|
| 60 | - * @since 3.20.0 |
|
| 61 | - * |
|
| 62 | - * @param int $post_id Post ID. |
|
| 63 | - * |
|
| 64 | - * @return string The post permalink. |
|
| 65 | - */ |
|
| 66 | - public static function get_production_permalink( $post_id ) { |
|
| 57 | + /** |
|
| 58 | + * Get the {@link WP_Post} permalink allowing 3rd parties to alter the URL. |
|
| 59 | + * |
|
| 60 | + * @since 3.20.0 |
|
| 61 | + * |
|
| 62 | + * @param int $post_id Post ID. |
|
| 63 | + * |
|
| 64 | + * @return string The post permalink. |
|
| 65 | + */ |
|
| 66 | + public static function get_production_permalink( $post_id ) { |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
| 70 | - * when the production environment is copied over from a staging environment with staging |
|
| 71 | - * URLs. |
|
| 72 | - * |
|
| 73 | - * @since 3.20.0 |
|
| 74 | - * |
|
| 75 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 76 | - * |
|
| 77 | - * @param string $permalink_url The default permalink. |
|
| 78 | - * @param int $post_id The post id. |
|
| 79 | - */ |
|
| 80 | - return apply_filters( 'wl_production_permalink', get_permalink( $post_id ), $post_id ); |
|
| 81 | - } |
|
| 68 | + /** |
|
| 69 | + * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
|
| 70 | + * when the production environment is copied over from a staging environment with staging |
|
| 71 | + * URLs. |
|
| 72 | + * |
|
| 73 | + * @since 3.20.0 |
|
| 74 | + * |
|
| 75 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 76 | + * |
|
| 77 | + * @param string $permalink_url The default permalink. |
|
| 78 | + * @param int $post_id The post id. |
|
| 79 | + */ |
|
| 80 | + return apply_filters( 'wl_production_permalink', get_permalink( $post_id ), $post_id ); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | 83 | } |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @param integer $post_id the post ID of the post the adopter relates to. |
| 34 | 34 | */ |
| 35 | - public function __construct( $post_id ) { |
|
| 35 | + public function __construct($post_id) { |
|
| 36 | 36 | |
| 37 | 37 | $this->post_id = $post_id; |
| 38 | 38 | |
@@ -49,9 +49,9 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function word_count() { |
| 51 | 51 | |
| 52 | - $post = get_post( $this->post_id ); |
|
| 52 | + $post = get_post($this->post_id); |
|
| 53 | 53 | |
| 54 | - return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) ); |
|
| 54 | + return str_word_count(strip_tags(strip_shortcodes($post->post_content))); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | * |
| 64 | 64 | * @return string The post permalink. |
| 65 | 65 | */ |
| 66 | - public static function get_production_permalink( $post_id ) { |
|
| 66 | + public static function get_production_permalink($post_id) { |
|
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * The `wl_production_permalink` filter allows to change the permalink, this is useful in contexts |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | * @param string $permalink_url The default permalink. |
| 78 | 78 | * @param int $post_id The post id. |
| 79 | 79 | */ |
| 80 | - return apply_filters( 'wl_production_permalink', get_permalink( $post_id ), $post_id ); |
|
| 80 | + return apply_filters('wl_production_permalink', get_permalink($post_id), $post_id); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | } |
@@ -13,106 +13,106 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Schema_Location_Property_Service extends Wordlift_Property_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The meta key used to store data for this property. We don't use wl_url to |
|
| 18 | - * avoid potential confusion about other URLs. |
|
| 19 | - * |
|
| 20 | - * @since 3.7.0 |
|
| 21 | - */ |
|
| 22 | - const META_KEY = 'wl_location'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function get_rdf_predicate() { |
|
| 28 | - |
|
| 29 | - return 'http://schema.org/location'; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * {@inheritdoc} |
|
| 34 | - */ |
|
| 35 | - public function get_rdf_data_type() { |
|
| 36 | - |
|
| 37 | - return 'xsd:anyURI'; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * {@inheritdoc} |
|
| 42 | - */ |
|
| 43 | - public function get_data_type() { |
|
| 44 | - |
|
| 45 | - return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * {@inheritdoc} |
|
| 50 | - */ |
|
| 51 | - public function get_cardinality() { |
|
| 52 | - |
|
| 53 | - return INF; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - */ |
|
| 59 | - public function get_metabox_class() { |
|
| 60 | - |
|
| 61 | - return 'WL_Metabox_Field'; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * {@inheritdoc} |
|
| 66 | - */ |
|
| 67 | - public function get_metabox_label() { |
|
| 68 | - |
|
| 69 | - return 'Location(s)'; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @var |
|
| 74 | - */ |
|
| 75 | - private $sparql_service; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Create a Wordlift_Schema_Location_Property_Service instance. |
|
| 79 | - * @since 3.7.0 |
|
| 80 | - * |
|
| 81 | - * @param Wordlift_Sparql_Service $sparql_service |
|
| 82 | - */ |
|
| 83 | - public function __construct( $sparql_service ) { |
|
| 84 | - parent::__construct(); |
|
| 85 | - |
|
| 86 | - $this->sparql_service = $sparql_service; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Get the schema:url value for the specified post/entity. |
|
| 91 | - * |
|
| 92 | - * @since 3.7.0 |
|
| 93 | - * |
|
| 94 | - * @param int $post_id The post id. |
|
| 95 | - * |
|
| 96 | - * @return array|NULL The schema:url value or NULL if not set. |
|
| 97 | - */ |
|
| 98 | - public function get( $post_id ) { |
|
| 99 | - |
|
| 100 | - // Get the schema:url values set in WP. |
|
| 101 | - $values = get_post_meta( $post_id, self::META_KEY, FALSE ); |
|
| 102 | - |
|
| 103 | - // Finally return whatever values the editor set. |
|
| 104 | - return $values; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * {@inheritdoc} |
|
| 109 | - */ |
|
| 110 | - public function sanitize( $value ) { |
|
| 111 | - |
|
| 112 | - // TODO: check that it's an URL or that is <permalink> |
|
| 113 | - |
|
| 114 | - return $value; |
|
| 115 | - } |
|
| 16 | + /** |
|
| 17 | + * The meta key used to store data for this property. We don't use wl_url to |
|
| 18 | + * avoid potential confusion about other URLs. |
|
| 19 | + * |
|
| 20 | + * @since 3.7.0 |
|
| 21 | + */ |
|
| 22 | + const META_KEY = 'wl_location'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function get_rdf_predicate() { |
|
| 28 | + |
|
| 29 | + return 'http://schema.org/location'; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * {@inheritdoc} |
|
| 34 | + */ |
|
| 35 | + public function get_rdf_data_type() { |
|
| 36 | + |
|
| 37 | + return 'xsd:anyURI'; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * {@inheritdoc} |
|
| 42 | + */ |
|
| 43 | + public function get_data_type() { |
|
| 44 | + |
|
| 45 | + return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * {@inheritdoc} |
|
| 50 | + */ |
|
| 51 | + public function get_cardinality() { |
|
| 52 | + |
|
| 53 | + return INF; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + */ |
|
| 59 | + public function get_metabox_class() { |
|
| 60 | + |
|
| 61 | + return 'WL_Metabox_Field'; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * {@inheritdoc} |
|
| 66 | + */ |
|
| 67 | + public function get_metabox_label() { |
|
| 68 | + |
|
| 69 | + return 'Location(s)'; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @var |
|
| 74 | + */ |
|
| 75 | + private $sparql_service; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Create a Wordlift_Schema_Location_Property_Service instance. |
|
| 79 | + * @since 3.7.0 |
|
| 80 | + * |
|
| 81 | + * @param Wordlift_Sparql_Service $sparql_service |
|
| 82 | + */ |
|
| 83 | + public function __construct( $sparql_service ) { |
|
| 84 | + parent::__construct(); |
|
| 85 | + |
|
| 86 | + $this->sparql_service = $sparql_service; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Get the schema:url value for the specified post/entity. |
|
| 91 | + * |
|
| 92 | + * @since 3.7.0 |
|
| 93 | + * |
|
| 94 | + * @param int $post_id The post id. |
|
| 95 | + * |
|
| 96 | + * @return array|NULL The schema:url value or NULL if not set. |
|
| 97 | + */ |
|
| 98 | + public function get( $post_id ) { |
|
| 99 | + |
|
| 100 | + // Get the schema:url values set in WP. |
|
| 101 | + $values = get_post_meta( $post_id, self::META_KEY, FALSE ); |
|
| 102 | + |
|
| 103 | + // Finally return whatever values the editor set. |
|
| 104 | + return $values; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * {@inheritdoc} |
|
| 109 | + */ |
|
| 110 | + public function sanitize( $value ) { |
|
| 111 | + |
|
| 112 | + // TODO: check that it's an URL or that is <permalink> |
|
| 113 | + |
|
| 114 | + return $value; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | // /** |
| 118 | 118 | // * Generate an insert query that inserts the schema:url values for the specified |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @param Wordlift_Sparql_Service $sparql_service |
| 82 | 82 | */ |
| 83 | - public function __construct( $sparql_service ) { |
|
| 83 | + public function __construct($sparql_service) { |
|
| 84 | 84 | parent::__construct(); |
| 85 | 85 | |
| 86 | 86 | $this->sparql_service = $sparql_service; |
@@ -95,10 +95,10 @@ discard block |
||
| 95 | 95 | * |
| 96 | 96 | * @return array|NULL The schema:url value or NULL if not set. |
| 97 | 97 | */ |
| 98 | - public function get( $post_id ) { |
|
| 98 | + public function get($post_id) { |
|
| 99 | 99 | |
| 100 | 100 | // Get the schema:url values set in WP. |
| 101 | - $values = get_post_meta( $post_id, self::META_KEY, FALSE ); |
|
| 101 | + $values = get_post_meta($post_id, self::META_KEY, FALSE); |
|
| 102 | 102 | |
| 103 | 103 | // Finally return whatever values the editor set. |
| 104 | 104 | return $values; |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | /** |
| 108 | 108 | * {@inheritdoc} |
| 109 | 109 | */ |
| 110 | - public function sanitize( $value ) { |
|
| 110 | + public function sanitize($value) { |
|
| 111 | 111 | |
| 112 | 112 | // TODO: check that it's an URL or that is <permalink> |
| 113 | 113 | |
@@ -14,32 +14,32 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Url_Property_Service extends Wordlift_Simple_Property_Service { |
| 16 | 16 | |
| 17 | - const META_KEY = 'wl_schema_url'; |
|
| 17 | + const META_KEY = 'wl_schema_url'; |
|
| 18 | 18 | |
| 19 | - public function get( $post_id, $meta_key ) { |
|
| 19 | + public function get( $post_id, $meta_key ) { |
|
| 20 | 20 | |
| 21 | - // Get the meta values and push the <permalink> to |
|
| 22 | - // ensure that default url will be added to the schema:url's. |
|
| 23 | - $urls = array_unique( // We need to avoid duplicates. |
|
| 24 | - array_merge( |
|
| 25 | - parent::get( $post_id, $meta_key ), // Get default meta values |
|
| 26 | - array( |
|
| 27 | - '<permalink>', // Add the permalink in case it was removed. |
|
| 28 | - ) |
|
| 29 | - ) |
|
| 30 | - ); |
|
| 21 | + // Get the meta values and push the <permalink> to |
|
| 22 | + // ensure that default url will be added to the schema:url's. |
|
| 23 | + $urls = array_unique( // We need to avoid duplicates. |
|
| 24 | + array_merge( |
|
| 25 | + parent::get( $post_id, $meta_key ), // Get default meta values |
|
| 26 | + array( |
|
| 27 | + '<permalink>', // Add the permalink in case it was removed. |
|
| 28 | + ) |
|
| 29 | + ) |
|
| 30 | + ); |
|
| 31 | 31 | |
| 32 | - // Convert <permalink> in actual permalink values. |
|
| 33 | - return array_map( function ( $item ) use ( $post_id ) { |
|
| 34 | - /* |
|
| 32 | + // Convert <permalink> in actual permalink values. |
|
| 33 | + return array_map( function ( $item ) use ( $post_id ) { |
|
| 34 | + /* |
|
| 35 | 35 | * If `<permalink>` get the production permalink. |
| 36 | 36 | * |
| 37 | 37 | * @since 3.20.0 |
| 38 | 38 | * |
| 39 | 39 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 40 | 40 | */ |
| 41 | - return '<permalink>' === $item ? Wordlift_Post_Adapter::get_production_permalink( $post_id ) : $item; |
|
| 42 | - }, $urls ); |
|
| 43 | - } |
|
| 41 | + return '<permalink>' === $item ? Wordlift_Post_Adapter::get_production_permalink( $post_id ) : $item; |
|
| 42 | + }, $urls ); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | } |
@@ -16,13 +16,13 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | const META_KEY = 'wl_schema_url'; |
| 18 | 18 | |
| 19 | - public function get( $post_id, $meta_key ) { |
|
| 19 | + public function get($post_id, $meta_key) { |
|
| 20 | 20 | |
| 21 | 21 | // Get the meta values and push the <permalink> to |
| 22 | 22 | // ensure that default url will be added to the schema:url's. |
| 23 | 23 | $urls = array_unique( // We need to avoid duplicates. |
| 24 | 24 | array_merge( |
| 25 | - parent::get( $post_id, $meta_key ), // Get default meta values |
|
| 25 | + parent::get($post_id, $meta_key), // Get default meta values |
|
| 26 | 26 | array( |
| 27 | 27 | '<permalink>', // Add the permalink in case it was removed. |
| 28 | 28 | ) |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | ); |
| 31 | 31 | |
| 32 | 32 | // Convert <permalink> in actual permalink values. |
| 33 | - return array_map( function ( $item ) use ( $post_id ) { |
|
| 33 | + return array_map(function($item) use ($post_id) { |
|
| 34 | 34 | /* |
| 35 | 35 | * If `<permalink>` get the production permalink. |
| 36 | 36 | * |
@@ -38,8 +38,8 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @see https://github.com/insideout10/wordlift-plugin/issues/850. |
| 40 | 40 | */ |
| 41 | - return '<permalink>' === $item ? Wordlift_Post_Adapter::get_production_permalink( $post_id ) : $item; |
|
| 42 | - }, $urls ); |
|
| 41 | + return '<permalink>' === $item ? Wordlift_Post_Adapter::get_production_permalink($post_id) : $item; |
|
| 42 | + }, $urls); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | } |
@@ -10,212 +10,212 @@ discard block |
||
| 10 | 10 | */ |
| 11 | 11 | class Wordlift_User_Service { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * The meta key where the user's URI is stored. |
|
| 15 | - * |
|
| 16 | - * @since 3.1.7 |
|
| 17 | - */ |
|
| 18 | - const URI_META_KEY = '_wl_uri'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The user meta key where the deny entity edit flag is stored. |
|
| 22 | - * |
|
| 23 | - * @since 3.14.0 |
|
| 24 | - */ |
|
| 25 | - const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The meta key holding the entity id representing a {@link WP_User}. |
|
| 29 | - * |
|
| 30 | - * @since 3.14.0 |
|
| 31 | - */ |
|
| 32 | - const ENTITY_META_KEY = '_wl_entity'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The Log service. |
|
| 36 | - * |
|
| 37 | - * @since 3.1.7 |
|
| 38 | - * @access private |
|
| 39 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 40 | - */ |
|
| 41 | - private $log_service; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The singleton instance of the User service. |
|
| 45 | - * |
|
| 46 | - * @since 3.1.7 |
|
| 47 | - * @access private |
|
| 48 | - * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
| 49 | - */ |
|
| 50 | - private static $instance; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The {@link Wordlift_Sparql_Service} instance. |
|
| 54 | - * |
|
| 55 | - * @since 3.18.0 |
|
| 56 | - * @access private |
|
| 57 | - * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 58 | - */ |
|
| 59 | - private $sparql_service; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The Entity service. |
|
| 63 | - * |
|
| 64 | - * @since 3.18.0 |
|
| 65 | - * @access private |
|
| 66 | - * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 67 | - */ |
|
| 68 | - private $entity_service; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Create an instance of the User service. |
|
| 72 | - * |
|
| 73 | - * @since 3.1.7 |
|
| 74 | - * |
|
| 75 | - * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 76 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 77 | - */ |
|
| 78 | - public function __construct( $sparql_service, $entity_service ) { |
|
| 79 | - |
|
| 80 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 81 | - |
|
| 82 | - self::$instance = $this; |
|
| 83 | - |
|
| 84 | - $this->sparql_service = $sparql_service; |
|
| 85 | - $this->entity_service = $entity_service; |
|
| 86 | - |
|
| 87 | - add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get the singleton instance of the User service. |
|
| 92 | - * |
|
| 93 | - * @since 3.1.7 |
|
| 94 | - * @return \Wordlift_User_Service The singleton instance of the User service. |
|
| 95 | - */ |
|
| 96 | - public static function get_instance() { |
|
| 97 | - |
|
| 98 | - return self::$instance; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get the URI for a user. |
|
| 103 | - * |
|
| 104 | - * @since 3.1.7 |
|
| 105 | - * |
|
| 106 | - * @param int $user_id The user id |
|
| 107 | - * |
|
| 108 | - * @return false|string The user's URI or false in case of failure. |
|
| 109 | - */ |
|
| 110 | - public function get_uri( $user_id ) { |
|
| 111 | - |
|
| 112 | - // Try to get the URI stored in the user's meta and return it if available. |
|
| 113 | - if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 114 | - return $user_uri; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - // Try to build an URI, return false in case of failure. |
|
| 118 | - if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 119 | - return false; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // Store the URI for future requests (we need a "permanent" URI). |
|
| 123 | - $this->_set_uri( $user_id, $user_uri ); |
|
| 124 | - |
|
| 125 | - return $user_uri; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Set the `id` of the entity representing a {@link WP_User}. |
|
| 130 | - * |
|
| 131 | - * If the `id` is set to 0 (or less) then the meta is deleted. |
|
| 132 | - * |
|
| 133 | - * @since 3.14.0 |
|
| 134 | - * |
|
| 135 | - * @param int $user_id The {@link WP_User}. |
|
| 136 | - * @param int $value The entity {@link WP_Post} `id`. |
|
| 137 | - * |
|
| 138 | - * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 139 | - */ |
|
| 140 | - public function set_entity( $user_id, $value ) { |
|
| 141 | - |
|
| 142 | - return 0 < $value |
|
| 143 | - ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 144 | - : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
|
| 149 | - * |
|
| 150 | - * @since 3.14.0 |
|
| 151 | - * |
|
| 152 | - * @param int $user_id The {@link WP_User}'s `id`. |
|
| 153 | - * |
|
| 154 | - * @return string|false The entity {@link WP_Post} `id` or an empty string if not set or false if the object id is invalid |
|
| 155 | - */ |
|
| 156 | - public function get_entity( $user_id ) { |
|
| 157 | - |
|
| 158 | - return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Get the user's URI stored in the user's meta. |
|
| 163 | - * |
|
| 164 | - * @since 3.1.7 |
|
| 165 | - * |
|
| 166 | - * @param int $user_id The user id. |
|
| 167 | - * |
|
| 168 | - * @return false|string The user's URI or false if not found. |
|
| 169 | - */ |
|
| 170 | - private function _get_uri( $user_id ) { |
|
| 171 | - |
|
| 172 | - $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 173 | - |
|
| 174 | - if ( empty( $user_uri ) ) { |
|
| 175 | - return false; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return $user_uri; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Build an URI for a user. |
|
| 183 | - * |
|
| 184 | - * @since 3.1.7 |
|
| 185 | - * |
|
| 186 | - * @param int $user_id The user's id. |
|
| 187 | - * |
|
| 188 | - * @return false|string The user's URI or false in case of failure. |
|
| 189 | - */ |
|
| 190 | - private function _build_uri( $user_id ) { |
|
| 191 | - |
|
| 192 | - // Get the user, return false in case of failure. |
|
| 193 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 194 | - return false; |
|
| 195 | - }; |
|
| 196 | - |
|
| 197 | - // If the nicename is not set, return a failure. |
|
| 198 | - if ( empty( $user->user_nicename ) ) { |
|
| 199 | - return false; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Store the URI in user's meta. |
|
| 207 | - * |
|
| 208 | - * @since 3.1.7 |
|
| 209 | - * |
|
| 210 | - * @param int $user_id The user's id. |
|
| 211 | - * @param string $user_uri The user's uri. |
|
| 212 | - * |
|
| 213 | - * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 214 | - */ |
|
| 215 | - private function _set_uri( $user_id, $user_uri ) { |
|
| 216 | - |
|
| 217 | - return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 218 | - } |
|
| 13 | + /** |
|
| 14 | + * The meta key where the user's URI is stored. |
|
| 15 | + * |
|
| 16 | + * @since 3.1.7 |
|
| 17 | + */ |
|
| 18 | + const URI_META_KEY = '_wl_uri'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The user meta key where the deny entity edit flag is stored. |
|
| 22 | + * |
|
| 23 | + * @since 3.14.0 |
|
| 24 | + */ |
|
| 25 | + const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The meta key holding the entity id representing a {@link WP_User}. |
|
| 29 | + * |
|
| 30 | + * @since 3.14.0 |
|
| 31 | + */ |
|
| 32 | + const ENTITY_META_KEY = '_wl_entity'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The Log service. |
|
| 36 | + * |
|
| 37 | + * @since 3.1.7 |
|
| 38 | + * @access private |
|
| 39 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 40 | + */ |
|
| 41 | + private $log_service; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The singleton instance of the User service. |
|
| 45 | + * |
|
| 46 | + * @since 3.1.7 |
|
| 47 | + * @access private |
|
| 48 | + * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
| 49 | + */ |
|
| 50 | + private static $instance; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The {@link Wordlift_Sparql_Service} instance. |
|
| 54 | + * |
|
| 55 | + * @since 3.18.0 |
|
| 56 | + * @access private |
|
| 57 | + * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 58 | + */ |
|
| 59 | + private $sparql_service; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The Entity service. |
|
| 63 | + * |
|
| 64 | + * @since 3.18.0 |
|
| 65 | + * @access private |
|
| 66 | + * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 67 | + */ |
|
| 68 | + private $entity_service; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Create an instance of the User service. |
|
| 72 | + * |
|
| 73 | + * @since 3.1.7 |
|
| 74 | + * |
|
| 75 | + * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 76 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 77 | + */ |
|
| 78 | + public function __construct( $sparql_service, $entity_service ) { |
|
| 79 | + |
|
| 80 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 81 | + |
|
| 82 | + self::$instance = $this; |
|
| 83 | + |
|
| 84 | + $this->sparql_service = $sparql_service; |
|
| 85 | + $this->entity_service = $entity_service; |
|
| 86 | + |
|
| 87 | + add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get the singleton instance of the User service. |
|
| 92 | + * |
|
| 93 | + * @since 3.1.7 |
|
| 94 | + * @return \Wordlift_User_Service The singleton instance of the User service. |
|
| 95 | + */ |
|
| 96 | + public static function get_instance() { |
|
| 97 | + |
|
| 98 | + return self::$instance; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get the URI for a user. |
|
| 103 | + * |
|
| 104 | + * @since 3.1.7 |
|
| 105 | + * |
|
| 106 | + * @param int $user_id The user id |
|
| 107 | + * |
|
| 108 | + * @return false|string The user's URI or false in case of failure. |
|
| 109 | + */ |
|
| 110 | + public function get_uri( $user_id ) { |
|
| 111 | + |
|
| 112 | + // Try to get the URI stored in the user's meta and return it if available. |
|
| 113 | + if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 114 | + return $user_uri; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + // Try to build an URI, return false in case of failure. |
|
| 118 | + if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 119 | + return false; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // Store the URI for future requests (we need a "permanent" URI). |
|
| 123 | + $this->_set_uri( $user_id, $user_uri ); |
|
| 124 | + |
|
| 125 | + return $user_uri; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Set the `id` of the entity representing a {@link WP_User}. |
|
| 130 | + * |
|
| 131 | + * If the `id` is set to 0 (or less) then the meta is deleted. |
|
| 132 | + * |
|
| 133 | + * @since 3.14.0 |
|
| 134 | + * |
|
| 135 | + * @param int $user_id The {@link WP_User}. |
|
| 136 | + * @param int $value The entity {@link WP_Post} `id`. |
|
| 137 | + * |
|
| 138 | + * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 139 | + */ |
|
| 140 | + public function set_entity( $user_id, $value ) { |
|
| 141 | + |
|
| 142 | + return 0 < $value |
|
| 143 | + ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 144 | + : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
|
| 149 | + * |
|
| 150 | + * @since 3.14.0 |
|
| 151 | + * |
|
| 152 | + * @param int $user_id The {@link WP_User}'s `id`. |
|
| 153 | + * |
|
| 154 | + * @return string|false The entity {@link WP_Post} `id` or an empty string if not set or false if the object id is invalid |
|
| 155 | + */ |
|
| 156 | + public function get_entity( $user_id ) { |
|
| 157 | + |
|
| 158 | + return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Get the user's URI stored in the user's meta. |
|
| 163 | + * |
|
| 164 | + * @since 3.1.7 |
|
| 165 | + * |
|
| 166 | + * @param int $user_id The user id. |
|
| 167 | + * |
|
| 168 | + * @return false|string The user's URI or false if not found. |
|
| 169 | + */ |
|
| 170 | + private function _get_uri( $user_id ) { |
|
| 171 | + |
|
| 172 | + $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 173 | + |
|
| 174 | + if ( empty( $user_uri ) ) { |
|
| 175 | + return false; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return $user_uri; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Build an URI for a user. |
|
| 183 | + * |
|
| 184 | + * @since 3.1.7 |
|
| 185 | + * |
|
| 186 | + * @param int $user_id The user's id. |
|
| 187 | + * |
|
| 188 | + * @return false|string The user's URI or false in case of failure. |
|
| 189 | + */ |
|
| 190 | + private function _build_uri( $user_id ) { |
|
| 191 | + |
|
| 192 | + // Get the user, return false in case of failure. |
|
| 193 | + if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 194 | + return false; |
|
| 195 | + }; |
|
| 196 | + |
|
| 197 | + // If the nicename is not set, return a failure. |
|
| 198 | + if ( empty( $user->user_nicename ) ) { |
|
| 199 | + return false; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Store the URI in user's meta. |
|
| 207 | + * |
|
| 208 | + * @since 3.1.7 |
|
| 209 | + * |
|
| 210 | + * @param int $user_id The user's id. |
|
| 211 | + * @param string $user_uri The user's uri. |
|
| 212 | + * |
|
| 213 | + * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 214 | + */ |
|
| 215 | + private function _set_uri( $user_id, $user_uri ) { |
|
| 216 | + |
|
| 217 | + return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | 220 | // /** |
| 221 | 221 | // * Get the delete query. |
@@ -287,270 +287,270 @@ discard block |
||
| 287 | 287 | // return $query; |
| 288 | 288 | // } |
| 289 | 289 | |
| 290 | - /** |
|
| 291 | - * Mark an editor user as denied from editing entities. |
|
| 292 | - * Does nothing if the user is not an editor |
|
| 293 | - * |
|
| 294 | - * @since 3.14.0 |
|
| 295 | - * |
|
| 296 | - * @param integer $user_id The ID of the user |
|
| 297 | - */ |
|
| 298 | - public function deny_editor_entity_create( $user_id ) { |
|
| 299 | - |
|
| 300 | - // Bail out if the user is not an editor. |
|
| 301 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 302 | - return; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - // The user explicitly do not have the capability. |
|
| 306 | - update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 307 | - |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Remove the "deny entity editing" mark from an editor user. |
|
| 312 | - * Does nothing if the user is not an editor |
|
| 313 | - * |
|
| 314 | - * @since 3.14.0 |
|
| 315 | - * |
|
| 316 | - * @param integer $user_id The ID of the user |
|
| 317 | - */ |
|
| 318 | - public function allow_editor_entity_create( $user_id ) { |
|
| 319 | - |
|
| 320 | - // Bail out if the user is not an editor. |
|
| 321 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 322 | - return; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - // The user explicitly do not have the capability. |
|
| 326 | - delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 327 | - |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get whether the 'deny editor entity editing' flag is set. |
|
| 332 | - * |
|
| 333 | - * @since 3.14.0 |
|
| 334 | - * |
|
| 335 | - * @param int $user_id The {@link WP_User} `id`. |
|
| 336 | - * |
|
| 337 | - * @return int bool True if editing is denied otherwise false. |
|
| 338 | - */ |
|
| 339 | - public function is_deny_editor_entity_create( $user_id ) { |
|
| 340 | - |
|
| 341 | - return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Check whether the {@link WP_User} with the specified `id` is an editor, |
|
| 346 | - * i.e. has the `editor` role. |
|
| 347 | - * |
|
| 348 | - * @since 3.14.0 |
|
| 349 | - * |
|
| 350 | - * @param int $user_id The {@link WP_User} `id`. |
|
| 351 | - * |
|
| 352 | - * @return bool True if the {@link WP_User} is an editor otherwise false. |
|
| 353 | - */ |
|
| 354 | - public function is_editor( $user_id ) { |
|
| 355 | - |
|
| 356 | - // Get the user. |
|
| 357 | - $user = get_user_by( 'id', $user_id ); |
|
| 358 | - |
|
| 359 | - // Return true, if the user is found and has the `editor` role. |
|
| 360 | - return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Check if an editor can create entities. |
|
| 365 | - * |
|
| 366 | - * @since 3.14.0 |
|
| 367 | - * |
|
| 368 | - * @param int $user_id The user id of the user being checked. |
|
| 369 | - * |
|
| 370 | - * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
|
| 371 | - */ |
|
| 372 | - public function editor_can_create_entities( $user_id ) { |
|
| 373 | - |
|
| 374 | - // Return true if not an editor. |
|
| 375 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 376 | - return true; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - // Check if the user explicitly denied. |
|
| 380 | - return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Filter capabilities of user. |
|
| 385 | - * |
|
| 386 | - * Deny the capability of managing and editing entities for some users. |
|
| 387 | - * |
|
| 388 | - * @since 3.14.0 |
|
| 389 | - * |
|
| 390 | - * @param array $allcaps All the capabilities of the user |
|
| 391 | - * @param array $cap [0] Required capability |
|
| 392 | - * @param array $args [0] Requested capability |
|
| 393 | - * [1] User ID |
|
| 394 | - * [2] Associated object ID |
|
| 395 | - * |
|
| 396 | - * @return array The capabilities array. |
|
| 397 | - */ |
|
| 398 | - public function has_cap( $allcaps, $cap, $args ) { |
|
| 399 | - /* |
|
| 290 | + /** |
|
| 291 | + * Mark an editor user as denied from editing entities. |
|
| 292 | + * Does nothing if the user is not an editor |
|
| 293 | + * |
|
| 294 | + * @since 3.14.0 |
|
| 295 | + * |
|
| 296 | + * @param integer $user_id The ID of the user |
|
| 297 | + */ |
|
| 298 | + public function deny_editor_entity_create( $user_id ) { |
|
| 299 | + |
|
| 300 | + // Bail out if the user is not an editor. |
|
| 301 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 302 | + return; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + // The user explicitly do not have the capability. |
|
| 306 | + update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 307 | + |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Remove the "deny entity editing" mark from an editor user. |
|
| 312 | + * Does nothing if the user is not an editor |
|
| 313 | + * |
|
| 314 | + * @since 3.14.0 |
|
| 315 | + * |
|
| 316 | + * @param integer $user_id The ID of the user |
|
| 317 | + */ |
|
| 318 | + public function allow_editor_entity_create( $user_id ) { |
|
| 319 | + |
|
| 320 | + // Bail out if the user is not an editor. |
|
| 321 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 322 | + return; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + // The user explicitly do not have the capability. |
|
| 326 | + delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 327 | + |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get whether the 'deny editor entity editing' flag is set. |
|
| 332 | + * |
|
| 333 | + * @since 3.14.0 |
|
| 334 | + * |
|
| 335 | + * @param int $user_id The {@link WP_User} `id`. |
|
| 336 | + * |
|
| 337 | + * @return int bool True if editing is denied otherwise false. |
|
| 338 | + */ |
|
| 339 | + public function is_deny_editor_entity_create( $user_id ) { |
|
| 340 | + |
|
| 341 | + return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Check whether the {@link WP_User} with the specified `id` is an editor, |
|
| 346 | + * i.e. has the `editor` role. |
|
| 347 | + * |
|
| 348 | + * @since 3.14.0 |
|
| 349 | + * |
|
| 350 | + * @param int $user_id The {@link WP_User} `id`. |
|
| 351 | + * |
|
| 352 | + * @return bool True if the {@link WP_User} is an editor otherwise false. |
|
| 353 | + */ |
|
| 354 | + public function is_editor( $user_id ) { |
|
| 355 | + |
|
| 356 | + // Get the user. |
|
| 357 | + $user = get_user_by( 'id', $user_id ); |
|
| 358 | + |
|
| 359 | + // Return true, if the user is found and has the `editor` role. |
|
| 360 | + return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Check if an editor can create entities. |
|
| 365 | + * |
|
| 366 | + * @since 3.14.0 |
|
| 367 | + * |
|
| 368 | + * @param int $user_id The user id of the user being checked. |
|
| 369 | + * |
|
| 370 | + * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
|
| 371 | + */ |
|
| 372 | + public function editor_can_create_entities( $user_id ) { |
|
| 373 | + |
|
| 374 | + // Return true if not an editor. |
|
| 375 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 376 | + return true; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + // Check if the user explicitly denied. |
|
| 380 | + return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Filter capabilities of user. |
|
| 385 | + * |
|
| 386 | + * Deny the capability of managing and editing entities for some users. |
|
| 387 | + * |
|
| 388 | + * @since 3.14.0 |
|
| 389 | + * |
|
| 390 | + * @param array $allcaps All the capabilities of the user |
|
| 391 | + * @param array $cap [0] Required capability |
|
| 392 | + * @param array $args [0] Requested capability |
|
| 393 | + * [1] User ID |
|
| 394 | + * [2] Associated object ID |
|
| 395 | + * |
|
| 396 | + * @return array The capabilities array. |
|
| 397 | + */ |
|
| 398 | + public function has_cap( $allcaps, $cap, $args ) { |
|
| 399 | + /* |
|
| 400 | 400 | * For entity management/editing related capabilities |
| 401 | 401 | * check that an editor was not explicitly denied (in user profile) |
| 402 | 402 | * the capability. |
| 403 | 403 | */ |
| 404 | 404 | |
| 405 | - /* |
|
| 405 | + /* |
|
| 406 | 406 | * Need protection against the case of edit_user and likes which do not |
| 407 | 407 | * require a capability, just request one. |
| 408 | 408 | */ |
| 409 | - if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
| 410 | - return $allcaps; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - if ( |
|
| 414 | - ( 'edit_wordlift_entity' === $cap[0] ) || |
|
| 415 | - ( 'edit_wordlift_entities' === $cap[0] ) || |
|
| 416 | - ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
| 417 | - ( 'publish_wordlift_entities' === $cap[0] ) || |
|
| 418 | - ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
| 419 | - ( 'delete_wordlift_entity' === $cap[0] ) || |
|
| 420 | - ( 'delete_wordlift_entities' === $cap[0] ) || |
|
| 421 | - ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
| 422 | - ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
| 423 | - ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
| 424 | - ) { |
|
| 425 | - $user_id = $args[1]; |
|
| 426 | - |
|
| 427 | - if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 428 | - $allcaps[ $cap[0] ] = false; |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - return $allcaps; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * Hook on update user meta to check if the user author has changed. |
|
| 437 | - * If so we need to execute sparql query that will update all user posts author triple. |
|
| 438 | - * |
|
| 439 | - * @since 3.18.0 |
|
| 440 | - * |
|
| 441 | - * @param null $null |
|
| 442 | - * @param int $object_id The user ID. |
|
| 443 | - * @param string $meta_key The meta key name. |
|
| 444 | - * @param mixed $meta_value Meta value. |
|
| 445 | - * @param mixed $prev_value The previous metadata value. |
|
| 446 | - * |
|
| 447 | - * @return null Null if the `meta_key` is not `Wordlift_User_Service::ENTITY_META_KEY` |
|
| 448 | - * or if the author has not changed. |
|
| 449 | - */ |
|
| 450 | - public function update_user_metadata( $null, $object_id, $meta_key, $meta_value, $prev_value ) { |
|
| 451 | - // Bail if the meta key is not the author meta. |
|
| 452 | - if ( $meta_key !== Wordlift_User_Service::ENTITY_META_KEY ) { |
|
| 453 | - return null; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - // Check whether the user is associated with any of the existing publishers/ |
|
| 457 | - $entity_id = $this->get_entity( $object_id ); |
|
| 458 | - |
|
| 459 | - if ( false === $entity_id ) { |
|
| 460 | - // An error occurred. |
|
| 461 | - $this->log_service->error( "An error occurred: entity_id can't be false." ); |
|
| 462 | - |
|
| 463 | - return; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - // Get the old uri if the entity is set.. |
|
| 467 | - $old_uri = ! empty( $entity_id ) |
|
| 468 | - ? $this->entity_service->get_uri( $entity_id ) |
|
| 469 | - : $this->get_uri( $object_id ); |
|
| 470 | - |
|
| 471 | - // Get the new user uri's. |
|
| 472 | - $new_uri = $this->entity_service->get_uri( $meta_value ); |
|
| 473 | - |
|
| 474 | - // Bail if the uri is the same. |
|
| 475 | - if ( $old_uri === $new_uri ) { |
|
| 476 | - return null; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - $this->update_author( $old_uri, $new_uri ); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Hook on delete user meta to execute sparql query |
|
| 484 | - * that will update all user posts author triple. |
|
| 485 | - * |
|
| 486 | - * @since 3.18.0 |
|
| 487 | - * |
|
| 488 | - * @param null $null |
|
| 489 | - * @param int $object_id The user ID. |
|
| 490 | - * @param string $meta_key The meta key name. |
|
| 491 | - * @param mixed $meta_value Meta value. |
|
| 492 | - * @param bool $delete_all Whether to delete the matching metadata entries |
|
| 493 | - * for all objects. |
|
| 494 | - * |
|
| 495 | - * @return null Null if the `meta_key` is not `Wordlift_User_Service::ENTITY_META_KEY` |
|
| 496 | - * or if the author has not changed. |
|
| 497 | - */ |
|
| 498 | - public function delete_user_metadata( $null, $object_id, $meta_key, $meta_value, $delete_all ) { |
|
| 499 | - // Bail if the meta key is not the author meta. |
|
| 500 | - if ( $meta_key !== Wordlift_User_Service::ENTITY_META_KEY ) { |
|
| 501 | - return null; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - // Check whether the user is associated with any of the existing publishers/ |
|
| 505 | - $entity_id = $this->get_entity( $object_id ); |
|
| 506 | - |
|
| 507 | - if ( false === $entity_id ) { |
|
| 508 | - // An error occurred. |
|
| 509 | - $this->log_service->error( "An error occurred: entity_id can't be false." ); |
|
| 510 | - |
|
| 511 | - return; |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - // Get the old uri if the entity is set. |
|
| 515 | - $old_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 516 | - |
|
| 517 | - $new_uri = $this->get_uri( $object_id ); |
|
| 518 | - |
|
| 519 | - $this->update_author( $old_uri, $new_uri ); |
|
| 520 | - |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Update the schema:author when the user author is changed. |
|
| 525 | - * |
|
| 526 | - * @since 3.18.0 |
|
| 527 | - * |
|
| 528 | - * @param string $old_uri The old uri to remove. |
|
| 529 | - * @param string $new_uri The new uri to add. |
|
| 530 | - */ |
|
| 531 | - private function update_author( $old_uri, $new_uri ) { |
|
| 532 | - // Bail in case one of the uris is empty. |
|
| 533 | - if ( empty( $old_uri ) || empty( $new_uri ) ) { |
|
| 534 | - // An error occurred. |
|
| 535 | - $this->log_service->error( "An error occurred: old_uri and/or new_uri can't be null." ); |
|
| 536 | - |
|
| 537 | - return; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - // Build the update query. |
|
| 541 | - $query = sprintf( |
|
| 542 | - 'DELETE { ?s <%1$s> <%2$s> } INSERT { ?s <%1$s> <%3$s> } WHERE { ?s <%1$s> <%2$s> }', |
|
| 543 | - // Schema:author triple. |
|
| 544 | - $this->sparql_service->escape_uri( Wordlift_Query_Builder::SCHEMA_AUTHOR_URI ), |
|
| 545 | - // Old author uri to remove, |
|
| 546 | - $this->sparql_service->escape_uri( $old_uri ), |
|
| 547 | - // New author uri to add, |
|
| 548 | - $this->sparql_service->escape_uri( $new_uri ) |
|
| 549 | - ); |
|
| 550 | - |
|
| 551 | - // Execute the query and update the author. |
|
| 552 | - $this->sparql_service->execute( $query ); |
|
| 553 | - |
|
| 554 | - } |
|
| 409 | + if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
| 410 | + return $allcaps; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + if ( |
|
| 414 | + ( 'edit_wordlift_entity' === $cap[0] ) || |
|
| 415 | + ( 'edit_wordlift_entities' === $cap[0] ) || |
|
| 416 | + ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
| 417 | + ( 'publish_wordlift_entities' === $cap[0] ) || |
|
| 418 | + ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
| 419 | + ( 'delete_wordlift_entity' === $cap[0] ) || |
|
| 420 | + ( 'delete_wordlift_entities' === $cap[0] ) || |
|
| 421 | + ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
| 422 | + ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
| 423 | + ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
| 424 | + ) { |
|
| 425 | + $user_id = $args[1]; |
|
| 426 | + |
|
| 427 | + if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 428 | + $allcaps[ $cap[0] ] = false; |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + return $allcaps; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * Hook on update user meta to check if the user author has changed. |
|
| 437 | + * If so we need to execute sparql query that will update all user posts author triple. |
|
| 438 | + * |
|
| 439 | + * @since 3.18.0 |
|
| 440 | + * |
|
| 441 | + * @param null $null |
|
| 442 | + * @param int $object_id The user ID. |
|
| 443 | + * @param string $meta_key The meta key name. |
|
| 444 | + * @param mixed $meta_value Meta value. |
|
| 445 | + * @param mixed $prev_value The previous metadata value. |
|
| 446 | + * |
|
| 447 | + * @return null Null if the `meta_key` is not `Wordlift_User_Service::ENTITY_META_KEY` |
|
| 448 | + * or if the author has not changed. |
|
| 449 | + */ |
|
| 450 | + public function update_user_metadata( $null, $object_id, $meta_key, $meta_value, $prev_value ) { |
|
| 451 | + // Bail if the meta key is not the author meta. |
|
| 452 | + if ( $meta_key !== Wordlift_User_Service::ENTITY_META_KEY ) { |
|
| 453 | + return null; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + // Check whether the user is associated with any of the existing publishers/ |
|
| 457 | + $entity_id = $this->get_entity( $object_id ); |
|
| 458 | + |
|
| 459 | + if ( false === $entity_id ) { |
|
| 460 | + // An error occurred. |
|
| 461 | + $this->log_service->error( "An error occurred: entity_id can't be false." ); |
|
| 462 | + |
|
| 463 | + return; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + // Get the old uri if the entity is set.. |
|
| 467 | + $old_uri = ! empty( $entity_id ) |
|
| 468 | + ? $this->entity_service->get_uri( $entity_id ) |
|
| 469 | + : $this->get_uri( $object_id ); |
|
| 470 | + |
|
| 471 | + // Get the new user uri's. |
|
| 472 | + $new_uri = $this->entity_service->get_uri( $meta_value ); |
|
| 473 | + |
|
| 474 | + // Bail if the uri is the same. |
|
| 475 | + if ( $old_uri === $new_uri ) { |
|
| 476 | + return null; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + $this->update_author( $old_uri, $new_uri ); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Hook on delete user meta to execute sparql query |
|
| 484 | + * that will update all user posts author triple. |
|
| 485 | + * |
|
| 486 | + * @since 3.18.0 |
|
| 487 | + * |
|
| 488 | + * @param null $null |
|
| 489 | + * @param int $object_id The user ID. |
|
| 490 | + * @param string $meta_key The meta key name. |
|
| 491 | + * @param mixed $meta_value Meta value. |
|
| 492 | + * @param bool $delete_all Whether to delete the matching metadata entries |
|
| 493 | + * for all objects. |
|
| 494 | + * |
|
| 495 | + * @return null Null if the `meta_key` is not `Wordlift_User_Service::ENTITY_META_KEY` |
|
| 496 | + * or if the author has not changed. |
|
| 497 | + */ |
|
| 498 | + public function delete_user_metadata( $null, $object_id, $meta_key, $meta_value, $delete_all ) { |
|
| 499 | + // Bail if the meta key is not the author meta. |
|
| 500 | + if ( $meta_key !== Wordlift_User_Service::ENTITY_META_KEY ) { |
|
| 501 | + return null; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + // Check whether the user is associated with any of the existing publishers/ |
|
| 505 | + $entity_id = $this->get_entity( $object_id ); |
|
| 506 | + |
|
| 507 | + if ( false === $entity_id ) { |
|
| 508 | + // An error occurred. |
|
| 509 | + $this->log_service->error( "An error occurred: entity_id can't be false." ); |
|
| 510 | + |
|
| 511 | + return; |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + // Get the old uri if the entity is set. |
|
| 515 | + $old_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 516 | + |
|
| 517 | + $new_uri = $this->get_uri( $object_id ); |
|
| 518 | + |
|
| 519 | + $this->update_author( $old_uri, $new_uri ); |
|
| 520 | + |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Update the schema:author when the user author is changed. |
|
| 525 | + * |
|
| 526 | + * @since 3.18.0 |
|
| 527 | + * |
|
| 528 | + * @param string $old_uri The old uri to remove. |
|
| 529 | + * @param string $new_uri The new uri to add. |
|
| 530 | + */ |
|
| 531 | + private function update_author( $old_uri, $new_uri ) { |
|
| 532 | + // Bail in case one of the uris is empty. |
|
| 533 | + if ( empty( $old_uri ) || empty( $new_uri ) ) { |
|
| 534 | + // An error occurred. |
|
| 535 | + $this->log_service->error( "An error occurred: old_uri and/or new_uri can't be null." ); |
|
| 536 | + |
|
| 537 | + return; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + // Build the update query. |
|
| 541 | + $query = sprintf( |
|
| 542 | + 'DELETE { ?s <%1$s> <%2$s> } INSERT { ?s <%1$s> <%3$s> } WHERE { ?s <%1$s> <%2$s> }', |
|
| 543 | + // Schema:author triple. |
|
| 544 | + $this->sparql_service->escape_uri( Wordlift_Query_Builder::SCHEMA_AUTHOR_URI ), |
|
| 545 | + // Old author uri to remove, |
|
| 546 | + $this->sparql_service->escape_uri( $old_uri ), |
|
| 547 | + // New author uri to add, |
|
| 548 | + $this->sparql_service->escape_uri( $new_uri ) |
|
| 549 | + ); |
|
| 550 | + |
|
| 551 | + // Execute the query and update the author. |
|
| 552 | + $this->sparql_service->execute( $query ); |
|
| 553 | + |
|
| 554 | + } |
|
| 555 | 555 | |
| 556 | 556 | } |
@@ -75,16 +75,16 @@ discard block |
||
| 75 | 75 | * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
| 76 | 76 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
| 77 | 77 | */ |
| 78 | - public function __construct( $sparql_service, $entity_service ) { |
|
| 78 | + public function __construct($sparql_service, $entity_service) { |
|
| 79 | 79 | |
| 80 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 80 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_User_Service'); |
|
| 81 | 81 | |
| 82 | 82 | self::$instance = $this; |
| 83 | 83 | |
| 84 | 84 | $this->sparql_service = $sparql_service; |
| 85 | 85 | $this->entity_service = $entity_service; |
| 86 | 86 | |
| 87 | - add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 87 | + add_filter('user_has_cap', array($this, 'has_cap'), 10, 3); |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
@@ -107,20 +107,20 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return false|string The user's URI or false in case of failure. |
| 109 | 109 | */ |
| 110 | - public function get_uri( $user_id ) { |
|
| 110 | + public function get_uri($user_id) { |
|
| 111 | 111 | |
| 112 | 112 | // Try to get the URI stored in the user's meta and return it if available. |
| 113 | - if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 113 | + if (false !== ($user_uri = $this->_get_uri($user_id))) { |
|
| 114 | 114 | return $user_uri; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // Try to build an URI, return false in case of failure. |
| 118 | - if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 118 | + if (false === ($user_uri = $this->_build_uri($user_id))) { |
|
| 119 | 119 | return false; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // Store the URI for future requests (we need a "permanent" URI). |
| 123 | - $this->_set_uri( $user_id, $user_uri ); |
|
| 123 | + $this->_set_uri($user_id, $user_uri); |
|
| 124 | 124 | |
| 125 | 125 | return $user_uri; |
| 126 | 126 | } |
@@ -137,11 +137,11 @@ discard block |
||
| 137 | 137 | * |
| 138 | 138 | * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
| 139 | 139 | */ |
| 140 | - public function set_entity( $user_id, $value ) { |
|
| 140 | + public function set_entity($user_id, $value) { |
|
| 141 | 141 | |
| 142 | 142 | return 0 < $value |
| 143 | - ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 144 | - : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 143 | + ? update_user_meta($user_id, self::ENTITY_META_KEY, $value) |
|
| 144 | + : delete_user_meta($user_id, self::ENTITY_META_KEY); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -153,9 +153,9 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @return string|false The entity {@link WP_Post} `id` or an empty string if not set or false if the object id is invalid |
| 155 | 155 | */ |
| 156 | - public function get_entity( $user_id ) { |
|
| 156 | + public function get_entity($user_id) { |
|
| 157 | 157 | |
| 158 | - return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 158 | + return get_user_meta($user_id, self::ENTITY_META_KEY, true); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
@@ -167,11 +167,11 @@ discard block |
||
| 167 | 167 | * |
| 168 | 168 | * @return false|string The user's URI or false if not found. |
| 169 | 169 | */ |
| 170 | - private function _get_uri( $user_id ) { |
|
| 170 | + private function _get_uri($user_id) { |
|
| 171 | 171 | |
| 172 | - $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 172 | + $user_uri = get_user_meta($user_id, self::URI_META_KEY, true); |
|
| 173 | 173 | |
| 174 | - if ( empty( $user_uri ) ) { |
|
| 174 | + if (empty($user_uri)) { |
|
| 175 | 175 | return false; |
| 176 | 176 | } |
| 177 | 177 | |
@@ -187,19 +187,19 @@ discard block |
||
| 187 | 187 | * |
| 188 | 188 | * @return false|string The user's URI or false in case of failure. |
| 189 | 189 | */ |
| 190 | - private function _build_uri( $user_id ) { |
|
| 190 | + private function _build_uri($user_id) { |
|
| 191 | 191 | |
| 192 | 192 | // Get the user, return false in case of failure. |
| 193 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 193 | + if (false === ($user = get_userdata($user_id))) { |
|
| 194 | 194 | return false; |
| 195 | 195 | }; |
| 196 | 196 | |
| 197 | 197 | // If the nicename is not set, return a failure. |
| 198 | - if ( empty( $user->user_nicename ) ) { |
|
| 198 | + if (empty($user->user_nicename)) { |
|
| 199 | 199 | return false; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 202 | + return wl_configuration_get_redlink_dataset_uri()."/user/$user->user_nicename"; |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | /** |
@@ -212,9 +212,9 @@ discard block |
||
| 212 | 212 | * |
| 213 | 213 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
| 214 | 214 | */ |
| 215 | - private function _set_uri( $user_id, $user_uri ) { |
|
| 215 | + private function _set_uri($user_id, $user_uri) { |
|
| 216 | 216 | |
| 217 | - return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 217 | + return update_user_meta($user_id, self::URI_META_KEY, $user_uri); |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | // /** |
@@ -295,15 +295,15 @@ discard block |
||
| 295 | 295 | * |
| 296 | 296 | * @param integer $user_id The ID of the user |
| 297 | 297 | */ |
| 298 | - public function deny_editor_entity_create( $user_id ) { |
|
| 298 | + public function deny_editor_entity_create($user_id) { |
|
| 299 | 299 | |
| 300 | 300 | // Bail out if the user is not an editor. |
| 301 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 301 | + if ( ! $this->is_editor($user_id)) { |
|
| 302 | 302 | return; |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | // The user explicitly do not have the capability. |
| 306 | - update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 306 | + update_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes'); |
|
| 307 | 307 | |
| 308 | 308 | } |
| 309 | 309 | |
@@ -315,15 +315,15 @@ discard block |
||
| 315 | 315 | * |
| 316 | 316 | * @param integer $user_id The ID of the user |
| 317 | 317 | */ |
| 318 | - public function allow_editor_entity_create( $user_id ) { |
|
| 318 | + public function allow_editor_entity_create($user_id) { |
|
| 319 | 319 | |
| 320 | 320 | // Bail out if the user is not an editor. |
| 321 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 321 | + if ( ! $this->is_editor($user_id)) { |
|
| 322 | 322 | return; |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | // The user explicitly do not have the capability. |
| 326 | - delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 326 | + delete_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY); |
|
| 327 | 327 | |
| 328 | 328 | } |
| 329 | 329 | |
@@ -336,9 +336,9 @@ discard block |
||
| 336 | 336 | * |
| 337 | 337 | * @return int bool True if editing is denied otherwise false. |
| 338 | 338 | */ |
| 339 | - public function is_deny_editor_entity_create( $user_id ) { |
|
| 339 | + public function is_deny_editor_entity_create($user_id) { |
|
| 340 | 340 | |
| 341 | - return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 341 | + return 'yes' === get_user_option(self::DENY_ENTITY_CREATE_META_KEY, $user_id); |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | /** |
@@ -351,13 +351,13 @@ discard block |
||
| 351 | 351 | * |
| 352 | 352 | * @return bool True if the {@link WP_User} is an editor otherwise false. |
| 353 | 353 | */ |
| 354 | - public function is_editor( $user_id ) { |
|
| 354 | + public function is_editor($user_id) { |
|
| 355 | 355 | |
| 356 | 356 | // Get the user. |
| 357 | - $user = get_user_by( 'id', $user_id ); |
|
| 357 | + $user = get_user_by('id', $user_id); |
|
| 358 | 358 | |
| 359 | 359 | // Return true, if the user is found and has the `editor` role. |
| 360 | - return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 360 | + return is_a($user, 'WP_User') && in_array('editor', (array) $user->roles); |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | /** |
@@ -369,15 +369,15 @@ discard block |
||
| 369 | 369 | * |
| 370 | 370 | * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
| 371 | 371 | */ |
| 372 | - public function editor_can_create_entities( $user_id ) { |
|
| 372 | + public function editor_can_create_entities($user_id) { |
|
| 373 | 373 | |
| 374 | 374 | // Return true if not an editor. |
| 375 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 375 | + if ( ! $this->is_editor($user_id)) { |
|
| 376 | 376 | return true; |
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | // Check if the user explicitly denied. |
| 380 | - return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 380 | + return ! $this->is_deny_editor_entity_create($user_id); |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | /** |
@@ -395,7 +395,7 @@ discard block |
||
| 395 | 395 | * |
| 396 | 396 | * @return array The capabilities array. |
| 397 | 397 | */ |
| 398 | - public function has_cap( $allcaps, $cap, $args ) { |
|
| 398 | + public function has_cap($allcaps, $cap, $args) { |
|
| 399 | 399 | /* |
| 400 | 400 | * For entity management/editing related capabilities |
| 401 | 401 | * check that an editor was not explicitly denied (in user profile) |
@@ -406,26 +406,26 @@ discard block |
||
| 406 | 406 | * Need protection against the case of edit_user and likes which do not |
| 407 | 407 | * require a capability, just request one. |
| 408 | 408 | */ |
| 409 | - if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
| 409 | + if (empty($cap) || ! isset($cap[0])) { |
|
| 410 | 410 | return $allcaps; |
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | if ( |
| 414 | - ( 'edit_wordlift_entity' === $cap[0] ) || |
|
| 415 | - ( 'edit_wordlift_entities' === $cap[0] ) || |
|
| 416 | - ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
| 417 | - ( 'publish_wordlift_entities' === $cap[0] ) || |
|
| 418 | - ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
| 419 | - ( 'delete_wordlift_entity' === $cap[0] ) || |
|
| 420 | - ( 'delete_wordlift_entities' === $cap[0] ) || |
|
| 421 | - ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
| 422 | - ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
| 423 | - ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
| 414 | + ('edit_wordlift_entity' === $cap[0]) || |
|
| 415 | + ('edit_wordlift_entities' === $cap[0]) || |
|
| 416 | + ('edit_others_wordlift_entities' === $cap[0]) || |
|
| 417 | + ('publish_wordlift_entities' === $cap[0]) || |
|
| 418 | + ('read_private_wordlift_entities' === $cap[0]) || |
|
| 419 | + ('delete_wordlift_entity' === $cap[0]) || |
|
| 420 | + ('delete_wordlift_entities' === $cap[0]) || |
|
| 421 | + ('delete_others_wordlift_entities' === $cap[0]) || |
|
| 422 | + ('delete_published_wordlift_entities' === $cap[0]) || |
|
| 423 | + ('delete_private_wordlift_entities' === $cap[0]) |
|
| 424 | 424 | ) { |
| 425 | 425 | $user_id = $args[1]; |
| 426 | 426 | |
| 427 | - if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 428 | - $allcaps[ $cap[0] ] = false; |
|
| 427 | + if ( ! $this->editor_can_create_entities($user_id)) { |
|
| 428 | + $allcaps[$cap[0]] = false; |
|
| 429 | 429 | } |
| 430 | 430 | } |
| 431 | 431 | |
@@ -447,36 +447,36 @@ discard block |
||
| 447 | 447 | * @return null Null if the `meta_key` is not `Wordlift_User_Service::ENTITY_META_KEY` |
| 448 | 448 | * or if the author has not changed. |
| 449 | 449 | */ |
| 450 | - public function update_user_metadata( $null, $object_id, $meta_key, $meta_value, $prev_value ) { |
|
| 450 | + public function update_user_metadata($null, $object_id, $meta_key, $meta_value, $prev_value) { |
|
| 451 | 451 | // Bail if the meta key is not the author meta. |
| 452 | - if ( $meta_key !== Wordlift_User_Service::ENTITY_META_KEY ) { |
|
| 452 | + if ($meta_key !== Wordlift_User_Service::ENTITY_META_KEY) { |
|
| 453 | 453 | return null; |
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | // Check whether the user is associated with any of the existing publishers/ |
| 457 | - $entity_id = $this->get_entity( $object_id ); |
|
| 457 | + $entity_id = $this->get_entity($object_id); |
|
| 458 | 458 | |
| 459 | - if ( false === $entity_id ) { |
|
| 459 | + if (false === $entity_id) { |
|
| 460 | 460 | // An error occurred. |
| 461 | - $this->log_service->error( "An error occurred: entity_id can't be false." ); |
|
| 461 | + $this->log_service->error("An error occurred: entity_id can't be false."); |
|
| 462 | 462 | |
| 463 | 463 | return; |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | // Get the old uri if the entity is set.. |
| 467 | - $old_uri = ! empty( $entity_id ) |
|
| 468 | - ? $this->entity_service->get_uri( $entity_id ) |
|
| 469 | - : $this->get_uri( $object_id ); |
|
| 467 | + $old_uri = ! empty($entity_id) |
|
| 468 | + ? $this->entity_service->get_uri($entity_id) |
|
| 469 | + : $this->get_uri($object_id); |
|
| 470 | 470 | |
| 471 | 471 | // Get the new user uri's. |
| 472 | - $new_uri = $this->entity_service->get_uri( $meta_value ); |
|
| 472 | + $new_uri = $this->entity_service->get_uri($meta_value); |
|
| 473 | 473 | |
| 474 | 474 | // Bail if the uri is the same. |
| 475 | - if ( $old_uri === $new_uri ) { |
|
| 475 | + if ($old_uri === $new_uri) { |
|
| 476 | 476 | return null; |
| 477 | 477 | } |
| 478 | 478 | |
| 479 | - $this->update_author( $old_uri, $new_uri ); |
|
| 479 | + $this->update_author($old_uri, $new_uri); |
|
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | /** |
@@ -495,28 +495,28 @@ discard block |
||
| 495 | 495 | * @return null Null if the `meta_key` is not `Wordlift_User_Service::ENTITY_META_KEY` |
| 496 | 496 | * or if the author has not changed. |
| 497 | 497 | */ |
| 498 | - public function delete_user_metadata( $null, $object_id, $meta_key, $meta_value, $delete_all ) { |
|
| 498 | + public function delete_user_metadata($null, $object_id, $meta_key, $meta_value, $delete_all) { |
|
| 499 | 499 | // Bail if the meta key is not the author meta. |
| 500 | - if ( $meta_key !== Wordlift_User_Service::ENTITY_META_KEY ) { |
|
| 500 | + if ($meta_key !== Wordlift_User_Service::ENTITY_META_KEY) { |
|
| 501 | 501 | return null; |
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | // Check whether the user is associated with any of the existing publishers/ |
| 505 | - $entity_id = $this->get_entity( $object_id ); |
|
| 505 | + $entity_id = $this->get_entity($object_id); |
|
| 506 | 506 | |
| 507 | - if ( false === $entity_id ) { |
|
| 507 | + if (false === $entity_id) { |
|
| 508 | 508 | // An error occurred. |
| 509 | - $this->log_service->error( "An error occurred: entity_id can't be false." ); |
|
| 509 | + $this->log_service->error("An error occurred: entity_id can't be false."); |
|
| 510 | 510 | |
| 511 | 511 | return; |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | 514 | // Get the old uri if the entity is set. |
| 515 | - $old_uri = $this->entity_service->get_uri( $entity_id ); |
|
| 515 | + $old_uri = $this->entity_service->get_uri($entity_id); |
|
| 516 | 516 | |
| 517 | - $new_uri = $this->get_uri( $object_id ); |
|
| 517 | + $new_uri = $this->get_uri($object_id); |
|
| 518 | 518 | |
| 519 | - $this->update_author( $old_uri, $new_uri ); |
|
| 519 | + $this->update_author($old_uri, $new_uri); |
|
| 520 | 520 | |
| 521 | 521 | } |
| 522 | 522 | |
@@ -528,11 +528,11 @@ discard block |
||
| 528 | 528 | * @param string $old_uri The old uri to remove. |
| 529 | 529 | * @param string $new_uri The new uri to add. |
| 530 | 530 | */ |
| 531 | - private function update_author( $old_uri, $new_uri ) { |
|
| 531 | + private function update_author($old_uri, $new_uri) { |
|
| 532 | 532 | // Bail in case one of the uris is empty. |
| 533 | - if ( empty( $old_uri ) || empty( $new_uri ) ) { |
|
| 533 | + if (empty($old_uri) || empty($new_uri)) { |
|
| 534 | 534 | // An error occurred. |
| 535 | - $this->log_service->error( "An error occurred: old_uri and/or new_uri can't be null." ); |
|
| 535 | + $this->log_service->error("An error occurred: old_uri and/or new_uri can't be null."); |
|
| 536 | 536 | |
| 537 | 537 | return; |
| 538 | 538 | } |
@@ -541,15 +541,15 @@ discard block |
||
| 541 | 541 | $query = sprintf( |
| 542 | 542 | 'DELETE { ?s <%1$s> <%2$s> } INSERT { ?s <%1$s> <%3$s> } WHERE { ?s <%1$s> <%2$s> }', |
| 543 | 543 | // Schema:author triple. |
| 544 | - $this->sparql_service->escape_uri( Wordlift_Query_Builder::SCHEMA_AUTHOR_URI ), |
|
| 544 | + $this->sparql_service->escape_uri(Wordlift_Query_Builder::SCHEMA_AUTHOR_URI), |
|
| 545 | 545 | // Old author uri to remove, |
| 546 | - $this->sparql_service->escape_uri( $old_uri ), |
|
| 546 | + $this->sparql_service->escape_uri($old_uri), |
|
| 547 | 547 | // New author uri to add, |
| 548 | - $this->sparql_service->escape_uri( $new_uri ) |
|
| 548 | + $this->sparql_service->escape_uri($new_uri) |
|
| 549 | 549 | ); |
| 550 | 550 | |
| 551 | 551 | // Execute the query and update the author. |
| 552 | - $this->sparql_service->execute( $query ); |
|
| 552 | + $this->sparql_service->execute($query); |
|
| 553 | 553 | |
| 554 | 554 | } |
| 555 | 555 | |
@@ -13,121 +13,121 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | class Wordlift_Schema_Url_Property_Service extends Wordlift_Property_Service { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The meta key used to store data for this property. We don't use wl_url to |
|
| 18 | - * avoid potential confusion about other URLs. |
|
| 19 | - * |
|
| 20 | - * @since 3.6.0 |
|
| 21 | - */ |
|
| 22 | - const META_KEY = 'wl_schema_url'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * {@inheritdoc} |
|
| 26 | - */ |
|
| 27 | - public function get_rdf_predicate() { |
|
| 28 | - |
|
| 29 | - return 'http://schema.org/url'; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * {@inheritdoc} |
|
| 34 | - */ |
|
| 35 | - public function get_rdf_data_type() { |
|
| 36 | - |
|
| 37 | - return 'xsd:anyURI'; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * {@inheritdoc} |
|
| 42 | - */ |
|
| 43 | - public function get_data_type() { |
|
| 44 | - |
|
| 45 | - return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * {@inheritdoc} |
|
| 50 | - */ |
|
| 51 | - public function get_cardinality() { |
|
| 52 | - |
|
| 53 | - return INF; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - */ |
|
| 59 | - public function get_metabox_class() { |
|
| 60 | - |
|
| 61 | - return 'WL_Metabox_Field'; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * {@inheritdoc} |
|
| 66 | - */ |
|
| 67 | - public function get_metabox_label() { |
|
| 68 | - |
|
| 69 | - return __( 'Web Site(s)', 'wordlift' ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @var |
|
| 74 | - */ |
|
| 75 | - private $sparql_service; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Create a Wordlift_Schema_Url_Property_Service instance. |
|
| 79 | - * @since 3.6.0 |
|
| 80 | - * |
|
| 81 | - * @param Wordlift_Sparql_Service $sparql_service |
|
| 82 | - */ |
|
| 83 | - public function __construct( $sparql_service ) { |
|
| 84 | - parent::__construct(); |
|
| 85 | - |
|
| 86 | - // Finally listen for metadata requests for this field. |
|
| 87 | - $this->add_filter_get_post_metadata(); |
|
| 88 | - |
|
| 89 | - $this->sparql_service = $sparql_service; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Get the schema:url value for the specified post/entity. |
|
| 94 | - * |
|
| 95 | - * @since 3.6.0 |
|
| 96 | - * |
|
| 97 | - * @param int $post_id The post id. |
|
| 98 | - * |
|
| 99 | - * @return array|NULL The schema:url value or NULL if not set. |
|
| 100 | - */ |
|
| 101 | - public function get( $post_id ) { |
|
| 102 | - |
|
| 103 | - // Get the schema:url values set in WP. |
|
| 104 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 105 | - |
|
| 106 | - // If the property has never been set, we set its default value the first |
|
| 107 | - // time to <permalink>. |
|
| 108 | - if ( empty( $values ) ) { |
|
| 109 | - return array( '<permalink>' ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // If there's only one value and that value is empty, we return NULL, i.e. |
|
| 113 | - // variable not set. |
|
| 114 | - if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
| 115 | - return null; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // Finally return whatever values the editor set. |
|
| 119 | - return $values; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * {@inheritdoc} |
|
| 124 | - */ |
|
| 125 | - public function sanitize( $value ) { |
|
| 126 | - |
|
| 127 | - // TODO: check that it's an URL or that is <permalink> |
|
| 128 | - |
|
| 129 | - return $value; |
|
| 130 | - } |
|
| 16 | + /** |
|
| 17 | + * The meta key used to store data for this property. We don't use wl_url to |
|
| 18 | + * avoid potential confusion about other URLs. |
|
| 19 | + * |
|
| 20 | + * @since 3.6.0 |
|
| 21 | + */ |
|
| 22 | + const META_KEY = 'wl_schema_url'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * {@inheritdoc} |
|
| 26 | + */ |
|
| 27 | + public function get_rdf_predicate() { |
|
| 28 | + |
|
| 29 | + return 'http://schema.org/url'; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * {@inheritdoc} |
|
| 34 | + */ |
|
| 35 | + public function get_rdf_data_type() { |
|
| 36 | + |
|
| 37 | + return 'xsd:anyURI'; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * {@inheritdoc} |
|
| 42 | + */ |
|
| 43 | + public function get_data_type() { |
|
| 44 | + |
|
| 45 | + return Wordlift_Schema_Service::DATA_TYPE_URI; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * {@inheritdoc} |
|
| 50 | + */ |
|
| 51 | + public function get_cardinality() { |
|
| 52 | + |
|
| 53 | + return INF; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + */ |
|
| 59 | + public function get_metabox_class() { |
|
| 60 | + |
|
| 61 | + return 'WL_Metabox_Field'; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * {@inheritdoc} |
|
| 66 | + */ |
|
| 67 | + public function get_metabox_label() { |
|
| 68 | + |
|
| 69 | + return __( 'Web Site(s)', 'wordlift' ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @var |
|
| 74 | + */ |
|
| 75 | + private $sparql_service; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Create a Wordlift_Schema_Url_Property_Service instance. |
|
| 79 | + * @since 3.6.0 |
|
| 80 | + * |
|
| 81 | + * @param Wordlift_Sparql_Service $sparql_service |
|
| 82 | + */ |
|
| 83 | + public function __construct( $sparql_service ) { |
|
| 84 | + parent::__construct(); |
|
| 85 | + |
|
| 86 | + // Finally listen for metadata requests for this field. |
|
| 87 | + $this->add_filter_get_post_metadata(); |
|
| 88 | + |
|
| 89 | + $this->sparql_service = $sparql_service; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Get the schema:url value for the specified post/entity. |
|
| 94 | + * |
|
| 95 | + * @since 3.6.0 |
|
| 96 | + * |
|
| 97 | + * @param int $post_id The post id. |
|
| 98 | + * |
|
| 99 | + * @return array|NULL The schema:url value or NULL if not set. |
|
| 100 | + */ |
|
| 101 | + public function get( $post_id ) { |
|
| 102 | + |
|
| 103 | + // Get the schema:url values set in WP. |
|
| 104 | + $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 105 | + |
|
| 106 | + // If the property has never been set, we set its default value the first |
|
| 107 | + // time to <permalink>. |
|
| 108 | + if ( empty( $values ) ) { |
|
| 109 | + return array( '<permalink>' ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // If there's only one value and that value is empty, we return NULL, i.e. |
|
| 113 | + // variable not set. |
|
| 114 | + if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
| 115 | + return null; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // Finally return whatever values the editor set. |
|
| 119 | + return $values; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * {@inheritdoc} |
|
| 124 | + */ |
|
| 125 | + public function sanitize( $value ) { |
|
| 126 | + |
|
| 127 | + // TODO: check that it's an URL or that is <permalink> |
|
| 128 | + |
|
| 129 | + return $value; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | 132 | // /** |
| 133 | 133 | // * Generate an insert query that inserts the schema:url values for the specified |
@@ -159,63 +159,63 @@ discard block |
||
| 159 | 159 | // return $q->build(); |
| 160 | 160 | // } |
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * Get direct calls to read this meta and alter the response according to our |
|
| 164 | - * own strategy, i.e. if a value has never been set for this meta, then return |
|
| 165 | - * <permalink>. |
|
| 166 | - * |
|
| 167 | - * @since 3.6.0 |
|
| 168 | - * |
|
| 169 | - * @param mixed $value The original value. |
|
| 170 | - * @param int $object_id The post id. |
|
| 171 | - * @param string $meta_key The meta key. We expect wl_schema_url or we return straight the value. |
|
| 172 | - * @param bool $single Whether to return a single value. |
|
| 173 | - * |
|
| 174 | - * @return array|mixed|NULL|string |
|
| 175 | - */ |
|
| 176 | - public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
| 162 | + /** |
|
| 163 | + * Get direct calls to read this meta and alter the response according to our |
|
| 164 | + * own strategy, i.e. if a value has never been set for this meta, then return |
|
| 165 | + * <permalink>. |
|
| 166 | + * |
|
| 167 | + * @since 3.6.0 |
|
| 168 | + * |
|
| 169 | + * @param mixed $value The original value. |
|
| 170 | + * @param int $object_id The post id. |
|
| 171 | + * @param string $meta_key The meta key. We expect wl_schema_url or we return straight the value. |
|
| 172 | + * @param bool $single Whether to return a single value. |
|
| 173 | + * |
|
| 174 | + * @return array|mixed|NULL|string |
|
| 175 | + */ |
|
| 176 | + public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
| 177 | 177 | |
| 178 | - // It's not us, return the value. |
|
| 179 | - if ( self::META_KEY !== $meta_key ) { |
|
| 180 | - return $value; |
|
| 181 | - } |
|
| 178 | + // It's not us, return the value. |
|
| 179 | + if ( self::META_KEY !== $meta_key ) { |
|
| 180 | + return $value; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - $this->remove_filter_get_post_metadata(); |
|
| 183 | + $this->remove_filter_get_post_metadata(); |
|
| 184 | 184 | |
| 185 | - $new_value = $this->get( $object_id ); |
|
| 185 | + $new_value = $this->get( $object_id ); |
|
| 186 | 186 | |
| 187 | - $this->add_filter_get_post_metadata(); |
|
| 187 | + $this->add_filter_get_post_metadata(); |
|
| 188 | 188 | |
| 189 | - // If we must return a single value, but we don't have a value, then return an empty string. |
|
| 190 | - if ( $single && ( is_null( $new_value ) || empty( $new_value ) ) ) { |
|
| 191 | - return ''; |
|
| 192 | - } |
|
| 189 | + // If we must return a single value, but we don't have a value, then return an empty string. |
|
| 190 | + if ( $single && ( is_null( $new_value ) || empty( $new_value ) ) ) { |
|
| 191 | + return ''; |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - // If we have a value and we need to return it as single, return the first value. |
|
| 195 | - if ( $single ) { |
|
| 196 | - return $new_value[0]; |
|
| 197 | - } |
|
| 194 | + // If we have a value and we need to return it as single, return the first value. |
|
| 195 | + if ( $single ) { |
|
| 196 | + return $new_value[0]; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - // Otherwise return the array. |
|
| 200 | - return $new_value; |
|
| 201 | - } |
|
| 199 | + // Otherwise return the array. |
|
| 200 | + return $new_value; |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - private function add_filter_get_post_metadata() { |
|
| 203 | + private function add_filter_get_post_metadata() { |
|
| 204 | 204 | |
| 205 | - add_filter( 'get_post_metadata', array( |
|
| 206 | - $this, |
|
| 207 | - 'get_post_metadata', |
|
| 208 | - ), 10, 4 ); |
|
| 205 | + add_filter( 'get_post_metadata', array( |
|
| 206 | + $this, |
|
| 207 | + 'get_post_metadata', |
|
| 208 | + ), 10, 4 ); |
|
| 209 | 209 | |
| 210 | - } |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - private function remove_filter_get_post_metadata() { |
|
| 212 | + private function remove_filter_get_post_metadata() { |
|
| 213 | 213 | |
| 214 | - remove_filter( 'get_post_metadata', array( |
|
| 215 | - $this, |
|
| 216 | - 'get_post_metadata', |
|
| 217 | - ), 10 ); |
|
| 214 | + remove_filter( 'get_post_metadata', array( |
|
| 215 | + $this, |
|
| 216 | + 'get_post_metadata', |
|
| 217 | + ), 10 ); |
|
| 218 | 218 | |
| 219 | - } |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | 221 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | public function get_metabox_label() { |
| 68 | 68 | |
| 69 | - return __( 'Web Site(s)', 'wordlift' ); |
|
| 69 | + return __('Web Site(s)', 'wordlift'); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @param Wordlift_Sparql_Service $sparql_service |
| 82 | 82 | */ |
| 83 | - public function __construct( $sparql_service ) { |
|
| 83 | + public function __construct($sparql_service) { |
|
| 84 | 84 | parent::__construct(); |
| 85 | 85 | |
| 86 | 86 | // Finally listen for metadata requests for this field. |
@@ -98,20 +98,20 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * @return array|NULL The schema:url value or NULL if not set. |
| 100 | 100 | */ |
| 101 | - public function get( $post_id ) { |
|
| 101 | + public function get($post_id) { |
|
| 102 | 102 | |
| 103 | 103 | // Get the schema:url values set in WP. |
| 104 | - $values = get_post_meta( $post_id, self::META_KEY, false ); |
|
| 104 | + $values = get_post_meta($post_id, self::META_KEY, false); |
|
| 105 | 105 | |
| 106 | 106 | // If the property has never been set, we set its default value the first |
| 107 | 107 | // time to <permalink>. |
| 108 | - if ( empty( $values ) ) { |
|
| 109 | - return array( '<permalink>' ); |
|
| 108 | + if (empty($values)) { |
|
| 109 | + return array('<permalink>'); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // If there's only one value and that value is empty, we return NULL, i.e. |
| 113 | 113 | // variable not set. |
| 114 | - if ( 1 === count( $values ) && empty( $values[0] ) ) { |
|
| 114 | + if (1 === count($values) && empty($values[0])) { |
|
| 115 | 115 | return null; |
| 116 | 116 | } |
| 117 | 117 | |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | /** |
| 123 | 123 | * {@inheritdoc} |
| 124 | 124 | */ |
| 125 | - public function sanitize( $value ) { |
|
| 125 | + public function sanitize($value) { |
|
| 126 | 126 | |
| 127 | 127 | // TODO: check that it's an URL or that is <permalink> |
| 128 | 128 | |
@@ -173,26 +173,26 @@ discard block |
||
| 173 | 173 | * |
| 174 | 174 | * @return array|mixed|NULL|string |
| 175 | 175 | */ |
| 176 | - public function get_post_metadata( $value, $object_id, $meta_key, $single ) { |
|
| 176 | + public function get_post_metadata($value, $object_id, $meta_key, $single) { |
|
| 177 | 177 | |
| 178 | 178 | // It's not us, return the value. |
| 179 | - if ( self::META_KEY !== $meta_key ) { |
|
| 179 | + if (self::META_KEY !== $meta_key) { |
|
| 180 | 180 | return $value; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | $this->remove_filter_get_post_metadata(); |
| 184 | 184 | |
| 185 | - $new_value = $this->get( $object_id ); |
|
| 185 | + $new_value = $this->get($object_id); |
|
| 186 | 186 | |
| 187 | 187 | $this->add_filter_get_post_metadata(); |
| 188 | 188 | |
| 189 | 189 | // If we must return a single value, but we don't have a value, then return an empty string. |
| 190 | - if ( $single && ( is_null( $new_value ) || empty( $new_value ) ) ) { |
|
| 190 | + if ($single && (is_null($new_value) || empty($new_value))) { |
|
| 191 | 191 | return ''; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | // If we have a value and we need to return it as single, return the first value. |
| 195 | - if ( $single ) { |
|
| 195 | + if ($single) { |
|
| 196 | 196 | return $new_value[0]; |
| 197 | 197 | } |
| 198 | 198 | |
@@ -202,19 +202,19 @@ discard block |
||
| 202 | 202 | |
| 203 | 203 | private function add_filter_get_post_metadata() { |
| 204 | 204 | |
| 205 | - add_filter( 'get_post_metadata', array( |
|
| 205 | + add_filter('get_post_metadata', array( |
|
| 206 | 206 | $this, |
| 207 | 207 | 'get_post_metadata', |
| 208 | - ), 10, 4 ); |
|
| 208 | + ), 10, 4); |
|
| 209 | 209 | |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | private function remove_filter_get_post_metadata() { |
| 213 | 213 | |
| 214 | - remove_filter( 'get_post_metadata', array( |
|
| 214 | + remove_filter('get_post_metadata', array( |
|
| 215 | 215 | $this, |
| 216 | 216 | 'get_post_metadata', |
| 217 | - ), 10 ); |
|
| 217 | + ), 10); |
|
| 218 | 218 | |
| 219 | 219 | } |
| 220 | 220 | |