@@ -20,128 +20,128 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Rebuild_Service extends Wordlift_Listable { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * A {@link Wordlift_Log_Service} instance. |
|
| 25 | - * |
|
| 26 | - * @since 3.6.0 |
|
| 27 | - * @access private |
|
| 28 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | - */ |
|
| 30 | - private $log; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * A {@link Wordlift_Sparql_Service} instance. |
|
| 34 | - * |
|
| 35 | - * @since 3.6.0 |
|
| 36 | - * @access private |
|
| 37 | - * @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
| 38 | - */ |
|
| 39 | - private $sparql_service; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The {@link Wordlift_Uri_Service} instance. |
|
| 43 | - * |
|
| 44 | - * @since 3.15.0 |
|
| 45 | - * @access private |
|
| 46 | - * @var \Wordlift_Uri_Service $uri_service The {@link Wordlift_Uri_Service} instance. |
|
| 47 | - */ |
|
| 48 | - private $uri_service; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Create an instance of Wordlift_Rebuild_Service. |
|
| 52 | - * |
|
| 53 | - * @since 3.6.0 |
|
| 54 | - * |
|
| 55 | - * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
| 56 | - * @param \Wordlift_Uri_Service $uri_service |
|
| 57 | - */ |
|
| 58 | - public function __construct( $sparql_service, $uri_service ) { |
|
| 59 | - |
|
| 60 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 61 | - |
|
| 62 | - $this->sparql_service = $sparql_service; |
|
| 63 | - $this->uri_service = $uri_service; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
| 68 | - * it with local data. |
|
| 69 | - * |
|
| 70 | - * @since 3.6.0 |
|
| 71 | - */ |
|
| 72 | - public function rebuild() { |
|
| 73 | - |
|
| 74 | - ob_clean(); |
|
| 75 | - |
|
| 76 | - // Give ourselves some time to process the data. |
|
| 77 | - set_time_limit( 21600 ); // 6 hours |
|
| 78 | - |
|
| 79 | - // Send textual output. |
|
| 80 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 81 | - |
|
| 82 | - // We start at 0 by default and get to max. |
|
| 83 | - $offset = $_GET['offset'] ?: 0; |
|
| 84 | - $limit = $_GET['limit'] ?: 1; |
|
| 85 | - $max = $offset + $limit; |
|
| 86 | - |
|
| 87 | - // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
|
| 88 | - $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 89 | - |
|
| 90 | - // If we're starting at offset 0, then delete existing URIs and data from |
|
| 91 | - // the remote dataset. |
|
| 92 | - if ( 0 === $offset ) { |
|
| 93 | - |
|
| 94 | - // Clear out all generated URIs, since the dataset URI might have changed |
|
| 95 | - // in the process. |
|
| 96 | - $this->uri_service->delete_all(); |
|
| 97 | - |
|
| 98 | - // Delete all the triples in the remote dataset. |
|
| 99 | - $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 100 | - |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // Go through the list of published entities and posts and call the (legacy) |
|
| 104 | - // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 105 | - // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 106 | - // and will cycle through all the posts w/ a very small memory footprint |
|
| 107 | - // in order to avoid memory errors. |
|
| 108 | - |
|
| 109 | - $count = 0; |
|
| 110 | - $log = $this->log; |
|
| 111 | - $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 112 | - $count ++; |
|
| 113 | - $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 114 | - wl_linked_data_save_post( $post->ID ); |
|
| 115 | - }, array( |
|
| 116 | - 'post_status' => 'publish', |
|
| 117 | - ), $offset, $max ); |
|
| 118 | - |
|
| 119 | - // Redirect to the next chunk. |
|
| 120 | - if ( $count == $limit ) { |
|
| 121 | - $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 122 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Flush the cache. |
|
| 126 | - Wordlift_File_Cache_Service::flush_all(); |
|
| 127 | - |
|
| 128 | - // Rebuild also the references. |
|
| 129 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild_references') ); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
| 134 | - * |
|
| 135 | - * @since 3.9.8 |
|
| 136 | - * |
|
| 137 | - * @param string $url The URL to redirect to. |
|
| 138 | - */ |
|
| 139 | - public function redirect( $url ) { |
|
| 140 | - |
|
| 141 | - ob_clean(); |
|
| 142 | - |
|
| 143 | - @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 144 | - ?> |
|
| 23 | + /** |
|
| 24 | + * A {@link Wordlift_Log_Service} instance. |
|
| 25 | + * |
|
| 26 | + * @since 3.6.0 |
|
| 27 | + * @access private |
|
| 28 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 29 | + */ |
|
| 30 | + private $log; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * A {@link Wordlift_Sparql_Service} instance. |
|
| 34 | + * |
|
| 35 | + * @since 3.6.0 |
|
| 36 | + * @access private |
|
| 37 | + * @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
| 38 | + */ |
|
| 39 | + private $sparql_service; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The {@link Wordlift_Uri_Service} instance. |
|
| 43 | + * |
|
| 44 | + * @since 3.15.0 |
|
| 45 | + * @access private |
|
| 46 | + * @var \Wordlift_Uri_Service $uri_service The {@link Wordlift_Uri_Service} instance. |
|
| 47 | + */ |
|
| 48 | + private $uri_service; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Create an instance of Wordlift_Rebuild_Service. |
|
| 52 | + * |
|
| 53 | + * @since 3.6.0 |
|
| 54 | + * |
|
| 55 | + * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
| 56 | + * @param \Wordlift_Uri_Service $uri_service |
|
| 57 | + */ |
|
| 58 | + public function __construct( $sparql_service, $uri_service ) { |
|
| 59 | + |
|
| 60 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 61 | + |
|
| 62 | + $this->sparql_service = $sparql_service; |
|
| 63 | + $this->uri_service = $uri_service; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
| 68 | + * it with local data. |
|
| 69 | + * |
|
| 70 | + * @since 3.6.0 |
|
| 71 | + */ |
|
| 72 | + public function rebuild() { |
|
| 73 | + |
|
| 74 | + ob_clean(); |
|
| 75 | + |
|
| 76 | + // Give ourselves some time to process the data. |
|
| 77 | + set_time_limit( 21600 ); // 6 hours |
|
| 78 | + |
|
| 79 | + // Send textual output. |
|
| 80 | + header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 81 | + |
|
| 82 | + // We start at 0 by default and get to max. |
|
| 83 | + $offset = $_GET['offset'] ?: 0; |
|
| 84 | + $limit = $_GET['limit'] ?: 1; |
|
| 85 | + $max = $offset + $limit; |
|
| 86 | + |
|
| 87 | + // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
|
| 88 | + $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 89 | + |
|
| 90 | + // If we're starting at offset 0, then delete existing URIs and data from |
|
| 91 | + // the remote dataset. |
|
| 92 | + if ( 0 === $offset ) { |
|
| 93 | + |
|
| 94 | + // Clear out all generated URIs, since the dataset URI might have changed |
|
| 95 | + // in the process. |
|
| 96 | + $this->uri_service->delete_all(); |
|
| 97 | + |
|
| 98 | + // Delete all the triples in the remote dataset. |
|
| 99 | + $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 100 | + |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // Go through the list of published entities and posts and call the (legacy) |
|
| 104 | + // `wl_linked_data_save_post` function for each one. We're using the `process` |
|
| 105 | + // function which is provided by the parent `Wordlift_Listable` abstract class |
|
| 106 | + // and will cycle through all the posts w/ a very small memory footprint |
|
| 107 | + // in order to avoid memory errors. |
|
| 108 | + |
|
| 109 | + $count = 0; |
|
| 110 | + $log = $this->log; |
|
| 111 | + $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 112 | + $count ++; |
|
| 113 | + $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 114 | + wl_linked_data_save_post( $post->ID ); |
|
| 115 | + }, array( |
|
| 116 | + 'post_status' => 'publish', |
|
| 117 | + ), $offset, $max ); |
|
| 118 | + |
|
| 119 | + // Redirect to the next chunk. |
|
| 120 | + if ( $count == $limit ) { |
|
| 121 | + $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 122 | + $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Flush the cache. |
|
| 126 | + Wordlift_File_Cache_Service::flush_all(); |
|
| 127 | + |
|
| 128 | + // Rebuild also the references. |
|
| 129 | + $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild_references') ); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
| 134 | + * |
|
| 135 | + * @since 3.9.8 |
|
| 136 | + * |
|
| 137 | + * @param string $url The URL to redirect to. |
|
| 138 | + */ |
|
| 139 | + public function redirect( $url ) { |
|
| 140 | + |
|
| 141 | + ob_clean(); |
|
| 142 | + |
|
| 143 | + @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 144 | + ?> |
|
| 145 | 145 | <html> |
| 146 | 146 | <head> |
| 147 | 147 | <meta http-equiv="refresh" |
@@ -153,37 +153,37 @@ discard block |
||
| 153 | 153 | </html> |
| 154 | 154 | <?php |
| 155 | 155 | |
| 156 | - exit; |
|
| 156 | + exit; |
|
| 157 | 157 | |
| 158 | - } |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * List the items starting at the specified offset and up to the specified limit. |
|
| 162 | - * |
|
| 160 | + /** |
|
| 161 | + * List the items starting at the specified offset and up to the specified limit. |
|
| 162 | + * |
|
| 163 | 163 | * @since 3.19.5 remove Polylang hooks. |
| 164 | - * @since 3.6.0 |
|
| 165 | - * |
|
| 166 | - * @param int $offset The start offset. |
|
| 167 | - * @param int $limit The maximum number of items to return. |
|
| 168 | - * @param array $args Additional arguments. |
|
| 169 | - * |
|
| 170 | - * @return array A array of items (or an empty array if no items are found). |
|
| 171 | - */ |
|
| 172 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 173 | - |
|
| 174 | - $actual_args = wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 175 | - 'offset' => $offset, |
|
| 176 | - 'numberposts' => $limit, |
|
| 177 | - 'fields' => 'all', |
|
| 178 | - 'orderby' => 'ID', |
|
| 179 | - 'order' => 'ASC', |
|
| 180 | - 'post_status' => 'any', |
|
| 181 | - 'cache_results' => false, |
|
| 182 | - ) ) ); |
|
| 183 | - |
|
| 184 | - $this->log->trace( 'Using ' . var_export( $actual_args, true ) ); |
|
| 185 | - |
|
| 186 | - return get_posts( $actual_args ); |
|
| 187 | - } |
|
| 164 | + * @since 3.6.0 |
|
| 165 | + * |
|
| 166 | + * @param int $offset The start offset. |
|
| 167 | + * @param int $limit The maximum number of items to return. |
|
| 168 | + * @param array $args Additional arguments. |
|
| 169 | + * |
|
| 170 | + * @return array A array of items (or an empty array if no items are found). |
|
| 171 | + */ |
|
| 172 | + function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 173 | + |
|
| 174 | + $actual_args = wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 175 | + 'offset' => $offset, |
|
| 176 | + 'numberposts' => $limit, |
|
| 177 | + 'fields' => 'all', |
|
| 178 | + 'orderby' => 'ID', |
|
| 179 | + 'order' => 'ASC', |
|
| 180 | + 'post_status' => 'any', |
|
| 181 | + 'cache_results' => false, |
|
| 182 | + ) ) ); |
|
| 183 | + |
|
| 184 | + $this->log->trace( 'Using ' . var_export( $actual_args, true ) ); |
|
| 185 | + |
|
| 186 | + return get_posts( $actual_args ); |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | 189 | } |
@@ -55,9 +55,9 @@ discard block |
||
| 55 | 55 | * @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
| 56 | 56 | * @param \Wordlift_Uri_Service $uri_service |
| 57 | 57 | */ |
| 58 | - public function __construct( $sparql_service, $uri_service ) { |
|
| 58 | + public function __construct($sparql_service, $uri_service) { |
|
| 59 | 59 | |
| 60 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
| 60 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rebuild_Service'); |
|
| 61 | 61 | |
| 62 | 62 | $this->sparql_service = $sparql_service; |
| 63 | 63 | $this->uri_service = $uri_service; |
@@ -74,10 +74,10 @@ discard block |
||
| 74 | 74 | ob_clean(); |
| 75 | 75 | |
| 76 | 76 | // Give ourselves some time to process the data. |
| 77 | - set_time_limit( 21600 ); // 6 hours |
|
| 77 | + set_time_limit(21600); // 6 hours |
|
| 78 | 78 | |
| 79 | 79 | // Send textual output. |
| 80 | - header( 'Content-type: text/plain; charset=utf-8' ); |
|
| 80 | + header('Content-type: text/plain; charset=utf-8'); |
|
| 81 | 81 | |
| 82 | 82 | // We start at 0 by default and get to max. |
| 83 | 83 | $offset = $_GET['offset'] ?: 0; |
@@ -85,18 +85,18 @@ discard block |
||
| 85 | 85 | $max = $offset + $limit; |
| 86 | 86 | |
| 87 | 87 | // Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
| 88 | - $asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
| 88 | + $asynchronous = isset($_GET['wl-async']) && 'true' === $_GET['wl-async']; |
|
| 89 | 89 | |
| 90 | 90 | // If we're starting at offset 0, then delete existing URIs and data from |
| 91 | 91 | // the remote dataset. |
| 92 | - if ( 0 === $offset ) { |
|
| 92 | + if (0 === $offset) { |
|
| 93 | 93 | |
| 94 | 94 | // Clear out all generated URIs, since the dataset URI might have changed |
| 95 | 95 | // in the process. |
| 96 | 96 | $this->uri_service->delete_all(); |
| 97 | 97 | |
| 98 | 98 | // Delete all the triples in the remote dataset. |
| 99 | - $this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
| 99 | + $this->sparql_service->execute('DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };'); |
|
| 100 | 100 | |
| 101 | 101 | } |
| 102 | 102 | |
@@ -108,25 +108,25 @@ discard block |
||
| 108 | 108 | |
| 109 | 109 | $count = 0; |
| 110 | 110 | $log = $this->log; |
| 111 | - $this->process( function ( $post ) use ( &$count, $log ) { |
|
| 112 | - $count ++; |
|
| 113 | - $log->trace( "Going to save post $count, ID $post->ID..." ); |
|
| 114 | - wl_linked_data_save_post( $post->ID ); |
|
| 111 | + $this->process(function($post) use (&$count, $log) { |
|
| 112 | + $count++; |
|
| 113 | + $log->trace("Going to save post $count, ID $post->ID..."); |
|
| 114 | + wl_linked_data_save_post($post->ID); |
|
| 115 | 115 | }, array( |
| 116 | 116 | 'post_status' => 'publish', |
| 117 | - ), $offset, $max ); |
|
| 117 | + ), $offset, $max); |
|
| 118 | 118 | |
| 119 | 119 | // Redirect to the next chunk. |
| 120 | - if ( $count == $limit ) { |
|
| 121 | - $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
| 122 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 120 | + if ($count == $limit) { |
|
| 121 | + $log->trace('Redirecting to post #'.($offset + 1).'...'); |
|
| 122 | + $this->redirect(admin_url('admin-ajax.php?action=wl_rebuild&offset='.($offset + $limit).'&limit='.$limit.'&wl-async='.($asynchronous ? 'true' : 'false'))); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Flush the cache. |
| 126 | 126 | Wordlift_File_Cache_Service::flush_all(); |
| 127 | 127 | |
| 128 | 128 | // Rebuild also the references. |
| 129 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild_references') ); |
|
| 129 | + $this->redirect(admin_url('admin-ajax.php?action=wl_rebuild_references')); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -136,16 +136,16 @@ discard block |
||
| 136 | 136 | * |
| 137 | 137 | * @param string $url The URL to redirect to. |
| 138 | 138 | */ |
| 139 | - public function redirect( $url ) { |
|
| 139 | + public function redirect($url) { |
|
| 140 | 140 | |
| 141 | 141 | ob_clean(); |
| 142 | 142 | |
| 143 | - @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 143 | + @header('Content-Type: text/html; charset='.get_option('blog_charset')); |
|
| 144 | 144 | ?> |
| 145 | 145 | <html> |
| 146 | 146 | <head> |
| 147 | 147 | <meta http-equiv="refresh" |
| 148 | - content="0; <?php echo esc_attr( $url ); ?>"> |
|
| 148 | + content="0; <?php echo esc_attr($url); ?>"> |
|
| 149 | 149 | </head> |
| 150 | 150 | <body> |
| 151 | 151 | Rebuilding, please wait... |
@@ -169,9 +169,9 @@ discard block |
||
| 169 | 169 | * |
| 170 | 170 | * @return array A array of items (or an empty array if no items are found). |
| 171 | 171 | */ |
| 172 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 172 | + function find($offset = 0, $limit = 10, $args = array()) { |
|
| 173 | 173 | |
| 174 | - $actual_args = wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 174 | + $actual_args = wp_parse_args($args, Wordlift_Entity_Service::add_criterias(array( |
|
| 175 | 175 | 'offset' => $offset, |
| 176 | 176 | 'numberposts' => $limit, |
| 177 | 177 | 'fields' => 'all', |
@@ -179,11 +179,11 @@ discard block |
||
| 179 | 179 | 'order' => 'ASC', |
| 180 | 180 | 'post_status' => 'any', |
| 181 | 181 | 'cache_results' => false, |
| 182 | - ) ) ); |
|
| 182 | + ))); |
|
| 183 | 183 | |
| 184 | - $this->log->trace( 'Using ' . var_export( $actual_args, true ) ); |
|
| 184 | + $this->log->trace('Using '.var_export($actual_args, true)); |
|
| 185 | 185 | |
| 186 | - return get_posts( $actual_args ); |
|
| 186 | + return get_posts($actual_args); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | } |
@@ -7,466 +7,466 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Entity_Service { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * The Log service. |
|
| 12 | - * |
|
| 13 | - * @since 3.2.0 |
|
| 14 | - * @access private |
|
| 15 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | - */ |
|
| 17 | - private $log; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The UI service. |
|
| 21 | - * |
|
| 22 | - * @since 3.2.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | - */ |
|
| 26 | - private $ui_service; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The {@link Wordlift_Relation_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.15.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 34 | - */ |
|
| 35 | - private $relation_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.16.3 |
|
| 41 | - * @access private |
|
| 42 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 43 | - */ |
|
| 44 | - private $entity_uri_service; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The entity post type name. |
|
| 48 | - * |
|
| 49 | - * @since 3.1.0 |
|
| 50 | - */ |
|
| 51 | - const TYPE_NAME = 'entity'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The alternative label meta key. |
|
| 55 | - * |
|
| 56 | - * @since 3.2.0 |
|
| 57 | - */ |
|
| 58 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The alternative label input template. |
|
| 62 | - * |
|
| 63 | - * @since 3.2.0 |
|
| 64 | - */ |
|
| 65 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 66 | - const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 10 | + /** |
|
| 11 | + * The Log service. |
|
| 12 | + * |
|
| 13 | + * @since 3.2.0 |
|
| 14 | + * @access private |
|
| 15 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 16 | + */ |
|
| 17 | + private $log; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The UI service. |
|
| 21 | + * |
|
| 22 | + * @since 3.2.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_UI_Service $ui_service The UI service. |
|
| 25 | + */ |
|
| 26 | + private $ui_service; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The {@link Wordlift_Relation_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.15.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 34 | + */ |
|
| 35 | + private $relation_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.16.3 |
|
| 41 | + * @access private |
|
| 42 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 43 | + */ |
|
| 44 | + private $entity_uri_service; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The entity post type name. |
|
| 48 | + * |
|
| 49 | + * @since 3.1.0 |
|
| 50 | + */ |
|
| 51 | + const TYPE_NAME = 'entity'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The alternative label meta key. |
|
| 55 | + * |
|
| 56 | + * @since 3.2.0 |
|
| 57 | + */ |
|
| 58 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The alternative label input template. |
|
| 62 | + * |
|
| 63 | + * @since 3.2.0 |
|
| 64 | + */ |
|
| 65 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 66 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 67 | 67 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 68 | 68 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 69 | 69 | <button class="button wl-delete-button">%s</button> |
| 70 | 70 | </div>'; |
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * A singleton instance of the Entity service. |
|
| 74 | - * |
|
| 75 | - * @since 3.2.0 |
|
| 76 | - * @access private |
|
| 77 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 78 | - */ |
|
| 79 | - private static $instance; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Create a Wordlift_Entity_Service instance. |
|
| 83 | - * |
|
| 84 | - * @since 3.2.0 |
|
| 85 | - * |
|
| 86 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 87 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 88 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 89 | - */ |
|
| 90 | - public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 91 | - |
|
| 92 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 93 | - |
|
| 94 | - $this->ui_service = $ui_service; |
|
| 95 | - $this->relation_service = $relation_service; |
|
| 96 | - $this->entity_uri_service = $entity_uri_service; |
|
| 97 | - |
|
| 98 | - // Set the singleton instance. |
|
| 99 | - self::$instance = $this; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Get the singleton instance of the Entity service. |
|
| 104 | - * |
|
| 105 | - * @since 3.2.0 |
|
| 106 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 107 | - */ |
|
| 108 | - public static function get_instance() { |
|
| 109 | - |
|
| 110 | - return self::$instance; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 115 | - * something which is not an article. |
|
| 116 | - * |
|
| 117 | - * @since 3.1.0 |
|
| 118 | - * |
|
| 119 | - * @param int $post_id A post id. |
|
| 120 | - * |
|
| 121 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | - */ |
|
| 123 | - public function is_entity( $post_id ) { |
|
| 124 | - |
|
| 125 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 126 | - |
|
| 127 | - if ( is_wp_error( $terms ) ) { |
|
| 128 | - $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 129 | - |
|
| 130 | - return false; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if ( empty( $terms ) ) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /* |
|
| 72 | + /** |
|
| 73 | + * A singleton instance of the Entity service. |
|
| 74 | + * |
|
| 75 | + * @since 3.2.0 |
|
| 76 | + * @access private |
|
| 77 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 78 | + */ |
|
| 79 | + private static $instance; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Create a Wordlift_Entity_Service instance. |
|
| 83 | + * |
|
| 84 | + * @since 3.2.0 |
|
| 85 | + * |
|
| 86 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 87 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 88 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 89 | + */ |
|
| 90 | + public function __construct( $ui_service, $relation_service, $entity_uri_service ) { |
|
| 91 | + |
|
| 92 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 93 | + |
|
| 94 | + $this->ui_service = $ui_service; |
|
| 95 | + $this->relation_service = $relation_service; |
|
| 96 | + $this->entity_uri_service = $entity_uri_service; |
|
| 97 | + |
|
| 98 | + // Set the singleton instance. |
|
| 99 | + self::$instance = $this; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Get the singleton instance of the Entity service. |
|
| 104 | + * |
|
| 105 | + * @since 3.2.0 |
|
| 106 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 107 | + */ |
|
| 108 | + public static function get_instance() { |
|
| 109 | + |
|
| 110 | + return self::$instance; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 115 | + * something which is not an article. |
|
| 116 | + * |
|
| 117 | + * @since 3.1.0 |
|
| 118 | + * |
|
| 119 | + * @param int $post_id A post id. |
|
| 120 | + * |
|
| 121 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 122 | + */ |
|
| 123 | + public function is_entity( $post_id ) { |
|
| 124 | + |
|
| 125 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 126 | + |
|
| 127 | + if ( is_wp_error( $terms ) ) { |
|
| 128 | + $this->log->error( "Cannot get the terms for post $post_id: " . $terms->get_error_message() ); |
|
| 129 | + |
|
| 130 | + return false; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if ( empty( $terms ) ) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /* |
|
| 138 | 138 | * We don't consider an `article` to be an entity. |
| 139 | 139 | * |
| 140 | 140 | * @since 3.20.0 At least one associated mustn't be an `article`. |
| 141 | 141 | * |
| 142 | 142 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 143 | 143 | */ |
| 144 | - foreach ( $terms as $term ) { |
|
| 145 | - if ( 'article' !== $term->slug ) { |
|
| 146 | - return true; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the proper classification scope for a given entity post |
|
| 155 | - * |
|
| 156 | - * @since 3.5.0 |
|
| 157 | - * |
|
| 158 | - * @param integer $post_id An entity post id. |
|
| 159 | - * |
|
| 160 | - * @param string $default The default classification scope, `what` if not |
|
| 161 | - * provided. |
|
| 162 | - * |
|
| 163 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 164 | - */ |
|
| 165 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 166 | - |
|
| 167 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 168 | - return $default; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // Retrieve the entity type |
|
| 172 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 173 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 174 | - // Retrieve classification boxes configuration |
|
| 175 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 176 | - foreach ( $classification_boxes as $cb ) { |
|
| 177 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 178 | - return $cb['id']; |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $default; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Check whether a {@link WP_Post} is used. |
|
| 187 | - * |
|
| 188 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 189 | - * |
|
| 190 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 191 | - */ |
|
| 192 | - public function is_used( $post_id ) { |
|
| 193 | - |
|
| 194 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 195 | - return null; |
|
| 196 | - } |
|
| 197 | - // Retrieve the post |
|
| 198 | - $entity = get_post( $post_id ); |
|
| 199 | - |
|
| 200 | - global $wpdb; |
|
| 201 | - // Retrieve Wordlift relation instances table name |
|
| 202 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 203 | - |
|
| 204 | - // Check is it's referenced / related to another post / entity |
|
| 205 | - $stmt = $wpdb->prepare( |
|
| 206 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 207 | - $entity->ID |
|
| 208 | - ); |
|
| 209 | - |
|
| 210 | - // Perform the query |
|
| 211 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 212 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 213 | - if ( 0 < $relation_instances ) { |
|
| 214 | - return true; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Check if the entity uri is used as meta_value |
|
| 218 | - $stmt = $wpdb->prepare( |
|
| 219 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 220 | - $entity->ID, |
|
| 221 | - wl_get_entity_uri( $entity->ID ) |
|
| 222 | - ); |
|
| 223 | - // Perform the query |
|
| 224 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 225 | - |
|
| 226 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 227 | - if ( 0 < $meta_instances ) { |
|
| 228 | - return true; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // If we are here, it means the current entity is not used at the moment |
|
| 232 | - return false; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 237 | - * |
|
| 238 | - * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 239 | - * @since 3.2.0 |
|
| 240 | - * |
|
| 241 | - * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 242 | - * |
|
| 243 | - * @param string $uri The entity URI. |
|
| 244 | - * |
|
| 245 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 246 | - */ |
|
| 247 | - public function get_entity_post_by_uri( $uri ) { |
|
| 248 | - |
|
| 249 | - return $this->entity_uri_service->get_entity( $uri ); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 254 | - * we check that the post we're saving is the current post. |
|
| 255 | - * |
|
| 256 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 257 | - * |
|
| 258 | - * @since 3.2.0 |
|
| 259 | - * |
|
| 260 | - * @param int $post_id Post ID. |
|
| 261 | - * @param WP_Post $post Post object. |
|
| 262 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 263 | - */ |
|
| 264 | - public function save_post( $post_id, $post, $update ) { |
|
| 265 | - |
|
| 266 | - // Avoid doing anything if post is autosave or a revision. |
|
| 267 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 268 | - return; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // We're setting the alternative label that have been provided via the UI |
|
| 272 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 273 | - // programmatically by some other function: we need to check therefore if |
|
| 274 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 275 | - // |
|
| 276 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 277 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 278 | - return; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - // Get the alt labels from the request (or empty array). |
|
| 282 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 283 | - |
|
| 284 | - // Set the alternative labels. |
|
| 285 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 286 | - |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Set the alternative labels. |
|
| 291 | - * |
|
| 292 | - * @since 3.2.0 |
|
| 293 | - * |
|
| 294 | - * @param int $post_id The post id. |
|
| 295 | - * @param array $alt_labels An array of labels. |
|
| 296 | - */ |
|
| 297 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 298 | - |
|
| 299 | - // Force $alt_labels to be an array |
|
| 300 | - if ( ! is_array( $alt_labels ) ) { |
|
| 301 | - $alt_labels = array( $alt_labels ); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 305 | - |
|
| 306 | - // Delete all the existing alternate labels. |
|
| 307 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 308 | - |
|
| 309 | - // Set the alternative labels. |
|
| 310 | - foreach ( $alt_labels as $alt_label ) { |
|
| 311 | - if ( ! empty( $alt_label ) ) { |
|
| 312 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Retrieve the alternate labels. |
|
| 320 | - * |
|
| 321 | - * @since 3.2.0 |
|
| 322 | - * |
|
| 323 | - * @param int $post_id Post id. |
|
| 324 | - * |
|
| 325 | - * @return mixed An array of alternative labels. |
|
| 326 | - */ |
|
| 327 | - public function get_alternative_labels( $post_id ) { |
|
| 328 | - |
|
| 329 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 334 | - * |
|
| 335 | - * @since 3.12.0 |
|
| 336 | - * |
|
| 337 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 338 | - * |
|
| 339 | - * @return array An array with the entity title and labels. |
|
| 340 | - */ |
|
| 341 | - public function get_labels( $post_id ) { |
|
| 342 | - |
|
| 343 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 348 | - * |
|
| 349 | - * @since 3.2.0 |
|
| 350 | - * |
|
| 351 | - * @param WP_Post $post Post object. |
|
| 352 | - */ |
|
| 353 | - public function edit_form_before_permalink( $post ) { |
|
| 354 | - |
|
| 355 | - // If it's not an entity, return. |
|
| 356 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 357 | - return; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - // Print the input template. |
|
| 361 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 362 | - |
|
| 363 | - // Print all the currently set alternative labels. |
|
| 364 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 365 | - |
|
| 366 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 367 | - |
|
| 368 | - }; |
|
| 369 | - |
|
| 370 | - // Print the button. |
|
| 371 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 372 | - |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Get the URI for the entity with the specified post id. |
|
| 377 | - * |
|
| 378 | - * @since 3.6.0 |
|
| 379 | - * |
|
| 380 | - * @param int $post_id The entity post id. |
|
| 381 | - * |
|
| 382 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 383 | - */ |
|
| 384 | - public function get_uri( $post_id ) { |
|
| 385 | - |
|
| 386 | - // If a null is given, nothing to do |
|
| 387 | - if ( null == $post_id ) { |
|
| 388 | - return null; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 392 | - |
|
| 393 | - // If the dataset uri is not properly configured, null is returned |
|
| 394 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 395 | - return null; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - // Set the URI if it isn't set yet. |
|
| 399 | - $post_status = get_post_status( $post_id ); |
|
| 400 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 401 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 402 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - return $uri; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Get the alternative label input HTML code. |
|
| 411 | - * |
|
| 412 | - * @since 3.2.0 |
|
| 413 | - * |
|
| 414 | - * @param string $value The input value. |
|
| 415 | - * |
|
| 416 | - * @return string The input HTML code. |
|
| 417 | - */ |
|
| 418 | - private function get_alternative_label_input( $value = '' ) { |
|
| 419 | - |
|
| 420 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Get the number of entity posts published in this blog. |
|
| 425 | - * |
|
| 426 | - * @since 3.6.0 |
|
| 427 | - * |
|
| 428 | - * @return int The number of published entity posts. |
|
| 429 | - */ |
|
| 430 | - public function count() { |
|
| 431 | - |
|
| 432 | - $posts = get_posts( $this->add_criterias( array( |
|
| 433 | - 'post_status' => 'any', |
|
| 434 | - 'numberposts' => - 1, |
|
| 435 | - ) ) ); |
|
| 436 | - |
|
| 437 | - return count( $posts ); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 442 | - * call. |
|
| 443 | - * |
|
| 444 | - * @since 3.15.0 |
|
| 445 | - * |
|
| 446 | - * @param array $args The arguments for a `get_posts` call. |
|
| 447 | - * |
|
| 448 | - * @return array The arguments for a `get_posts` call. |
|
| 449 | - */ |
|
| 450 | - public static function add_criterias( $args ) { |
|
| 451 | - |
|
| 452 | - // Build an optimal tax-query. |
|
| 453 | - $tax_query = array( |
|
| 454 | - 'relation' => 'AND', |
|
| 455 | - array( |
|
| 456 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 457 | - 'operator' => 'EXISTS', |
|
| 458 | - ), |
|
| 459 | - array( |
|
| 460 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 461 | - 'field' => 'slug', |
|
| 462 | - 'terms' => 'article', |
|
| 463 | - 'operator' => 'NOT IN', |
|
| 464 | - ), |
|
| 465 | - ); |
|
| 466 | - |
|
| 467 | - return $args + array( |
|
| 468 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 469 | - /* |
|
| 144 | + foreach ( $terms as $term ) { |
|
| 145 | + if ( 'article' !== $term->slug ) { |
|
| 146 | + return true; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the proper classification scope for a given entity post |
|
| 155 | + * |
|
| 156 | + * @since 3.5.0 |
|
| 157 | + * |
|
| 158 | + * @param integer $post_id An entity post id. |
|
| 159 | + * |
|
| 160 | + * @param string $default The default classification scope, `what` if not |
|
| 161 | + * provided. |
|
| 162 | + * |
|
| 163 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 164 | + */ |
|
| 165 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 166 | + |
|
| 167 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 168 | + return $default; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // Retrieve the entity type |
|
| 172 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 173 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 174 | + // Retrieve classification boxes configuration |
|
| 175 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 176 | + foreach ( $classification_boxes as $cb ) { |
|
| 177 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 178 | + return $cb['id']; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $default; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Check whether a {@link WP_Post} is used. |
|
| 187 | + * |
|
| 188 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 189 | + * |
|
| 190 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 191 | + */ |
|
| 192 | + public function is_used( $post_id ) { |
|
| 193 | + |
|
| 194 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 195 | + return null; |
|
| 196 | + } |
|
| 197 | + // Retrieve the post |
|
| 198 | + $entity = get_post( $post_id ); |
|
| 199 | + |
|
| 200 | + global $wpdb; |
|
| 201 | + // Retrieve Wordlift relation instances table name |
|
| 202 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 203 | + |
|
| 204 | + // Check is it's referenced / related to another post / entity |
|
| 205 | + $stmt = $wpdb->prepare( |
|
| 206 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 207 | + $entity->ID |
|
| 208 | + ); |
|
| 209 | + |
|
| 210 | + // Perform the query |
|
| 211 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 212 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 213 | + if ( 0 < $relation_instances ) { |
|
| 214 | + return true; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Check if the entity uri is used as meta_value |
|
| 218 | + $stmt = $wpdb->prepare( |
|
| 219 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 220 | + $entity->ID, |
|
| 221 | + wl_get_entity_uri( $entity->ID ) |
|
| 222 | + ); |
|
| 223 | + // Perform the query |
|
| 224 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 225 | + |
|
| 226 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 227 | + if ( 0 < $meta_instances ) { |
|
| 228 | + return true; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // If we are here, it means the current entity is not used at the moment |
|
| 232 | + return false; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 237 | + * |
|
| 238 | + * @since 3.16.3 deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 239 | + * @since 3.2.0 |
|
| 240 | + * |
|
| 241 | + * @deprecated in favor of Wordlift_Entity_Uri_Service->get_entity( $uri ); |
|
| 242 | + * |
|
| 243 | + * @param string $uri The entity URI. |
|
| 244 | + * |
|
| 245 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 246 | + */ |
|
| 247 | + public function get_entity_post_by_uri( $uri ) { |
|
| 248 | + |
|
| 249 | + return $this->entity_uri_service->get_entity( $uri ); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 254 | + * we check that the post we're saving is the current post. |
|
| 255 | + * |
|
| 256 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 257 | + * |
|
| 258 | + * @since 3.2.0 |
|
| 259 | + * |
|
| 260 | + * @param int $post_id Post ID. |
|
| 261 | + * @param WP_Post $post Post object. |
|
| 262 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 263 | + */ |
|
| 264 | + public function save_post( $post_id, $post, $update ) { |
|
| 265 | + |
|
| 266 | + // Avoid doing anything if post is autosave or a revision. |
|
| 267 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 268 | + return; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // We're setting the alternative label that have been provided via the UI |
|
| 272 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 273 | + // programmatically by some other function: we need to check therefore if |
|
| 274 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 275 | + // |
|
| 276 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 277 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 278 | + return; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + // Get the alt labels from the request (or empty array). |
|
| 282 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 283 | + |
|
| 284 | + // Set the alternative labels. |
|
| 285 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 286 | + |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Set the alternative labels. |
|
| 291 | + * |
|
| 292 | + * @since 3.2.0 |
|
| 293 | + * |
|
| 294 | + * @param int $post_id The post id. |
|
| 295 | + * @param array $alt_labels An array of labels. |
|
| 296 | + */ |
|
| 297 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 298 | + |
|
| 299 | + // Force $alt_labels to be an array |
|
| 300 | + if ( ! is_array( $alt_labels ) ) { |
|
| 301 | + $alt_labels = array( $alt_labels ); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 305 | + |
|
| 306 | + // Delete all the existing alternate labels. |
|
| 307 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 308 | + |
|
| 309 | + // Set the alternative labels. |
|
| 310 | + foreach ( $alt_labels as $alt_label ) { |
|
| 311 | + if ( ! empty( $alt_label ) ) { |
|
| 312 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Retrieve the alternate labels. |
|
| 320 | + * |
|
| 321 | + * @since 3.2.0 |
|
| 322 | + * |
|
| 323 | + * @param int $post_id Post id. |
|
| 324 | + * |
|
| 325 | + * @return mixed An array of alternative labels. |
|
| 326 | + */ |
|
| 327 | + public function get_alternative_labels( $post_id ) { |
|
| 328 | + |
|
| 329 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 334 | + * |
|
| 335 | + * @since 3.12.0 |
|
| 336 | + * |
|
| 337 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 338 | + * |
|
| 339 | + * @return array An array with the entity title and labels. |
|
| 340 | + */ |
|
| 341 | + public function get_labels( $post_id ) { |
|
| 342 | + |
|
| 343 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 348 | + * |
|
| 349 | + * @since 3.2.0 |
|
| 350 | + * |
|
| 351 | + * @param WP_Post $post Post object. |
|
| 352 | + */ |
|
| 353 | + public function edit_form_before_permalink( $post ) { |
|
| 354 | + |
|
| 355 | + // If it's not an entity, return. |
|
| 356 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 357 | + return; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + // Print the input template. |
|
| 361 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 362 | + |
|
| 363 | + // Print all the currently set alternative labels. |
|
| 364 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 365 | + |
|
| 366 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 367 | + |
|
| 368 | + }; |
|
| 369 | + |
|
| 370 | + // Print the button. |
|
| 371 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 372 | + |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Get the URI for the entity with the specified post id. |
|
| 377 | + * |
|
| 378 | + * @since 3.6.0 |
|
| 379 | + * |
|
| 380 | + * @param int $post_id The entity post id. |
|
| 381 | + * |
|
| 382 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 383 | + */ |
|
| 384 | + public function get_uri( $post_id ) { |
|
| 385 | + |
|
| 386 | + // If a null is given, nothing to do |
|
| 387 | + if ( null == $post_id ) { |
|
| 388 | + return null; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 392 | + |
|
| 393 | + // If the dataset uri is not properly configured, null is returned |
|
| 394 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 395 | + return null; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + // Set the URI if it isn't set yet. |
|
| 399 | + $post_status = get_post_status( $post_id ); |
|
| 400 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 401 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 402 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + return $uri; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Get the alternative label input HTML code. |
|
| 411 | + * |
|
| 412 | + * @since 3.2.0 |
|
| 413 | + * |
|
| 414 | + * @param string $value The input value. |
|
| 415 | + * |
|
| 416 | + * @return string The input HTML code. |
|
| 417 | + */ |
|
| 418 | + private function get_alternative_label_input( $value = '' ) { |
|
| 419 | + |
|
| 420 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Get the number of entity posts published in this blog. |
|
| 425 | + * |
|
| 426 | + * @since 3.6.0 |
|
| 427 | + * |
|
| 428 | + * @return int The number of published entity posts. |
|
| 429 | + */ |
|
| 430 | + public function count() { |
|
| 431 | + |
|
| 432 | + $posts = get_posts( $this->add_criterias( array( |
|
| 433 | + 'post_status' => 'any', |
|
| 434 | + 'numberposts' => - 1, |
|
| 435 | + ) ) ); |
|
| 436 | + |
|
| 437 | + return count( $posts ); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 442 | + * call. |
|
| 443 | + * |
|
| 444 | + * @since 3.15.0 |
|
| 445 | + * |
|
| 446 | + * @param array $args The arguments for a `get_posts` call. |
|
| 447 | + * |
|
| 448 | + * @return array The arguments for a `get_posts` call. |
|
| 449 | + */ |
|
| 450 | + public static function add_criterias( $args ) { |
|
| 451 | + |
|
| 452 | + // Build an optimal tax-query. |
|
| 453 | + $tax_query = array( |
|
| 454 | + 'relation' => 'AND', |
|
| 455 | + array( |
|
| 456 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 457 | + 'operator' => 'EXISTS', |
|
| 458 | + ), |
|
| 459 | + array( |
|
| 460 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 461 | + 'field' => 'slug', |
|
| 462 | + 'terms' => 'article', |
|
| 463 | + 'operator' => 'NOT IN', |
|
| 464 | + ), |
|
| 465 | + ); |
|
| 466 | + |
|
| 467 | + return $args + array( |
|
| 468 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 469 | + /* |
|
| 470 | 470 | * Ensure compatibility with Polylang. |
| 471 | 471 | * |
| 472 | 472 | * @see https://github.com/insideout10/wordlift-plugin/issues/855. |
@@ -474,102 +474,102 @@ discard block |
||
| 474 | 474 | * |
| 475 | 475 | * @since 3.19.5 |
| 476 | 476 | */ |
| 477 | - 'lang' => '', |
|
| 478 | - 'tax_query' => $tax_query, |
|
| 479 | - ); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Create a new entity. |
|
| 484 | - * |
|
| 485 | - * @since 3.9.0 |
|
| 486 | - * |
|
| 487 | - * @param string $name The entity name. |
|
| 488 | - * @param string $type_uri The entity's type URI. |
|
| 489 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 490 | - * @param string $status The post status, by default 'publish'. |
|
| 491 | - * |
|
| 492 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 493 | - */ |
|
| 494 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 495 | - |
|
| 496 | - // Create an entity for the publisher. |
|
| 497 | - $post_id = wp_insert_post( array( |
|
| 498 | - 'post_type' => self::TYPE_NAME, |
|
| 499 | - 'post_title' => $name, |
|
| 500 | - 'post_status' => $status, |
|
| 501 | - 'post_content' => '', |
|
| 502 | - ) ); |
|
| 503 | - |
|
| 504 | - // Return the error if any. |
|
| 505 | - if ( is_wp_error( $post_id ) ) { |
|
| 506 | - return $post_id; |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - // Set the entity logo. |
|
| 510 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 511 | - set_post_thumbnail( $post_id, $logo ); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - // Set the entity type. |
|
| 515 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 516 | - |
|
| 517 | - return $post_id; |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Get the entities related to the one with the specified id. By default only |
|
| 522 | - * published entities will be returned. |
|
| 523 | - * |
|
| 524 | - * @since 3.10.0 |
|
| 525 | - * |
|
| 526 | - * @param int $id The post id. |
|
| 527 | - * @param string $post_status The target post status (default = publish). |
|
| 528 | - * |
|
| 529 | - * @return array An array of post ids. |
|
| 530 | - */ |
|
| 531 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 532 | - |
|
| 533 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * Get the list of entities. |
|
| 538 | - * |
|
| 539 | - * @since 3.12.2 |
|
| 540 | - * |
|
| 541 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 542 | - * |
|
| 543 | - * @return array An array of entity posts. |
|
| 544 | - */ |
|
| 545 | - public function get( $params = array() ) { |
|
| 546 | - |
|
| 547 | - // Set the defaults. |
|
| 548 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 549 | - |
|
| 550 | - // Merge the defaults with the provided parameters. |
|
| 551 | - $args = wp_parse_args( $params, $defaults ); |
|
| 552 | - |
|
| 553 | - // Call the `get_posts` function. |
|
| 554 | - return get_posts( $args ); |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * The list of post type names which can be used for entities |
|
| 559 | - * |
|
| 560 | - * Criteria is that the post type is public. The list of valid post types |
|
| 561 | - * can be overridden with a filter. |
|
| 562 | - * |
|
| 563 | - * @since 3.15.0 |
|
| 564 | - * |
|
| 565 | - * @return array Array containing the names of the valid post types. |
|
| 566 | - */ |
|
| 567 | - static function valid_entity_post_types() { |
|
| 568 | - |
|
| 569 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 570 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 571 | - |
|
| 572 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 573 | - } |
|
| 477 | + 'lang' => '', |
|
| 478 | + 'tax_query' => $tax_query, |
|
| 479 | + ); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Create a new entity. |
|
| 484 | + * |
|
| 485 | + * @since 3.9.0 |
|
| 486 | + * |
|
| 487 | + * @param string $name The entity name. |
|
| 488 | + * @param string $type_uri The entity's type URI. |
|
| 489 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 490 | + * @param string $status The post status, by default 'publish'. |
|
| 491 | + * |
|
| 492 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 493 | + */ |
|
| 494 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 495 | + |
|
| 496 | + // Create an entity for the publisher. |
|
| 497 | + $post_id = wp_insert_post( array( |
|
| 498 | + 'post_type' => self::TYPE_NAME, |
|
| 499 | + 'post_title' => $name, |
|
| 500 | + 'post_status' => $status, |
|
| 501 | + 'post_content' => '', |
|
| 502 | + ) ); |
|
| 503 | + |
|
| 504 | + // Return the error if any. |
|
| 505 | + if ( is_wp_error( $post_id ) ) { |
|
| 506 | + return $post_id; |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + // Set the entity logo. |
|
| 510 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 511 | + set_post_thumbnail( $post_id, $logo ); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + // Set the entity type. |
|
| 515 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 516 | + |
|
| 517 | + return $post_id; |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Get the entities related to the one with the specified id. By default only |
|
| 522 | + * published entities will be returned. |
|
| 523 | + * |
|
| 524 | + * @since 3.10.0 |
|
| 525 | + * |
|
| 526 | + * @param int $id The post id. |
|
| 527 | + * @param string $post_status The target post status (default = publish). |
|
| 528 | + * |
|
| 529 | + * @return array An array of post ids. |
|
| 530 | + */ |
|
| 531 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 532 | + |
|
| 533 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Get the list of entities. |
|
| 538 | + * |
|
| 539 | + * @since 3.12.2 |
|
| 540 | + * |
|
| 541 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 542 | + * |
|
| 543 | + * @return array An array of entity posts. |
|
| 544 | + */ |
|
| 545 | + public function get( $params = array() ) { |
|
| 546 | + |
|
| 547 | + // Set the defaults. |
|
| 548 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 549 | + |
|
| 550 | + // Merge the defaults with the provided parameters. |
|
| 551 | + $args = wp_parse_args( $params, $defaults ); |
|
| 552 | + |
|
| 553 | + // Call the `get_posts` function. |
|
| 554 | + return get_posts( $args ); |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * The list of post type names which can be used for entities |
|
| 559 | + * |
|
| 560 | + * Criteria is that the post type is public. The list of valid post types |
|
| 561 | + * can be overridden with a filter. |
|
| 562 | + * |
|
| 563 | + * @since 3.15.0 |
|
| 564 | + * |
|
| 565 | + * @return array Array containing the names of the valid post types. |
|
| 566 | + */ |
|
| 567 | + static function valid_entity_post_types() { |
|
| 568 | + |
|
| 569 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 570 | + $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 571 | + |
|
| 572 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 573 | + } |
|
| 574 | 574 | |
| 575 | 575 | } |