@@ -7,134 +7,134 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Log_Service { |
| 9 | 9 | |
| 10 | - const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s'; |
|
| 11 | - |
|
| 12 | - const ERROR = 4; |
|
| 13 | - const WARN = 3; |
|
| 14 | - const INFO = 2; |
|
| 15 | - const DEBUG = 1; |
|
| 16 | - const TRACE = 0; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * The class related to the logs. |
|
| 20 | - * |
|
| 21 | - * @since 1.0.0 |
|
| 22 | - * @access private |
|
| 23 | - * @var string $class_name The class related to the logs. |
|
| 24 | - */ |
|
| 25 | - private $class_name; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The log levels for printing in log lines. |
|
| 29 | - * |
|
| 30 | - * @var array $levels An array of log levels. |
|
| 31 | - */ |
|
| 32 | - private static $levels = array( |
|
| 33 | - self::TRACE => 'TRACE', |
|
| 34 | - self::DEBUG => 'DEBUG', |
|
| 35 | - self::INFO => 'INFO', |
|
| 36 | - self::WARN => 'WARN', |
|
| 37 | - self::ERROR => 'ERROR', |
|
| 38 | - ); |
|
| 10 | + const MESSAGE_TEMPLATE = '%-6s [%-40.40s] %s'; |
|
| 11 | + |
|
| 12 | + const ERROR = 4; |
|
| 13 | + const WARN = 3; |
|
| 14 | + const INFO = 2; |
|
| 15 | + const DEBUG = 1; |
|
| 16 | + const TRACE = 0; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * The class related to the logs. |
|
| 20 | + * |
|
| 21 | + * @since 1.0.0 |
|
| 22 | + * @access private |
|
| 23 | + * @var string $class_name The class related to the logs. |
|
| 24 | + */ |
|
| 25 | + private $class_name; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The log levels for printing in log lines. |
|
| 29 | + * |
|
| 30 | + * @var array $levels An array of log levels. |
|
| 31 | + */ |
|
| 32 | + private static $levels = array( |
|
| 33 | + self::TRACE => 'TRACE', |
|
| 34 | + self::DEBUG => 'DEBUG', |
|
| 35 | + self::INFO => 'INFO', |
|
| 36 | + self::WARN => 'WARN', |
|
| 37 | + self::ERROR => 'ERROR', |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * A singleton instance for legacy logging. |
|
| 42 | - * |
|
| 43 | - * @since 3.10.0 |
|
| 44 | - * @access private |
|
| 45 | - * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging. |
|
| 46 | - */ |
|
| 47 | - private static $instance = null; |
|
| 40 | + /** |
|
| 41 | + * A singleton instance for legacy logging. |
|
| 42 | + * |
|
| 43 | + * @since 3.10.0 |
|
| 44 | + * @access private |
|
| 45 | + * @var \Wordlift_Log_Service $instance A singleton instance for legacy logging. |
|
| 46 | + */ |
|
| 47 | + private static $instance = null; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Create an instance of the Log service. |
|
| 51 | - * @since 1.0.0 |
|
| 52 | - * |
|
| 53 | - * @param string $class_name The class related to the logs. |
|
| 54 | - */ |
|
| 55 | - public function __construct( $class_name ) { |
|
| 49 | + /** |
|
| 50 | + * Create an instance of the Log service. |
|
| 51 | + * @since 1.0.0 |
|
| 52 | + * |
|
| 53 | + * @param string $class_name The class related to the logs. |
|
| 54 | + */ |
|
| 55 | + public function __construct( $class_name ) { |
|
| 56 | 56 | |
| 57 | - $this->class_name = $class_name; |
|
| 57 | + $this->class_name = $class_name; |
|
| 58 | 58 | |
| 59 | - } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Get the ROOT logger. |
|
| 63 | - * |
|
| 64 | - * @since 3.10.0 |
|
| 65 | - * |
|
| 66 | - * @return \Wordlift_Log_Service A singleton instance for legacy logging. |
|
| 67 | - */ |
|
| 68 | - public static function get_instance() { |
|
| 61 | + /** |
|
| 62 | + * Get the ROOT logger. |
|
| 63 | + * |
|
| 64 | + * @since 3.10.0 |
|
| 65 | + * |
|
| 66 | + * @return \Wordlift_Log_Service A singleton instance for legacy logging. |
|
| 67 | + */ |
|
| 68 | + public static function get_instance() { |
|
| 69 | 69 | |
| 70 | - return self::$instance ?: self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
| 71 | - } |
|
| 70 | + return self::$instance ?: self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | |
| 74 | - public static function get_logger( $class_name ) { |
|
| 74 | + public static function get_logger( $class_name ) { |
|
| 75 | 75 | |
| 76 | - return new Wordlift_Log_Service( $class_name ); |
|
| 76 | + return new Wordlift_Log_Service( $class_name ); |
|
| 77 | 77 | |
| 78 | - } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Log a message. |
|
| 82 | - * |
|
| 83 | - * @since 1.0.0 |
|
| 84 | - * |
|
| 85 | - * @param string $level The log level. |
|
| 86 | - * @param string $message The message to log. |
|
| 87 | - */ |
|
| 88 | - public function log( $level, $message ) { |
|
| 80 | + /** |
|
| 81 | + * Log a message. |
|
| 82 | + * |
|
| 83 | + * @since 1.0.0 |
|
| 84 | + * |
|
| 85 | + * @param string $level The log level. |
|
| 86 | + * @param string $message The message to log. |
|
| 87 | + */ |
|
| 88 | + public function log( $level, $message ) { |
|
| 89 | 89 | |
| 90 | - // Bail out if WP debug is disabled. |
|
| 91 | - if ( $level < self::INFO && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
| 92 | - return; |
|
| 93 | - } |
|
| 90 | + // Bail out if WP debug is disabled. |
|
| 91 | + if ( $level < self::INFO && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
| 92 | + return; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - // Bail out if the log message is below the minimum log level. |
|
| 96 | - if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
| 97 | - return; |
|
| 98 | - } |
|
| 95 | + // Bail out if the log message is below the minimum log level. |
|
| 96 | + if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - // Bail out if there's a filter and we don't match it. |
|
| 101 | - if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$this->class_name($|,)/", WL_LOG_FILTER ) ) { |
|
| 102 | - return; |
|
| 103 | - } |
|
| 100 | + // Bail out if there's a filter and we don't match it. |
|
| 101 | + if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$this->class_name($|,)/", WL_LOG_FILTER ) ) { |
|
| 102 | + return; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - // Finally log the message. |
|
| 106 | - error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
| 105 | + // Finally log the message. |
|
| 106 | + error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
| 107 | 107 | |
| 108 | - } |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - public function error( $message ) { |
|
| 110 | + public function error( $message ) { |
|
| 111 | 111 | |
| 112 | - $this->log( self::ERROR, $message ); |
|
| 112 | + $this->log( self::ERROR, $message ); |
|
| 113 | 113 | |
| 114 | - } |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - public function warn( $message ) { |
|
| 116 | + public function warn( $message ) { |
|
| 117 | 117 | |
| 118 | - $this->log( self::WARN, $message ); |
|
| 118 | + $this->log( self::WARN, $message ); |
|
| 119 | 119 | |
| 120 | - } |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - public function info( $message ) { |
|
| 122 | + public function info( $message ) { |
|
| 123 | 123 | |
| 124 | - $this->log( self::INFO, $message ); |
|
| 124 | + $this->log( self::INFO, $message ); |
|
| 125 | 125 | |
| 126 | - } |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - public function debug( $message ) { |
|
| 128 | + public function debug( $message ) { |
|
| 129 | 129 | |
| 130 | - $this->log( self::DEBUG, $message ); |
|
| 130 | + $this->log( self::DEBUG, $message ); |
|
| 131 | 131 | |
| 132 | - } |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - public function trace( $message ) { |
|
| 134 | + public function trace( $message ) { |
|
| 135 | 135 | |
| 136 | - $this->log( self::TRACE, $message ); |
|
| 136 | + $this->log( self::TRACE, $message ); |
|
| 137 | 137 | |
| 138 | - } |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | 140 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * |
| 53 | 53 | * @param string $class_name The class related to the logs. |
| 54 | 54 | */ |
| 55 | - public function __construct( $class_name ) { |
|
| 55 | + public function __construct($class_name) { |
|
| 56 | 56 | |
| 57 | 57 | $this->class_name = $class_name; |
| 58 | 58 | |
@@ -67,13 +67,13 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public static function get_instance() { |
| 69 | 69 | |
| 70 | - return self::$instance ?: self::$instance = new Wordlift_Log_Service( 'ROOT' ); |
|
| 70 | + return self::$instance ?: self::$instance = new Wordlift_Log_Service('ROOT'); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | |
| 74 | - public static function get_logger( $class_name ) { |
|
| 74 | + public static function get_logger($class_name) { |
|
| 75 | 75 | |
| 76 | - return new Wordlift_Log_Service( $class_name ); |
|
| 76 | + return new Wordlift_Log_Service($class_name); |
|
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | |
@@ -85,55 +85,55 @@ discard block |
||
| 85 | 85 | * @param string $level The log level. |
| 86 | 86 | * @param string $message The message to log. |
| 87 | 87 | */ |
| 88 | - public function log( $level, $message ) { |
|
| 88 | + public function log($level, $message) { |
|
| 89 | 89 | |
| 90 | 90 | // Bail out if WP debug is disabled. |
| 91 | - if ( $level < self::INFO && ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) ) { |
|
| 91 | + if ($level < self::INFO && ( ! defined('WP_DEBUG') || false === WP_DEBUG)) { |
|
| 92 | 92 | return; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // Bail out if the log message is below the minimum log level. |
| 96 | - if ( defined( 'WL_LOG_LEVEL' ) && $level < intval( WL_LOG_LEVEL ) ) { |
|
| 96 | + if (defined('WL_LOG_LEVEL') && $level < intval(WL_LOG_LEVEL)) { |
|
| 97 | 97 | return; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Bail out if there's a filter and we don't match it. |
| 101 | - if ( defined( 'WL_LOG_FILTER' ) && 1 !== preg_match( "/(^|,)$this->class_name($|,)/", WL_LOG_FILTER ) ) { |
|
| 101 | + if (defined('WL_LOG_FILTER') && 1 !== preg_match("/(^|,)$this->class_name($|,)/", WL_LOG_FILTER)) { |
|
| 102 | 102 | return; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // Finally log the message. |
| 106 | - error_log( sprintf( self::MESSAGE_TEMPLATE, self::$levels[ $level ], $this->class_name, is_array( $message ) ? implode( ', ', $message ) : $message ) ); |
|
| 106 | + error_log(sprintf(self::MESSAGE_TEMPLATE, self::$levels[$level], $this->class_name, is_array($message) ? implode(', ', $message) : $message)); |
|
| 107 | 107 | |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - public function error( $message ) { |
|
| 110 | + public function error($message) { |
|
| 111 | 111 | |
| 112 | - $this->log( self::ERROR, $message ); |
|
| 112 | + $this->log(self::ERROR, $message); |
|
| 113 | 113 | |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - public function warn( $message ) { |
|
| 116 | + public function warn($message) { |
|
| 117 | 117 | |
| 118 | - $this->log( self::WARN, $message ); |
|
| 118 | + $this->log(self::WARN, $message); |
|
| 119 | 119 | |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | - public function info( $message ) { |
|
| 122 | + public function info($message) { |
|
| 123 | 123 | |
| 124 | - $this->log( self::INFO, $message ); |
|
| 124 | + $this->log(self::INFO, $message); |
|
| 125 | 125 | |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - public function debug( $message ) { |
|
| 128 | + public function debug($message) { |
|
| 129 | 129 | |
| 130 | - $this->log( self::DEBUG, $message ); |
|
| 130 | + $this->log(self::DEBUG, $message); |
|
| 131 | 131 | |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - public function trace( $message ) { |
|
| 134 | + public function trace($message) { |
|
| 135 | 135 | |
| 136 | - $this->log( self::TRACE, $message ); |
|
| 136 | + $this->log(self::TRACE, $message); |
|
| 137 | 137 | |
| 138 | 138 | } |
| 139 | 139 | |
@@ -20,127 +20,127 @@ 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 | - $this->process( function ( $post ) use ( &$count ) { |
|
| 111 | - $count ++; |
|
| 112 | - wl_linked_data_save_post( $post->ID ); |
|
| 113 | - }, array( |
|
| 114 | - 'post_status' => 'publish', |
|
| 115 | - ), $offset, $max ); |
|
| 116 | - |
|
| 117 | - // Redirect to the next chunk. |
|
| 118 | - if ( $count == $limit ) { |
|
| 119 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - echo( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 123 | - |
|
| 124 | - // If we're being called as AJAX, die here. |
|
| 125 | - if ( DOING_AJAX ) { |
|
| 126 | - wp_die(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
| 133 | - * |
|
| 134 | - * @since 3.9.8 |
|
| 135 | - * |
|
| 136 | - * @param string $url The URL to redirect to. |
|
| 137 | - */ |
|
| 138 | - private function redirect( $url ) { |
|
| 139 | - |
|
| 140 | - ob_clean(); |
|
| 141 | - |
|
| 142 | - @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 143 | - ?> |
|
| 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 | + $this->process( function ( $post ) use ( &$count ) { |
|
| 111 | + $count ++; |
|
| 112 | + wl_linked_data_save_post( $post->ID ); |
|
| 113 | + }, array( |
|
| 114 | + 'post_status' => 'publish', |
|
| 115 | + ), $offset, $max ); |
|
| 116 | + |
|
| 117 | + // Redirect to the next chunk. |
|
| 118 | + if ( $count == $limit ) { |
|
| 119 | + $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + echo( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 123 | + |
|
| 124 | + // If we're being called as AJAX, die here. |
|
| 125 | + if ( DOING_AJAX ) { |
|
| 126 | + wp_die(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
| 133 | + * |
|
| 134 | + * @since 3.9.8 |
|
| 135 | + * |
|
| 136 | + * @param string $url The URL to redirect to. |
|
| 137 | + */ |
|
| 138 | + private function redirect( $url ) { |
|
| 139 | + |
|
| 140 | + ob_clean(); |
|
| 141 | + |
|
| 142 | + @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 143 | + ?> |
|
| 144 | 144 | <html> |
| 145 | 145 | <head> |
| 146 | 146 | <meta http-equiv="refresh" |
@@ -152,31 +152,31 @@ discard block |
||
| 152 | 152 | </html> |
| 153 | 153 | <?php |
| 154 | 154 | |
| 155 | - exit; |
|
| 156 | - |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * List the items starting at the specified offset and up to the specified limit. |
|
| 161 | - * |
|
| 162 | - * @since 3.6.0 |
|
| 163 | - * |
|
| 164 | - * @param int $offset The start offset. |
|
| 165 | - * @param int $limit The maximum number of items to return. |
|
| 166 | - * @param array $args Additional arguments. |
|
| 167 | - * |
|
| 168 | - * @return array A array of items (or an empty array if no items are found). |
|
| 169 | - */ |
|
| 170 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 171 | - |
|
| 172 | - return get_posts( wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 173 | - 'offset' => $offset, |
|
| 174 | - 'numberposts' => $limit, |
|
| 175 | - 'fields' => 'all', |
|
| 176 | - 'orderby' => 'ID', |
|
| 177 | - 'order' => 'ASC', |
|
| 178 | - 'post_status' => 'any', |
|
| 179 | - ) ) ) ); |
|
| 180 | - } |
|
| 155 | + exit; |
|
| 156 | + |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * List the items starting at the specified offset and up to the specified limit. |
|
| 161 | + * |
|
| 162 | + * @since 3.6.0 |
|
| 163 | + * |
|
| 164 | + * @param int $offset The start offset. |
|
| 165 | + * @param int $limit The maximum number of items to return. |
|
| 166 | + * @param array $args Additional arguments. |
|
| 167 | + * |
|
| 168 | + * @return array A array of items (or an empty array if no items are found). |
|
| 169 | + */ |
|
| 170 | + function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 171 | + |
|
| 172 | + return get_posts( wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 173 | + 'offset' => $offset, |
|
| 174 | + 'numberposts' => $limit, |
|
| 175 | + 'fields' => 'all', |
|
| 176 | + 'orderby' => 'ID', |
|
| 177 | + 'order' => 'ASC', |
|
| 178 | + 'post_status' => 'any', |
|
| 179 | + ) ) ) ); |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | 182 | } |
@@ -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 | |
@@ -107,22 +107,22 @@ discard block |
||
| 107 | 107 | // in order to avoid memory errors. |
| 108 | 108 | |
| 109 | 109 | $count = 0; |
| 110 | - $this->process( function ( $post ) use ( &$count ) { |
|
| 111 | - $count ++; |
|
| 112 | - wl_linked_data_save_post( $post->ID ); |
|
| 110 | + $this->process(function($post) use (&$count) { |
|
| 111 | + $count++; |
|
| 112 | + wl_linked_data_save_post($post->ID); |
|
| 113 | 113 | }, array( |
| 114 | 114 | 'post_status' => 'publish', |
| 115 | - ), $offset, $max ); |
|
| 115 | + ), $offset, $max); |
|
| 116 | 116 | |
| 117 | 117 | // Redirect to the next chunk. |
| 118 | - if ( $count == $limit ) { |
|
| 119 | - $this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
| 118 | + if ($count == $limit) { |
|
| 119 | + $this->redirect(admin_url('admin-ajax.php?action=wl_rebuild&offset='.($offset + $limit).'&limit='.$limit.'&wl-async='.($asynchronous ? 'true' : 'false'))); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | - echo( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
| 122 | + echo("Rebuild complete [ count :: $count ][ limit :: $limit ]"); |
|
| 123 | 123 | |
| 124 | 124 | // If we're being called as AJAX, die here. |
| 125 | - if ( DOING_AJAX ) { |
|
| 125 | + if (DOING_AJAX) { |
|
| 126 | 126 | wp_die(); |
| 127 | 127 | } |
| 128 | 128 | |
@@ -135,16 +135,16 @@ discard block |
||
| 135 | 135 | * |
| 136 | 136 | * @param string $url The URL to redirect to. |
| 137 | 137 | */ |
| 138 | - private function redirect( $url ) { |
|
| 138 | + private function redirect($url) { |
|
| 139 | 139 | |
| 140 | 140 | ob_clean(); |
| 141 | 141 | |
| 142 | - @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
| 142 | + @header('Content-Type: text/html; charset='.get_option('blog_charset')); |
|
| 143 | 143 | ?> |
| 144 | 144 | <html> |
| 145 | 145 | <head> |
| 146 | 146 | <meta http-equiv="refresh" |
| 147 | - content="0; <?php echo esc_attr( $url ); ?>"> |
|
| 147 | + content="0; <?php echo esc_attr($url); ?>"> |
|
| 148 | 148 | </head> |
| 149 | 149 | <body> |
| 150 | 150 | Rebuilding, please wait... |
@@ -167,16 +167,16 @@ discard block |
||
| 167 | 167 | * |
| 168 | 168 | * @return array A array of items (or an empty array if no items are found). |
| 169 | 169 | */ |
| 170 | - function find( $offset = 0, $limit = 10, $args = array() ) { |
|
| 170 | + function find($offset = 0, $limit = 10, $args = array()) { |
|
| 171 | 171 | |
| 172 | - return get_posts( wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
| 172 | + return get_posts(wp_parse_args($args, Wordlift_Entity_Service::add_criterias(array( |
|
| 173 | 173 | 'offset' => $offset, |
| 174 | 174 | 'numberposts' => $limit, |
| 175 | 175 | 'fields' => 'all', |
| 176 | 176 | 'orderby' => 'ID', |
| 177 | 177 | 'order' => 'ASC', |
| 178 | 178 | 'post_status' => 'any', |
| 179 | - ) ) ) ); |
|
| 179 | + )))); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | } |
@@ -7,628 +7,628 @@ |
||
| 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 entity post type name. |
|
| 39 | - * |
|
| 40 | - * @since 3.1.0 |
|
| 41 | - */ |
|
| 42 | - const TYPE_NAME = 'entity'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The alternative label meta key. |
|
| 46 | - * |
|
| 47 | - * @since 3.2.0 |
|
| 48 | - */ |
|
| 49 | - const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * The alternative label input template. |
|
| 53 | - * |
|
| 54 | - * @since 3.2.0 |
|
| 55 | - */ |
|
| 56 | - // TODO: this should be moved to a class that deals with HTML code. |
|
| 57 | - 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 entity post type name. |
|
| 39 | + * |
|
| 40 | + * @since 3.1.0 |
|
| 41 | + */ |
|
| 42 | + const TYPE_NAME = 'entity'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The alternative label meta key. |
|
| 46 | + * |
|
| 47 | + * @since 3.2.0 |
|
| 48 | + */ |
|
| 49 | + const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * The alternative label input template. |
|
| 53 | + * |
|
| 54 | + * @since 3.2.0 |
|
| 55 | + */ |
|
| 56 | + // TODO: this should be moved to a class that deals with HTML code. |
|
| 57 | + const ALTERNATIVE_LABEL_INPUT_TEMPLATE = '<div class="wl-alternative-label"> |
|
| 58 | 58 | <label class="screen-reader-text" id="wl-alternative-label-prompt-text" for="wl-alternative-label">Enter alternative label here</label> |
| 59 | 59 | <input name="wl_alternative_label[]" size="30" value="%s" id="wl-alternative-label" type="text"> |
| 60 | 60 | <button class="button wl-delete-button">%s</button> |
| 61 | 61 | </div>'; |
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * A singleton instance of the Entity service. |
|
| 65 | - * |
|
| 66 | - * @since 3.2.0 |
|
| 67 | - * @access private |
|
| 68 | - * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 69 | - */ |
|
| 70 | - private static $instance; |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Create a Wordlift_Entity_Service instance. |
|
| 74 | - * |
|
| 75 | - * @since 3.2.0 |
|
| 76 | - * |
|
| 77 | - * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 78 | - * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 79 | - */ |
|
| 80 | - public function __construct( $ui_service, $relation_service ) { |
|
| 81 | - |
|
| 82 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 83 | - |
|
| 84 | - $this->ui_service = $ui_service; |
|
| 85 | - $this->relation_service = $relation_service; |
|
| 86 | - |
|
| 87 | - // Set the singleton instance. |
|
| 88 | - self::$instance = $this; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the singleton instance of the Entity service. |
|
| 93 | - * |
|
| 94 | - * @since 3.2.0 |
|
| 95 | - * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 96 | - */ |
|
| 97 | - public static function get_instance() { |
|
| 98 | - |
|
| 99 | - return self::$instance; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Determines whether a post is an entity or not. Entity is in this context |
|
| 104 | - * something which is not an article. |
|
| 105 | - * |
|
| 106 | - * @since 3.1.0 |
|
| 107 | - * |
|
| 108 | - * @param int $post_id A post id. |
|
| 109 | - * |
|
| 110 | - * @return bool Return true if the post is an entity otherwise false. |
|
| 111 | - */ |
|
| 112 | - public function is_entity( $post_id ) { |
|
| 113 | - |
|
| 114 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 115 | - |
|
| 116 | - if ( 0 === count( $terms ) ) { |
|
| 117 | - return false; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // We don't consider an `article` to be an entity. |
|
| 121 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 122 | - return true; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Get the proper classification scope for a given entity post |
|
| 130 | - * |
|
| 131 | - * @since 3.5.0 |
|
| 132 | - * |
|
| 133 | - * @param integer $post_id An entity post id. |
|
| 134 | - * |
|
| 135 | - * @param string $default The default classification scope, `what` if not |
|
| 136 | - * provided. |
|
| 137 | - * |
|
| 138 | - * @return string Returns a classification scope (e.g. 'what'). |
|
| 139 | - */ |
|
| 140 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 141 | - |
|
| 142 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 143 | - return $default; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Retrieve the entity type |
|
| 147 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 148 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 149 | - // Retrieve classification boxes configuration |
|
| 150 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 151 | - foreach ( $classification_boxes as $cb ) { |
|
| 152 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 153 | - return $cb['id']; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $default; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Check whether a {@link WP_Post} is used. |
|
| 162 | - * |
|
| 163 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 164 | - * |
|
| 165 | - * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 166 | - */ |
|
| 167 | - public function is_used( $post_id ) { |
|
| 168 | - |
|
| 169 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 170 | - return null; |
|
| 171 | - } |
|
| 172 | - // Retrieve the post |
|
| 173 | - $entity = get_post( $post_id ); |
|
| 174 | - |
|
| 175 | - global $wpdb; |
|
| 176 | - // Retrieve Wordlift relation instances table name |
|
| 177 | - $table_name = wl_core_get_relation_instances_table_name(); |
|
| 178 | - |
|
| 179 | - // Check is it's referenced / related to another post / entity |
|
| 180 | - $stmt = $wpdb->prepare( |
|
| 181 | - "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 182 | - $entity->ID |
|
| 183 | - ); |
|
| 184 | - |
|
| 185 | - // Perform the query |
|
| 186 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 187 | - // If there is at least one relation instance for the current entity, then it's used |
|
| 188 | - if ( 0 < $relation_instances ) { |
|
| 189 | - return true; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // Check if the entity uri is used as meta_value |
|
| 193 | - $stmt = $wpdb->prepare( |
|
| 194 | - "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 195 | - $entity->ID, |
|
| 196 | - wl_get_entity_uri( $entity->ID ) |
|
| 197 | - ); |
|
| 198 | - // Perform the query |
|
| 199 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 200 | - |
|
| 201 | - // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 202 | - if ( 0 < $meta_instances ) { |
|
| 203 | - return true; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - // If we are here, it means the current entity is not used at the moment |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Determines whether a given uri is an internal uri or not. |
|
| 212 | - * |
|
| 213 | - * @since 3.3.2 |
|
| 214 | - * |
|
| 215 | - * @param int $uri An uri. |
|
| 216 | - * |
|
| 217 | - * @return true if the uri internal to the current dataset otherwise false. |
|
| 218 | - */ |
|
| 219 | - public function is_internal_uri( $uri ) { |
|
| 220 | - |
|
| 221 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 226 | - * |
|
| 227 | - * @since 3.2.0 |
|
| 228 | - * |
|
| 229 | - * @param string $uri The entity URI. |
|
| 230 | - * |
|
| 231 | - * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 232 | - */ |
|
| 233 | - public function get_entity_post_by_uri( $uri ) { |
|
| 234 | - |
|
| 235 | - // Check if we've been provided with a value otherwise return null. |
|
| 236 | - if ( empty( $uri ) ) { |
|
| 237 | - return null; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $query_args = array( |
|
| 241 | - // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 242 | - 'ignore_sticky_posts' => 1, |
|
| 243 | - 'posts_per_page' => 1, |
|
| 244 | - 'post_status' => 'any', |
|
| 245 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 246 | - 'meta_query' => array( |
|
| 247 | - array( |
|
| 248 | - 'key' => WL_ENTITY_URL_META_NAME, |
|
| 249 | - 'value' => $uri, |
|
| 250 | - 'compare' => '=', |
|
| 251 | - ), |
|
| 252 | - ), |
|
| 253 | - // The following query can be avoided, it's superfluous since |
|
| 254 | - // we're looking for a post with a specific Entity URI. This query |
|
| 255 | - // may in fact consume up to 50% of the timing necessary to load a |
|
| 256 | - // page. |
|
| 257 | - // |
|
| 258 | - // See https://github.com/insideout10/wordlift-plugin/issues/674. |
|
| 259 | - // 'tax_query' => array( |
|
| 260 | - // 'relation' => 'AND', |
|
| 261 | - // array( |
|
| 262 | - // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 263 | - // 'operator' => 'EXISTS', |
|
| 264 | - // ), |
|
| 265 | - // array( |
|
| 266 | - // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 267 | - // 'field' => 'slug', |
|
| 268 | - // 'terms' => 'article', |
|
| 269 | - // 'operator' => 'NOT IN', |
|
| 270 | - // ), |
|
| 271 | - // ), |
|
| 272 | - ); |
|
| 273 | - |
|
| 274 | - // Only if the current uri is not an internal uri, entity search is |
|
| 275 | - // performed also looking at sameAs values. |
|
| 276 | - // |
|
| 277 | - // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 278 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 279 | - |
|
| 280 | - $query_args['meta_query']['relation'] = 'OR'; |
|
| 281 | - $query_args['meta_query'][] = array( |
|
| 282 | - 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 283 | - 'value' => $uri, |
|
| 284 | - 'compare' => '=', |
|
| 285 | - ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $posts = get_posts( $query_args ); |
|
| 63 | + /** |
|
| 64 | + * A singleton instance of the Entity service. |
|
| 65 | + * |
|
| 66 | + * @since 3.2.0 |
|
| 67 | + * @access private |
|
| 68 | + * @var \Wordlift_Entity_Service $instance A singleton instance of the Entity service. |
|
| 69 | + */ |
|
| 70 | + private static $instance; |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Create a Wordlift_Entity_Service instance. |
|
| 74 | + * |
|
| 75 | + * @since 3.2.0 |
|
| 76 | + * |
|
| 77 | + * @param \Wordlift_UI_Service $ui_service The UI service. |
|
| 78 | + * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
|
| 79 | + */ |
|
| 80 | + public function __construct( $ui_service, $relation_service ) { |
|
| 81 | + |
|
| 82 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 83 | + |
|
| 84 | + $this->ui_service = $ui_service; |
|
| 85 | + $this->relation_service = $relation_service; |
|
| 86 | + |
|
| 87 | + // Set the singleton instance. |
|
| 88 | + self::$instance = $this; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the singleton instance of the Entity service. |
|
| 93 | + * |
|
| 94 | + * @since 3.2.0 |
|
| 95 | + * @return \Wordlift_Entity_Service The singleton instance of the Entity service. |
|
| 96 | + */ |
|
| 97 | + public static function get_instance() { |
|
| 98 | + |
|
| 99 | + return self::$instance; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Determines whether a post is an entity or not. Entity is in this context |
|
| 104 | + * something which is not an article. |
|
| 105 | + * |
|
| 106 | + * @since 3.1.0 |
|
| 107 | + * |
|
| 108 | + * @param int $post_id A post id. |
|
| 109 | + * |
|
| 110 | + * @return bool Return true if the post is an entity otherwise false. |
|
| 111 | + */ |
|
| 112 | + public function is_entity( $post_id ) { |
|
| 113 | + |
|
| 114 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 115 | + |
|
| 116 | + if ( 0 === count( $terms ) ) { |
|
| 117 | + return false; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // We don't consider an `article` to be an entity. |
|
| 121 | + if ( 'article' !== $terms[0]->slug ) { |
|
| 122 | + return true; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Get the proper classification scope for a given entity post |
|
| 130 | + * |
|
| 131 | + * @since 3.5.0 |
|
| 132 | + * |
|
| 133 | + * @param integer $post_id An entity post id. |
|
| 134 | + * |
|
| 135 | + * @param string $default The default classification scope, `what` if not |
|
| 136 | + * provided. |
|
| 137 | + * |
|
| 138 | + * @return string Returns a classification scope (e.g. 'what'). |
|
| 139 | + */ |
|
| 140 | + public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 141 | + |
|
| 142 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 143 | + return $default; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // Retrieve the entity type |
|
| 147 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 148 | + $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 149 | + // Retrieve classification boxes configuration |
|
| 150 | + $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 151 | + foreach ( $classification_boxes as $cb ) { |
|
| 152 | + if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 153 | + return $cb['id']; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $default; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Check whether a {@link WP_Post} is used. |
|
| 162 | + * |
|
| 163 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 164 | + * |
|
| 165 | + * @return bool|null Null if it's not an entity, otherwise true if it's used. |
|
| 166 | + */ |
|
| 167 | + public function is_used( $post_id ) { |
|
| 168 | + |
|
| 169 | + if ( false === $this->is_entity( $post_id ) ) { |
|
| 170 | + return null; |
|
| 171 | + } |
|
| 172 | + // Retrieve the post |
|
| 173 | + $entity = get_post( $post_id ); |
|
| 174 | + |
|
| 175 | + global $wpdb; |
|
| 176 | + // Retrieve Wordlift relation instances table name |
|
| 177 | + $table_name = wl_core_get_relation_instances_table_name(); |
|
| 178 | + |
|
| 179 | + // Check is it's referenced / related to another post / entity |
|
| 180 | + $stmt = $wpdb->prepare( |
|
| 181 | + "SELECT COUNT(*) FROM $table_name WHERE object_id = %d", |
|
| 182 | + $entity->ID |
|
| 183 | + ); |
|
| 184 | + |
|
| 185 | + // Perform the query |
|
| 186 | + $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 187 | + // If there is at least one relation instance for the current entity, then it's used |
|
| 188 | + if ( 0 < $relation_instances ) { |
|
| 189 | + return true; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // Check if the entity uri is used as meta_value |
|
| 193 | + $stmt = $wpdb->prepare( |
|
| 194 | + "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
|
| 195 | + $entity->ID, |
|
| 196 | + wl_get_entity_uri( $entity->ID ) |
|
| 197 | + ); |
|
| 198 | + // Perform the query |
|
| 199 | + $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 200 | + |
|
| 201 | + // If there is at least one meta that refers the current entity uri, then current entity is used |
|
| 202 | + if ( 0 < $meta_instances ) { |
|
| 203 | + return true; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + // If we are here, it means the current entity is not used at the moment |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Determines whether a given uri is an internal uri or not. |
|
| 212 | + * |
|
| 213 | + * @since 3.3.2 |
|
| 214 | + * |
|
| 215 | + * @param int $uri An uri. |
|
| 216 | + * |
|
| 217 | + * @return true if the uri internal to the current dataset otherwise false. |
|
| 218 | + */ |
|
| 219 | + public function is_internal_uri( $uri ) { |
|
| 220 | + |
|
| 221 | + return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Find entity posts by the entity URI. Entity as searched by their entity URI or same as. |
|
| 226 | + * |
|
| 227 | + * @since 3.2.0 |
|
| 228 | + * |
|
| 229 | + * @param string $uri The entity URI. |
|
| 230 | + * |
|
| 231 | + * @return WP_Post|null A WP_Post instance or null if not found. |
|
| 232 | + */ |
|
| 233 | + public function get_entity_post_by_uri( $uri ) { |
|
| 234 | + |
|
| 235 | + // Check if we've been provided with a value otherwise return null. |
|
| 236 | + if ( empty( $uri ) ) { |
|
| 237 | + return null; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $query_args = array( |
|
| 241 | + // See https://github.com/insideout10/wordlift-plugin/issues/654. |
|
| 242 | + 'ignore_sticky_posts' => 1, |
|
| 243 | + 'posts_per_page' => 1, |
|
| 244 | + 'post_status' => 'any', |
|
| 245 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 246 | + 'meta_query' => array( |
|
| 247 | + array( |
|
| 248 | + 'key' => WL_ENTITY_URL_META_NAME, |
|
| 249 | + 'value' => $uri, |
|
| 250 | + 'compare' => '=', |
|
| 251 | + ), |
|
| 252 | + ), |
|
| 253 | + // The following query can be avoided, it's superfluous since |
|
| 254 | + // we're looking for a post with a specific Entity URI. This query |
|
| 255 | + // may in fact consume up to 50% of the timing necessary to load a |
|
| 256 | + // page. |
|
| 257 | + // |
|
| 258 | + // See https://github.com/insideout10/wordlift-plugin/issues/674. |
|
| 259 | + // 'tax_query' => array( |
|
| 260 | + // 'relation' => 'AND', |
|
| 261 | + // array( |
|
| 262 | + // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 263 | + // 'operator' => 'EXISTS', |
|
| 264 | + // ), |
|
| 265 | + // array( |
|
| 266 | + // 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 267 | + // 'field' => 'slug', |
|
| 268 | + // 'terms' => 'article', |
|
| 269 | + // 'operator' => 'NOT IN', |
|
| 270 | + // ), |
|
| 271 | + // ), |
|
| 272 | + ); |
|
| 273 | + |
|
| 274 | + // Only if the current uri is not an internal uri, entity search is |
|
| 275 | + // performed also looking at sameAs values. |
|
| 276 | + // |
|
| 277 | + // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
|
| 278 | + if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 279 | + |
|
| 280 | + $query_args['meta_query']['relation'] = 'OR'; |
|
| 281 | + $query_args['meta_query'][] = array( |
|
| 282 | + 'key' => Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 283 | + 'value' => $uri, |
|
| 284 | + 'compare' => '=', |
|
| 285 | + ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $posts = get_posts( $query_args ); |
|
| 289 | 289 | |
| 290 | 290 | // $query = new WP_Query( $query_args ); |
| 291 | 291 | // |
| 292 | 292 | // // Get the matching entity posts. |
| 293 | 293 | // $posts = $query->get_posts(); |
| 294 | 294 | |
| 295 | - // Return null if no post is found. |
|
| 296 | - if ( 0 === count( $posts ) ) { |
|
| 297 | - return null; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - // Return the found post. |
|
| 301 | - return current( $posts ); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 306 | - * we check that the post we're saving is the current post. |
|
| 307 | - * |
|
| 308 | - * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 309 | - * |
|
| 310 | - * @since 3.2.0 |
|
| 311 | - * |
|
| 312 | - * @param int $post_id Post ID. |
|
| 313 | - * @param WP_Post $post Post object. |
|
| 314 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 315 | - */ |
|
| 316 | - public function save_post( $post_id, $post, $update ) { |
|
| 317 | - |
|
| 318 | - // Avoid doing anything if post is autosave or a revision. |
|
| 319 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 320 | - return; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - // We're setting the alternative label that have been provided via the UI |
|
| 324 | - // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 325 | - // programmatically by some other function: we need to check therefore if |
|
| 326 | - // the $post_id in the save_post call matches the post id set in the request. |
|
| 327 | - // |
|
| 328 | - // If this is not the current post being saved or if it's not an entity, return. |
|
| 329 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 330 | - return; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - // Get the alt labels from the request (or empty array). |
|
| 334 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 335 | - |
|
| 336 | - // Set the alternative labels. |
|
| 337 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 338 | - |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Set the alternative labels. |
|
| 343 | - * |
|
| 344 | - * @since 3.2.0 |
|
| 345 | - * |
|
| 346 | - * @param int $post_id The post id. |
|
| 347 | - * @param array $alt_labels An array of labels. |
|
| 348 | - */ |
|
| 349 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 350 | - |
|
| 351 | - // Force $alt_labels to be an array |
|
| 352 | - if ( ! is_array( $alt_labels ) ) { |
|
| 353 | - $alt_labels = array( $alt_labels ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 357 | - |
|
| 358 | - // Delete all the existing alternate labels. |
|
| 359 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 360 | - |
|
| 361 | - // Set the alternative labels. |
|
| 362 | - foreach ( $alt_labels as $alt_label ) { |
|
| 363 | - if ( ! empty( $alt_label ) ) { |
|
| 364 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Retrieve the alternate labels. |
|
| 372 | - * |
|
| 373 | - * @since 3.2.0 |
|
| 374 | - * |
|
| 375 | - * @param int $post_id Post id. |
|
| 376 | - * |
|
| 377 | - * @return mixed An array of alternative labels. |
|
| 378 | - */ |
|
| 379 | - public function get_alternative_labels( $post_id ) { |
|
| 380 | - |
|
| 381 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 386 | - * |
|
| 387 | - * @since 3.12.0 |
|
| 388 | - * |
|
| 389 | - * @param int $post_id The entity {@link WP_Post} id. |
|
| 390 | - * |
|
| 391 | - * @return array An array with the entity title and labels. |
|
| 392 | - */ |
|
| 393 | - public function get_labels( $post_id ) { |
|
| 394 | - |
|
| 395 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 400 | - * |
|
| 401 | - * @since 3.2.0 |
|
| 402 | - * |
|
| 403 | - * @param WP_Post $post Post object. |
|
| 404 | - */ |
|
| 405 | - public function edit_form_before_permalink( $post ) { |
|
| 406 | - |
|
| 407 | - // If it's not an entity, return. |
|
| 408 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 409 | - return; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - // Print the input template. |
|
| 413 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 414 | - |
|
| 415 | - // Print all the currently set alternative labels. |
|
| 416 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 417 | - |
|
| 418 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 419 | - |
|
| 420 | - }; |
|
| 421 | - |
|
| 422 | - // Print the button. |
|
| 423 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 424 | - |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Get the URI for the entity with the specified post id. |
|
| 429 | - * |
|
| 430 | - * @since 3.6.0 |
|
| 431 | - * |
|
| 432 | - * @param int $post_id The entity post id. |
|
| 433 | - * |
|
| 434 | - * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 435 | - */ |
|
| 436 | - public function get_uri( $post_id ) { |
|
| 437 | - |
|
| 438 | - // If a null is given, nothing to do |
|
| 439 | - if ( null == $post_id ) { |
|
| 440 | - return null; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 444 | - |
|
| 445 | - // If the dataset uri is not properly configured, null is returned |
|
| 446 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 447 | - return null; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - // Set the URI if it isn't set yet. |
|
| 451 | - $post_status = get_post_status( $post_id ); |
|
| 452 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 453 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 454 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - return $uri; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - |
|
| 461 | - /** |
|
| 462 | - * Get the alternative label input HTML code. |
|
| 463 | - * |
|
| 464 | - * @since 3.2.0 |
|
| 465 | - * |
|
| 466 | - * @param string $value The input value. |
|
| 467 | - * |
|
| 468 | - * @return string The input HTML code. |
|
| 469 | - */ |
|
| 470 | - private function get_alternative_label_input( $value = '' ) { |
|
| 471 | - |
|
| 472 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Get the number of entity posts published in this blog. |
|
| 477 | - * |
|
| 478 | - * @since 3.6.0 |
|
| 479 | - * |
|
| 480 | - * @return int The number of published entity posts. |
|
| 481 | - */ |
|
| 482 | - public function count() { |
|
| 483 | - |
|
| 484 | - $posts = get_posts( $this->add_criterias( array( |
|
| 485 | - 'post_status' => 'any', |
|
| 486 | - 'numberposts' => - 1, |
|
| 487 | - ) ) ); |
|
| 488 | - |
|
| 489 | - return count( $posts ); |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 494 | - * call. |
|
| 495 | - * |
|
| 496 | - * @since 3.15.0 |
|
| 497 | - * |
|
| 498 | - * @param array $args The arguments for a `get_posts` call. |
|
| 499 | - * |
|
| 500 | - * @return array The arguments for a `get_posts` call. |
|
| 501 | - */ |
|
| 502 | - public static function add_criterias( $args ) { |
|
| 503 | - |
|
| 504 | - return $args + array( |
|
| 505 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 506 | - 'tax_query' => array( |
|
| 507 | - array( |
|
| 508 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 509 | - 'terms' => self::get_entity_terms(), |
|
| 510 | - ), |
|
| 511 | - ), |
|
| 512 | - ); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * Get the entity terms IDs which represent an entity. |
|
| 517 | - * |
|
| 518 | - * @since 3.15.0 |
|
| 519 | - * |
|
| 520 | - * @return array An array of terms' ids. |
|
| 521 | - */ |
|
| 522 | - public static function get_entity_terms() { |
|
| 523 | - |
|
| 524 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 525 | - 'hide_empty' => false, |
|
| 526 | - // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 527 | - // An issue has been opened with the AAM plugin author as well. |
|
| 528 | - // |
|
| 529 | - // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 530 | - // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 531 | - 'fields' => 'all', |
|
| 532 | - ) ); |
|
| 533 | - |
|
| 534 | - return array_map( function ( $term ) { |
|
| 535 | - return $term->term_id; |
|
| 536 | - }, array_filter( $terms, function ( $term ) { |
|
| 537 | - return 'article' !== $term->slug; |
|
| 538 | - } ) ); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * Create a new entity. |
|
| 543 | - * |
|
| 544 | - * @since 3.9.0 |
|
| 545 | - * |
|
| 546 | - * @param string $name The entity name. |
|
| 547 | - * @param string $type_uri The entity's type URI. |
|
| 548 | - * @param null $logo The entity logo id (or NULL if none). |
|
| 549 | - * @param string $status The post status, by default 'publish'. |
|
| 550 | - * |
|
| 551 | - * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 552 | - */ |
|
| 553 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 554 | - |
|
| 555 | - // Create an entity for the publisher. |
|
| 556 | - $post_id = wp_insert_post( array( |
|
| 557 | - 'post_type' => self::TYPE_NAME, |
|
| 558 | - 'post_title' => $name, |
|
| 559 | - 'post_status' => $status, |
|
| 560 | - 'post_content' => '', |
|
| 561 | - ) ); |
|
| 562 | - |
|
| 563 | - // Return the error if any. |
|
| 564 | - if ( is_wp_error( $post_id ) ) { |
|
| 565 | - return $post_id; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - // Set the entity logo. |
|
| 569 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 570 | - set_post_thumbnail( $post_id, $logo ); |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - // Set the entity type. |
|
| 574 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 575 | - |
|
| 576 | - return $post_id; |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * Get the entities related to the one with the specified id. By default only |
|
| 581 | - * published entities will be returned. |
|
| 582 | - * |
|
| 583 | - * @since 3.10.0 |
|
| 584 | - * |
|
| 585 | - * @param int $id The post id. |
|
| 586 | - * @param string $post_status The target post status (default = publish). |
|
| 587 | - * |
|
| 588 | - * @return array An array of post ids. |
|
| 589 | - */ |
|
| 590 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 591 | - |
|
| 592 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Get the list of entities. |
|
| 597 | - * |
|
| 598 | - * @since 3.12.2 |
|
| 599 | - * |
|
| 600 | - * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 601 | - * |
|
| 602 | - * @return array An array of entity posts. |
|
| 603 | - */ |
|
| 604 | - public function get( $params = array() ) { |
|
| 605 | - |
|
| 606 | - // Set the defaults. |
|
| 607 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 608 | - |
|
| 609 | - // Merge the defaults with the provided parameters. |
|
| 610 | - $args = wp_parse_args( $params, $defaults ); |
|
| 611 | - |
|
| 612 | - // Call the `get_posts` function. |
|
| 613 | - return get_posts( $args ); |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * The list of post type names which can be used for entities |
|
| 618 | - * |
|
| 619 | - * Criteria is that the post type is public. The list of valid post types |
|
| 620 | - * can be overridden with a filter. |
|
| 621 | - * |
|
| 622 | - * @since 3.15.0 |
|
| 623 | - * |
|
| 624 | - * @return array Array containing the names of the valid post types. |
|
| 625 | - */ |
|
| 626 | - static function valid_entity_post_types() { |
|
| 627 | - |
|
| 628 | - // Ignore builtins in the call to avoid getting attachments. |
|
| 629 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 630 | - |
|
| 631 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 632 | - } |
|
| 295 | + // Return null if no post is found. |
|
| 296 | + if ( 0 === count( $posts ) ) { |
|
| 297 | + return null; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + // Return the found post. |
|
| 301 | + return current( $posts ); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Fires once a post has been saved. This function uses the $_REQUEST, therefore |
|
| 306 | + * we check that the post we're saving is the current post. |
|
| 307 | + * |
|
| 308 | + * @see https://github.com/insideout10/wordlift-plugin/issues/363 |
|
| 309 | + * |
|
| 310 | + * @since 3.2.0 |
|
| 311 | + * |
|
| 312 | + * @param int $post_id Post ID. |
|
| 313 | + * @param WP_Post $post Post object. |
|
| 314 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 315 | + */ |
|
| 316 | + public function save_post( $post_id, $post, $update ) { |
|
| 317 | + |
|
| 318 | + // Avoid doing anything if post is autosave or a revision. |
|
| 319 | + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 320 | + return; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + // We're setting the alternative label that have been provided via the UI |
|
| 324 | + // (in fact we're using $_REQUEST), while save_post may be also called |
|
| 325 | + // programmatically by some other function: we need to check therefore if |
|
| 326 | + // the $post_id in the save_post call matches the post id set in the request. |
|
| 327 | + // |
|
| 328 | + // If this is not the current post being saved or if it's not an entity, return. |
|
| 329 | + if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 330 | + return; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + // Get the alt labels from the request (or empty array). |
|
| 334 | + $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 335 | + |
|
| 336 | + // Set the alternative labels. |
|
| 337 | + $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 338 | + |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Set the alternative labels. |
|
| 343 | + * |
|
| 344 | + * @since 3.2.0 |
|
| 345 | + * |
|
| 346 | + * @param int $post_id The post id. |
|
| 347 | + * @param array $alt_labels An array of labels. |
|
| 348 | + */ |
|
| 349 | + public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 350 | + |
|
| 351 | + // Force $alt_labels to be an array |
|
| 352 | + if ( ! is_array( $alt_labels ) ) { |
|
| 353 | + $alt_labels = array( $alt_labels ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 357 | + |
|
| 358 | + // Delete all the existing alternate labels. |
|
| 359 | + delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 360 | + |
|
| 361 | + // Set the alternative labels. |
|
| 362 | + foreach ( $alt_labels as $alt_label ) { |
|
| 363 | + if ( ! empty( $alt_label ) ) { |
|
| 364 | + add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Retrieve the alternate labels. |
|
| 372 | + * |
|
| 373 | + * @since 3.2.0 |
|
| 374 | + * |
|
| 375 | + * @param int $post_id Post id. |
|
| 376 | + * |
|
| 377 | + * @return mixed An array of alternative labels. |
|
| 378 | + */ |
|
| 379 | + public function get_alternative_labels( $post_id ) { |
|
| 380 | + |
|
| 381 | + return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Retrieve the labels for an entity, i.e. the title + the synonyms. |
|
| 386 | + * |
|
| 387 | + * @since 3.12.0 |
|
| 388 | + * |
|
| 389 | + * @param int $post_id The entity {@link WP_Post} id. |
|
| 390 | + * |
|
| 391 | + * @return array An array with the entity title and labels. |
|
| 392 | + */ |
|
| 393 | + public function get_labels( $post_id ) { |
|
| 394 | + |
|
| 395 | + return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Fires before the permalink field in the edit form (this event is available in WP from 4.1.0). |
|
| 400 | + * |
|
| 401 | + * @since 3.2.0 |
|
| 402 | + * |
|
| 403 | + * @param WP_Post $post Post object. |
|
| 404 | + */ |
|
| 405 | + public function edit_form_before_permalink( $post ) { |
|
| 406 | + |
|
| 407 | + // If it's not an entity, return. |
|
| 408 | + if ( ! $this->is_entity( $post->ID ) ) { |
|
| 409 | + return; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + // Print the input template. |
|
| 413 | + $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 414 | + |
|
| 415 | + // Print all the currently set alternative labels. |
|
| 416 | + foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 417 | + |
|
| 418 | + echo $this->get_alternative_label_input( $alt_label ); |
|
| 419 | + |
|
| 420 | + }; |
|
| 421 | + |
|
| 422 | + // Print the button. |
|
| 423 | + $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 424 | + |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Get the URI for the entity with the specified post id. |
|
| 429 | + * |
|
| 430 | + * @since 3.6.0 |
|
| 431 | + * |
|
| 432 | + * @param int $post_id The entity post id. |
|
| 433 | + * |
|
| 434 | + * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
|
| 435 | + */ |
|
| 436 | + public function get_uri( $post_id ) { |
|
| 437 | + |
|
| 438 | + // If a null is given, nothing to do |
|
| 439 | + if ( null == $post_id ) { |
|
| 440 | + return null; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 444 | + |
|
| 445 | + // If the dataset uri is not properly configured, null is returned |
|
| 446 | + if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 447 | + return null; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + // Set the URI if it isn't set yet. |
|
| 451 | + $post_status = get_post_status( $post_id ); |
|
| 452 | + if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 453 | + $uri = wl_build_entity_uri( $post_id ); |
|
| 454 | + wl_set_entity_uri( $post_id, $uri ); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + return $uri; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Get the alternative label input HTML code. |
|
| 463 | + * |
|
| 464 | + * @since 3.2.0 |
|
| 465 | + * |
|
| 466 | + * @param string $value The input value. |
|
| 467 | + * |
|
| 468 | + * @return string The input HTML code. |
|
| 469 | + */ |
|
| 470 | + private function get_alternative_label_input( $value = '' ) { |
|
| 471 | + |
|
| 472 | + return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Get the number of entity posts published in this blog. |
|
| 477 | + * |
|
| 478 | + * @since 3.6.0 |
|
| 479 | + * |
|
| 480 | + * @return int The number of published entity posts. |
|
| 481 | + */ |
|
| 482 | + public function count() { |
|
| 483 | + |
|
| 484 | + $posts = get_posts( $this->add_criterias( array( |
|
| 485 | + 'post_status' => 'any', |
|
| 486 | + 'numberposts' => - 1, |
|
| 487 | + ) ) ); |
|
| 488 | + |
|
| 489 | + return count( $posts ); |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * Add the entity filtering criterias to the arguments for a `get_posts` |
|
| 494 | + * call. |
|
| 495 | + * |
|
| 496 | + * @since 3.15.0 |
|
| 497 | + * |
|
| 498 | + * @param array $args The arguments for a `get_posts` call. |
|
| 499 | + * |
|
| 500 | + * @return array The arguments for a `get_posts` call. |
|
| 501 | + */ |
|
| 502 | + public static function add_criterias( $args ) { |
|
| 503 | + |
|
| 504 | + return $args + array( |
|
| 505 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 506 | + 'tax_query' => array( |
|
| 507 | + array( |
|
| 508 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 509 | + 'terms' => self::get_entity_terms(), |
|
| 510 | + ), |
|
| 511 | + ), |
|
| 512 | + ); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * Get the entity terms IDs which represent an entity. |
|
| 517 | + * |
|
| 518 | + * @since 3.15.0 |
|
| 519 | + * |
|
| 520 | + * @return array An array of terms' ids. |
|
| 521 | + */ |
|
| 522 | + public static function get_entity_terms() { |
|
| 523 | + |
|
| 524 | + $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 525 | + 'hide_empty' => false, |
|
| 526 | + // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 527 | + // An issue has been opened with the AAM plugin author as well. |
|
| 528 | + // |
|
| 529 | + // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 530 | + // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 531 | + 'fields' => 'all', |
|
| 532 | + ) ); |
|
| 533 | + |
|
| 534 | + return array_map( function ( $term ) { |
|
| 535 | + return $term->term_id; |
|
| 536 | + }, array_filter( $terms, function ( $term ) { |
|
| 537 | + return 'article' !== $term->slug; |
|
| 538 | + } ) ); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * Create a new entity. |
|
| 543 | + * |
|
| 544 | + * @since 3.9.0 |
|
| 545 | + * |
|
| 546 | + * @param string $name The entity name. |
|
| 547 | + * @param string $type_uri The entity's type URI. |
|
| 548 | + * @param null $logo The entity logo id (or NULL if none). |
|
| 549 | + * @param string $status The post status, by default 'publish'. |
|
| 550 | + * |
|
| 551 | + * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
|
| 552 | + */ |
|
| 553 | + public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 554 | + |
|
| 555 | + // Create an entity for the publisher. |
|
| 556 | + $post_id = wp_insert_post( array( |
|
| 557 | + 'post_type' => self::TYPE_NAME, |
|
| 558 | + 'post_title' => $name, |
|
| 559 | + 'post_status' => $status, |
|
| 560 | + 'post_content' => '', |
|
| 561 | + ) ); |
|
| 562 | + |
|
| 563 | + // Return the error if any. |
|
| 564 | + if ( is_wp_error( $post_id ) ) { |
|
| 565 | + return $post_id; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + // Set the entity logo. |
|
| 569 | + if ( $logo && is_numeric( $logo ) ) { |
|
| 570 | + set_post_thumbnail( $post_id, $logo ); |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + // Set the entity type. |
|
| 574 | + Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 575 | + |
|
| 576 | + return $post_id; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + /** |
|
| 580 | + * Get the entities related to the one with the specified id. By default only |
|
| 581 | + * published entities will be returned. |
|
| 582 | + * |
|
| 583 | + * @since 3.10.0 |
|
| 584 | + * |
|
| 585 | + * @param int $id The post id. |
|
| 586 | + * @param string $post_status The target post status (default = publish). |
|
| 587 | + * |
|
| 588 | + * @return array An array of post ids. |
|
| 589 | + */ |
|
| 590 | + public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 591 | + |
|
| 592 | + return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Get the list of entities. |
|
| 597 | + * |
|
| 598 | + * @since 3.12.2 |
|
| 599 | + * |
|
| 600 | + * @param array $params Custom parameters for WordPress' own {@link get_posts} function. |
|
| 601 | + * |
|
| 602 | + * @return array An array of entity posts. |
|
| 603 | + */ |
|
| 604 | + public function get( $params = array() ) { |
|
| 605 | + |
|
| 606 | + // Set the defaults. |
|
| 607 | + $defaults = array( 'post_type' => 'entity' ); |
|
| 608 | + |
|
| 609 | + // Merge the defaults with the provided parameters. |
|
| 610 | + $args = wp_parse_args( $params, $defaults ); |
|
| 611 | + |
|
| 612 | + // Call the `get_posts` function. |
|
| 613 | + return get_posts( $args ); |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * The list of post type names which can be used for entities |
|
| 618 | + * |
|
| 619 | + * Criteria is that the post type is public. The list of valid post types |
|
| 620 | + * can be overridden with a filter. |
|
| 621 | + * |
|
| 622 | + * @since 3.15.0 |
|
| 623 | + * |
|
| 624 | + * @return array Array containing the names of the valid post types. |
|
| 625 | + */ |
|
| 626 | + static function valid_entity_post_types() { |
|
| 627 | + |
|
| 628 | + // Ignore builtins in the call to avoid getting attachments. |
|
| 629 | + $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 630 | + |
|
| 631 | + return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 632 | + } |
|
| 633 | 633 | |
| 634 | 634 | } |
@@ -77,9 +77,9 @@ discard block |
||
| 77 | 77 | * @param \Wordlift_UI_Service $ui_service The UI service. |
| 78 | 78 | * @param \Wordlift_Relation_Service $relation_service The {@link Wordlift_Relation_Service} instance. |
| 79 | 79 | */ |
| 80 | - public function __construct( $ui_service, $relation_service ) { |
|
| 80 | + public function __construct($ui_service, $relation_service) { |
|
| 81 | 81 | |
| 82 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Service' ); |
|
| 82 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Service'); |
|
| 83 | 83 | |
| 84 | 84 | $this->ui_service = $ui_service; |
| 85 | 85 | $this->relation_service = $relation_service; |
@@ -109,16 +109,16 @@ discard block |
||
| 109 | 109 | * |
| 110 | 110 | * @return bool Return true if the post is an entity otherwise false. |
| 111 | 111 | */ |
| 112 | - public function is_entity( $post_id ) { |
|
| 112 | + public function is_entity($post_id) { |
|
| 113 | 113 | |
| 114 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 114 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 115 | 115 | |
| 116 | - if ( 0 === count( $terms ) ) { |
|
| 116 | + if (0 === count($terms)) { |
|
| 117 | 117 | return false; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // We don't consider an `article` to be an entity. |
| 121 | - if ( 'article' !== $terms[0]->slug ) { |
|
| 121 | + if ('article' !== $terms[0]->slug) { |
|
| 122 | 122 | return true; |
| 123 | 123 | } |
| 124 | 124 | |
@@ -137,19 +137,19 @@ discard block |
||
| 137 | 137 | * |
| 138 | 138 | * @return string Returns a classification scope (e.g. 'what'). |
| 139 | 139 | */ |
| 140 | - public function get_classification_scope_for( $post_id, $default = WL_WHAT_RELATION ) { |
|
| 140 | + public function get_classification_scope_for($post_id, $default = WL_WHAT_RELATION) { |
|
| 141 | 141 | |
| 142 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 142 | + if (false === $this->is_entity($post_id)) { |
|
| 143 | 143 | return $default; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // Retrieve the entity type |
| 147 | - $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 148 | - $entity_type = str_replace( 'wl-', '', $entity_type_arr['css_class'] ); |
|
| 147 | + $entity_type_arr = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 148 | + $entity_type = str_replace('wl-', '', $entity_type_arr['css_class']); |
|
| 149 | 149 | // Retrieve classification boxes configuration |
| 150 | - $classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES ); |
|
| 151 | - foreach ( $classification_boxes as $cb ) { |
|
| 152 | - if ( in_array( $entity_type, $cb['registeredTypes'] ) ) { |
|
| 150 | + $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); |
|
| 151 | + foreach ($classification_boxes as $cb) { |
|
| 152 | + if (in_array($entity_type, $cb['registeredTypes'])) { |
|
| 153 | 153 | return $cb['id']; |
| 154 | 154 | } |
| 155 | 155 | } |
@@ -164,13 +164,13 @@ discard block |
||
| 164 | 164 | * |
| 165 | 165 | * @return bool|null Null if it's not an entity, otherwise true if it's used. |
| 166 | 166 | */ |
| 167 | - public function is_used( $post_id ) { |
|
| 167 | + public function is_used($post_id) { |
|
| 168 | 168 | |
| 169 | - if ( false === $this->is_entity( $post_id ) ) { |
|
| 169 | + if (false === $this->is_entity($post_id)) { |
|
| 170 | 170 | return null; |
| 171 | 171 | } |
| 172 | 172 | // Retrieve the post |
| 173 | - $entity = get_post( $post_id ); |
|
| 173 | + $entity = get_post($post_id); |
|
| 174 | 174 | |
| 175 | 175 | global $wpdb; |
| 176 | 176 | // Retrieve Wordlift relation instances table name |
@@ -183,9 +183,9 @@ discard block |
||
| 183 | 183 | ); |
| 184 | 184 | |
| 185 | 185 | // Perform the query |
| 186 | - $relation_instances = (int) $wpdb->get_var( $stmt ); |
|
| 186 | + $relation_instances = (int) $wpdb->get_var($stmt); |
|
| 187 | 187 | // If there is at least one relation instance for the current entity, then it's used |
| 188 | - if ( 0 < $relation_instances ) { |
|
| 188 | + if (0 < $relation_instances) { |
|
| 189 | 189 | return true; |
| 190 | 190 | } |
| 191 | 191 | |
@@ -193,13 +193,13 @@ discard block |
||
| 193 | 193 | $stmt = $wpdb->prepare( |
| 194 | 194 | "SELECT COUNT(*) FROM $wpdb->postmeta WHERE post_id != %d AND meta_value = %s", |
| 195 | 195 | $entity->ID, |
| 196 | - wl_get_entity_uri( $entity->ID ) |
|
| 196 | + wl_get_entity_uri($entity->ID) |
|
| 197 | 197 | ); |
| 198 | 198 | // Perform the query |
| 199 | - $meta_instances = (int) $wpdb->get_var( $stmt ); |
|
| 199 | + $meta_instances = (int) $wpdb->get_var($stmt); |
|
| 200 | 200 | |
| 201 | 201 | // If there is at least one meta that refers the current entity uri, then current entity is used |
| 202 | - if ( 0 < $meta_instances ) { |
|
| 202 | + if (0 < $meta_instances) { |
|
| 203 | 203 | return true; |
| 204 | 204 | } |
| 205 | 205 | |
@@ -216,9 +216,9 @@ discard block |
||
| 216 | 216 | * |
| 217 | 217 | * @return true if the uri internal to the current dataset otherwise false. |
| 218 | 218 | */ |
| 219 | - public function is_internal_uri( $uri ) { |
|
| 219 | + public function is_internal_uri($uri) { |
|
| 220 | 220 | |
| 221 | - return ( 0 === strrpos( $uri, wl_configuration_get_redlink_dataset_uri() ) ); |
|
| 221 | + return (0 === strrpos($uri, wl_configuration_get_redlink_dataset_uri())); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -230,10 +230,10 @@ discard block |
||
| 230 | 230 | * |
| 231 | 231 | * @return WP_Post|null A WP_Post instance or null if not found. |
| 232 | 232 | */ |
| 233 | - public function get_entity_post_by_uri( $uri ) { |
|
| 233 | + public function get_entity_post_by_uri($uri) { |
|
| 234 | 234 | |
| 235 | 235 | // Check if we've been provided with a value otherwise return null. |
| 236 | - if ( empty( $uri ) ) { |
|
| 236 | + if (empty($uri)) { |
|
| 237 | 237 | return null; |
| 238 | 238 | } |
| 239 | 239 | |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | // performed also looking at sameAs values. |
| 276 | 276 | // |
| 277 | 277 | // This solve issues like https://github.com/insideout10/wordlift-plugin/issues/237 |
| 278 | - if ( ! $this->is_internal_uri( $uri ) ) { |
|
| 278 | + if ( ! $this->is_internal_uri($uri)) { |
|
| 279 | 279 | |
| 280 | 280 | $query_args['meta_query']['relation'] = 'OR'; |
| 281 | 281 | $query_args['meta_query'][] = array( |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | ); |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | - $posts = get_posts( $query_args ); |
|
| 288 | + $posts = get_posts($query_args); |
|
| 289 | 289 | |
| 290 | 290 | // $query = new WP_Query( $query_args ); |
| 291 | 291 | // |
@@ -293,12 +293,12 @@ discard block |
||
| 293 | 293 | // $posts = $query->get_posts(); |
| 294 | 294 | |
| 295 | 295 | // Return null if no post is found. |
| 296 | - if ( 0 === count( $posts ) ) { |
|
| 296 | + if (0 === count($posts)) { |
|
| 297 | 297 | return null; |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | // Return the found post. |
| 301 | - return current( $posts ); |
|
| 301 | + return current($posts); |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /** |
@@ -313,10 +313,10 @@ discard block |
||
| 313 | 313 | * @param WP_Post $post Post object. |
| 314 | 314 | * @param bool $update Whether this is an existing post being updated or not. |
| 315 | 315 | */ |
| 316 | - public function save_post( $post_id, $post, $update ) { |
|
| 316 | + public function save_post($post_id, $post, $update) { |
|
| 317 | 317 | |
| 318 | 318 | // Avoid doing anything if post is autosave or a revision. |
| 319 | - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { |
|
| 319 | + if (wp_is_post_autosave($post) || wp_is_post_revision($post)) { |
|
| 320 | 320 | return; |
| 321 | 321 | } |
| 322 | 322 | |
@@ -326,15 +326,15 @@ discard block |
||
| 326 | 326 | // the $post_id in the save_post call matches the post id set in the request. |
| 327 | 327 | // |
| 328 | 328 | // If this is not the current post being saved or if it's not an entity, return. |
| 329 | - if ( ! isset( $_REQUEST['post_ID'] ) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity( $post_id ) ) { |
|
| 329 | + if ( ! isset($_REQUEST['post_ID']) || $_REQUEST['post_ID'] != $post_id || ! $this->is_entity($post_id)) { |
|
| 330 | 330 | return; |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | // Get the alt labels from the request (or empty array). |
| 334 | - $alt_labels = isset( $_REQUEST['wl_alternative_label'] ) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 334 | + $alt_labels = isset($_REQUEST['wl_alternative_label']) ? $_REQUEST['wl_alternative_label'] : array(); |
|
| 335 | 335 | |
| 336 | 336 | // Set the alternative labels. |
| 337 | - $this->set_alternative_labels( $post_id, $alt_labels ); |
|
| 337 | + $this->set_alternative_labels($post_id, $alt_labels); |
|
| 338 | 338 | |
| 339 | 339 | } |
| 340 | 340 | |
@@ -346,22 +346,22 @@ discard block |
||
| 346 | 346 | * @param int $post_id The post id. |
| 347 | 347 | * @param array $alt_labels An array of labels. |
| 348 | 348 | */ |
| 349 | - public function set_alternative_labels( $post_id, $alt_labels ) { |
|
| 349 | + public function set_alternative_labels($post_id, $alt_labels) { |
|
| 350 | 350 | |
| 351 | 351 | // Force $alt_labels to be an array |
| 352 | - if ( ! is_array( $alt_labels ) ) { |
|
| 353 | - $alt_labels = array( $alt_labels ); |
|
| 352 | + if ( ! is_array($alt_labels)) { |
|
| 353 | + $alt_labels = array($alt_labels); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | - $this->log->debug( "Setting alternative labels [ post id :: $post_id ][ alt labels :: " . implode( ',', $alt_labels ) . " ]" ); |
|
| 356 | + $this->log->debug("Setting alternative labels [ post id :: $post_id ][ alt labels :: ".implode(',', $alt_labels)." ]"); |
|
| 357 | 357 | |
| 358 | 358 | // Delete all the existing alternate labels. |
| 359 | - delete_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 359 | + delete_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 360 | 360 | |
| 361 | 361 | // Set the alternative labels. |
| 362 | - foreach ( $alt_labels as $alt_label ) { |
|
| 363 | - if ( ! empty( $alt_label ) ) { |
|
| 364 | - add_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label ); |
|
| 362 | + foreach ($alt_labels as $alt_label) { |
|
| 363 | + if ( ! empty($alt_label)) { |
|
| 364 | + add_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label); |
|
| 365 | 365 | } |
| 366 | 366 | } |
| 367 | 367 | |
@@ -376,9 +376,9 @@ discard block |
||
| 376 | 376 | * |
| 377 | 377 | * @return mixed An array of alternative labels. |
| 378 | 378 | */ |
| 379 | - public function get_alternative_labels( $post_id ) { |
|
| 379 | + public function get_alternative_labels($post_id) { |
|
| 380 | 380 | |
| 381 | - return get_post_meta( $post_id, self::ALTERNATIVE_LABEL_META_KEY ); |
|
| 381 | + return get_post_meta($post_id, self::ALTERNATIVE_LABEL_META_KEY); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -390,9 +390,9 @@ discard block |
||
| 390 | 390 | * |
| 391 | 391 | * @return array An array with the entity title and labels. |
| 392 | 392 | */ |
| 393 | - public function get_labels( $post_id ) { |
|
| 393 | + public function get_labels($post_id) { |
|
| 394 | 394 | |
| 395 | - return array_merge( (array) get_the_title( $post_id ), $this->get_alternative_labels( $post_id ) ); |
|
| 395 | + return array_merge((array) get_the_title($post_id), $this->get_alternative_labels($post_id)); |
|
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 398 | /** |
@@ -402,25 +402,25 @@ discard block |
||
| 402 | 402 | * |
| 403 | 403 | * @param WP_Post $post Post object. |
| 404 | 404 | */ |
| 405 | - public function edit_form_before_permalink( $post ) { |
|
| 405 | + public function edit_form_before_permalink($post) { |
|
| 406 | 406 | |
| 407 | 407 | // If it's not an entity, return. |
| 408 | - if ( ! $this->is_entity( $post->ID ) ) { |
|
| 408 | + if ( ! $this->is_entity($post->ID)) { |
|
| 409 | 409 | return; |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | // Print the input template. |
| 413 | - $this->ui_service->print_template( 'wl-tmpl-alternative-label-input', $this->get_alternative_label_input() ); |
|
| 413 | + $this->ui_service->print_template('wl-tmpl-alternative-label-input', $this->get_alternative_label_input()); |
|
| 414 | 414 | |
| 415 | 415 | // Print all the currently set alternative labels. |
| 416 | - foreach ( $this->get_alternative_labels( $post->ID ) as $alt_label ) { |
|
| 416 | + foreach ($this->get_alternative_labels($post->ID) as $alt_label) { |
|
| 417 | 417 | |
| 418 | - echo $this->get_alternative_label_input( $alt_label ); |
|
| 418 | + echo $this->get_alternative_label_input($alt_label); |
|
| 419 | 419 | |
| 420 | 420 | }; |
| 421 | 421 | |
| 422 | 422 | // Print the button. |
| 423 | - $this->ui_service->print_button( 'wl-add-alternative-labels-button', __( 'Add more titles', 'wordlift' ) ); |
|
| 423 | + $this->ui_service->print_button('wl-add-alternative-labels-button', __('Add more titles', 'wordlift')); |
|
| 424 | 424 | |
| 425 | 425 | } |
| 426 | 426 | |
@@ -433,25 +433,25 @@ discard block |
||
| 433 | 433 | * |
| 434 | 434 | * @return null|string The entity URI or NULL if not found or the dataset URI is not configured. |
| 435 | 435 | */ |
| 436 | - public function get_uri( $post_id ) { |
|
| 436 | + public function get_uri($post_id) { |
|
| 437 | 437 | |
| 438 | 438 | // If a null is given, nothing to do |
| 439 | - if ( null == $post_id ) { |
|
| 439 | + if (null == $post_id) { |
|
| 440 | 440 | return null; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | - $uri = get_post_meta( $post_id, WL_ENTITY_URL_META_NAME, true ); |
|
| 443 | + $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true); |
|
| 444 | 444 | |
| 445 | 445 | // If the dataset uri is not properly configured, null is returned |
| 446 | - if ( '' === wl_configuration_get_redlink_dataset_uri() ) { |
|
| 446 | + if ('' === wl_configuration_get_redlink_dataset_uri()) { |
|
| 447 | 447 | return null; |
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | // Set the URI if it isn't set yet. |
| 451 | - $post_status = get_post_status( $post_id ); |
|
| 452 | - if ( empty( $uri ) && 'auto-draft' !== $post_status && 'revision' !== $post_status ) { |
|
| 453 | - $uri = wl_build_entity_uri( $post_id ); |
|
| 454 | - wl_set_entity_uri( $post_id, $uri ); |
|
| 451 | + $post_status = get_post_status($post_id); |
|
| 452 | + if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) { |
|
| 453 | + $uri = wl_build_entity_uri($post_id); |
|
| 454 | + wl_set_entity_uri($post_id, $uri); |
|
| 455 | 455 | } |
| 456 | 456 | |
| 457 | 457 | return $uri; |
@@ -467,9 +467,9 @@ discard block |
||
| 467 | 467 | * |
| 468 | 468 | * @return string The input HTML code. |
| 469 | 469 | */ |
| 470 | - private function get_alternative_label_input( $value = '' ) { |
|
| 470 | + private function get_alternative_label_input($value = '') { |
|
| 471 | 471 | |
| 472 | - return sprintf( self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr( $value ), __( 'Delete', 'wordlift' ) ); |
|
| 472 | + return sprintf(self::ALTERNATIVE_LABEL_INPUT_TEMPLATE, esc_attr($value), __('Delete', 'wordlift')); |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | /** |
@@ -481,12 +481,12 @@ discard block |
||
| 481 | 481 | */ |
| 482 | 482 | public function count() { |
| 483 | 483 | |
| 484 | - $posts = get_posts( $this->add_criterias( array( |
|
| 484 | + $posts = get_posts($this->add_criterias(array( |
|
| 485 | 485 | 'post_status' => 'any', |
| 486 | - 'numberposts' => - 1, |
|
| 487 | - ) ) ); |
|
| 486 | + 'numberposts' => -1, |
|
| 487 | + ))); |
|
| 488 | 488 | |
| 489 | - return count( $posts ); |
|
| 489 | + return count($posts); |
|
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | /** |
@@ -499,7 +499,7 @@ discard block |
||
| 499 | 499 | * |
| 500 | 500 | * @return array The arguments for a `get_posts` call. |
| 501 | 501 | */ |
| 502 | - public static function add_criterias( $args ) { |
|
| 502 | + public static function add_criterias($args) { |
|
| 503 | 503 | |
| 504 | 504 | return $args + array( |
| 505 | 505 | 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
@@ -521,7 +521,7 @@ discard block |
||
| 521 | 521 | */ |
| 522 | 522 | public static function get_entity_terms() { |
| 523 | 523 | |
| 524 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 524 | + $terms = get_terms(Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 525 | 525 | 'hide_empty' => false, |
| 526 | 526 | // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
| 527 | 527 | // An issue has been opened with the AAM plugin author as well. |
@@ -529,13 +529,13 @@ discard block |
||
| 529 | 529 | // see https://github.com/insideout10/wordlift-plugin/issues/334 |
| 530 | 530 | // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
| 531 | 531 | 'fields' => 'all', |
| 532 | - ) ); |
|
| 532 | + )); |
|
| 533 | 533 | |
| 534 | - return array_map( function ( $term ) { |
|
| 534 | + return array_map(function($term) { |
|
| 535 | 535 | return $term->term_id; |
| 536 | - }, array_filter( $terms, function ( $term ) { |
|
| 536 | + }, array_filter($terms, function($term) { |
|
| 537 | 537 | return 'article' !== $term->slug; |
| 538 | - } ) ); |
|
| 538 | + } )); |
|
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | /** |
@@ -550,28 +550,28 @@ discard block |
||
| 550 | 550 | * |
| 551 | 551 | * @return int|WP_Error The entity post id or a {@link WP_Error} in case the `wp_insert_post` call fails. |
| 552 | 552 | */ |
| 553 | - public function create( $name, $type_uri, $logo = null, $status = 'publish' ) { |
|
| 553 | + public function create($name, $type_uri, $logo = null, $status = 'publish') { |
|
| 554 | 554 | |
| 555 | 555 | // Create an entity for the publisher. |
| 556 | - $post_id = wp_insert_post( array( |
|
| 556 | + $post_id = wp_insert_post(array( |
|
| 557 | 557 | 'post_type' => self::TYPE_NAME, |
| 558 | 558 | 'post_title' => $name, |
| 559 | 559 | 'post_status' => $status, |
| 560 | 560 | 'post_content' => '', |
| 561 | - ) ); |
|
| 561 | + )); |
|
| 562 | 562 | |
| 563 | 563 | // Return the error if any. |
| 564 | - if ( is_wp_error( $post_id ) ) { |
|
| 564 | + if (is_wp_error($post_id)) { |
|
| 565 | 565 | return $post_id; |
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | // Set the entity logo. |
| 569 | - if ( $logo && is_numeric( $logo ) ) { |
|
| 570 | - set_post_thumbnail( $post_id, $logo ); |
|
| 569 | + if ($logo && is_numeric($logo)) { |
|
| 570 | + set_post_thumbnail($post_id, $logo); |
|
| 571 | 571 | } |
| 572 | 572 | |
| 573 | 573 | // Set the entity type. |
| 574 | - Wordlift_Entity_Type_Service::get_instance()->set( $post_id, $type_uri ); |
|
| 574 | + Wordlift_Entity_Type_Service::get_instance()->set($post_id, $type_uri); |
|
| 575 | 575 | |
| 576 | 576 | return $post_id; |
| 577 | 577 | } |
@@ -587,9 +587,9 @@ discard block |
||
| 587 | 587 | * |
| 588 | 588 | * @return array An array of post ids. |
| 589 | 589 | */ |
| 590 | - public function get_related_entities( $id, $post_status = 'publish' ) { |
|
| 590 | + public function get_related_entities($id, $post_status = 'publish') { |
|
| 591 | 591 | |
| 592 | - return $this->relation_service->get_objects( $id, 'ids', null, $post_status ); |
|
| 592 | + return $this->relation_service->get_objects($id, 'ids', null, $post_status); |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | /** |
@@ -601,16 +601,16 @@ discard block |
||
| 601 | 601 | * |
| 602 | 602 | * @return array An array of entity posts. |
| 603 | 603 | */ |
| 604 | - public function get( $params = array() ) { |
|
| 604 | + public function get($params = array()) { |
|
| 605 | 605 | |
| 606 | 606 | // Set the defaults. |
| 607 | - $defaults = array( 'post_type' => 'entity' ); |
|
| 607 | + $defaults = array('post_type' => 'entity'); |
|
| 608 | 608 | |
| 609 | 609 | // Merge the defaults with the provided parameters. |
| 610 | - $args = wp_parse_args( $params, $defaults ); |
|
| 610 | + $args = wp_parse_args($params, $defaults); |
|
| 611 | 611 | |
| 612 | 612 | // Call the `get_posts` function. |
| 613 | - return get_posts( $args ); |
|
| 613 | + return get_posts($args); |
|
| 614 | 614 | } |
| 615 | 615 | |
| 616 | 616 | /** |
@@ -626,9 +626,9 @@ discard block |
||
| 626 | 626 | static function valid_entity_post_types() { |
| 627 | 627 | |
| 628 | 628 | // Ignore builtins in the call to avoid getting attachments. |
| 629 | - $post_types = array( 'post', 'page', self::TYPE_NAME ); |
|
| 629 | + $post_types = array('post', 'page', self::TYPE_NAME); |
|
| 630 | 630 | |
| 631 | - return apply_filters( 'wl_valid_entity_post_types', $post_types ); |
|
| 631 | + return apply_filters('wl_valid_entity_post_types', $post_types); |
|
| 632 | 632 | } |
| 633 | 633 | |
| 634 | 634 | } |
@@ -17,52 +17,52 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class Wordlift_Entity_Page_Service { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Set the entity post types as one to be included in archive pages. |
|
| 22 | - * |
|
| 23 | - * In order to have entities show up in standard WP categories (Posts categories) |
|
| 24 | - * we configure the `entity` post type, but we also need to alter the main |
|
| 25 | - * WP query (which by default queries posts only) to include the `entities`. |
|
| 26 | - * |
|
| 27 | - * @since 3.12.0 |
|
| 28 | - * |
|
| 29 | - * @param WP_Query $query WP's {@link WP_Query} instance. |
|
| 30 | - */ |
|
| 31 | - public function pre_get_posts( $query ) { |
|
| 32 | - |
|
| 33 | - // Only for the main query, avoid problems with widgets and what not. |
|
| 34 | - if ( ! $query->is_main_query() ) { |
|
| 35 | - return; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - // We don't want to alter the query if we're in the admin UI, if this is |
|
| 39 | - // not a entity type achieve query, or if the `suppress_filters` is set. |
|
| 40 | - // |
|
| 41 | - // Note that it is unlikely for `suppress_filter` to be set on the front |
|
| 42 | - // end, but let's be safe if it is set the calling code assumes no |
|
| 43 | - // modifications of queries. |
|
| 44 | - |
|
| 45 | - // Ignore admin side request, requests for which filters should be |
|
| 46 | - // suppressed, and when we are not on a entity type archive page. |
|
| 47 | - if ( is_admin() || |
|
| 48 | - ! is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ) || |
|
| 49 | - ! empty( $query->query_vars['suppress_filters'] ) |
|
| 50 | - ) { |
|
| 51 | - return; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - // Events should be sorted by start date in descending order. |
|
| 55 | - if ( is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
| 56 | - |
|
| 57 | - // Update the query to use the start time meta and desc order. |
|
| 58 | - $meta_query[] = array( |
|
| 59 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 60 | - ); |
|
| 61 | - $query->set( 'meta_query', $meta_query ); |
|
| 62 | - $query->set( 'orderby', 'meta_value' ); |
|
| 63 | - $query->set( 'order', 'DESC' ); |
|
| 64 | - } else { |
|
| 65 | - /* |
|
| 20 | + /** |
|
| 21 | + * Set the entity post types as one to be included in archive pages. |
|
| 22 | + * |
|
| 23 | + * In order to have entities show up in standard WP categories (Posts categories) |
|
| 24 | + * we configure the `entity` post type, but we also need to alter the main |
|
| 25 | + * WP query (which by default queries posts only) to include the `entities`. |
|
| 26 | + * |
|
| 27 | + * @since 3.12.0 |
|
| 28 | + * |
|
| 29 | + * @param WP_Query $query WP's {@link WP_Query} instance. |
|
| 30 | + */ |
|
| 31 | + public function pre_get_posts( $query ) { |
|
| 32 | + |
|
| 33 | + // Only for the main query, avoid problems with widgets and what not. |
|
| 34 | + if ( ! $query->is_main_query() ) { |
|
| 35 | + return; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + // We don't want to alter the query if we're in the admin UI, if this is |
|
| 39 | + // not a entity type achieve query, or if the `suppress_filters` is set. |
|
| 40 | + // |
|
| 41 | + // Note that it is unlikely for `suppress_filter` to be set on the front |
|
| 42 | + // end, but let's be safe if it is set the calling code assumes no |
|
| 43 | + // modifications of queries. |
|
| 44 | + |
|
| 45 | + // Ignore admin side request, requests for which filters should be |
|
| 46 | + // suppressed, and when we are not on a entity type archive page. |
|
| 47 | + if ( is_admin() || |
|
| 48 | + ! is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ) || |
|
| 49 | + ! empty( $query->query_vars['suppress_filters'] ) |
|
| 50 | + ) { |
|
| 51 | + return; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + // Events should be sorted by start date in descending order. |
|
| 55 | + if ( is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
| 56 | + |
|
| 57 | + // Update the query to use the start time meta and desc order. |
|
| 58 | + $meta_query[] = array( |
|
| 59 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 60 | + ); |
|
| 61 | + $query->set( 'meta_query', $meta_query ); |
|
| 62 | + $query->set( 'orderby', 'meta_value' ); |
|
| 63 | + $query->set( 'order', 'DESC' ); |
|
| 64 | + } else { |
|
| 65 | + /* |
|
| 66 | 66 | * All other entity types should be sorted by their connectivity. |
| 67 | 67 | * For this we need to query the relationship table which has |
| 68 | 68 | * to be done by manipulating the SQL generated for the query. |
@@ -70,73 +70,73 @@ discard block |
||
| 70 | 70 | * additional filters to handle it. |
| 71 | 71 | */ |
| 72 | 72 | |
| 73 | - add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 74 | - add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 75 | - add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Filter handler that sets the join part of a query to include the |
|
| 81 | - * relationship table to be able to use it in the sorting. |
|
| 82 | - * |
|
| 83 | - * @since 3.15.0 |
|
| 84 | - * |
|
| 85 | - * @param string $join_statement The join part of the SQL statement which is used for the query. |
|
| 86 | - * |
|
| 87 | - * @return string An join SQL which add the relationships table to the join. |
|
| 88 | - */ |
|
| 89 | - public function posts_join( $join_statement ) { |
|
| 90 | - |
|
| 91 | - global $wpdb; |
|
| 92 | - |
|
| 93 | - $join_statement .= " LEFT JOIN {$wpdb->prefix}wl_relation_instances ri " |
|
| 94 | - . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
|
| 95 | - |
|
| 96 | - // Remove to make sure it will not run agan in other context. |
|
| 97 | - remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 98 | - |
|
| 99 | - return $join_statement; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Filter handler that sets the groupby part of a query to include the |
|
| 104 | - * relationship table to be able to use it in the sorting. |
|
| 105 | - * |
|
| 106 | - * @since 3.15.0 |
|
| 107 | - * |
|
| 108 | - * @param string $groupby_statement The groupby part of the SQL statement which is used for the query. |
|
| 109 | - * |
|
| 110 | - * @return string A groupby SQL which add the relationships table to the join. |
|
| 111 | - */ |
|
| 112 | - public function posts_groupby( $groupby_statement ) { |
|
| 113 | - |
|
| 114 | - $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
| 115 | - |
|
| 116 | - // Remove to make sure it will not run agan in other context. |
|
| 117 | - remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 118 | - |
|
| 119 | - return $groupby_statement; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Filter handler that sets the orderby part of a query to sort by number of |
|
| 124 | - * relationships. |
|
| 125 | - * |
|
| 126 | - * @since 3.15.0 |
|
| 127 | - * |
|
| 128 | - * @param string $orderby_statement The orderby part of the SQL statement which is used for the query. |
|
| 129 | - * |
|
| 130 | - * @return string An orderby SQL which sorts by the number of relationships |
|
| 131 | - */ |
|
| 132 | - public function posts_orderby( $orderby_statement ) { |
|
| 133 | - |
|
| 134 | - $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
| 135 | - |
|
| 136 | - // Remove to make sure it will not run agan in other context. |
|
| 137 | - remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 138 | - |
|
| 139 | - return $orderby_statement; |
|
| 140 | - } |
|
| 73 | + add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 74 | + add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 75 | + add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Filter handler that sets the join part of a query to include the |
|
| 81 | + * relationship table to be able to use it in the sorting. |
|
| 82 | + * |
|
| 83 | + * @since 3.15.0 |
|
| 84 | + * |
|
| 85 | + * @param string $join_statement The join part of the SQL statement which is used for the query. |
|
| 86 | + * |
|
| 87 | + * @return string An join SQL which add the relationships table to the join. |
|
| 88 | + */ |
|
| 89 | + public function posts_join( $join_statement ) { |
|
| 90 | + |
|
| 91 | + global $wpdb; |
|
| 92 | + |
|
| 93 | + $join_statement .= " LEFT JOIN {$wpdb->prefix}wl_relation_instances ri " |
|
| 94 | + . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
|
| 95 | + |
|
| 96 | + // Remove to make sure it will not run agan in other context. |
|
| 97 | + remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 98 | + |
|
| 99 | + return $join_statement; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Filter handler that sets the groupby part of a query to include the |
|
| 104 | + * relationship table to be able to use it in the sorting. |
|
| 105 | + * |
|
| 106 | + * @since 3.15.0 |
|
| 107 | + * |
|
| 108 | + * @param string $groupby_statement The groupby part of the SQL statement which is used for the query. |
|
| 109 | + * |
|
| 110 | + * @return string A groupby SQL which add the relationships table to the join. |
|
| 111 | + */ |
|
| 112 | + public function posts_groupby( $groupby_statement ) { |
|
| 113 | + |
|
| 114 | + $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
| 115 | + |
|
| 116 | + // Remove to make sure it will not run agan in other context. |
|
| 117 | + remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 118 | + |
|
| 119 | + return $groupby_statement; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Filter handler that sets the orderby part of a query to sort by number of |
|
| 124 | + * relationships. |
|
| 125 | + * |
|
| 126 | + * @since 3.15.0 |
|
| 127 | + * |
|
| 128 | + * @param string $orderby_statement The orderby part of the SQL statement which is used for the query. |
|
| 129 | + * |
|
| 130 | + * @return string An orderby SQL which sorts by the number of relationships |
|
| 131 | + */ |
|
| 132 | + public function posts_orderby( $orderby_statement ) { |
|
| 133 | + |
|
| 134 | + $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
| 135 | + |
|
| 136 | + // Remove to make sure it will not run agan in other context. |
|
| 137 | + remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 138 | + |
|
| 139 | + return $orderby_statement; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | } |
@@ -28,10 +28,10 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @param WP_Query $query WP's {@link WP_Query} instance. |
| 30 | 30 | */ |
| 31 | - public function pre_get_posts( $query ) { |
|
| 31 | + public function pre_get_posts($query) { |
|
| 32 | 32 | |
| 33 | 33 | // Only for the main query, avoid problems with widgets and what not. |
| 34 | - if ( ! $query->is_main_query() ) { |
|
| 34 | + if ( ! $query->is_main_query()) { |
|
| 35 | 35 | return; |
| 36 | 36 | } |
| 37 | 37 | |
@@ -44,23 +44,23 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | // Ignore admin side request, requests for which filters should be |
| 46 | 46 | // suppressed, and when we are not on a entity type archive page. |
| 47 | - if ( is_admin() || |
|
| 48 | - ! is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ) || |
|
| 49 | - ! empty( $query->query_vars['suppress_filters'] ) |
|
| 47 | + if (is_admin() || |
|
| 48 | + ! is_tax(Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME) || |
|
| 49 | + ! empty($query->query_vars['suppress_filters']) |
|
| 50 | 50 | ) { |
| 51 | 51 | return; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | // Events should be sorted by start date in descending order. |
| 55 | - if ( is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'event' ) ) { |
|
| 55 | + if (is_tax(Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'event')) { |
|
| 56 | 56 | |
| 57 | 57 | // Update the query to use the start time meta and desc order. |
| 58 | 58 | $meta_query[] = array( |
| 59 | 59 | 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
| 60 | 60 | ); |
| 61 | - $query->set( 'meta_query', $meta_query ); |
|
| 62 | - $query->set( 'orderby', 'meta_value' ); |
|
| 63 | - $query->set( 'order', 'DESC' ); |
|
| 61 | + $query->set('meta_query', $meta_query); |
|
| 62 | + $query->set('orderby', 'meta_value'); |
|
| 63 | + $query->set('order', 'DESC'); |
|
| 64 | 64 | } else { |
| 65 | 65 | /* |
| 66 | 66 | * All other entity types should be sorted by their connectivity. |
@@ -70,9 +70,9 @@ discard block |
||
| 70 | 70 | * additional filters to handle it. |
| 71 | 71 | */ |
| 72 | 72 | |
| 73 | - add_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 74 | - add_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 75 | - add_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 73 | + add_filter('posts_join', array($this, 'posts_join')); |
|
| 74 | + add_filter('posts_groupby', array($this, 'posts_groupby')); |
|
| 75 | + add_filter('posts_orderby', array($this, 'posts_orderby')); |
|
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | * |
| 87 | 87 | * @return string An join SQL which add the relationships table to the join. |
| 88 | 88 | */ |
| 89 | - public function posts_join( $join_statement ) { |
|
| 89 | + public function posts_join($join_statement) { |
|
| 90 | 90 | |
| 91 | 91 | global $wpdb; |
| 92 | 92 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | . " ON (ri.object_id = {$wpdb->posts}.ID)"; |
| 95 | 95 | |
| 96 | 96 | // Remove to make sure it will not run agan in other context. |
| 97 | - remove_filter( 'posts_join', array( $this, 'posts_join' ) ); |
|
| 97 | + remove_filter('posts_join', array($this, 'posts_join')); |
|
| 98 | 98 | |
| 99 | 99 | return $join_statement; |
| 100 | 100 | } |
@@ -109,12 +109,12 @@ discard block |
||
| 109 | 109 | * |
| 110 | 110 | * @return string A groupby SQL which add the relationships table to the join. |
| 111 | 111 | */ |
| 112 | - public function posts_groupby( $groupby_statement ) { |
|
| 112 | + public function posts_groupby($groupby_statement) { |
|
| 113 | 113 | |
| 114 | - $groupby_statement = 'ri.object_id, ' . $groupby_statement; |
|
| 114 | + $groupby_statement = 'ri.object_id, '.$groupby_statement; |
|
| 115 | 115 | |
| 116 | 116 | // Remove to make sure it will not run agan in other context. |
| 117 | - remove_filter( 'posts_groupby', array( $this, 'posts_groupby' ) ); |
|
| 117 | + remove_filter('posts_groupby', array($this, 'posts_groupby')); |
|
| 118 | 118 | |
| 119 | 119 | return $groupby_statement; |
| 120 | 120 | } |
@@ -129,12 +129,12 @@ discard block |
||
| 129 | 129 | * |
| 130 | 130 | * @return string An orderby SQL which sorts by the number of relationships |
| 131 | 131 | */ |
| 132 | - public function posts_orderby( $orderby_statement ) { |
|
| 132 | + public function posts_orderby($orderby_statement) { |
|
| 133 | 133 | |
| 134 | - $orderby_statement = 'COUNT( ri.object_id ) DESC, ' . $orderby_statement; |
|
| 134 | + $orderby_statement = 'COUNT( ri.object_id ) DESC, '.$orderby_statement; |
|
| 135 | 135 | |
| 136 | 136 | // Remove to make sure it will not run agan in other context. |
| 137 | - remove_filter( 'posts_orderby', array( $this, 'posts_orderby' ) ); |
|
| 137 | + remove_filter('posts_orderby', array($this, 'posts_orderby')); |
|
| 138 | 138 | |
| 139 | 139 | return $orderby_statement; |
| 140 | 140 | } |
@@ -18,1285 +18,1285 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Schema_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The 'location created' field name. |
|
| 23 | - * |
|
| 24 | - * @since 3.5.0 |
|
| 25 | - */ |
|
| 26 | - const FIELD_LOCATION_CREATED = 'wl_location_created'; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The 'topic' field name. |
|
| 30 | - * |
|
| 31 | - * @since 3.5.0 |
|
| 32 | - */ |
|
| 33 | - const FIELD_TOPIC = 'wl_topic'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The 'author' field name. |
|
| 37 | - * |
|
| 38 | - * @since 3.1.0 |
|
| 39 | - */ |
|
| 40 | - const FIELD_AUTHOR = 'wl_author'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The 'same as' field name. |
|
| 44 | - * |
|
| 45 | - * @since 3.1.0 |
|
| 46 | - */ |
|
| 47 | - const FIELD_SAME_AS = 'entity_same_as'; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * The 'date start' field name. |
|
| 51 | - * |
|
| 52 | - * @since 3.1.0 |
|
| 53 | - */ |
|
| 54 | - const FIELD_DATE_START = 'wl_cal_date_start'; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The 'date end' field name. |
|
| 58 | - * |
|
| 59 | - * @since 3.1.0 |
|
| 60 | - */ |
|
| 61 | - const FIELD_DATE_END = 'wl_cal_date_end'; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * The 'location' field name. |
|
| 65 | - * |
|
| 66 | - * @since 3.1.0 |
|
| 67 | - */ |
|
| 68 | - const FIELD_LOCATION = 'wl_location'; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * The 'founder' field name. |
|
| 72 | - * |
|
| 73 | - * @since 3.1.0 |
|
| 74 | - */ |
|
| 75 | - const FIELD_FOUNDER = 'wl_founder'; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * The 'knows' field name. |
|
| 79 | - * |
|
| 80 | - * @since 3.1.0 |
|
| 81 | - */ |
|
| 82 | - const FIELD_KNOWS = 'wl_knows'; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The 'birth date' field name. |
|
| 86 | - * |
|
| 87 | - * @since 3.1.0 |
|
| 88 | - */ |
|
| 89 | - const FIELD_BIRTH_DATE = 'wl_birth_date'; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * The 'birth place' field name. |
|
| 93 | - * |
|
| 94 | - * @since 3.1.0 |
|
| 95 | - */ |
|
| 96 | - const FIELD_BIRTH_PLACE = 'wl_birth_place'; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * The 'latitude' field name. |
|
| 100 | - * |
|
| 101 | - * @since 3.1.0 |
|
| 102 | - */ |
|
| 103 | - const FIELD_GEO_LATITUDE = 'wl_geo_latitude'; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The 'longitude' field name. |
|
| 107 | - * |
|
| 108 | - * @since 3.1.0 |
|
| 109 | - */ |
|
| 110 | - const FIELD_GEO_LONGITUDE = 'wl_geo_longitude'; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * The 'streetAddress' field name. |
|
| 114 | - * |
|
| 115 | - * @since 3.1.0 |
|
| 116 | - */ |
|
| 117 | - const FIELD_ADDRESS = 'wl_address'; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * The 'postOfficeBoxNumber' field name. |
|
| 121 | - * |
|
| 122 | - * @since 3.3.0 |
|
| 123 | - */ |
|
| 124 | - const FIELD_ADDRESS_PO_BOX = 'wl_address_post_office_box'; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * The 'postalCode' field name. |
|
| 128 | - * |
|
| 129 | - * @since 3.3.0 |
|
| 130 | - */ |
|
| 131 | - const FIELD_ADDRESS_POSTAL_CODE = 'wl_address_postal_code'; |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * The 'addressLocality' field name. |
|
| 135 | - * |
|
| 136 | - * @since 3.3.0 |
|
| 137 | - */ |
|
| 138 | - const FIELD_ADDRESS_LOCALITY = 'wl_address_locality'; |
|
| 139 | - /** |
|
| 140 | - * The 'addressRegion' field name. |
|
| 141 | - * |
|
| 142 | - * @since 3.3.0 |
|
| 143 | - */ |
|
| 144 | - const FIELD_ADDRESS_REGION = 'wl_address_region'; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * The 'addressCountry' field name. |
|
| 148 | - * |
|
| 149 | - * @since 3.3.0 |
|
| 150 | - */ |
|
| 151 | - const FIELD_ADDRESS_COUNTRY = 'wl_address_country'; |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * The 'entity type' field name. |
|
| 155 | - * |
|
| 156 | - * @since 3.1.0 |
|
| 157 | - */ |
|
| 158 | - const FIELD_ENTITY_TYPE = 'wl_entity_type_uri'; |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * The 'email' field name. |
|
| 162 | - * |
|
| 163 | - * @since 3.2.0 |
|
| 164 | - */ |
|
| 165 | - const FIELD_EMAIL = 'wl_email'; |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * The 'affiliation' field name. |
|
| 169 | - * |
|
| 170 | - * @since 3.2.0 |
|
| 171 | - */ |
|
| 172 | - const FIELD_AFFILIATION = 'wl_affiliation'; |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * The 'telephone' field name. |
|
| 176 | - * |
|
| 177 | - * @since 3.8.0 |
|
| 178 | - */ |
|
| 179 | - const FIELD_TELEPHONE = 'wl_schema_telephone'; |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * The 'legalName' field name. |
|
| 183 | - * |
|
| 184 | - * @since 3.12.0 |
|
| 185 | - */ |
|
| 186 | - const FIELD_LEGAL_NAME = 'wl_schema_legal_name'; |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * The 'recipeCuisine' field name. |
|
| 190 | - * |
|
| 191 | - * @since 3.14.0 |
|
| 192 | - */ |
|
| 193 | - const FIELD_RECIPE_CUISINE = 'wl_schema_recipe_cuisine'; |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * The 'recipeIngredient' field name. |
|
| 197 | - * |
|
| 198 | - * @since 3.14.0 |
|
| 199 | - */ |
|
| 200 | - const FIELD_RECIPE_INGREDIENT = 'wl_schema_recipe_ingredient'; |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * The 'calories' field name. |
|
| 204 | - * |
|
| 205 | - * @since 3.14.0 |
|
| 206 | - */ |
|
| 207 | - const FIELD_NUTRITION_INFO_CALORIES = 'wl_schema_nutrition_information_calories'; |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * The 'recipeInstructions' field name. |
|
| 211 | - * |
|
| 212 | - * @since 3.14.0 |
|
| 213 | - */ |
|
| 214 | - const FIELD_RECIPE_INSTRUCTIONS = 'wl_schema_recipe_instructions'; |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * The 'recipeYield' field name. |
|
| 218 | - * |
|
| 219 | - * @since 3.14.0 |
|
| 220 | - */ |
|
| 221 | - const FIELD_RECIPE_YIELD = 'wl_schema_recipe_yield'; |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * The 'prepTime' field name. |
|
| 225 | - * |
|
| 226 | - * @since 3.14.0 |
|
| 227 | - */ |
|
| 228 | - const FIELD_PREP_TIME = 'wl_schema_prep_time'; |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * The 'cookTime' field name. |
|
| 232 | - * |
|
| 233 | - * @since 3.14.0 |
|
| 234 | - */ |
|
| 235 | - const FIELD_COOK_TIME = 'wl_schema_cook_time'; |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * The 'totalTime' field name. |
|
| 239 | - * |
|
| 240 | - * @since 3.14.0 |
|
| 241 | - */ |
|
| 242 | - const FIELD_TOTAL_TIME = 'wl_schema_total_time'; |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * The 'URI' data type name. |
|
| 246 | - * |
|
| 247 | - * @since 3.1.0 |
|
| 248 | - */ |
|
| 249 | - const DATA_TYPE_URI = 'uri'; |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * The 'date' data type name. |
|
| 253 | - * |
|
| 254 | - * @since 3.1.0 |
|
| 255 | - */ |
|
| 256 | - const DATA_TYPE_DATE = 'date'; |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * The 'dateTime' data type name. |
|
| 260 | - * |
|
| 261 | - * @since 3.15.0 |
|
| 262 | - */ |
|
| 263 | - const DATA_TYPE_DATE_TIME = 'dateTime'; |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * The 'time' data type name. |
|
| 267 | - * |
|
| 268 | - * @since 3.14.0 |
|
| 269 | - */ |
|
| 270 | - const DATA_TYPE_DURATION = 'duration'; |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * The 'double' data type name. |
|
| 274 | - * |
|
| 275 | - * @since 3.1.0 |
|
| 276 | - */ |
|
| 277 | - const DATA_TYPE_DOUBLE = 'double'; |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * The 'string' data type name. |
|
| 281 | - * |
|
| 282 | - * @since 3.1.0 |
|
| 283 | - */ |
|
| 284 | - const DATA_TYPE_STRING = 'string'; |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * The multiline text data type name. |
|
| 288 | - * |
|
| 289 | - * @since 3.14.0 |
|
| 290 | - */ |
|
| 291 | - const DATA_TYPE_MULTILINE = 'multiline'; |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * The 'integer' data type name. |
|
| 295 | - * |
|
| 296 | - * @since 3.1.0 |
|
| 297 | - */ |
|
| 298 | - const DATA_TYPE_INTEGER = 'int'; |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * The 'boolean' data type name. |
|
| 302 | - * |
|
| 303 | - * @since 3.1.0 |
|
| 304 | - */ |
|
| 305 | - const DATA_TYPE_BOOLEAN = 'bool'; |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * The schema.org Event type URI. |
|
| 309 | - * |
|
| 310 | - * @since 3.1.0 |
|
| 311 | - */ |
|
| 312 | - const SCHEMA_EVENT_TYPE = 'http://schema.org/Event'; |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * The Schema service singleton instance. |
|
| 316 | - * |
|
| 317 | - * @since 3.1.0 |
|
| 318 | - * @access private |
|
| 319 | - * @var \Wordlift_Schema_Service $instance The Schema service singleton instance. |
|
| 320 | - */ |
|
| 321 | - private static $instance; |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * WordLift's schema. |
|
| 325 | - * |
|
| 326 | - * @since 3.1.0 |
|
| 327 | - * @access private |
|
| 328 | - * @var array $schema WordLift's schema. |
|
| 329 | - */ |
|
| 330 | - private $schema; |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * The Log service. |
|
| 334 | - * |
|
| 335 | - * @since 3.1.0 |
|
| 336 | - * @access private |
|
| 337 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 338 | - */ |
|
| 339 | - private $log; |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * The {@link Wordlift_Post_Property_Storage_Factory} instance. |
|
| 343 | - * |
|
| 344 | - * @since 3.15.0 |
|
| 345 | - * @access private |
|
| 346 | - * @var \Wordlift_Storage_Factory $storage_factory The {@link Wordlift_Post_Property_Storage_Factory} instance. |
|
| 347 | - */ |
|
| 348 | - private $storage_factory; |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * The {@link Wordlift_Sparql_Tuple_Rendition_Factory} instance. |
|
| 352 | - * |
|
| 353 | - * @since 3.15.0 |
|
| 354 | - * @access private |
|
| 355 | - * @var \Wordlift_Sparql_Tuple_Rendition_Factory $rendition_factory The {@link Wordlift_Sparql_Tuple_Rendition_Factory} instance. |
|
| 356 | - */ |
|
| 357 | - private $rendition_factory; |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * The {@link Wordlift_Configuration_Service} instance. |
|
| 361 | - * |
|
| 362 | - * @since 3.15.0 |
|
| 363 | - * @access private |
|
| 364 | - * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 365 | - */ |
|
| 366 | - private $configuration_service; |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * The web site configured language code. |
|
| 370 | - * |
|
| 371 | - * @since 3.15.0 |
|
| 372 | - * @access private |
|
| 373 | - * @var string $language_code The web site configured language code. |
|
| 374 | - */ |
|
| 375 | - private $language_code; |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Wordlift_Schema_Service constructor. |
|
| 379 | - * |
|
| 380 | - * @since 3.1.0 |
|
| 381 | - * |
|
| 382 | - * @param \Wordlift_Storage_Factory $storage_factory The {@link Wordlift_Post_Property_Storage_Factory} instance. |
|
| 383 | - * @param \Wordlift_Sparql_Tuple_Rendition_Factory $rendition_factory The {@link Wordlift_Sparql_Tuple_Rendition_Factory} instance. |
|
| 384 | - * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 385 | - */ |
|
| 386 | - public function __construct( $storage_factory, $rendition_factory, $configuration_service ) { |
|
| 387 | - |
|
| 388 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
| 389 | - |
|
| 390 | - $this->storage_factory = $storage_factory; |
|
| 391 | - $this->rendition_factory = $rendition_factory; |
|
| 392 | - $this->configuration_service = $configuration_service; |
|
| 393 | - $this->language_code = $this->configuration_service->get_language_code(); |
|
| 394 | - |
|
| 395 | - // Set the taxonomy data. |
|
| 396 | - // Note: parent types must be defined before child types. |
|
| 397 | - $this->schema = array( |
|
| 398 | - 'article' => $this->get_article_schema(), |
|
| 399 | - 'thing' => $this->get_thing_schema(), |
|
| 400 | - 'creative-work' => $this->get_creative_work_schema(), |
|
| 401 | - 'event' => $this->get_event_schema(), |
|
| 402 | - 'organization' => $this->get_organization_schema(), |
|
| 403 | - 'person' => $this->get_person_schema(), |
|
| 404 | - 'place' => $this->get_place_schema(), |
|
| 405 | - 'localbusiness' => $this->get_local_business_schema(), |
|
| 406 | - 'recipe' => $this->get_recipe_schema(), |
|
| 407 | - ); |
|
| 408 | - |
|
| 409 | - // Create a singleton instance of the Schema service, useful to provide static functions to global functions. |
|
| 410 | - self::$instance = $this; |
|
| 411 | - |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Get a reference to the Schema service. |
|
| 416 | - * |
|
| 417 | - * @since 3.1.0 |
|
| 418 | - * |
|
| 419 | - * @return Wordlift_Schema_Service A reference to the Schema service. |
|
| 420 | - */ |
|
| 421 | - public static function get_instance() { |
|
| 422 | - |
|
| 423 | - return self::$instance; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Get the properties for a field with the specified key. The key is used as |
|
| 428 | - * meta key when the field's value is stored in WordPress meta data table. |
|
| 429 | - * |
|
| 430 | - * @since 3.6.0 |
|
| 431 | - * |
|
| 432 | - * @param string $key The field's key. |
|
| 433 | - * |
|
| 434 | - * @return null|array An array of field's properties or null if the field is not found. |
|
| 435 | - */ |
|
| 436 | - public function get_field( $key ) { |
|
| 437 | - |
|
| 438 | - // Parse each schema's fields until we find the one we're looking for, then |
|
| 439 | - // return its properties. |
|
| 440 | - foreach ( $this->schema as $_ => $schema ) { |
|
| 441 | - |
|
| 442 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
| 443 | - break; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - foreach ( $schema['custom_fields'] as $field => $props ) { |
|
| 447 | - if ( $key === $field ) { |
|
| 448 | - return $props; |
|
| 449 | - } |
|
| 450 | - } |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - return null; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Get the WordLift's schema. |
|
| 458 | - * |
|
| 459 | - * @param string $name The schema name. |
|
| 460 | - * |
|
| 461 | - * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
| 462 | - * |
|
| 463 | - * @since 3.1.0 |
|
| 464 | - */ |
|
| 465 | - public function get_schema( $name ) { |
|
| 466 | - // Check if the schema exists and, if not, return NULL. |
|
| 467 | - if ( ! isset( $this->schema[ $name ] ) ) { |
|
| 468 | - return null; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - // Return the requested schema. |
|
| 472 | - return $this->schema[ $name ]; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - /** |
|
| 476 | - * Get the WordLift's schema trough schema type uri. |
|
| 477 | - * |
|
| 478 | - * @param string $uri The schema uri. |
|
| 479 | - * |
|
| 480 | - * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
| 481 | - * |
|
| 482 | - * @since 3.3.0 |
|
| 483 | - */ |
|
| 484 | - public function get_schema_by_uri( $uri ) { |
|
| 485 | - |
|
| 486 | - foreach ( $this->schema as $name => $schema ) { |
|
| 487 | - if ( $schema['uri'] === $uri ) { |
|
| 488 | - return $schema; |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - return null; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - /** |
|
| 496 | - * Get the 'thing' schema. |
|
| 497 | - * |
|
| 498 | - * @return array An array with the schema configuration. |
|
| 499 | - * |
|
| 500 | - * @since 3.1.0 |
|
| 501 | - */ |
|
| 502 | - private function get_thing_schema() { |
|
| 503 | - |
|
| 504 | - return array( |
|
| 505 | - 'css_class' => 'wl-thing', |
|
| 506 | - 'uri' => 'http://schema.org/Thing', |
|
| 507 | - 'same_as' => array( '*' ), |
|
| 508 | - // set as default. |
|
| 509 | - 'custom_fields' => array( |
|
| 510 | - self::FIELD_SAME_AS => array( |
|
| 511 | - 'predicate' => 'http://schema.org/sameAs', |
|
| 512 | - 'type' => self::DATA_TYPE_URI, |
|
| 513 | - 'export_type' => 'http://schema.org/Thing', |
|
| 514 | - 'constraints' => array( |
|
| 515 | - 'cardinality' => INF, |
|
| 516 | - ), |
|
| 517 | - // We need a custom metabox. |
|
| 518 | - 'input_field' => 'sameas', |
|
| 519 | - ), |
|
| 520 | - // Add the schema:url property. |
|
| 521 | - Wordlift_Schema_Url_Property_Service::META_KEY => Wordlift_Schema_Url_Property_Service::get_instance() |
|
| 522 | - ->get_compat_definition(), |
|
| 523 | - ), |
|
| 524 | - // {{sameAs}} not present in the microdata template, |
|
| 525 | - // because it is treated separately in *wl_content_embed_item_microdata* |
|
| 526 | - 'templates' => array( |
|
| 527 | - 'subtitle' => '{{id}}', |
|
| 528 | - ), |
|
| 529 | - 'linked_data' => array( |
|
| 530 | - // ### Title to rdfs:label. |
|
| 531 | - $this->rendition_factory->create( |
|
| 532 | - $this->storage_factory->post_title(), |
|
| 533 | - Wordlift_Query_Builder::RDFS_LABEL_URI, |
|
| 534 | - null, |
|
| 535 | - $this->language_code |
|
| 536 | - ), |
|
| 537 | - // ### Title to dct:title. |
|
| 538 | - $this->rendition_factory->create( |
|
| 539 | - $this->storage_factory->post_title(), |
|
| 540 | - 'http://purl.org/dc/terms/title', |
|
| 541 | - null, |
|
| 542 | - $this->language_code |
|
| 543 | - ), |
|
| 544 | - // ### Alternative title to rdfs:label. |
|
| 545 | - $this->rendition_factory->create( |
|
| 546 | - $this->storage_factory->post_meta( Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ), |
|
| 547 | - Wordlift_Query_Builder::RDFS_LABEL_URI, |
|
| 548 | - null, |
|
| 549 | - $this->language_code |
|
| 550 | - ), |
|
| 551 | - // ### Alternative title to dct:title. |
|
| 552 | - $this->rendition_factory->create( |
|
| 553 | - $this->storage_factory->post_meta( Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ), |
|
| 554 | - 'http://purl.org/dc/terms/title', |
|
| 555 | - null, |
|
| 556 | - $this->language_code |
|
| 557 | - ), |
|
| 558 | - // ### schema:url. |
|
| 559 | - $this->rendition_factory->create( |
|
| 560 | - $this->storage_factory->url_property(), |
|
| 561 | - Wordlift_Query_Builder::SCHEMA_URL_URI, |
|
| 562 | - self::DATA_TYPE_URI |
|
| 563 | - ), |
|
| 564 | - // ### schema:description. |
|
| 565 | - $this->rendition_factory->create( |
|
| 566 | - $this->storage_factory->post_description_no_tags_no_shortcodes(), |
|
| 567 | - 'http://schema.org/description', |
|
| 568 | - null, |
|
| 569 | - $this->language_code |
|
| 570 | - ), |
|
| 571 | - // ### owl:sameAs. |
|
| 572 | - $this->rendition_factory->create( |
|
| 573 | - $this->storage_factory->post_meta( self::FIELD_SAME_AS ), |
|
| 574 | - 'http://www.w3.org/2002/07/owl#sameAs', |
|
| 575 | - self::DATA_TYPE_URI |
|
| 576 | - ), |
|
| 577 | - // ### rdf:type. |
|
| 578 | - $this->rendition_factory->create( |
|
| 579 | - $this->storage_factory->schema_class( $this ), |
|
| 580 | - Wordlift_Query_Builder::RDFS_TYPE_URI, |
|
| 581 | - self::DATA_TYPE_URI |
|
| 582 | - ), |
|
| 583 | - // ### schema:image. |
|
| 584 | - $this->rendition_factory->create( |
|
| 585 | - $this->storage_factory->post_images(), |
|
| 586 | - Wordlift_Query_Builder::SCHEMA_IMAGE_URI, |
|
| 587 | - self::DATA_TYPE_URI |
|
| 588 | - ), |
|
| 589 | - // ### dct:relation. |
|
| 590 | - $this->rendition_factory->create( |
|
| 591 | - $this->storage_factory->relations(), |
|
| 592 | - Wordlift_Query_Builder::DCTERMS_RELATION_URI, |
|
| 593 | - self::DATA_TYPE_URI |
|
| 594 | - ), |
|
| 595 | - ), |
|
| 596 | - ); |
|
| 597 | - |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * Get the 'creative work' schema. |
|
| 602 | - * |
|
| 603 | - * @return array An array with the schema configuration. |
|
| 604 | - * |
|
| 605 | - * @since 3.1.0 |
|
| 606 | - */ |
|
| 607 | - private function get_creative_work_schema() { |
|
| 608 | - |
|
| 609 | - $schema = array( |
|
| 610 | - 'label' => 'CreativeWork', |
|
| 611 | - 'description' => 'A creative work (or a Music Album).', |
|
| 612 | - 'parents' => array( 'thing' ), |
|
| 613 | - // Give term slug as parent. |
|
| 614 | - 'css_class' => 'wl-creative-work', |
|
| 615 | - 'uri' => 'http://schema.org/CreativeWork', |
|
| 616 | - 'same_as' => array( |
|
| 617 | - 'http://schema.org/MusicAlbum', |
|
| 618 | - 'http://schema.org/Product', |
|
| 619 | - ), |
|
| 620 | - 'custom_fields' => array( |
|
| 621 | - self::FIELD_AUTHOR => array( |
|
| 622 | - 'predicate' => 'http://schema.org/author', |
|
| 623 | - 'type' => self::DATA_TYPE_URI, |
|
| 624 | - 'export_type' => 'http://schema.org/Person', |
|
| 625 | - 'constraints' => array( |
|
| 626 | - 'uri_type' => array( 'Person', 'Organization' ), |
|
| 627 | - 'cardinality' => INF, |
|
| 628 | - ), |
|
| 629 | - ), |
|
| 630 | - ), |
|
| 631 | - 'linked_data' => array( |
|
| 632 | - // ### schema:author. |
|
| 633 | - $this->rendition_factory->create( |
|
| 634 | - $this->storage_factory->author_uri(), |
|
| 635 | - Wordlift_Query_Builder::SCHEMA_AUTHOR_URI, |
|
| 636 | - self::DATA_TYPE_URI |
|
| 637 | - ), |
|
| 638 | - ), |
|
| 639 | - 'templates' => array( |
|
| 640 | - 'subtitle' => '{{id}}', |
|
| 641 | - ), |
|
| 642 | - ); |
|
| 643 | - |
|
| 644 | - // Merge the custom fields with those provided by the thing schema. |
|
| 645 | - $parent_schema = $this->get_thing_schema(); |
|
| 646 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 647 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 648 | - |
|
| 649 | - return $schema; |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Get the 'event' schema. |
|
| 654 | - * |
|
| 655 | - * @return array An array with the schema configuration. |
|
| 656 | - * |
|
| 657 | - * @since 3.1.0 |
|
| 658 | - */ |
|
| 659 | - private function get_event_schema() { |
|
| 660 | - |
|
| 661 | - $schema = array( |
|
| 662 | - 'label' => 'Event', |
|
| 663 | - 'description' => 'An event . ', |
|
| 664 | - 'parents' => array( 'thing' ), |
|
| 665 | - 'css_class' => 'wl-event', |
|
| 666 | - 'uri' => self::SCHEMA_EVENT_TYPE, |
|
| 667 | - 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
| 668 | - 'custom_fields' => array( |
|
| 669 | - self::FIELD_DATE_START => array( |
|
| 670 | - 'predicate' => 'http://schema.org/startDate', |
|
| 671 | - 'type' => self::DATA_TYPE_DATE, |
|
| 672 | - 'export_type' => 'xsd:dateTime', |
|
| 673 | - 'constraints' => '', |
|
| 674 | - ), |
|
| 675 | - self::FIELD_DATE_END => array( |
|
| 676 | - 'predicate' => 'http://schema.org/endDate', |
|
| 677 | - 'type' => self::DATA_TYPE_DATE, |
|
| 678 | - 'export_type' => 'xsd:dateTime', |
|
| 679 | - 'constraints' => '', |
|
| 680 | - ), |
|
| 681 | - self::FIELD_LOCATION => array( |
|
| 682 | - 'predicate' => 'http://schema.org/location', |
|
| 683 | - 'type' => self::DATA_TYPE_URI, |
|
| 684 | - 'export_type' => 'http://schema.org/PostalAddress', |
|
| 685 | - 'constraints' => array( |
|
| 686 | - 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
| 687 | - 'cardinality' => INF, |
|
| 688 | - ), |
|
| 689 | - ), |
|
| 690 | - ), |
|
| 691 | - 'linked_data' => array( |
|
| 692 | - // ### schema:startDate. |
|
| 693 | - $this->rendition_factory->create( |
|
| 694 | - $this->storage_factory->post_meta( self::FIELD_DATE_START ), |
|
| 695 | - 'http://schema.org/startDate', |
|
| 696 | - self::DATA_TYPE_DATE_TIME |
|
| 697 | - ), |
|
| 698 | - // ### schema:endDate. |
|
| 699 | - $this->rendition_factory->create( |
|
| 700 | - $this->storage_factory->post_meta( self::FIELD_DATE_END ), |
|
| 701 | - 'http://schema.org/endDate', |
|
| 702 | - self::DATA_TYPE_DATE_TIME |
|
| 703 | - ), |
|
| 704 | - // ### schema:location. |
|
| 705 | - $this->rendition_factory->create( |
|
| 706 | - $this->storage_factory->post_meta_to_uri( self::FIELD_LOCATION ), |
|
| 707 | - 'http://schema.org/location', |
|
| 708 | - self::DATA_TYPE_URI |
|
| 709 | - ), |
|
| 710 | - ), |
|
| 711 | - 'templates' => array( |
|
| 712 | - 'subtitle' => '{{id}}', |
|
| 713 | - ), |
|
| 714 | - ); |
|
| 715 | - |
|
| 716 | - // Merge the custom fields with those provided by the thing schema. |
|
| 717 | - $parent_schema = $this->get_thing_schema(); |
|
| 718 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 719 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 720 | - |
|
| 721 | - return $schema; |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - /** |
|
| 725 | - * Get the 'organization' schema. |
|
| 726 | - * |
|
| 727 | - * @return array An array with the schema configuration. |
|
| 728 | - * |
|
| 729 | - * @since 3.1.0 |
|
| 730 | - */ |
|
| 731 | - private function get_organization_schema() { |
|
| 732 | - |
|
| 733 | - $schema = array( |
|
| 734 | - 'label' => 'Organization', |
|
| 735 | - 'description' => 'An organization, including a government or a newspaper.', |
|
| 736 | - 'parents' => array( 'thing' ), |
|
| 737 | - 'css_class' => 'wl-organization', |
|
| 738 | - 'uri' => 'http://schema.org/Organization', |
|
| 739 | - 'same_as' => array( |
|
| 740 | - 'http://rdf.freebase.com/ns/organization.organization', |
|
| 741 | - 'http://rdf.freebase.com/ns/government.government', |
|
| 742 | - 'http://schema.org/Newspaper', |
|
| 743 | - ), |
|
| 744 | - 'custom_fields' => array( |
|
| 745 | - self::FIELD_LEGAL_NAME => array( |
|
| 746 | - 'predicate' => 'http://schema.org/legalName', |
|
| 747 | - 'type' => self::DATA_TYPE_STRING, |
|
| 748 | - 'export_type' => 'xsd:string', |
|
| 749 | - 'constraints' => '', |
|
| 750 | - ), |
|
| 751 | - self::FIELD_FOUNDER => array( |
|
| 752 | - 'predicate' => 'http://schema.org/founder', |
|
| 753 | - 'type' => self::DATA_TYPE_URI, |
|
| 754 | - 'export_type' => 'http://schema.org/Person', |
|
| 755 | - 'constraints' => array( |
|
| 756 | - 'uri_type' => 'Person', |
|
| 757 | - 'cardinality' => INF, |
|
| 758 | - ), |
|
| 759 | - ), |
|
| 760 | - self::FIELD_ADDRESS => array( |
|
| 761 | - 'predicate' => 'http://schema.org/streetAddress', |
|
| 762 | - 'type' => self::DATA_TYPE_STRING, |
|
| 763 | - 'export_type' => 'xsd:string', |
|
| 764 | - 'constraints' => '', |
|
| 765 | - // To build custom metabox. |
|
| 766 | - 'input_field' => 'address', |
|
| 767 | - ), |
|
| 768 | - self::FIELD_ADDRESS_PO_BOX => array( |
|
| 769 | - 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
| 770 | - 'type' => self::DATA_TYPE_STRING, |
|
| 771 | - 'export_type' => 'xsd:string', |
|
| 772 | - 'constraints' => '', |
|
| 773 | - // To build custom metabox. |
|
| 774 | - 'input_field' => 'address', |
|
| 775 | - ), |
|
| 776 | - self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
| 777 | - 'predicate' => 'http://schema.org/postalCode', |
|
| 778 | - 'type' => self::DATA_TYPE_STRING, |
|
| 779 | - 'export_type' => 'xsd:string', |
|
| 780 | - 'constraints' => '', |
|
| 781 | - // To build custom metabox. |
|
| 782 | - 'input_field' => 'address', |
|
| 783 | - ), |
|
| 784 | - self::FIELD_ADDRESS_LOCALITY => array( |
|
| 785 | - 'predicate' => 'http://schema.org/addressLocality', |
|
| 786 | - 'type' => self::DATA_TYPE_STRING, |
|
| 787 | - 'export_type' => 'xsd:string', |
|
| 788 | - 'constraints' => '', |
|
| 789 | - // To build custom metabox. |
|
| 790 | - 'input_field' => 'address', |
|
| 791 | - ), |
|
| 792 | - self::FIELD_ADDRESS_REGION => array( |
|
| 793 | - 'predicate' => 'http://schema.org/addressRegion', |
|
| 794 | - 'type' => self::DATA_TYPE_STRING, |
|
| 795 | - 'export_type' => 'xsd:string', |
|
| 796 | - 'constraints' => '', |
|
| 797 | - // To build custom metabox. |
|
| 798 | - 'input_field' => 'address', |
|
| 799 | - ), |
|
| 800 | - self::FIELD_ADDRESS_COUNTRY => array( |
|
| 801 | - 'predicate' => 'http://schema.org/addressCountry', |
|
| 802 | - 'type' => self::DATA_TYPE_STRING, |
|
| 803 | - 'export_type' => 'xsd:string', |
|
| 804 | - 'constraints' => '', |
|
| 805 | - // To build custom metabox. |
|
| 806 | - 'input_field' => 'address', |
|
| 807 | - ), |
|
| 808 | - self::FIELD_EMAIL => array( |
|
| 809 | - 'predicate' => 'http://schema.org/email', |
|
| 810 | - 'type' => self::DATA_TYPE_STRING, |
|
| 811 | - 'export_type' => 'xsd:string', |
|
| 812 | - 'constraints' => '', |
|
| 813 | - ), |
|
| 814 | - self::FIELD_TELEPHONE => array( |
|
| 815 | - 'predicate' => 'http://schema.org/telephone', |
|
| 816 | - 'type' => self::DATA_TYPE_STRING, |
|
| 817 | - 'export_type' => 'xsd:string', |
|
| 818 | - 'constraints' => '', |
|
| 819 | - ), |
|
| 820 | - ), |
|
| 821 | - 'linked_data' => array( |
|
| 822 | - // ### schema:legalName. |
|
| 823 | - $this->rendition_factory->create( |
|
| 824 | - $this->storage_factory->post_meta( self::FIELD_LEGAL_NAME ), |
|
| 825 | - 'http://schema.org/legalName' |
|
| 826 | - ), |
|
| 827 | - // ### schema:founder. |
|
| 828 | - $this->rendition_factory->create( |
|
| 829 | - $this->storage_factory->post_meta_to_uri( self::FIELD_FOUNDER ), |
|
| 830 | - 'http://schema.org/founder', |
|
| 831 | - self::DATA_TYPE_URI |
|
| 832 | - ), |
|
| 833 | - // ### schema:email. |
|
| 834 | - $this->rendition_factory->create( |
|
| 835 | - $this->storage_factory->post_meta( self::FIELD_EMAIL ), |
|
| 836 | - 'http://schema.org/email' |
|
| 837 | - ), |
|
| 838 | - // ### schema:telephone. |
|
| 839 | - $this->rendition_factory->create( |
|
| 840 | - $this->storage_factory->post_meta( self::FIELD_TELEPHONE ), |
|
| 841 | - 'http://schema.org/telephone' |
|
| 842 | - ), |
|
| 843 | - ), |
|
| 844 | - 'templates' => array( |
|
| 845 | - 'subtitle' => '{{id}}', |
|
| 846 | - ), |
|
| 847 | - ); |
|
| 848 | - |
|
| 849 | - // Merge the custom fields with those provided by the thing schema. |
|
| 850 | - $parent_schema = $this->get_thing_schema(); |
|
| 851 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 852 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 853 | - |
|
| 854 | - return $schema; |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * Get the 'person' schema. |
|
| 859 | - * |
|
| 860 | - * @return array An array with the schema configuration. |
|
| 861 | - * |
|
| 862 | - * @since 3.1.0 |
|
| 863 | - */ |
|
| 864 | - private function get_person_schema() { |
|
| 865 | - |
|
| 866 | - $schema = array( |
|
| 867 | - 'label' => 'Person', |
|
| 868 | - 'description' => 'A person (or a music artist).', |
|
| 869 | - 'parents' => array( 'thing' ), |
|
| 870 | - 'css_class' => 'wl-person', |
|
| 871 | - 'uri' => 'http://schema.org/Person', |
|
| 872 | - 'same_as' => array( |
|
| 873 | - 'http://rdf.freebase.com/ns/people.person', |
|
| 874 | - 'http://rdf.freebase.com/ns/music.artist', |
|
| 875 | - 'http://dbpedia.org/class/yago/LivingPeople', |
|
| 876 | - ), |
|
| 877 | - 'custom_fields' => array( |
|
| 878 | - self::FIELD_KNOWS => array( |
|
| 879 | - 'predicate' => 'http://schema.org/knows', |
|
| 880 | - 'type' => self::DATA_TYPE_URI, |
|
| 881 | - 'export_type' => 'http://schema.org/Person', |
|
| 882 | - 'constraints' => array( |
|
| 883 | - 'uri_type' => 'Person', |
|
| 884 | - 'cardinality' => INF, |
|
| 885 | - ), |
|
| 886 | - ), |
|
| 887 | - self::FIELD_BIRTH_DATE => array( |
|
| 888 | - 'predicate' => 'http://schema.org/birthDate', |
|
| 889 | - 'type' => self::DATA_TYPE_DATE, |
|
| 890 | - 'export_type' => 'xsd:date', |
|
| 891 | - 'constraints' => '', |
|
| 892 | - ), |
|
| 893 | - self::FIELD_BIRTH_PLACE => array( |
|
| 894 | - 'predicate' => 'http://schema.org/birthPlace', |
|
| 895 | - 'type' => self::DATA_TYPE_URI, |
|
| 896 | - 'export_type' => 'http://schema.org/Place', |
|
| 897 | - 'constraints' => array( |
|
| 898 | - 'uri_type' => 'Place', |
|
| 899 | - ), |
|
| 900 | - ), |
|
| 901 | - self::FIELD_AFFILIATION => array( |
|
| 902 | - 'predicate' => 'http://schema.org/affiliation', |
|
| 903 | - 'type' => self::DATA_TYPE_URI, |
|
| 904 | - 'export_type' => 'http://schema.org/Organization', |
|
| 905 | - 'constraints' => array( |
|
| 906 | - 'uri_type' => array( |
|
| 907 | - 'Organization', |
|
| 908 | - 'LocalBusiness', |
|
| 909 | - ), |
|
| 910 | - 'cardinality' => INF, |
|
| 911 | - ), |
|
| 912 | - ), |
|
| 913 | - self::FIELD_EMAIL => array( |
|
| 914 | - 'predicate' => 'http://schema.org/email', |
|
| 915 | - 'type' => self::DATA_TYPE_STRING, |
|
| 916 | - 'export_type' => 'xsd:string', |
|
| 917 | - 'constraints' => array( |
|
| 918 | - 'cardinality' => INF, |
|
| 919 | - ), |
|
| 920 | - ), |
|
| 921 | - ), |
|
| 922 | - 'linked_data' => array( |
|
| 923 | - // ### schema:knows. |
|
| 924 | - $this->rendition_factory->create( |
|
| 925 | - $this->storage_factory->post_meta_to_uri( self::FIELD_KNOWS ), |
|
| 926 | - 'http://schema.org/knows', |
|
| 927 | - self::DATA_TYPE_URI |
|
| 928 | - ), |
|
| 929 | - // ### schema:birthDate. |
|
| 930 | - $this->rendition_factory->create( |
|
| 931 | - $this->storage_factory->post_meta( self::FIELD_BIRTH_DATE ), |
|
| 932 | - 'http://schema.org/birthDate', |
|
| 933 | - self::DATA_TYPE_DATE |
|
| 934 | - ), |
|
| 935 | - // ### schema:birthPlace. |
|
| 936 | - $this->rendition_factory->create( |
|
| 937 | - $this->storage_factory->post_meta_to_uri( self::FIELD_BIRTH_PLACE ), |
|
| 938 | - 'http://schema.org/birthPlace', |
|
| 939 | - self::DATA_TYPE_URI |
|
| 940 | - ), |
|
| 941 | - // ### schema:affiliation. |
|
| 942 | - $this->rendition_factory->create( |
|
| 943 | - $this->storage_factory->post_meta_to_uri( self::FIELD_AFFILIATION ), |
|
| 944 | - 'http://schema.org/affiliation', |
|
| 945 | - self::DATA_TYPE_URI |
|
| 946 | - ), |
|
| 947 | - // ### schema:email. |
|
| 948 | - $this->rendition_factory->create( |
|
| 949 | - $this->storage_factory->post_meta( self::FIELD_EMAIL ), |
|
| 950 | - 'http://schema.org/email' |
|
| 951 | - ), |
|
| 952 | - ), |
|
| 953 | - 'templates' => array( |
|
| 954 | - 'subtitle' => '{{id}}', |
|
| 955 | - ), |
|
| 956 | - ); |
|
| 957 | - |
|
| 958 | - // Merge the custom fields with those provided by the thing schema. |
|
| 959 | - $parent_schema = $this->get_thing_schema(); |
|
| 960 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 961 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 962 | - |
|
| 963 | - return $schema; |
|
| 964 | - |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - /** |
|
| 968 | - * Get the 'place' schema. |
|
| 969 | - * |
|
| 970 | - * @return array An array with the schema configuration. |
|
| 971 | - * |
|
| 972 | - * @since 3.1.0 |
|
| 973 | - */ |
|
| 974 | - private function get_place_schema() { |
|
| 975 | - |
|
| 976 | - $schema = array( |
|
| 977 | - 'label' => 'Place', |
|
| 978 | - 'description' => 'A place.', |
|
| 979 | - 'parents' => array( 'thing' ), |
|
| 980 | - 'css_class' => 'wl-place', |
|
| 981 | - 'uri' => 'http://schema.org/Place', |
|
| 982 | - 'same_as' => array( |
|
| 983 | - 'http://rdf.freebase.com/ns/location.location', |
|
| 984 | - 'http://www.opengis.net/gml/_Feature', |
|
| 985 | - ), |
|
| 986 | - 'custom_fields' => array( |
|
| 987 | - self::FIELD_GEO_LATITUDE => array( |
|
| 988 | - 'predicate' => 'http://schema.org/latitude', |
|
| 989 | - 'type' => self::DATA_TYPE_DOUBLE, |
|
| 990 | - 'export_type' => 'xsd:double', |
|
| 991 | - 'constraints' => '', |
|
| 992 | - // To build custom metabox. |
|
| 993 | - 'input_field' => 'coordinates', |
|
| 994 | - ), |
|
| 995 | - self::FIELD_GEO_LONGITUDE => array( |
|
| 996 | - 'predicate' => 'http://schema.org/longitude', |
|
| 997 | - 'type' => self::DATA_TYPE_DOUBLE, |
|
| 998 | - 'export_type' => 'xsd:double', |
|
| 999 | - 'constraints' => '', |
|
| 1000 | - // To build custom metabox. |
|
| 1001 | - 'input_field' => 'coordinates', |
|
| 1002 | - ), |
|
| 1003 | - self::FIELD_ADDRESS => array( |
|
| 1004 | - 'predicate' => 'http://schema.org/streetAddress', |
|
| 1005 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1006 | - 'export_type' => 'xsd:string', |
|
| 1007 | - 'constraints' => '', |
|
| 1008 | - // To build custom metabox. |
|
| 1009 | - 'input_field' => 'address', |
|
| 1010 | - ), |
|
| 1011 | - self::FIELD_ADDRESS_PO_BOX => array( |
|
| 1012 | - 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
| 1013 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1014 | - 'export_type' => 'xsd:string', |
|
| 1015 | - 'constraints' => '', |
|
| 1016 | - // To build custom metabox. |
|
| 1017 | - 'input_field' => 'address', |
|
| 1018 | - ), |
|
| 1019 | - self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
| 1020 | - 'predicate' => 'http://schema.org/postalCode', |
|
| 1021 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1022 | - 'export_type' => 'xsd:string', |
|
| 1023 | - 'constraints' => '', |
|
| 1024 | - // To build custom metabox. |
|
| 1025 | - 'input_field' => 'address', |
|
| 1026 | - ), |
|
| 1027 | - self::FIELD_ADDRESS_LOCALITY => array( |
|
| 1028 | - 'predicate' => 'http://schema.org/addressLocality', |
|
| 1029 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1030 | - 'export_type' => 'xsd:string', |
|
| 1031 | - 'constraints' => '', |
|
| 1032 | - // To build custom metabox. |
|
| 1033 | - 'input_field' => 'address', |
|
| 1034 | - ), |
|
| 1035 | - self::FIELD_ADDRESS_REGION => array( |
|
| 1036 | - 'predicate' => 'http://schema.org/addressRegion', |
|
| 1037 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1038 | - 'export_type' => 'xsd:string', |
|
| 1039 | - 'constraints' => '', |
|
| 1040 | - // To build custom metabox. |
|
| 1041 | - 'input_field' => 'address', |
|
| 1042 | - ), |
|
| 1043 | - self::FIELD_ADDRESS_COUNTRY => array( |
|
| 1044 | - 'predicate' => 'http://schema.org/addressCountry', |
|
| 1045 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1046 | - 'export_type' => 'xsd:string', |
|
| 1047 | - 'constraints' => '', |
|
| 1048 | - // To build custom metabox. |
|
| 1049 | - 'input_field' => 'address', |
|
| 1050 | - ), |
|
| 1051 | - ), |
|
| 1052 | - 'linked_data' => array(), |
|
| 1053 | - 'templates' => array( |
|
| 1054 | - 'subtitle' => '{{id}}', |
|
| 1055 | - ), |
|
| 1056 | - ); |
|
| 1057 | - |
|
| 1058 | - // Merge the custom fields with those provided by the thing schema. |
|
| 1059 | - $parent_schema = $this->get_thing_schema(); |
|
| 1060 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 1061 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 1062 | - |
|
| 1063 | - return $schema; |
|
| 1064 | - } |
|
| 1065 | - |
|
| 1066 | - /** |
|
| 1067 | - * Get the 'local business' schema. |
|
| 1068 | - * |
|
| 1069 | - * @return array An array with the schema configuration. |
|
| 1070 | - * |
|
| 1071 | - * @since 3.1.0 |
|
| 1072 | - */ |
|
| 1073 | - private function get_local_business_schema() { |
|
| 1074 | - |
|
| 1075 | - $schema = array( |
|
| 1076 | - 'label' => 'LocalBusiness', |
|
| 1077 | - 'description' => 'A local business.', |
|
| 1078 | - 'parents' => array( 'place', 'organization' ), |
|
| 1079 | - 'css_class' => 'wl-local-business', |
|
| 1080 | - 'uri' => 'http://schema.org/LocalBusiness', |
|
| 1081 | - 'same_as' => array( |
|
| 1082 | - 'http://rdf.freebase.com/ns/business/business_location', |
|
| 1083 | - 'https://schema.org/Store', |
|
| 1084 | - ), |
|
| 1085 | - 'custom_fields' => array(), |
|
| 1086 | - 'linked_data' => array(), |
|
| 1087 | - 'templates' => array( |
|
| 1088 | - 'subtitle' => '{{id}}', |
|
| 1089 | - ), |
|
| 1090 | - ); |
|
| 1091 | - |
|
| 1092 | - // Merge the custom fields with those provided by the place and organization schema. |
|
| 1093 | - $place_schema = $this->get_place_schema(); |
|
| 1094 | - $organization_schema = $this->get_organization_schema(); |
|
| 1095 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
| 1096 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $place_schema['linked_data'], $organization_schema['linked_data'] ); |
|
| 1097 | - |
|
| 1098 | - return $schema; |
|
| 1099 | - } |
|
| 1100 | - |
|
| 1101 | - /** |
|
| 1102 | - * Get the 'recipe' schema. |
|
| 1103 | - * |
|
| 1104 | - * @return array An array with the schema configuration. |
|
| 1105 | - * |
|
| 1106 | - * @since 3.14.0 |
|
| 1107 | - */ |
|
| 1108 | - private function get_recipe_schema() { |
|
| 1109 | - |
|
| 1110 | - $schema = array( |
|
| 1111 | - 'label' => 'Recipe', |
|
| 1112 | - 'description' => 'A Recipe.', |
|
| 1113 | - 'parents' => array( 'CreativeWork' ), |
|
| 1114 | - 'css_class' => 'wl-recipe', |
|
| 1115 | - 'uri' => 'http://schema.org/Recipe', |
|
| 1116 | - 'same_as' => array(), |
|
| 1117 | - 'templates' => array( |
|
| 1118 | - 'subtitle' => '{{id}}', |
|
| 1119 | - ), |
|
| 1120 | - 'custom_fields' => array( |
|
| 1121 | - self::FIELD_RECIPE_CUISINE => array( |
|
| 1122 | - 'predicate' => 'http://schema.org/recipeCuisine', |
|
| 1123 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1124 | - 'export_type' => 'xsd:string', |
|
| 1125 | - 'constraints' => '', |
|
| 1126 | - 'metabox' => array( |
|
| 1127 | - 'label' => __( 'Recipe cuisine', 'wordlift' ), |
|
| 1128 | - ), |
|
| 1129 | - ), |
|
| 1130 | - self::FIELD_RECIPE_INGREDIENT => array( |
|
| 1131 | - 'predicate' => 'http://schema.org/recipeIngredient', |
|
| 1132 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1133 | - 'export_type' => 'xsd:string', |
|
| 1134 | - 'constraints' => array( |
|
| 1135 | - 'cardinality' => INF, |
|
| 1136 | - ), |
|
| 1137 | - 'metabox' => array( |
|
| 1138 | - 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
| 1139 | - ), |
|
| 1140 | - ), |
|
| 1141 | - self::FIELD_RECIPE_INSTRUCTIONS => array( |
|
| 1142 | - 'predicate' => 'http://schema.org/recipeInstructions', |
|
| 1143 | - 'type' => self::DATA_TYPE_MULTILINE, |
|
| 1144 | - 'export_type' => 'xsd:string', |
|
| 1145 | - 'constraints' => '', |
|
| 1146 | - 'metabox' => array( |
|
| 1147 | - 'class' => 'Wordlift_Metabox_Field_Multiline', |
|
| 1148 | - 'label' => __( 'Recipe instructions', 'wordlift' ), |
|
| 1149 | - ), |
|
| 1150 | - ), |
|
| 1151 | - self::FIELD_RECIPE_YIELD => array( |
|
| 1152 | - 'predicate' => 'http://schema.org/recipeYield', |
|
| 1153 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1154 | - 'export_type' => 'xsd:string', |
|
| 1155 | - 'constraints' => '', |
|
| 1156 | - 'metabox' => array( |
|
| 1157 | - 'label' => __( 'Recipe number of servings', 'wordlift' ), |
|
| 1158 | - ), |
|
| 1159 | - ), |
|
| 1160 | - self::FIELD_RECIPE_INGREDIENT => array( |
|
| 1161 | - 'predicate' => 'http://schema.org/recipeIngredient', |
|
| 1162 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1163 | - 'export_type' => 'xsd:string', |
|
| 1164 | - 'constraints' => array( |
|
| 1165 | - 'cardinality' => INF, |
|
| 1166 | - ), |
|
| 1167 | - 'metabox' => array( |
|
| 1168 | - 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
| 1169 | - ), |
|
| 1170 | - ), |
|
| 1171 | - self::FIELD_NUTRITION_INFO_CALORIES => array( |
|
| 1172 | - 'predicate' => 'http://schema.org/calories', |
|
| 1173 | - 'type' => self::DATA_TYPE_STRING, |
|
| 1174 | - 'export_type' => 'xsd:string', |
|
| 1175 | - 'constraints' => '', |
|
| 1176 | - 'metabox' => array( |
|
| 1177 | - 'label' => __( 'Calories (e.g. 240 calories)', 'wordlift' ), |
|
| 1178 | - ), |
|
| 1179 | - ), |
|
| 1180 | - self::FIELD_PREP_TIME => array( |
|
| 1181 | - 'predicate' => 'http://schema.org/prepTime', |
|
| 1182 | - 'type' => self::DATA_TYPE_DURATION, |
|
| 1183 | - 'export_type' => 'xsd:time', |
|
| 1184 | - 'constraints' => '', |
|
| 1185 | - 'metabox' => array( |
|
| 1186 | - 'class' => 'Wordlift_Metabox_Field_Duration', |
|
| 1187 | - 'label' => __( 'Recipe preparation time (e.g. 1:30)', 'wordlift' ), |
|
| 1188 | - ), |
|
| 1189 | - ), |
|
| 1190 | - self::FIELD_COOK_TIME => array( |
|
| 1191 | - 'predicate' => 'http://schema.org/cookTime', |
|
| 1192 | - 'type' => self::DATA_TYPE_DURATION, |
|
| 1193 | - 'export_type' => 'xsd:time', |
|
| 1194 | - 'constraints' => '', |
|
| 1195 | - 'metabox' => array( |
|
| 1196 | - 'class' => 'Wordlift_Metabox_Field_Duration', |
|
| 1197 | - 'label' => __( 'Recipe cook time (e.g. 1:30)', 'wordlift' ), |
|
| 1198 | - ), |
|
| 1199 | - ), |
|
| 1200 | - self::FIELD_TOTAL_TIME => array( |
|
| 1201 | - 'predicate' => 'http://schema.org/totalTime', |
|
| 1202 | - 'type' => self::DATA_TYPE_DURATION, |
|
| 1203 | - 'export_type' => 'xsd:time', |
|
| 1204 | - 'constraints' => '', |
|
| 1205 | - 'metabox' => array( |
|
| 1206 | - 'class' => 'Wordlift_Metabox_Field_Duration', |
|
| 1207 | - 'label' => __( 'Recipe total time (e.g. 1:30)', 'wordlift' ), |
|
| 1208 | - ), |
|
| 1209 | - ), |
|
| 1210 | - ), |
|
| 1211 | - 'linked_data' => array( |
|
| 1212 | - // ### schema:recipeCuisine. |
|
| 1213 | - $this->rendition_factory->create( |
|
| 1214 | - $this->storage_factory->post_meta( self::FIELD_RECIPE_CUISINE ), |
|
| 1215 | - 'http://schema.org/recipeCuisine' |
|
| 1216 | - ), |
|
| 1217 | - // ### schema:recipeIngredient. |
|
| 1218 | - $this->rendition_factory->create( |
|
| 1219 | - $this->storage_factory->post_meta( self::FIELD_RECIPE_INGREDIENT ), |
|
| 1220 | - 'http://schema.org/recipeIngredient' |
|
| 1221 | - ), |
|
| 1222 | - // ### schema:prepTime. |
|
| 1223 | - $this->rendition_factory->create( |
|
| 1224 | - $this->storage_factory->post_meta( self::FIELD_PREP_TIME ), |
|
| 1225 | - 'http://schema.org/prepTime', |
|
| 1226 | - self::DATA_TYPE_DURATION |
|
| 1227 | - ), |
|
| 1228 | - // ### schema:cookTime. |
|
| 1229 | - $this->rendition_factory->create( |
|
| 1230 | - $this->storage_factory->post_meta( self::FIELD_COOK_TIME ), |
|
| 1231 | - 'http://schema.org/cookTime', |
|
| 1232 | - self::DATA_TYPE_DURATION |
|
| 1233 | - ), |
|
| 1234 | - // ### schema:totalTime. |
|
| 1235 | - $this->rendition_factory->create( |
|
| 1236 | - $this->storage_factory->post_meta( self::FIELD_TOTAL_TIME ), |
|
| 1237 | - 'http://schema.org/totalTime', |
|
| 1238 | - self::DATA_TYPE_DURATION |
|
| 1239 | - ), |
|
| 1240 | - ), |
|
| 1241 | - ); |
|
| 1242 | - |
|
| 1243 | - // Merge the custom fields with those provided by the parent schema. |
|
| 1244 | - $parent_schema = $this->get_creative_work_schema(); |
|
| 1245 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 1246 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 1247 | - |
|
| 1248 | - return $schema; |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - /** |
|
| 1252 | - * Get the 'article' schema. |
|
| 1253 | - * |
|
| 1254 | - * @return array An array with the schema configuration. |
|
| 1255 | - * |
|
| 1256 | - * @since 3.15.0 |
|
| 1257 | - */ |
|
| 1258 | - private function get_article_schema() { |
|
| 1259 | - |
|
| 1260 | - $schema = array( |
|
| 1261 | - 'label' => 'Article', |
|
| 1262 | - 'description' => 'An Article.', |
|
| 1263 | - 'parents' => array(), |
|
| 1264 | - 'css_class' => 'wl-article', |
|
| 1265 | - 'uri' => 'http://schema.org/Article', |
|
| 1266 | - 'same_as' => array(), |
|
| 1267 | - 'templates' => array( |
|
| 1268 | - 'subtitle' => '{{id}}', |
|
| 1269 | - ), |
|
| 1270 | - 'custom_fields' => array(), |
|
| 1271 | - 'linked_data' => array(), |
|
| 1272 | - ); |
|
| 1273 | - |
|
| 1274 | - return $schema; |
|
| 1275 | - } |
|
| 1276 | - |
|
| 1277 | - /** |
|
| 1278 | - * Get all the predicates. |
|
| 1279 | - * |
|
| 1280 | - * @since 3.15.0 |
|
| 1281 | - * |
|
| 1282 | - * @return array An array of predicates. |
|
| 1283 | - */ |
|
| 1284 | - public function get_all_predicates() { |
|
| 1285 | - |
|
| 1286 | - // Get the custom fields. |
|
| 1287 | - $renditions = array_reduce( $this->schema, function ( $carry, $item ) { |
|
| 1288 | - return array_merge( $carry, $item['linked_data'] ); |
|
| 1289 | - }, array() ); |
|
| 1290 | - |
|
| 1291 | - // Create a new array of predicates from the custom fields. The initial |
|
| 1292 | - // array contains just the `http://www.w3.org/1999/02/22-rdf-syntax-ns#type` |
|
| 1293 | - // (a, rdf:type) predicate (use the full URI). |
|
| 1294 | - $predicates = array_unique( array_reduce( $renditions, function ( $carry, $item ) { |
|
| 1295 | - return array_merge( $carry, (array) $item->get_predicate() ); |
|
| 1296 | - }, array() ) ); |
|
| 1297 | - |
|
| 1298 | - // Finally return the predicates array. |
|
| 1299 | - return $predicates; |
|
| 1300 | - } |
|
| 21 | + /** |
|
| 22 | + * The 'location created' field name. |
|
| 23 | + * |
|
| 24 | + * @since 3.5.0 |
|
| 25 | + */ |
|
| 26 | + const FIELD_LOCATION_CREATED = 'wl_location_created'; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The 'topic' field name. |
|
| 30 | + * |
|
| 31 | + * @since 3.5.0 |
|
| 32 | + */ |
|
| 33 | + const FIELD_TOPIC = 'wl_topic'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The 'author' field name. |
|
| 37 | + * |
|
| 38 | + * @since 3.1.0 |
|
| 39 | + */ |
|
| 40 | + const FIELD_AUTHOR = 'wl_author'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The 'same as' field name. |
|
| 44 | + * |
|
| 45 | + * @since 3.1.0 |
|
| 46 | + */ |
|
| 47 | + const FIELD_SAME_AS = 'entity_same_as'; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * The 'date start' field name. |
|
| 51 | + * |
|
| 52 | + * @since 3.1.0 |
|
| 53 | + */ |
|
| 54 | + const FIELD_DATE_START = 'wl_cal_date_start'; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The 'date end' field name. |
|
| 58 | + * |
|
| 59 | + * @since 3.1.0 |
|
| 60 | + */ |
|
| 61 | + const FIELD_DATE_END = 'wl_cal_date_end'; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * The 'location' field name. |
|
| 65 | + * |
|
| 66 | + * @since 3.1.0 |
|
| 67 | + */ |
|
| 68 | + const FIELD_LOCATION = 'wl_location'; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * The 'founder' field name. |
|
| 72 | + * |
|
| 73 | + * @since 3.1.0 |
|
| 74 | + */ |
|
| 75 | + const FIELD_FOUNDER = 'wl_founder'; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * The 'knows' field name. |
|
| 79 | + * |
|
| 80 | + * @since 3.1.0 |
|
| 81 | + */ |
|
| 82 | + const FIELD_KNOWS = 'wl_knows'; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The 'birth date' field name. |
|
| 86 | + * |
|
| 87 | + * @since 3.1.0 |
|
| 88 | + */ |
|
| 89 | + const FIELD_BIRTH_DATE = 'wl_birth_date'; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * The 'birth place' field name. |
|
| 93 | + * |
|
| 94 | + * @since 3.1.0 |
|
| 95 | + */ |
|
| 96 | + const FIELD_BIRTH_PLACE = 'wl_birth_place'; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * The 'latitude' field name. |
|
| 100 | + * |
|
| 101 | + * @since 3.1.0 |
|
| 102 | + */ |
|
| 103 | + const FIELD_GEO_LATITUDE = 'wl_geo_latitude'; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The 'longitude' field name. |
|
| 107 | + * |
|
| 108 | + * @since 3.1.0 |
|
| 109 | + */ |
|
| 110 | + const FIELD_GEO_LONGITUDE = 'wl_geo_longitude'; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * The 'streetAddress' field name. |
|
| 114 | + * |
|
| 115 | + * @since 3.1.0 |
|
| 116 | + */ |
|
| 117 | + const FIELD_ADDRESS = 'wl_address'; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * The 'postOfficeBoxNumber' field name. |
|
| 121 | + * |
|
| 122 | + * @since 3.3.0 |
|
| 123 | + */ |
|
| 124 | + const FIELD_ADDRESS_PO_BOX = 'wl_address_post_office_box'; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * The 'postalCode' field name. |
|
| 128 | + * |
|
| 129 | + * @since 3.3.0 |
|
| 130 | + */ |
|
| 131 | + const FIELD_ADDRESS_POSTAL_CODE = 'wl_address_postal_code'; |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * The 'addressLocality' field name. |
|
| 135 | + * |
|
| 136 | + * @since 3.3.0 |
|
| 137 | + */ |
|
| 138 | + const FIELD_ADDRESS_LOCALITY = 'wl_address_locality'; |
|
| 139 | + /** |
|
| 140 | + * The 'addressRegion' field name. |
|
| 141 | + * |
|
| 142 | + * @since 3.3.0 |
|
| 143 | + */ |
|
| 144 | + const FIELD_ADDRESS_REGION = 'wl_address_region'; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * The 'addressCountry' field name. |
|
| 148 | + * |
|
| 149 | + * @since 3.3.0 |
|
| 150 | + */ |
|
| 151 | + const FIELD_ADDRESS_COUNTRY = 'wl_address_country'; |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * The 'entity type' field name. |
|
| 155 | + * |
|
| 156 | + * @since 3.1.0 |
|
| 157 | + */ |
|
| 158 | + const FIELD_ENTITY_TYPE = 'wl_entity_type_uri'; |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * The 'email' field name. |
|
| 162 | + * |
|
| 163 | + * @since 3.2.0 |
|
| 164 | + */ |
|
| 165 | + const FIELD_EMAIL = 'wl_email'; |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * The 'affiliation' field name. |
|
| 169 | + * |
|
| 170 | + * @since 3.2.0 |
|
| 171 | + */ |
|
| 172 | + const FIELD_AFFILIATION = 'wl_affiliation'; |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * The 'telephone' field name. |
|
| 176 | + * |
|
| 177 | + * @since 3.8.0 |
|
| 178 | + */ |
|
| 179 | + const FIELD_TELEPHONE = 'wl_schema_telephone'; |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * The 'legalName' field name. |
|
| 183 | + * |
|
| 184 | + * @since 3.12.0 |
|
| 185 | + */ |
|
| 186 | + const FIELD_LEGAL_NAME = 'wl_schema_legal_name'; |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * The 'recipeCuisine' field name. |
|
| 190 | + * |
|
| 191 | + * @since 3.14.0 |
|
| 192 | + */ |
|
| 193 | + const FIELD_RECIPE_CUISINE = 'wl_schema_recipe_cuisine'; |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * The 'recipeIngredient' field name. |
|
| 197 | + * |
|
| 198 | + * @since 3.14.0 |
|
| 199 | + */ |
|
| 200 | + const FIELD_RECIPE_INGREDIENT = 'wl_schema_recipe_ingredient'; |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * The 'calories' field name. |
|
| 204 | + * |
|
| 205 | + * @since 3.14.0 |
|
| 206 | + */ |
|
| 207 | + const FIELD_NUTRITION_INFO_CALORIES = 'wl_schema_nutrition_information_calories'; |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * The 'recipeInstructions' field name. |
|
| 211 | + * |
|
| 212 | + * @since 3.14.0 |
|
| 213 | + */ |
|
| 214 | + const FIELD_RECIPE_INSTRUCTIONS = 'wl_schema_recipe_instructions'; |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * The 'recipeYield' field name. |
|
| 218 | + * |
|
| 219 | + * @since 3.14.0 |
|
| 220 | + */ |
|
| 221 | + const FIELD_RECIPE_YIELD = 'wl_schema_recipe_yield'; |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * The 'prepTime' field name. |
|
| 225 | + * |
|
| 226 | + * @since 3.14.0 |
|
| 227 | + */ |
|
| 228 | + const FIELD_PREP_TIME = 'wl_schema_prep_time'; |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * The 'cookTime' field name. |
|
| 232 | + * |
|
| 233 | + * @since 3.14.0 |
|
| 234 | + */ |
|
| 235 | + const FIELD_COOK_TIME = 'wl_schema_cook_time'; |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * The 'totalTime' field name. |
|
| 239 | + * |
|
| 240 | + * @since 3.14.0 |
|
| 241 | + */ |
|
| 242 | + const FIELD_TOTAL_TIME = 'wl_schema_total_time'; |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * The 'URI' data type name. |
|
| 246 | + * |
|
| 247 | + * @since 3.1.0 |
|
| 248 | + */ |
|
| 249 | + const DATA_TYPE_URI = 'uri'; |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * The 'date' data type name. |
|
| 253 | + * |
|
| 254 | + * @since 3.1.0 |
|
| 255 | + */ |
|
| 256 | + const DATA_TYPE_DATE = 'date'; |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * The 'dateTime' data type name. |
|
| 260 | + * |
|
| 261 | + * @since 3.15.0 |
|
| 262 | + */ |
|
| 263 | + const DATA_TYPE_DATE_TIME = 'dateTime'; |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * The 'time' data type name. |
|
| 267 | + * |
|
| 268 | + * @since 3.14.0 |
|
| 269 | + */ |
|
| 270 | + const DATA_TYPE_DURATION = 'duration'; |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * The 'double' data type name. |
|
| 274 | + * |
|
| 275 | + * @since 3.1.0 |
|
| 276 | + */ |
|
| 277 | + const DATA_TYPE_DOUBLE = 'double'; |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * The 'string' data type name. |
|
| 281 | + * |
|
| 282 | + * @since 3.1.0 |
|
| 283 | + */ |
|
| 284 | + const DATA_TYPE_STRING = 'string'; |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * The multiline text data type name. |
|
| 288 | + * |
|
| 289 | + * @since 3.14.0 |
|
| 290 | + */ |
|
| 291 | + const DATA_TYPE_MULTILINE = 'multiline'; |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * The 'integer' data type name. |
|
| 295 | + * |
|
| 296 | + * @since 3.1.0 |
|
| 297 | + */ |
|
| 298 | + const DATA_TYPE_INTEGER = 'int'; |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * The 'boolean' data type name. |
|
| 302 | + * |
|
| 303 | + * @since 3.1.0 |
|
| 304 | + */ |
|
| 305 | + const DATA_TYPE_BOOLEAN = 'bool'; |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * The schema.org Event type URI. |
|
| 309 | + * |
|
| 310 | + * @since 3.1.0 |
|
| 311 | + */ |
|
| 312 | + const SCHEMA_EVENT_TYPE = 'http://schema.org/Event'; |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * The Schema service singleton instance. |
|
| 316 | + * |
|
| 317 | + * @since 3.1.0 |
|
| 318 | + * @access private |
|
| 319 | + * @var \Wordlift_Schema_Service $instance The Schema service singleton instance. |
|
| 320 | + */ |
|
| 321 | + private static $instance; |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * WordLift's schema. |
|
| 325 | + * |
|
| 326 | + * @since 3.1.0 |
|
| 327 | + * @access private |
|
| 328 | + * @var array $schema WordLift's schema. |
|
| 329 | + */ |
|
| 330 | + private $schema; |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * The Log service. |
|
| 334 | + * |
|
| 335 | + * @since 3.1.0 |
|
| 336 | + * @access private |
|
| 337 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 338 | + */ |
|
| 339 | + private $log; |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * The {@link Wordlift_Post_Property_Storage_Factory} instance. |
|
| 343 | + * |
|
| 344 | + * @since 3.15.0 |
|
| 345 | + * @access private |
|
| 346 | + * @var \Wordlift_Storage_Factory $storage_factory The {@link Wordlift_Post_Property_Storage_Factory} instance. |
|
| 347 | + */ |
|
| 348 | + private $storage_factory; |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * The {@link Wordlift_Sparql_Tuple_Rendition_Factory} instance. |
|
| 352 | + * |
|
| 353 | + * @since 3.15.0 |
|
| 354 | + * @access private |
|
| 355 | + * @var \Wordlift_Sparql_Tuple_Rendition_Factory $rendition_factory The {@link Wordlift_Sparql_Tuple_Rendition_Factory} instance. |
|
| 356 | + */ |
|
| 357 | + private $rendition_factory; |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * The {@link Wordlift_Configuration_Service} instance. |
|
| 361 | + * |
|
| 362 | + * @since 3.15.0 |
|
| 363 | + * @access private |
|
| 364 | + * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 365 | + */ |
|
| 366 | + private $configuration_service; |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * The web site configured language code. |
|
| 370 | + * |
|
| 371 | + * @since 3.15.0 |
|
| 372 | + * @access private |
|
| 373 | + * @var string $language_code The web site configured language code. |
|
| 374 | + */ |
|
| 375 | + private $language_code; |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Wordlift_Schema_Service constructor. |
|
| 379 | + * |
|
| 380 | + * @since 3.1.0 |
|
| 381 | + * |
|
| 382 | + * @param \Wordlift_Storage_Factory $storage_factory The {@link Wordlift_Post_Property_Storage_Factory} instance. |
|
| 383 | + * @param \Wordlift_Sparql_Tuple_Rendition_Factory $rendition_factory The {@link Wordlift_Sparql_Tuple_Rendition_Factory} instance. |
|
| 384 | + * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
|
| 385 | + */ |
|
| 386 | + public function __construct( $storage_factory, $rendition_factory, $configuration_service ) { |
|
| 387 | + |
|
| 388 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
| 389 | + |
|
| 390 | + $this->storage_factory = $storage_factory; |
|
| 391 | + $this->rendition_factory = $rendition_factory; |
|
| 392 | + $this->configuration_service = $configuration_service; |
|
| 393 | + $this->language_code = $this->configuration_service->get_language_code(); |
|
| 394 | + |
|
| 395 | + // Set the taxonomy data. |
|
| 396 | + // Note: parent types must be defined before child types. |
|
| 397 | + $this->schema = array( |
|
| 398 | + 'article' => $this->get_article_schema(), |
|
| 399 | + 'thing' => $this->get_thing_schema(), |
|
| 400 | + 'creative-work' => $this->get_creative_work_schema(), |
|
| 401 | + 'event' => $this->get_event_schema(), |
|
| 402 | + 'organization' => $this->get_organization_schema(), |
|
| 403 | + 'person' => $this->get_person_schema(), |
|
| 404 | + 'place' => $this->get_place_schema(), |
|
| 405 | + 'localbusiness' => $this->get_local_business_schema(), |
|
| 406 | + 'recipe' => $this->get_recipe_schema(), |
|
| 407 | + ); |
|
| 408 | + |
|
| 409 | + // Create a singleton instance of the Schema service, useful to provide static functions to global functions. |
|
| 410 | + self::$instance = $this; |
|
| 411 | + |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Get a reference to the Schema service. |
|
| 416 | + * |
|
| 417 | + * @since 3.1.0 |
|
| 418 | + * |
|
| 419 | + * @return Wordlift_Schema_Service A reference to the Schema service. |
|
| 420 | + */ |
|
| 421 | + public static function get_instance() { |
|
| 422 | + |
|
| 423 | + return self::$instance; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Get the properties for a field with the specified key. The key is used as |
|
| 428 | + * meta key when the field's value is stored in WordPress meta data table. |
|
| 429 | + * |
|
| 430 | + * @since 3.6.0 |
|
| 431 | + * |
|
| 432 | + * @param string $key The field's key. |
|
| 433 | + * |
|
| 434 | + * @return null|array An array of field's properties or null if the field is not found. |
|
| 435 | + */ |
|
| 436 | + public function get_field( $key ) { |
|
| 437 | + |
|
| 438 | + // Parse each schema's fields until we find the one we're looking for, then |
|
| 439 | + // return its properties. |
|
| 440 | + foreach ( $this->schema as $_ => $schema ) { |
|
| 441 | + |
|
| 442 | + if ( ! isset( $schema['custom_fields'] ) ) { |
|
| 443 | + break; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + foreach ( $schema['custom_fields'] as $field => $props ) { |
|
| 447 | + if ( $key === $field ) { |
|
| 448 | + return $props; |
|
| 449 | + } |
|
| 450 | + } |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + return null; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Get the WordLift's schema. |
|
| 458 | + * |
|
| 459 | + * @param string $name The schema name. |
|
| 460 | + * |
|
| 461 | + * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
| 462 | + * |
|
| 463 | + * @since 3.1.0 |
|
| 464 | + */ |
|
| 465 | + public function get_schema( $name ) { |
|
| 466 | + // Check if the schema exists and, if not, return NULL. |
|
| 467 | + if ( ! isset( $this->schema[ $name ] ) ) { |
|
| 468 | + return null; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + // Return the requested schema. |
|
| 472 | + return $this->schema[ $name ]; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + /** |
|
| 476 | + * Get the WordLift's schema trough schema type uri. |
|
| 477 | + * |
|
| 478 | + * @param string $uri The schema uri. |
|
| 479 | + * |
|
| 480 | + * @return array|null An array with the schema configuration or NULL if the schema is not found. |
|
| 481 | + * |
|
| 482 | + * @since 3.3.0 |
|
| 483 | + */ |
|
| 484 | + public function get_schema_by_uri( $uri ) { |
|
| 485 | + |
|
| 486 | + foreach ( $this->schema as $name => $schema ) { |
|
| 487 | + if ( $schema['uri'] === $uri ) { |
|
| 488 | + return $schema; |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + return null; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + /** |
|
| 496 | + * Get the 'thing' schema. |
|
| 497 | + * |
|
| 498 | + * @return array An array with the schema configuration. |
|
| 499 | + * |
|
| 500 | + * @since 3.1.0 |
|
| 501 | + */ |
|
| 502 | + private function get_thing_schema() { |
|
| 503 | + |
|
| 504 | + return array( |
|
| 505 | + 'css_class' => 'wl-thing', |
|
| 506 | + 'uri' => 'http://schema.org/Thing', |
|
| 507 | + 'same_as' => array( '*' ), |
|
| 508 | + // set as default. |
|
| 509 | + 'custom_fields' => array( |
|
| 510 | + self::FIELD_SAME_AS => array( |
|
| 511 | + 'predicate' => 'http://schema.org/sameAs', |
|
| 512 | + 'type' => self::DATA_TYPE_URI, |
|
| 513 | + 'export_type' => 'http://schema.org/Thing', |
|
| 514 | + 'constraints' => array( |
|
| 515 | + 'cardinality' => INF, |
|
| 516 | + ), |
|
| 517 | + // We need a custom metabox. |
|
| 518 | + 'input_field' => 'sameas', |
|
| 519 | + ), |
|
| 520 | + // Add the schema:url property. |
|
| 521 | + Wordlift_Schema_Url_Property_Service::META_KEY => Wordlift_Schema_Url_Property_Service::get_instance() |
|
| 522 | + ->get_compat_definition(), |
|
| 523 | + ), |
|
| 524 | + // {{sameAs}} not present in the microdata template, |
|
| 525 | + // because it is treated separately in *wl_content_embed_item_microdata* |
|
| 526 | + 'templates' => array( |
|
| 527 | + 'subtitle' => '{{id}}', |
|
| 528 | + ), |
|
| 529 | + 'linked_data' => array( |
|
| 530 | + // ### Title to rdfs:label. |
|
| 531 | + $this->rendition_factory->create( |
|
| 532 | + $this->storage_factory->post_title(), |
|
| 533 | + Wordlift_Query_Builder::RDFS_LABEL_URI, |
|
| 534 | + null, |
|
| 535 | + $this->language_code |
|
| 536 | + ), |
|
| 537 | + // ### Title to dct:title. |
|
| 538 | + $this->rendition_factory->create( |
|
| 539 | + $this->storage_factory->post_title(), |
|
| 540 | + 'http://purl.org/dc/terms/title', |
|
| 541 | + null, |
|
| 542 | + $this->language_code |
|
| 543 | + ), |
|
| 544 | + // ### Alternative title to rdfs:label. |
|
| 545 | + $this->rendition_factory->create( |
|
| 546 | + $this->storage_factory->post_meta( Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ), |
|
| 547 | + Wordlift_Query_Builder::RDFS_LABEL_URI, |
|
| 548 | + null, |
|
| 549 | + $this->language_code |
|
| 550 | + ), |
|
| 551 | + // ### Alternative title to dct:title. |
|
| 552 | + $this->rendition_factory->create( |
|
| 553 | + $this->storage_factory->post_meta( Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ), |
|
| 554 | + 'http://purl.org/dc/terms/title', |
|
| 555 | + null, |
|
| 556 | + $this->language_code |
|
| 557 | + ), |
|
| 558 | + // ### schema:url. |
|
| 559 | + $this->rendition_factory->create( |
|
| 560 | + $this->storage_factory->url_property(), |
|
| 561 | + Wordlift_Query_Builder::SCHEMA_URL_URI, |
|
| 562 | + self::DATA_TYPE_URI |
|
| 563 | + ), |
|
| 564 | + // ### schema:description. |
|
| 565 | + $this->rendition_factory->create( |
|
| 566 | + $this->storage_factory->post_description_no_tags_no_shortcodes(), |
|
| 567 | + 'http://schema.org/description', |
|
| 568 | + null, |
|
| 569 | + $this->language_code |
|
| 570 | + ), |
|
| 571 | + // ### owl:sameAs. |
|
| 572 | + $this->rendition_factory->create( |
|
| 573 | + $this->storage_factory->post_meta( self::FIELD_SAME_AS ), |
|
| 574 | + 'http://www.w3.org/2002/07/owl#sameAs', |
|
| 575 | + self::DATA_TYPE_URI |
|
| 576 | + ), |
|
| 577 | + // ### rdf:type. |
|
| 578 | + $this->rendition_factory->create( |
|
| 579 | + $this->storage_factory->schema_class( $this ), |
|
| 580 | + Wordlift_Query_Builder::RDFS_TYPE_URI, |
|
| 581 | + self::DATA_TYPE_URI |
|
| 582 | + ), |
|
| 583 | + // ### schema:image. |
|
| 584 | + $this->rendition_factory->create( |
|
| 585 | + $this->storage_factory->post_images(), |
|
| 586 | + Wordlift_Query_Builder::SCHEMA_IMAGE_URI, |
|
| 587 | + self::DATA_TYPE_URI |
|
| 588 | + ), |
|
| 589 | + // ### dct:relation. |
|
| 590 | + $this->rendition_factory->create( |
|
| 591 | + $this->storage_factory->relations(), |
|
| 592 | + Wordlift_Query_Builder::DCTERMS_RELATION_URI, |
|
| 593 | + self::DATA_TYPE_URI |
|
| 594 | + ), |
|
| 595 | + ), |
|
| 596 | + ); |
|
| 597 | + |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * Get the 'creative work' schema. |
|
| 602 | + * |
|
| 603 | + * @return array An array with the schema configuration. |
|
| 604 | + * |
|
| 605 | + * @since 3.1.0 |
|
| 606 | + */ |
|
| 607 | + private function get_creative_work_schema() { |
|
| 608 | + |
|
| 609 | + $schema = array( |
|
| 610 | + 'label' => 'CreativeWork', |
|
| 611 | + 'description' => 'A creative work (or a Music Album).', |
|
| 612 | + 'parents' => array( 'thing' ), |
|
| 613 | + // Give term slug as parent. |
|
| 614 | + 'css_class' => 'wl-creative-work', |
|
| 615 | + 'uri' => 'http://schema.org/CreativeWork', |
|
| 616 | + 'same_as' => array( |
|
| 617 | + 'http://schema.org/MusicAlbum', |
|
| 618 | + 'http://schema.org/Product', |
|
| 619 | + ), |
|
| 620 | + 'custom_fields' => array( |
|
| 621 | + self::FIELD_AUTHOR => array( |
|
| 622 | + 'predicate' => 'http://schema.org/author', |
|
| 623 | + 'type' => self::DATA_TYPE_URI, |
|
| 624 | + 'export_type' => 'http://schema.org/Person', |
|
| 625 | + 'constraints' => array( |
|
| 626 | + 'uri_type' => array( 'Person', 'Organization' ), |
|
| 627 | + 'cardinality' => INF, |
|
| 628 | + ), |
|
| 629 | + ), |
|
| 630 | + ), |
|
| 631 | + 'linked_data' => array( |
|
| 632 | + // ### schema:author. |
|
| 633 | + $this->rendition_factory->create( |
|
| 634 | + $this->storage_factory->author_uri(), |
|
| 635 | + Wordlift_Query_Builder::SCHEMA_AUTHOR_URI, |
|
| 636 | + self::DATA_TYPE_URI |
|
| 637 | + ), |
|
| 638 | + ), |
|
| 639 | + 'templates' => array( |
|
| 640 | + 'subtitle' => '{{id}}', |
|
| 641 | + ), |
|
| 642 | + ); |
|
| 643 | + |
|
| 644 | + // Merge the custom fields with those provided by the thing schema. |
|
| 645 | + $parent_schema = $this->get_thing_schema(); |
|
| 646 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 647 | + $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 648 | + |
|
| 649 | + return $schema; |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Get the 'event' schema. |
|
| 654 | + * |
|
| 655 | + * @return array An array with the schema configuration. |
|
| 656 | + * |
|
| 657 | + * @since 3.1.0 |
|
| 658 | + */ |
|
| 659 | + private function get_event_schema() { |
|
| 660 | + |
|
| 661 | + $schema = array( |
|
| 662 | + 'label' => 'Event', |
|
| 663 | + 'description' => 'An event . ', |
|
| 664 | + 'parents' => array( 'thing' ), |
|
| 665 | + 'css_class' => 'wl-event', |
|
| 666 | + 'uri' => self::SCHEMA_EVENT_TYPE, |
|
| 667 | + 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
| 668 | + 'custom_fields' => array( |
|
| 669 | + self::FIELD_DATE_START => array( |
|
| 670 | + 'predicate' => 'http://schema.org/startDate', |
|
| 671 | + 'type' => self::DATA_TYPE_DATE, |
|
| 672 | + 'export_type' => 'xsd:dateTime', |
|
| 673 | + 'constraints' => '', |
|
| 674 | + ), |
|
| 675 | + self::FIELD_DATE_END => array( |
|
| 676 | + 'predicate' => 'http://schema.org/endDate', |
|
| 677 | + 'type' => self::DATA_TYPE_DATE, |
|
| 678 | + 'export_type' => 'xsd:dateTime', |
|
| 679 | + 'constraints' => '', |
|
| 680 | + ), |
|
| 681 | + self::FIELD_LOCATION => array( |
|
| 682 | + 'predicate' => 'http://schema.org/location', |
|
| 683 | + 'type' => self::DATA_TYPE_URI, |
|
| 684 | + 'export_type' => 'http://schema.org/PostalAddress', |
|
| 685 | + 'constraints' => array( |
|
| 686 | + 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
| 687 | + 'cardinality' => INF, |
|
| 688 | + ), |
|
| 689 | + ), |
|
| 690 | + ), |
|
| 691 | + 'linked_data' => array( |
|
| 692 | + // ### schema:startDate. |
|
| 693 | + $this->rendition_factory->create( |
|
| 694 | + $this->storage_factory->post_meta( self::FIELD_DATE_START ), |
|
| 695 | + 'http://schema.org/startDate', |
|
| 696 | + self::DATA_TYPE_DATE_TIME |
|
| 697 | + ), |
|
| 698 | + // ### schema:endDate. |
|
| 699 | + $this->rendition_factory->create( |
|
| 700 | + $this->storage_factory->post_meta( self::FIELD_DATE_END ), |
|
| 701 | + 'http://schema.org/endDate', |
|
| 702 | + self::DATA_TYPE_DATE_TIME |
|
| 703 | + ), |
|
| 704 | + // ### schema:location. |
|
| 705 | + $this->rendition_factory->create( |
|
| 706 | + $this->storage_factory->post_meta_to_uri( self::FIELD_LOCATION ), |
|
| 707 | + 'http://schema.org/location', |
|
| 708 | + self::DATA_TYPE_URI |
|
| 709 | + ), |
|
| 710 | + ), |
|
| 711 | + 'templates' => array( |
|
| 712 | + 'subtitle' => '{{id}}', |
|
| 713 | + ), |
|
| 714 | + ); |
|
| 715 | + |
|
| 716 | + // Merge the custom fields with those provided by the thing schema. |
|
| 717 | + $parent_schema = $this->get_thing_schema(); |
|
| 718 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 719 | + $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 720 | + |
|
| 721 | + return $schema; |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + /** |
|
| 725 | + * Get the 'organization' schema. |
|
| 726 | + * |
|
| 727 | + * @return array An array with the schema configuration. |
|
| 728 | + * |
|
| 729 | + * @since 3.1.0 |
|
| 730 | + */ |
|
| 731 | + private function get_organization_schema() { |
|
| 732 | + |
|
| 733 | + $schema = array( |
|
| 734 | + 'label' => 'Organization', |
|
| 735 | + 'description' => 'An organization, including a government or a newspaper.', |
|
| 736 | + 'parents' => array( 'thing' ), |
|
| 737 | + 'css_class' => 'wl-organization', |
|
| 738 | + 'uri' => 'http://schema.org/Organization', |
|
| 739 | + 'same_as' => array( |
|
| 740 | + 'http://rdf.freebase.com/ns/organization.organization', |
|
| 741 | + 'http://rdf.freebase.com/ns/government.government', |
|
| 742 | + 'http://schema.org/Newspaper', |
|
| 743 | + ), |
|
| 744 | + 'custom_fields' => array( |
|
| 745 | + self::FIELD_LEGAL_NAME => array( |
|
| 746 | + 'predicate' => 'http://schema.org/legalName', |
|
| 747 | + 'type' => self::DATA_TYPE_STRING, |
|
| 748 | + 'export_type' => 'xsd:string', |
|
| 749 | + 'constraints' => '', |
|
| 750 | + ), |
|
| 751 | + self::FIELD_FOUNDER => array( |
|
| 752 | + 'predicate' => 'http://schema.org/founder', |
|
| 753 | + 'type' => self::DATA_TYPE_URI, |
|
| 754 | + 'export_type' => 'http://schema.org/Person', |
|
| 755 | + 'constraints' => array( |
|
| 756 | + 'uri_type' => 'Person', |
|
| 757 | + 'cardinality' => INF, |
|
| 758 | + ), |
|
| 759 | + ), |
|
| 760 | + self::FIELD_ADDRESS => array( |
|
| 761 | + 'predicate' => 'http://schema.org/streetAddress', |
|
| 762 | + 'type' => self::DATA_TYPE_STRING, |
|
| 763 | + 'export_type' => 'xsd:string', |
|
| 764 | + 'constraints' => '', |
|
| 765 | + // To build custom metabox. |
|
| 766 | + 'input_field' => 'address', |
|
| 767 | + ), |
|
| 768 | + self::FIELD_ADDRESS_PO_BOX => array( |
|
| 769 | + 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
| 770 | + 'type' => self::DATA_TYPE_STRING, |
|
| 771 | + 'export_type' => 'xsd:string', |
|
| 772 | + 'constraints' => '', |
|
| 773 | + // To build custom metabox. |
|
| 774 | + 'input_field' => 'address', |
|
| 775 | + ), |
|
| 776 | + self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
| 777 | + 'predicate' => 'http://schema.org/postalCode', |
|
| 778 | + 'type' => self::DATA_TYPE_STRING, |
|
| 779 | + 'export_type' => 'xsd:string', |
|
| 780 | + 'constraints' => '', |
|
| 781 | + // To build custom metabox. |
|
| 782 | + 'input_field' => 'address', |
|
| 783 | + ), |
|
| 784 | + self::FIELD_ADDRESS_LOCALITY => array( |
|
| 785 | + 'predicate' => 'http://schema.org/addressLocality', |
|
| 786 | + 'type' => self::DATA_TYPE_STRING, |
|
| 787 | + 'export_type' => 'xsd:string', |
|
| 788 | + 'constraints' => '', |
|
| 789 | + // To build custom metabox. |
|
| 790 | + 'input_field' => 'address', |
|
| 791 | + ), |
|
| 792 | + self::FIELD_ADDRESS_REGION => array( |
|
| 793 | + 'predicate' => 'http://schema.org/addressRegion', |
|
| 794 | + 'type' => self::DATA_TYPE_STRING, |
|
| 795 | + 'export_type' => 'xsd:string', |
|
| 796 | + 'constraints' => '', |
|
| 797 | + // To build custom metabox. |
|
| 798 | + 'input_field' => 'address', |
|
| 799 | + ), |
|
| 800 | + self::FIELD_ADDRESS_COUNTRY => array( |
|
| 801 | + 'predicate' => 'http://schema.org/addressCountry', |
|
| 802 | + 'type' => self::DATA_TYPE_STRING, |
|
| 803 | + 'export_type' => 'xsd:string', |
|
| 804 | + 'constraints' => '', |
|
| 805 | + // To build custom metabox. |
|
| 806 | + 'input_field' => 'address', |
|
| 807 | + ), |
|
| 808 | + self::FIELD_EMAIL => array( |
|
| 809 | + 'predicate' => 'http://schema.org/email', |
|
| 810 | + 'type' => self::DATA_TYPE_STRING, |
|
| 811 | + 'export_type' => 'xsd:string', |
|
| 812 | + 'constraints' => '', |
|
| 813 | + ), |
|
| 814 | + self::FIELD_TELEPHONE => array( |
|
| 815 | + 'predicate' => 'http://schema.org/telephone', |
|
| 816 | + 'type' => self::DATA_TYPE_STRING, |
|
| 817 | + 'export_type' => 'xsd:string', |
|
| 818 | + 'constraints' => '', |
|
| 819 | + ), |
|
| 820 | + ), |
|
| 821 | + 'linked_data' => array( |
|
| 822 | + // ### schema:legalName. |
|
| 823 | + $this->rendition_factory->create( |
|
| 824 | + $this->storage_factory->post_meta( self::FIELD_LEGAL_NAME ), |
|
| 825 | + 'http://schema.org/legalName' |
|
| 826 | + ), |
|
| 827 | + // ### schema:founder. |
|
| 828 | + $this->rendition_factory->create( |
|
| 829 | + $this->storage_factory->post_meta_to_uri( self::FIELD_FOUNDER ), |
|
| 830 | + 'http://schema.org/founder', |
|
| 831 | + self::DATA_TYPE_URI |
|
| 832 | + ), |
|
| 833 | + // ### schema:email. |
|
| 834 | + $this->rendition_factory->create( |
|
| 835 | + $this->storage_factory->post_meta( self::FIELD_EMAIL ), |
|
| 836 | + 'http://schema.org/email' |
|
| 837 | + ), |
|
| 838 | + // ### schema:telephone. |
|
| 839 | + $this->rendition_factory->create( |
|
| 840 | + $this->storage_factory->post_meta( self::FIELD_TELEPHONE ), |
|
| 841 | + 'http://schema.org/telephone' |
|
| 842 | + ), |
|
| 843 | + ), |
|
| 844 | + 'templates' => array( |
|
| 845 | + 'subtitle' => '{{id}}', |
|
| 846 | + ), |
|
| 847 | + ); |
|
| 848 | + |
|
| 849 | + // Merge the custom fields with those provided by the thing schema. |
|
| 850 | + $parent_schema = $this->get_thing_schema(); |
|
| 851 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 852 | + $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 853 | + |
|
| 854 | + return $schema; |
|
| 855 | + } |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * Get the 'person' schema. |
|
| 859 | + * |
|
| 860 | + * @return array An array with the schema configuration. |
|
| 861 | + * |
|
| 862 | + * @since 3.1.0 |
|
| 863 | + */ |
|
| 864 | + private function get_person_schema() { |
|
| 865 | + |
|
| 866 | + $schema = array( |
|
| 867 | + 'label' => 'Person', |
|
| 868 | + 'description' => 'A person (or a music artist).', |
|
| 869 | + 'parents' => array( 'thing' ), |
|
| 870 | + 'css_class' => 'wl-person', |
|
| 871 | + 'uri' => 'http://schema.org/Person', |
|
| 872 | + 'same_as' => array( |
|
| 873 | + 'http://rdf.freebase.com/ns/people.person', |
|
| 874 | + 'http://rdf.freebase.com/ns/music.artist', |
|
| 875 | + 'http://dbpedia.org/class/yago/LivingPeople', |
|
| 876 | + ), |
|
| 877 | + 'custom_fields' => array( |
|
| 878 | + self::FIELD_KNOWS => array( |
|
| 879 | + 'predicate' => 'http://schema.org/knows', |
|
| 880 | + 'type' => self::DATA_TYPE_URI, |
|
| 881 | + 'export_type' => 'http://schema.org/Person', |
|
| 882 | + 'constraints' => array( |
|
| 883 | + 'uri_type' => 'Person', |
|
| 884 | + 'cardinality' => INF, |
|
| 885 | + ), |
|
| 886 | + ), |
|
| 887 | + self::FIELD_BIRTH_DATE => array( |
|
| 888 | + 'predicate' => 'http://schema.org/birthDate', |
|
| 889 | + 'type' => self::DATA_TYPE_DATE, |
|
| 890 | + 'export_type' => 'xsd:date', |
|
| 891 | + 'constraints' => '', |
|
| 892 | + ), |
|
| 893 | + self::FIELD_BIRTH_PLACE => array( |
|
| 894 | + 'predicate' => 'http://schema.org/birthPlace', |
|
| 895 | + 'type' => self::DATA_TYPE_URI, |
|
| 896 | + 'export_type' => 'http://schema.org/Place', |
|
| 897 | + 'constraints' => array( |
|
| 898 | + 'uri_type' => 'Place', |
|
| 899 | + ), |
|
| 900 | + ), |
|
| 901 | + self::FIELD_AFFILIATION => array( |
|
| 902 | + 'predicate' => 'http://schema.org/affiliation', |
|
| 903 | + 'type' => self::DATA_TYPE_URI, |
|
| 904 | + 'export_type' => 'http://schema.org/Organization', |
|
| 905 | + 'constraints' => array( |
|
| 906 | + 'uri_type' => array( |
|
| 907 | + 'Organization', |
|
| 908 | + 'LocalBusiness', |
|
| 909 | + ), |
|
| 910 | + 'cardinality' => INF, |
|
| 911 | + ), |
|
| 912 | + ), |
|
| 913 | + self::FIELD_EMAIL => array( |
|
| 914 | + 'predicate' => 'http://schema.org/email', |
|
| 915 | + 'type' => self::DATA_TYPE_STRING, |
|
| 916 | + 'export_type' => 'xsd:string', |
|
| 917 | + 'constraints' => array( |
|
| 918 | + 'cardinality' => INF, |
|
| 919 | + ), |
|
| 920 | + ), |
|
| 921 | + ), |
|
| 922 | + 'linked_data' => array( |
|
| 923 | + // ### schema:knows. |
|
| 924 | + $this->rendition_factory->create( |
|
| 925 | + $this->storage_factory->post_meta_to_uri( self::FIELD_KNOWS ), |
|
| 926 | + 'http://schema.org/knows', |
|
| 927 | + self::DATA_TYPE_URI |
|
| 928 | + ), |
|
| 929 | + // ### schema:birthDate. |
|
| 930 | + $this->rendition_factory->create( |
|
| 931 | + $this->storage_factory->post_meta( self::FIELD_BIRTH_DATE ), |
|
| 932 | + 'http://schema.org/birthDate', |
|
| 933 | + self::DATA_TYPE_DATE |
|
| 934 | + ), |
|
| 935 | + // ### schema:birthPlace. |
|
| 936 | + $this->rendition_factory->create( |
|
| 937 | + $this->storage_factory->post_meta_to_uri( self::FIELD_BIRTH_PLACE ), |
|
| 938 | + 'http://schema.org/birthPlace', |
|
| 939 | + self::DATA_TYPE_URI |
|
| 940 | + ), |
|
| 941 | + // ### schema:affiliation. |
|
| 942 | + $this->rendition_factory->create( |
|
| 943 | + $this->storage_factory->post_meta_to_uri( self::FIELD_AFFILIATION ), |
|
| 944 | + 'http://schema.org/affiliation', |
|
| 945 | + self::DATA_TYPE_URI |
|
| 946 | + ), |
|
| 947 | + // ### schema:email. |
|
| 948 | + $this->rendition_factory->create( |
|
| 949 | + $this->storage_factory->post_meta( self::FIELD_EMAIL ), |
|
| 950 | + 'http://schema.org/email' |
|
| 951 | + ), |
|
| 952 | + ), |
|
| 953 | + 'templates' => array( |
|
| 954 | + 'subtitle' => '{{id}}', |
|
| 955 | + ), |
|
| 956 | + ); |
|
| 957 | + |
|
| 958 | + // Merge the custom fields with those provided by the thing schema. |
|
| 959 | + $parent_schema = $this->get_thing_schema(); |
|
| 960 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 961 | + $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 962 | + |
|
| 963 | + return $schema; |
|
| 964 | + |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + /** |
|
| 968 | + * Get the 'place' schema. |
|
| 969 | + * |
|
| 970 | + * @return array An array with the schema configuration. |
|
| 971 | + * |
|
| 972 | + * @since 3.1.0 |
|
| 973 | + */ |
|
| 974 | + private function get_place_schema() { |
|
| 975 | + |
|
| 976 | + $schema = array( |
|
| 977 | + 'label' => 'Place', |
|
| 978 | + 'description' => 'A place.', |
|
| 979 | + 'parents' => array( 'thing' ), |
|
| 980 | + 'css_class' => 'wl-place', |
|
| 981 | + 'uri' => 'http://schema.org/Place', |
|
| 982 | + 'same_as' => array( |
|
| 983 | + 'http://rdf.freebase.com/ns/location.location', |
|
| 984 | + 'http://www.opengis.net/gml/_Feature', |
|
| 985 | + ), |
|
| 986 | + 'custom_fields' => array( |
|
| 987 | + self::FIELD_GEO_LATITUDE => array( |
|
| 988 | + 'predicate' => 'http://schema.org/latitude', |
|
| 989 | + 'type' => self::DATA_TYPE_DOUBLE, |
|
| 990 | + 'export_type' => 'xsd:double', |
|
| 991 | + 'constraints' => '', |
|
| 992 | + // To build custom metabox. |
|
| 993 | + 'input_field' => 'coordinates', |
|
| 994 | + ), |
|
| 995 | + self::FIELD_GEO_LONGITUDE => array( |
|
| 996 | + 'predicate' => 'http://schema.org/longitude', |
|
| 997 | + 'type' => self::DATA_TYPE_DOUBLE, |
|
| 998 | + 'export_type' => 'xsd:double', |
|
| 999 | + 'constraints' => '', |
|
| 1000 | + // To build custom metabox. |
|
| 1001 | + 'input_field' => 'coordinates', |
|
| 1002 | + ), |
|
| 1003 | + self::FIELD_ADDRESS => array( |
|
| 1004 | + 'predicate' => 'http://schema.org/streetAddress', |
|
| 1005 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1006 | + 'export_type' => 'xsd:string', |
|
| 1007 | + 'constraints' => '', |
|
| 1008 | + // To build custom metabox. |
|
| 1009 | + 'input_field' => 'address', |
|
| 1010 | + ), |
|
| 1011 | + self::FIELD_ADDRESS_PO_BOX => array( |
|
| 1012 | + 'predicate' => 'http://schema.org/postOfficeBoxNumber', |
|
| 1013 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1014 | + 'export_type' => 'xsd:string', |
|
| 1015 | + 'constraints' => '', |
|
| 1016 | + // To build custom metabox. |
|
| 1017 | + 'input_field' => 'address', |
|
| 1018 | + ), |
|
| 1019 | + self::FIELD_ADDRESS_POSTAL_CODE => array( |
|
| 1020 | + 'predicate' => 'http://schema.org/postalCode', |
|
| 1021 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1022 | + 'export_type' => 'xsd:string', |
|
| 1023 | + 'constraints' => '', |
|
| 1024 | + // To build custom metabox. |
|
| 1025 | + 'input_field' => 'address', |
|
| 1026 | + ), |
|
| 1027 | + self::FIELD_ADDRESS_LOCALITY => array( |
|
| 1028 | + 'predicate' => 'http://schema.org/addressLocality', |
|
| 1029 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1030 | + 'export_type' => 'xsd:string', |
|
| 1031 | + 'constraints' => '', |
|
| 1032 | + // To build custom metabox. |
|
| 1033 | + 'input_field' => 'address', |
|
| 1034 | + ), |
|
| 1035 | + self::FIELD_ADDRESS_REGION => array( |
|
| 1036 | + 'predicate' => 'http://schema.org/addressRegion', |
|
| 1037 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1038 | + 'export_type' => 'xsd:string', |
|
| 1039 | + 'constraints' => '', |
|
| 1040 | + // To build custom metabox. |
|
| 1041 | + 'input_field' => 'address', |
|
| 1042 | + ), |
|
| 1043 | + self::FIELD_ADDRESS_COUNTRY => array( |
|
| 1044 | + 'predicate' => 'http://schema.org/addressCountry', |
|
| 1045 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1046 | + 'export_type' => 'xsd:string', |
|
| 1047 | + 'constraints' => '', |
|
| 1048 | + // To build custom metabox. |
|
| 1049 | + 'input_field' => 'address', |
|
| 1050 | + ), |
|
| 1051 | + ), |
|
| 1052 | + 'linked_data' => array(), |
|
| 1053 | + 'templates' => array( |
|
| 1054 | + 'subtitle' => '{{id}}', |
|
| 1055 | + ), |
|
| 1056 | + ); |
|
| 1057 | + |
|
| 1058 | + // Merge the custom fields with those provided by the thing schema. |
|
| 1059 | + $parent_schema = $this->get_thing_schema(); |
|
| 1060 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 1061 | + $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 1062 | + |
|
| 1063 | + return $schema; |
|
| 1064 | + } |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * Get the 'local business' schema. |
|
| 1068 | + * |
|
| 1069 | + * @return array An array with the schema configuration. |
|
| 1070 | + * |
|
| 1071 | + * @since 3.1.0 |
|
| 1072 | + */ |
|
| 1073 | + private function get_local_business_schema() { |
|
| 1074 | + |
|
| 1075 | + $schema = array( |
|
| 1076 | + 'label' => 'LocalBusiness', |
|
| 1077 | + 'description' => 'A local business.', |
|
| 1078 | + 'parents' => array( 'place', 'organization' ), |
|
| 1079 | + 'css_class' => 'wl-local-business', |
|
| 1080 | + 'uri' => 'http://schema.org/LocalBusiness', |
|
| 1081 | + 'same_as' => array( |
|
| 1082 | + 'http://rdf.freebase.com/ns/business/business_location', |
|
| 1083 | + 'https://schema.org/Store', |
|
| 1084 | + ), |
|
| 1085 | + 'custom_fields' => array(), |
|
| 1086 | + 'linked_data' => array(), |
|
| 1087 | + 'templates' => array( |
|
| 1088 | + 'subtitle' => '{{id}}', |
|
| 1089 | + ), |
|
| 1090 | + ); |
|
| 1091 | + |
|
| 1092 | + // Merge the custom fields with those provided by the place and organization schema. |
|
| 1093 | + $place_schema = $this->get_place_schema(); |
|
| 1094 | + $organization_schema = $this->get_organization_schema(); |
|
| 1095 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
| 1096 | + $schema['linked_data'] = array_merge( $schema['linked_data'], $place_schema['linked_data'], $organization_schema['linked_data'] ); |
|
| 1097 | + |
|
| 1098 | + return $schema; |
|
| 1099 | + } |
|
| 1100 | + |
|
| 1101 | + /** |
|
| 1102 | + * Get the 'recipe' schema. |
|
| 1103 | + * |
|
| 1104 | + * @return array An array with the schema configuration. |
|
| 1105 | + * |
|
| 1106 | + * @since 3.14.0 |
|
| 1107 | + */ |
|
| 1108 | + private function get_recipe_schema() { |
|
| 1109 | + |
|
| 1110 | + $schema = array( |
|
| 1111 | + 'label' => 'Recipe', |
|
| 1112 | + 'description' => 'A Recipe.', |
|
| 1113 | + 'parents' => array( 'CreativeWork' ), |
|
| 1114 | + 'css_class' => 'wl-recipe', |
|
| 1115 | + 'uri' => 'http://schema.org/Recipe', |
|
| 1116 | + 'same_as' => array(), |
|
| 1117 | + 'templates' => array( |
|
| 1118 | + 'subtitle' => '{{id}}', |
|
| 1119 | + ), |
|
| 1120 | + 'custom_fields' => array( |
|
| 1121 | + self::FIELD_RECIPE_CUISINE => array( |
|
| 1122 | + 'predicate' => 'http://schema.org/recipeCuisine', |
|
| 1123 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1124 | + 'export_type' => 'xsd:string', |
|
| 1125 | + 'constraints' => '', |
|
| 1126 | + 'metabox' => array( |
|
| 1127 | + 'label' => __( 'Recipe cuisine', 'wordlift' ), |
|
| 1128 | + ), |
|
| 1129 | + ), |
|
| 1130 | + self::FIELD_RECIPE_INGREDIENT => array( |
|
| 1131 | + 'predicate' => 'http://schema.org/recipeIngredient', |
|
| 1132 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1133 | + 'export_type' => 'xsd:string', |
|
| 1134 | + 'constraints' => array( |
|
| 1135 | + 'cardinality' => INF, |
|
| 1136 | + ), |
|
| 1137 | + 'metabox' => array( |
|
| 1138 | + 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
| 1139 | + ), |
|
| 1140 | + ), |
|
| 1141 | + self::FIELD_RECIPE_INSTRUCTIONS => array( |
|
| 1142 | + 'predicate' => 'http://schema.org/recipeInstructions', |
|
| 1143 | + 'type' => self::DATA_TYPE_MULTILINE, |
|
| 1144 | + 'export_type' => 'xsd:string', |
|
| 1145 | + 'constraints' => '', |
|
| 1146 | + 'metabox' => array( |
|
| 1147 | + 'class' => 'Wordlift_Metabox_Field_Multiline', |
|
| 1148 | + 'label' => __( 'Recipe instructions', 'wordlift' ), |
|
| 1149 | + ), |
|
| 1150 | + ), |
|
| 1151 | + self::FIELD_RECIPE_YIELD => array( |
|
| 1152 | + 'predicate' => 'http://schema.org/recipeYield', |
|
| 1153 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1154 | + 'export_type' => 'xsd:string', |
|
| 1155 | + 'constraints' => '', |
|
| 1156 | + 'metabox' => array( |
|
| 1157 | + 'label' => __( 'Recipe number of servings', 'wordlift' ), |
|
| 1158 | + ), |
|
| 1159 | + ), |
|
| 1160 | + self::FIELD_RECIPE_INGREDIENT => array( |
|
| 1161 | + 'predicate' => 'http://schema.org/recipeIngredient', |
|
| 1162 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1163 | + 'export_type' => 'xsd:string', |
|
| 1164 | + 'constraints' => array( |
|
| 1165 | + 'cardinality' => INF, |
|
| 1166 | + ), |
|
| 1167 | + 'metabox' => array( |
|
| 1168 | + 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
| 1169 | + ), |
|
| 1170 | + ), |
|
| 1171 | + self::FIELD_NUTRITION_INFO_CALORIES => array( |
|
| 1172 | + 'predicate' => 'http://schema.org/calories', |
|
| 1173 | + 'type' => self::DATA_TYPE_STRING, |
|
| 1174 | + 'export_type' => 'xsd:string', |
|
| 1175 | + 'constraints' => '', |
|
| 1176 | + 'metabox' => array( |
|
| 1177 | + 'label' => __( 'Calories (e.g. 240 calories)', 'wordlift' ), |
|
| 1178 | + ), |
|
| 1179 | + ), |
|
| 1180 | + self::FIELD_PREP_TIME => array( |
|
| 1181 | + 'predicate' => 'http://schema.org/prepTime', |
|
| 1182 | + 'type' => self::DATA_TYPE_DURATION, |
|
| 1183 | + 'export_type' => 'xsd:time', |
|
| 1184 | + 'constraints' => '', |
|
| 1185 | + 'metabox' => array( |
|
| 1186 | + 'class' => 'Wordlift_Metabox_Field_Duration', |
|
| 1187 | + 'label' => __( 'Recipe preparation time (e.g. 1:30)', 'wordlift' ), |
|
| 1188 | + ), |
|
| 1189 | + ), |
|
| 1190 | + self::FIELD_COOK_TIME => array( |
|
| 1191 | + 'predicate' => 'http://schema.org/cookTime', |
|
| 1192 | + 'type' => self::DATA_TYPE_DURATION, |
|
| 1193 | + 'export_type' => 'xsd:time', |
|
| 1194 | + 'constraints' => '', |
|
| 1195 | + 'metabox' => array( |
|
| 1196 | + 'class' => 'Wordlift_Metabox_Field_Duration', |
|
| 1197 | + 'label' => __( 'Recipe cook time (e.g. 1:30)', 'wordlift' ), |
|
| 1198 | + ), |
|
| 1199 | + ), |
|
| 1200 | + self::FIELD_TOTAL_TIME => array( |
|
| 1201 | + 'predicate' => 'http://schema.org/totalTime', |
|
| 1202 | + 'type' => self::DATA_TYPE_DURATION, |
|
| 1203 | + 'export_type' => 'xsd:time', |
|
| 1204 | + 'constraints' => '', |
|
| 1205 | + 'metabox' => array( |
|
| 1206 | + 'class' => 'Wordlift_Metabox_Field_Duration', |
|
| 1207 | + 'label' => __( 'Recipe total time (e.g. 1:30)', 'wordlift' ), |
|
| 1208 | + ), |
|
| 1209 | + ), |
|
| 1210 | + ), |
|
| 1211 | + 'linked_data' => array( |
|
| 1212 | + // ### schema:recipeCuisine. |
|
| 1213 | + $this->rendition_factory->create( |
|
| 1214 | + $this->storage_factory->post_meta( self::FIELD_RECIPE_CUISINE ), |
|
| 1215 | + 'http://schema.org/recipeCuisine' |
|
| 1216 | + ), |
|
| 1217 | + // ### schema:recipeIngredient. |
|
| 1218 | + $this->rendition_factory->create( |
|
| 1219 | + $this->storage_factory->post_meta( self::FIELD_RECIPE_INGREDIENT ), |
|
| 1220 | + 'http://schema.org/recipeIngredient' |
|
| 1221 | + ), |
|
| 1222 | + // ### schema:prepTime. |
|
| 1223 | + $this->rendition_factory->create( |
|
| 1224 | + $this->storage_factory->post_meta( self::FIELD_PREP_TIME ), |
|
| 1225 | + 'http://schema.org/prepTime', |
|
| 1226 | + self::DATA_TYPE_DURATION |
|
| 1227 | + ), |
|
| 1228 | + // ### schema:cookTime. |
|
| 1229 | + $this->rendition_factory->create( |
|
| 1230 | + $this->storage_factory->post_meta( self::FIELD_COOK_TIME ), |
|
| 1231 | + 'http://schema.org/cookTime', |
|
| 1232 | + self::DATA_TYPE_DURATION |
|
| 1233 | + ), |
|
| 1234 | + // ### schema:totalTime. |
|
| 1235 | + $this->rendition_factory->create( |
|
| 1236 | + $this->storage_factory->post_meta( self::FIELD_TOTAL_TIME ), |
|
| 1237 | + 'http://schema.org/totalTime', |
|
| 1238 | + self::DATA_TYPE_DURATION |
|
| 1239 | + ), |
|
| 1240 | + ), |
|
| 1241 | + ); |
|
| 1242 | + |
|
| 1243 | + // Merge the custom fields with those provided by the parent schema. |
|
| 1244 | + $parent_schema = $this->get_creative_work_schema(); |
|
| 1245 | + $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 1246 | + $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 1247 | + |
|
| 1248 | + return $schema; |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + /** |
|
| 1252 | + * Get the 'article' schema. |
|
| 1253 | + * |
|
| 1254 | + * @return array An array with the schema configuration. |
|
| 1255 | + * |
|
| 1256 | + * @since 3.15.0 |
|
| 1257 | + */ |
|
| 1258 | + private function get_article_schema() { |
|
| 1259 | + |
|
| 1260 | + $schema = array( |
|
| 1261 | + 'label' => 'Article', |
|
| 1262 | + 'description' => 'An Article.', |
|
| 1263 | + 'parents' => array(), |
|
| 1264 | + 'css_class' => 'wl-article', |
|
| 1265 | + 'uri' => 'http://schema.org/Article', |
|
| 1266 | + 'same_as' => array(), |
|
| 1267 | + 'templates' => array( |
|
| 1268 | + 'subtitle' => '{{id}}', |
|
| 1269 | + ), |
|
| 1270 | + 'custom_fields' => array(), |
|
| 1271 | + 'linked_data' => array(), |
|
| 1272 | + ); |
|
| 1273 | + |
|
| 1274 | + return $schema; |
|
| 1275 | + } |
|
| 1276 | + |
|
| 1277 | + /** |
|
| 1278 | + * Get all the predicates. |
|
| 1279 | + * |
|
| 1280 | + * @since 3.15.0 |
|
| 1281 | + * |
|
| 1282 | + * @return array An array of predicates. |
|
| 1283 | + */ |
|
| 1284 | + public function get_all_predicates() { |
|
| 1285 | + |
|
| 1286 | + // Get the custom fields. |
|
| 1287 | + $renditions = array_reduce( $this->schema, function ( $carry, $item ) { |
|
| 1288 | + return array_merge( $carry, $item['linked_data'] ); |
|
| 1289 | + }, array() ); |
|
| 1290 | + |
|
| 1291 | + // Create a new array of predicates from the custom fields. The initial |
|
| 1292 | + // array contains just the `http://www.w3.org/1999/02/22-rdf-syntax-ns#type` |
|
| 1293 | + // (a, rdf:type) predicate (use the full URI). |
|
| 1294 | + $predicates = array_unique( array_reduce( $renditions, function ( $carry, $item ) { |
|
| 1295 | + return array_merge( $carry, (array) $item->get_predicate() ); |
|
| 1296 | + }, array() ) ); |
|
| 1297 | + |
|
| 1298 | + // Finally return the predicates array. |
|
| 1299 | + return $predicates; |
|
| 1300 | + } |
|
| 1301 | 1301 | |
| 1302 | 1302 | } |
@@ -383,9 +383,9 @@ discard block |
||
| 383 | 383 | * @param \Wordlift_Sparql_Tuple_Rendition_Factory $rendition_factory The {@link Wordlift_Sparql_Tuple_Rendition_Factory} instance. |
| 384 | 384 | * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. |
| 385 | 385 | */ |
| 386 | - public function __construct( $storage_factory, $rendition_factory, $configuration_service ) { |
|
| 386 | + public function __construct($storage_factory, $rendition_factory, $configuration_service) { |
|
| 387 | 387 | |
| 388 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Schema_Service' ); |
|
| 388 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Schema_Service'); |
|
| 389 | 389 | |
| 390 | 390 | $this->storage_factory = $storage_factory; |
| 391 | 391 | $this->rendition_factory = $rendition_factory; |
@@ -433,18 +433,18 @@ discard block |
||
| 433 | 433 | * |
| 434 | 434 | * @return null|array An array of field's properties or null if the field is not found. |
| 435 | 435 | */ |
| 436 | - public function get_field( $key ) { |
|
| 436 | + public function get_field($key) { |
|
| 437 | 437 | |
| 438 | 438 | // Parse each schema's fields until we find the one we're looking for, then |
| 439 | 439 | // return its properties. |
| 440 | - foreach ( $this->schema as $_ => $schema ) { |
|
| 440 | + foreach ($this->schema as $_ => $schema) { |
|
| 441 | 441 | |
| 442 | - if ( ! isset( $schema['custom_fields'] ) ) { |
|
| 442 | + if ( ! isset($schema['custom_fields'])) { |
|
| 443 | 443 | break; |
| 444 | 444 | } |
| 445 | 445 | |
| 446 | - foreach ( $schema['custom_fields'] as $field => $props ) { |
|
| 447 | - if ( $key === $field ) { |
|
| 446 | + foreach ($schema['custom_fields'] as $field => $props) { |
|
| 447 | + if ($key === $field) { |
|
| 448 | 448 | return $props; |
| 449 | 449 | } |
| 450 | 450 | } |
@@ -462,14 +462,14 @@ discard block |
||
| 462 | 462 | * |
| 463 | 463 | * @since 3.1.0 |
| 464 | 464 | */ |
| 465 | - public function get_schema( $name ) { |
|
| 465 | + public function get_schema($name) { |
|
| 466 | 466 | // Check if the schema exists and, if not, return NULL. |
| 467 | - if ( ! isset( $this->schema[ $name ] ) ) { |
|
| 467 | + if ( ! isset($this->schema[$name])) { |
|
| 468 | 468 | return null; |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | // Return the requested schema. |
| 472 | - return $this->schema[ $name ]; |
|
| 472 | + return $this->schema[$name]; |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | /** |
@@ -481,10 +481,10 @@ discard block |
||
| 481 | 481 | * |
| 482 | 482 | * @since 3.3.0 |
| 483 | 483 | */ |
| 484 | - public function get_schema_by_uri( $uri ) { |
|
| 484 | + public function get_schema_by_uri($uri) { |
|
| 485 | 485 | |
| 486 | - foreach ( $this->schema as $name => $schema ) { |
|
| 487 | - if ( $schema['uri'] === $uri ) { |
|
| 486 | + foreach ($this->schema as $name => $schema) { |
|
| 487 | + if ($schema['uri'] === $uri) { |
|
| 488 | 488 | return $schema; |
| 489 | 489 | } |
| 490 | 490 | } |
@@ -504,7 +504,7 @@ discard block |
||
| 504 | 504 | return array( |
| 505 | 505 | 'css_class' => 'wl-thing', |
| 506 | 506 | 'uri' => 'http://schema.org/Thing', |
| 507 | - 'same_as' => array( '*' ), |
|
| 507 | + 'same_as' => array('*'), |
|
| 508 | 508 | // set as default. |
| 509 | 509 | 'custom_fields' => array( |
| 510 | 510 | self::FIELD_SAME_AS => array( |
@@ -543,14 +543,14 @@ discard block |
||
| 543 | 543 | ), |
| 544 | 544 | // ### Alternative title to rdfs:label. |
| 545 | 545 | $this->rendition_factory->create( |
| 546 | - $this->storage_factory->post_meta( Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ), |
|
| 546 | + $this->storage_factory->post_meta(Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY), |
|
| 547 | 547 | Wordlift_Query_Builder::RDFS_LABEL_URI, |
| 548 | 548 | null, |
| 549 | 549 | $this->language_code |
| 550 | 550 | ), |
| 551 | 551 | // ### Alternative title to dct:title. |
| 552 | 552 | $this->rendition_factory->create( |
| 553 | - $this->storage_factory->post_meta( Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY ), |
|
| 553 | + $this->storage_factory->post_meta(Wordlift_Entity_Service::ALTERNATIVE_LABEL_META_KEY), |
|
| 554 | 554 | 'http://purl.org/dc/terms/title', |
| 555 | 555 | null, |
| 556 | 556 | $this->language_code |
@@ -570,13 +570,13 @@ discard block |
||
| 570 | 570 | ), |
| 571 | 571 | // ### owl:sameAs. |
| 572 | 572 | $this->rendition_factory->create( |
| 573 | - $this->storage_factory->post_meta( self::FIELD_SAME_AS ), |
|
| 573 | + $this->storage_factory->post_meta(self::FIELD_SAME_AS), |
|
| 574 | 574 | 'http://www.w3.org/2002/07/owl#sameAs', |
| 575 | 575 | self::DATA_TYPE_URI |
| 576 | 576 | ), |
| 577 | 577 | // ### rdf:type. |
| 578 | 578 | $this->rendition_factory->create( |
| 579 | - $this->storage_factory->schema_class( $this ), |
|
| 579 | + $this->storage_factory->schema_class($this), |
|
| 580 | 580 | Wordlift_Query_Builder::RDFS_TYPE_URI, |
| 581 | 581 | self::DATA_TYPE_URI |
| 582 | 582 | ), |
@@ -609,7 +609,7 @@ discard block |
||
| 609 | 609 | $schema = array( |
| 610 | 610 | 'label' => 'CreativeWork', |
| 611 | 611 | 'description' => 'A creative work (or a Music Album).', |
| 612 | - 'parents' => array( 'thing' ), |
|
| 612 | + 'parents' => array('thing'), |
|
| 613 | 613 | // Give term slug as parent. |
| 614 | 614 | 'css_class' => 'wl-creative-work', |
| 615 | 615 | 'uri' => 'http://schema.org/CreativeWork', |
@@ -623,7 +623,7 @@ discard block |
||
| 623 | 623 | 'type' => self::DATA_TYPE_URI, |
| 624 | 624 | 'export_type' => 'http://schema.org/Person', |
| 625 | 625 | 'constraints' => array( |
| 626 | - 'uri_type' => array( 'Person', 'Organization' ), |
|
| 626 | + 'uri_type' => array('Person', 'Organization'), |
|
| 627 | 627 | 'cardinality' => INF, |
| 628 | 628 | ), |
| 629 | 629 | ), |
@@ -643,8 +643,8 @@ discard block |
||
| 643 | 643 | |
| 644 | 644 | // Merge the custom fields with those provided by the thing schema. |
| 645 | 645 | $parent_schema = $this->get_thing_schema(); |
| 646 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 647 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 646 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
| 647 | + $schema['linked_data'] = array_merge($schema['linked_data'], $parent_schema['linked_data']); |
|
| 648 | 648 | |
| 649 | 649 | return $schema; |
| 650 | 650 | } |
@@ -661,10 +661,10 @@ discard block |
||
| 661 | 661 | $schema = array( |
| 662 | 662 | 'label' => 'Event', |
| 663 | 663 | 'description' => 'An event . ', |
| 664 | - 'parents' => array( 'thing' ), |
|
| 664 | + 'parents' => array('thing'), |
|
| 665 | 665 | 'css_class' => 'wl-event', |
| 666 | 666 | 'uri' => self::SCHEMA_EVENT_TYPE, |
| 667 | - 'same_as' => array( 'http://dbpedia.org/ontology/Event' ), |
|
| 667 | + 'same_as' => array('http://dbpedia.org/ontology/Event'), |
|
| 668 | 668 | 'custom_fields' => array( |
| 669 | 669 | self::FIELD_DATE_START => array( |
| 670 | 670 | 'predicate' => 'http://schema.org/startDate', |
@@ -683,7 +683,7 @@ discard block |
||
| 683 | 683 | 'type' => self::DATA_TYPE_URI, |
| 684 | 684 | 'export_type' => 'http://schema.org/PostalAddress', |
| 685 | 685 | 'constraints' => array( |
| 686 | - 'uri_type' => array( 'Place', 'LocalBusiness' ), |
|
| 686 | + 'uri_type' => array('Place', 'LocalBusiness'), |
|
| 687 | 687 | 'cardinality' => INF, |
| 688 | 688 | ), |
| 689 | 689 | ), |
@@ -691,19 +691,19 @@ discard block |
||
| 691 | 691 | 'linked_data' => array( |
| 692 | 692 | // ### schema:startDate. |
| 693 | 693 | $this->rendition_factory->create( |
| 694 | - $this->storage_factory->post_meta( self::FIELD_DATE_START ), |
|
| 694 | + $this->storage_factory->post_meta(self::FIELD_DATE_START), |
|
| 695 | 695 | 'http://schema.org/startDate', |
| 696 | 696 | self::DATA_TYPE_DATE_TIME |
| 697 | 697 | ), |
| 698 | 698 | // ### schema:endDate. |
| 699 | 699 | $this->rendition_factory->create( |
| 700 | - $this->storage_factory->post_meta( self::FIELD_DATE_END ), |
|
| 700 | + $this->storage_factory->post_meta(self::FIELD_DATE_END), |
|
| 701 | 701 | 'http://schema.org/endDate', |
| 702 | 702 | self::DATA_TYPE_DATE_TIME |
| 703 | 703 | ), |
| 704 | 704 | // ### schema:location. |
| 705 | 705 | $this->rendition_factory->create( |
| 706 | - $this->storage_factory->post_meta_to_uri( self::FIELD_LOCATION ), |
|
| 706 | + $this->storage_factory->post_meta_to_uri(self::FIELD_LOCATION), |
|
| 707 | 707 | 'http://schema.org/location', |
| 708 | 708 | self::DATA_TYPE_URI |
| 709 | 709 | ), |
@@ -715,8 +715,8 @@ discard block |
||
| 715 | 715 | |
| 716 | 716 | // Merge the custom fields with those provided by the thing schema. |
| 717 | 717 | $parent_schema = $this->get_thing_schema(); |
| 718 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 719 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 718 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
| 719 | + $schema['linked_data'] = array_merge($schema['linked_data'], $parent_schema['linked_data']); |
|
| 720 | 720 | |
| 721 | 721 | return $schema; |
| 722 | 722 | } |
@@ -733,7 +733,7 @@ discard block |
||
| 733 | 733 | $schema = array( |
| 734 | 734 | 'label' => 'Organization', |
| 735 | 735 | 'description' => 'An organization, including a government or a newspaper.', |
| 736 | - 'parents' => array( 'thing' ), |
|
| 736 | + 'parents' => array('thing'), |
|
| 737 | 737 | 'css_class' => 'wl-organization', |
| 738 | 738 | 'uri' => 'http://schema.org/Organization', |
| 739 | 739 | 'same_as' => array( |
@@ -821,23 +821,23 @@ discard block |
||
| 821 | 821 | 'linked_data' => array( |
| 822 | 822 | // ### schema:legalName. |
| 823 | 823 | $this->rendition_factory->create( |
| 824 | - $this->storage_factory->post_meta( self::FIELD_LEGAL_NAME ), |
|
| 824 | + $this->storage_factory->post_meta(self::FIELD_LEGAL_NAME), |
|
| 825 | 825 | 'http://schema.org/legalName' |
| 826 | 826 | ), |
| 827 | 827 | // ### schema:founder. |
| 828 | 828 | $this->rendition_factory->create( |
| 829 | - $this->storage_factory->post_meta_to_uri( self::FIELD_FOUNDER ), |
|
| 829 | + $this->storage_factory->post_meta_to_uri(self::FIELD_FOUNDER), |
|
| 830 | 830 | 'http://schema.org/founder', |
| 831 | 831 | self::DATA_TYPE_URI |
| 832 | 832 | ), |
| 833 | 833 | // ### schema:email. |
| 834 | 834 | $this->rendition_factory->create( |
| 835 | - $this->storage_factory->post_meta( self::FIELD_EMAIL ), |
|
| 835 | + $this->storage_factory->post_meta(self::FIELD_EMAIL), |
|
| 836 | 836 | 'http://schema.org/email' |
| 837 | 837 | ), |
| 838 | 838 | // ### schema:telephone. |
| 839 | 839 | $this->rendition_factory->create( |
| 840 | - $this->storage_factory->post_meta( self::FIELD_TELEPHONE ), |
|
| 840 | + $this->storage_factory->post_meta(self::FIELD_TELEPHONE), |
|
| 841 | 841 | 'http://schema.org/telephone' |
| 842 | 842 | ), |
| 843 | 843 | ), |
@@ -848,8 +848,8 @@ discard block |
||
| 848 | 848 | |
| 849 | 849 | // Merge the custom fields with those provided by the thing schema. |
| 850 | 850 | $parent_schema = $this->get_thing_schema(); |
| 851 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 852 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 851 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
| 852 | + $schema['linked_data'] = array_merge($schema['linked_data'], $parent_schema['linked_data']); |
|
| 853 | 853 | |
| 854 | 854 | return $schema; |
| 855 | 855 | } |
@@ -866,7 +866,7 @@ discard block |
||
| 866 | 866 | $schema = array( |
| 867 | 867 | 'label' => 'Person', |
| 868 | 868 | 'description' => 'A person (or a music artist).', |
| 869 | - 'parents' => array( 'thing' ), |
|
| 869 | + 'parents' => array('thing'), |
|
| 870 | 870 | 'css_class' => 'wl-person', |
| 871 | 871 | 'uri' => 'http://schema.org/Person', |
| 872 | 872 | 'same_as' => array( |
@@ -922,31 +922,31 @@ discard block |
||
| 922 | 922 | 'linked_data' => array( |
| 923 | 923 | // ### schema:knows. |
| 924 | 924 | $this->rendition_factory->create( |
| 925 | - $this->storage_factory->post_meta_to_uri( self::FIELD_KNOWS ), |
|
| 925 | + $this->storage_factory->post_meta_to_uri(self::FIELD_KNOWS), |
|
| 926 | 926 | 'http://schema.org/knows', |
| 927 | 927 | self::DATA_TYPE_URI |
| 928 | 928 | ), |
| 929 | 929 | // ### schema:birthDate. |
| 930 | 930 | $this->rendition_factory->create( |
| 931 | - $this->storage_factory->post_meta( self::FIELD_BIRTH_DATE ), |
|
| 931 | + $this->storage_factory->post_meta(self::FIELD_BIRTH_DATE), |
|
| 932 | 932 | 'http://schema.org/birthDate', |
| 933 | 933 | self::DATA_TYPE_DATE |
| 934 | 934 | ), |
| 935 | 935 | // ### schema:birthPlace. |
| 936 | 936 | $this->rendition_factory->create( |
| 937 | - $this->storage_factory->post_meta_to_uri( self::FIELD_BIRTH_PLACE ), |
|
| 937 | + $this->storage_factory->post_meta_to_uri(self::FIELD_BIRTH_PLACE), |
|
| 938 | 938 | 'http://schema.org/birthPlace', |
| 939 | 939 | self::DATA_TYPE_URI |
| 940 | 940 | ), |
| 941 | 941 | // ### schema:affiliation. |
| 942 | 942 | $this->rendition_factory->create( |
| 943 | - $this->storage_factory->post_meta_to_uri( self::FIELD_AFFILIATION ), |
|
| 943 | + $this->storage_factory->post_meta_to_uri(self::FIELD_AFFILIATION), |
|
| 944 | 944 | 'http://schema.org/affiliation', |
| 945 | 945 | self::DATA_TYPE_URI |
| 946 | 946 | ), |
| 947 | 947 | // ### schema:email. |
| 948 | 948 | $this->rendition_factory->create( |
| 949 | - $this->storage_factory->post_meta( self::FIELD_EMAIL ), |
|
| 949 | + $this->storage_factory->post_meta(self::FIELD_EMAIL), |
|
| 950 | 950 | 'http://schema.org/email' |
| 951 | 951 | ), |
| 952 | 952 | ), |
@@ -957,8 +957,8 @@ discard block |
||
| 957 | 957 | |
| 958 | 958 | // Merge the custom fields with those provided by the thing schema. |
| 959 | 959 | $parent_schema = $this->get_thing_schema(); |
| 960 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 961 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 960 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
| 961 | + $schema['linked_data'] = array_merge($schema['linked_data'], $parent_schema['linked_data']); |
|
| 962 | 962 | |
| 963 | 963 | return $schema; |
| 964 | 964 | |
@@ -976,7 +976,7 @@ discard block |
||
| 976 | 976 | $schema = array( |
| 977 | 977 | 'label' => 'Place', |
| 978 | 978 | 'description' => 'A place.', |
| 979 | - 'parents' => array( 'thing' ), |
|
| 979 | + 'parents' => array('thing'), |
|
| 980 | 980 | 'css_class' => 'wl-place', |
| 981 | 981 | 'uri' => 'http://schema.org/Place', |
| 982 | 982 | 'same_as' => array( |
@@ -1057,8 +1057,8 @@ discard block |
||
| 1057 | 1057 | |
| 1058 | 1058 | // Merge the custom fields with those provided by the thing schema. |
| 1059 | 1059 | $parent_schema = $this->get_thing_schema(); |
| 1060 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 1061 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 1060 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
| 1061 | + $schema['linked_data'] = array_merge($schema['linked_data'], $parent_schema['linked_data']); |
|
| 1062 | 1062 | |
| 1063 | 1063 | return $schema; |
| 1064 | 1064 | } |
@@ -1075,7 +1075,7 @@ discard block |
||
| 1075 | 1075 | $schema = array( |
| 1076 | 1076 | 'label' => 'LocalBusiness', |
| 1077 | 1077 | 'description' => 'A local business.', |
| 1078 | - 'parents' => array( 'place', 'organization' ), |
|
| 1078 | + 'parents' => array('place', 'organization'), |
|
| 1079 | 1079 | 'css_class' => 'wl-local-business', |
| 1080 | 1080 | 'uri' => 'http://schema.org/LocalBusiness', |
| 1081 | 1081 | 'same_as' => array( |
@@ -1092,8 +1092,8 @@ discard block |
||
| 1092 | 1092 | // Merge the custom fields with those provided by the place and organization schema. |
| 1093 | 1093 | $place_schema = $this->get_place_schema(); |
| 1094 | 1094 | $organization_schema = $this->get_organization_schema(); |
| 1095 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields'] ); |
|
| 1096 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $place_schema['linked_data'], $organization_schema['linked_data'] ); |
|
| 1095 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $place_schema['custom_fields'], $organization_schema['custom_fields']); |
|
| 1096 | + $schema['linked_data'] = array_merge($schema['linked_data'], $place_schema['linked_data'], $organization_schema['linked_data']); |
|
| 1097 | 1097 | |
| 1098 | 1098 | return $schema; |
| 1099 | 1099 | } |
@@ -1110,7 +1110,7 @@ discard block |
||
| 1110 | 1110 | $schema = array( |
| 1111 | 1111 | 'label' => 'Recipe', |
| 1112 | 1112 | 'description' => 'A Recipe.', |
| 1113 | - 'parents' => array( 'CreativeWork' ), |
|
| 1113 | + 'parents' => array('CreativeWork'), |
|
| 1114 | 1114 | 'css_class' => 'wl-recipe', |
| 1115 | 1115 | 'uri' => 'http://schema.org/Recipe', |
| 1116 | 1116 | 'same_as' => array(), |
@@ -1124,7 +1124,7 @@ discard block |
||
| 1124 | 1124 | 'export_type' => 'xsd:string', |
| 1125 | 1125 | 'constraints' => '', |
| 1126 | 1126 | 'metabox' => array( |
| 1127 | - 'label' => __( 'Recipe cuisine', 'wordlift' ), |
|
| 1127 | + 'label' => __('Recipe cuisine', 'wordlift'), |
|
| 1128 | 1128 | ), |
| 1129 | 1129 | ), |
| 1130 | 1130 | self::FIELD_RECIPE_INGREDIENT => array( |
@@ -1135,7 +1135,7 @@ discard block |
||
| 1135 | 1135 | 'cardinality' => INF, |
| 1136 | 1136 | ), |
| 1137 | 1137 | 'metabox' => array( |
| 1138 | - 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
| 1138 | + 'label' => __('Recipe ingredient', 'wordlift'), |
|
| 1139 | 1139 | ), |
| 1140 | 1140 | ), |
| 1141 | 1141 | self::FIELD_RECIPE_INSTRUCTIONS => array( |
@@ -1145,7 +1145,7 @@ discard block |
||
| 1145 | 1145 | 'constraints' => '', |
| 1146 | 1146 | 'metabox' => array( |
| 1147 | 1147 | 'class' => 'Wordlift_Metabox_Field_Multiline', |
| 1148 | - 'label' => __( 'Recipe instructions', 'wordlift' ), |
|
| 1148 | + 'label' => __('Recipe instructions', 'wordlift'), |
|
| 1149 | 1149 | ), |
| 1150 | 1150 | ), |
| 1151 | 1151 | self::FIELD_RECIPE_YIELD => array( |
@@ -1154,7 +1154,7 @@ discard block |
||
| 1154 | 1154 | 'export_type' => 'xsd:string', |
| 1155 | 1155 | 'constraints' => '', |
| 1156 | 1156 | 'metabox' => array( |
| 1157 | - 'label' => __( 'Recipe number of servings', 'wordlift' ), |
|
| 1157 | + 'label' => __('Recipe number of servings', 'wordlift'), |
|
| 1158 | 1158 | ), |
| 1159 | 1159 | ), |
| 1160 | 1160 | self::FIELD_RECIPE_INGREDIENT => array( |
@@ -1165,7 +1165,7 @@ discard block |
||
| 1165 | 1165 | 'cardinality' => INF, |
| 1166 | 1166 | ), |
| 1167 | 1167 | 'metabox' => array( |
| 1168 | - 'label' => __( 'Recipe ingredient', 'wordlift' ), |
|
| 1168 | + 'label' => __('Recipe ingredient', 'wordlift'), |
|
| 1169 | 1169 | ), |
| 1170 | 1170 | ), |
| 1171 | 1171 | self::FIELD_NUTRITION_INFO_CALORIES => array( |
@@ -1174,7 +1174,7 @@ discard block |
||
| 1174 | 1174 | 'export_type' => 'xsd:string', |
| 1175 | 1175 | 'constraints' => '', |
| 1176 | 1176 | 'metabox' => array( |
| 1177 | - 'label' => __( 'Calories (e.g. 240 calories)', 'wordlift' ), |
|
| 1177 | + 'label' => __('Calories (e.g. 240 calories)', 'wordlift'), |
|
| 1178 | 1178 | ), |
| 1179 | 1179 | ), |
| 1180 | 1180 | self::FIELD_PREP_TIME => array( |
@@ -1184,7 +1184,7 @@ discard block |
||
| 1184 | 1184 | 'constraints' => '', |
| 1185 | 1185 | 'metabox' => array( |
| 1186 | 1186 | 'class' => 'Wordlift_Metabox_Field_Duration', |
| 1187 | - 'label' => __( 'Recipe preparation time (e.g. 1:30)', 'wordlift' ), |
|
| 1187 | + 'label' => __('Recipe preparation time (e.g. 1:30)', 'wordlift'), |
|
| 1188 | 1188 | ), |
| 1189 | 1189 | ), |
| 1190 | 1190 | self::FIELD_COOK_TIME => array( |
@@ -1194,7 +1194,7 @@ discard block |
||
| 1194 | 1194 | 'constraints' => '', |
| 1195 | 1195 | 'metabox' => array( |
| 1196 | 1196 | 'class' => 'Wordlift_Metabox_Field_Duration', |
| 1197 | - 'label' => __( 'Recipe cook time (e.g. 1:30)', 'wordlift' ), |
|
| 1197 | + 'label' => __('Recipe cook time (e.g. 1:30)', 'wordlift'), |
|
| 1198 | 1198 | ), |
| 1199 | 1199 | ), |
| 1200 | 1200 | self::FIELD_TOTAL_TIME => array( |
@@ -1204,36 +1204,36 @@ discard block |
||
| 1204 | 1204 | 'constraints' => '', |
| 1205 | 1205 | 'metabox' => array( |
| 1206 | 1206 | 'class' => 'Wordlift_Metabox_Field_Duration', |
| 1207 | - 'label' => __( 'Recipe total time (e.g. 1:30)', 'wordlift' ), |
|
| 1207 | + 'label' => __('Recipe total time (e.g. 1:30)', 'wordlift'), |
|
| 1208 | 1208 | ), |
| 1209 | 1209 | ), |
| 1210 | 1210 | ), |
| 1211 | 1211 | 'linked_data' => array( |
| 1212 | 1212 | // ### schema:recipeCuisine. |
| 1213 | 1213 | $this->rendition_factory->create( |
| 1214 | - $this->storage_factory->post_meta( self::FIELD_RECIPE_CUISINE ), |
|
| 1214 | + $this->storage_factory->post_meta(self::FIELD_RECIPE_CUISINE), |
|
| 1215 | 1215 | 'http://schema.org/recipeCuisine' |
| 1216 | 1216 | ), |
| 1217 | 1217 | // ### schema:recipeIngredient. |
| 1218 | 1218 | $this->rendition_factory->create( |
| 1219 | - $this->storage_factory->post_meta( self::FIELD_RECIPE_INGREDIENT ), |
|
| 1219 | + $this->storage_factory->post_meta(self::FIELD_RECIPE_INGREDIENT), |
|
| 1220 | 1220 | 'http://schema.org/recipeIngredient' |
| 1221 | 1221 | ), |
| 1222 | 1222 | // ### schema:prepTime. |
| 1223 | 1223 | $this->rendition_factory->create( |
| 1224 | - $this->storage_factory->post_meta( self::FIELD_PREP_TIME ), |
|
| 1224 | + $this->storage_factory->post_meta(self::FIELD_PREP_TIME), |
|
| 1225 | 1225 | 'http://schema.org/prepTime', |
| 1226 | 1226 | self::DATA_TYPE_DURATION |
| 1227 | 1227 | ), |
| 1228 | 1228 | // ### schema:cookTime. |
| 1229 | 1229 | $this->rendition_factory->create( |
| 1230 | - $this->storage_factory->post_meta( self::FIELD_COOK_TIME ), |
|
| 1230 | + $this->storage_factory->post_meta(self::FIELD_COOK_TIME), |
|
| 1231 | 1231 | 'http://schema.org/cookTime', |
| 1232 | 1232 | self::DATA_TYPE_DURATION |
| 1233 | 1233 | ), |
| 1234 | 1234 | // ### schema:totalTime. |
| 1235 | 1235 | $this->rendition_factory->create( |
| 1236 | - $this->storage_factory->post_meta( self::FIELD_TOTAL_TIME ), |
|
| 1236 | + $this->storage_factory->post_meta(self::FIELD_TOTAL_TIME), |
|
| 1237 | 1237 | 'http://schema.org/totalTime', |
| 1238 | 1238 | self::DATA_TYPE_DURATION |
| 1239 | 1239 | ), |
@@ -1242,8 +1242,8 @@ discard block |
||
| 1242 | 1242 | |
| 1243 | 1243 | // Merge the custom fields with those provided by the parent schema. |
| 1244 | 1244 | $parent_schema = $this->get_creative_work_schema(); |
| 1245 | - $schema['custom_fields'] = array_merge( $schema['custom_fields'], $parent_schema['custom_fields'] ); |
|
| 1246 | - $schema['linked_data'] = array_merge( $schema['linked_data'], $parent_schema['linked_data'] ); |
|
| 1245 | + $schema['custom_fields'] = array_merge($schema['custom_fields'], $parent_schema['custom_fields']); |
|
| 1246 | + $schema['linked_data'] = array_merge($schema['linked_data'], $parent_schema['linked_data']); |
|
| 1247 | 1247 | |
| 1248 | 1248 | return $schema; |
| 1249 | 1249 | } |
@@ -1284,16 +1284,16 @@ discard block |
||
| 1284 | 1284 | public function get_all_predicates() { |
| 1285 | 1285 | |
| 1286 | 1286 | // Get the custom fields. |
| 1287 | - $renditions = array_reduce( $this->schema, function ( $carry, $item ) { |
|
| 1288 | - return array_merge( $carry, $item['linked_data'] ); |
|
| 1289 | - }, array() ); |
|
| 1287 | + $renditions = array_reduce($this->schema, function($carry, $item) { |
|
| 1288 | + return array_merge($carry, $item['linked_data']); |
|
| 1289 | + }, array()); |
|
| 1290 | 1290 | |
| 1291 | 1291 | // Create a new array of predicates from the custom fields. The initial |
| 1292 | 1292 | // array contains just the `http://www.w3.org/1999/02/22-rdf-syntax-ns#type` |
| 1293 | 1293 | // (a, rdf:type) predicate (use the full URI). |
| 1294 | - $predicates = array_unique( array_reduce( $renditions, function ( $carry, $item ) { |
|
| 1295 | - return array_merge( $carry, (array) $item->get_predicate() ); |
|
| 1296 | - }, array() ) ); |
|
| 1294 | + $predicates = array_unique(array_reduce($renditions, function($carry, $item) { |
|
| 1295 | + return array_merge($carry, (array) $item->get_predicate()); |
|
| 1296 | + }, array())); |
|
| 1297 | 1297 | |
| 1298 | 1298 | // Finally return the predicates array. |
| 1299 | 1299 | return $predicates; |
@@ -18,280 +18,280 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Linked_Data_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A {@link Wordlift_Log_Service} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private $log; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $entity_service; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.0 |
|
| 43 | - * @access private |
|
| 44 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 45 | - */ |
|
| 46 | - private $entity_type_service; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The {@link Wordlift_Schema_Service} instance. |
|
| 50 | - * |
|
| 51 | - * @since 3.15.0 |
|
| 52 | - * @access private |
|
| 53 | - * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 54 | - */ |
|
| 55 | - private $schema_service; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The {@link Wordlift_Linked_Data_Service} singleton instance. |
|
| 59 | - * |
|
| 60 | - * @since 3.15.0 |
|
| 61 | - * @access private |
|
| 62 | - * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance. |
|
| 63 | - */ |
|
| 64 | - private static $instance; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The {@link Wordlift_Sparql_Service} instance. |
|
| 68 | - * |
|
| 69 | - * @since 3.15.0 |
|
| 70 | - * @access private |
|
| 71 | - * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 72 | - */ |
|
| 73 | - private $sparql_service; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Create a {@link Wordlift_Linked_Data_Service} instance. |
|
| 77 | - * |
|
| 78 | - * @since 3.15.0 |
|
| 79 | - * |
|
| 80 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 81 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 82 | - * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 83 | - * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 84 | - */ |
|
| 85 | - public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) { |
|
| 86 | - |
|
| 87 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' ); |
|
| 88 | - |
|
| 89 | - $this->entity_service = $entity_service; |
|
| 90 | - $this->entity_type_service = $entity_type_service; |
|
| 91 | - $this->schema_service = $schema_service; |
|
| 92 | - $this->sparql_service = $sparql_service; |
|
| 93 | - |
|
| 94 | - self::$instance = $this; |
|
| 95 | - |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get the singleton instance of {@link Wordlift_Linked_Data_Service}. |
|
| 100 | - * |
|
| 101 | - * @since 3.15.0 |
|
| 102 | - * |
|
| 103 | - * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>. |
|
| 104 | - */ |
|
| 105 | - public static function get_instance() { |
|
| 106 | - |
|
| 107 | - return self::$instance; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Push a {@link WP_Post} to the Linked Data store. |
|
| 112 | - * |
|
| 113 | - * If the {@link WP_Post} is an entity and it's not of the `Article` type, |
|
| 114 | - * then it is pushed to the remote Linked Data store. |
|
| 115 | - * |
|
| 116 | - * @since 3.15.0 |
|
| 117 | - * |
|
| 118 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 119 | - */ |
|
| 120 | - public function push( $post_id ) { |
|
| 121 | - |
|
| 122 | - $this->log->debug( "Pushing post $post_id..." ); |
|
| 123 | - |
|
| 124 | - // Bail out if it's not an entity: we do NOT publish non entities or |
|
| 125 | - // entities of type `Article`s. |
|
| 126 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 127 | - $this->log->debug( "Post $post_id is not an entity." ); |
|
| 128 | - |
|
| 129 | - return; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Get the post and push it to the Linked Data store. |
|
| 133 | - $this->do_push( $post_id ); |
|
| 134 | - |
|
| 135 | - // Reindex the triple store if buffering is turned off. |
|
| 136 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 137 | - wordlift_reindex_triple_store(); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Remove the specified {@link WP_Post} from the Linked Data. |
|
| 144 | - * |
|
| 145 | - * @since 3.15.0 |
|
| 146 | - * |
|
| 147 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 148 | - */ |
|
| 149 | - public function remove( $post_id ) { |
|
| 150 | - |
|
| 151 | - // Get the delete statements. |
|
| 152 | - $deletes = $this->get_delete_statements( $post_id ); |
|
| 153 | - $delete_query = implode( "\n", $deletes ); |
|
| 154 | - $this->sparql_service->execute( $delete_query ); |
|
| 155 | - |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Push an entity to the Linked Data store. |
|
| 160 | - * |
|
| 161 | - * @since 3.15.0 |
|
| 162 | - * |
|
| 163 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 164 | - */ |
|
| 165 | - private function do_push( $post_id ) { |
|
| 166 | - $this->log->debug( "Doing post $post_id push..." ); |
|
| 167 | - |
|
| 168 | - // Get the post. |
|
| 169 | - $post = get_post( $post_id ); |
|
| 170 | - |
|
| 171 | - // Bail out if the post isn't found. |
|
| 172 | - if ( null === $post ) { |
|
| 173 | - $this->log->warn( "Post $post_id not found." ); |
|
| 174 | - |
|
| 175 | - return; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // Bail out if the post isn't published. |
|
| 179 | - if ( 'publish' !== $post->post_status ) { |
|
| 180 | - $this->log->info( "Post $post_id not published." ); |
|
| 181 | - |
|
| 182 | - return; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // Bail out if the URI isn't valid. |
|
| 186 | - if ( ! $this->has_valid_uri( $post_id ) ) { |
|
| 187 | - $this->log->warn( "Post $post_id URI invalid." ); |
|
| 188 | - |
|
| 189 | - return; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // First remove the post data. |
|
| 193 | - $this->remove( $post_id ); |
|
| 194 | - |
|
| 195 | - // Get the insert statements. |
|
| 196 | - $insert_tuples = $this->get_insert_tuples( $post_id ); |
|
| 197 | - $insert_query_body = implode( "\n", $insert_tuples ); |
|
| 198 | - $insert_query = "INSERT DATA { $insert_query_body };"; |
|
| 199 | - $this->sparql_service->execute( $insert_query ); |
|
| 200 | - |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Check if an entity's {@link WP_Post} has a valid URI. |
|
| 205 | - * |
|
| 206 | - * @since 3.15.0 |
|
| 207 | - * |
|
| 208 | - * @param int $post_id The entity's {@link WP_Post}'s id. |
|
| 209 | - * |
|
| 210 | - * @return bool True if the URI is valid otherwise false. |
|
| 211 | - */ |
|
| 212 | - private function has_valid_uri( $post_id ) { |
|
| 213 | - |
|
| 214 | - // Get the entity's URI. |
|
| 215 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 216 | - |
|
| 217 | - // If the URI isn't found, return false. |
|
| 218 | - if ( null === $uri ) { |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - // If the URI ends with a trailing slash, return false. |
|
| 223 | - if ( '/' === substr( $uri, - 1 ) ) { |
|
| 224 | - return false; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // URI is valid. |
|
| 228 | - return true; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Get the delete statements. |
|
| 233 | - * |
|
| 234 | - * @since 3.15.0 |
|
| 235 | - * |
|
| 236 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 237 | - * |
|
| 238 | - * @return array An array of delete statements. |
|
| 239 | - */ |
|
| 240 | - private function get_delete_statements( $post_id ) { |
|
| 241 | - |
|
| 242 | - // Get the entity URI. |
|
| 243 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 244 | - |
|
| 245 | - // Prepare the delete statements with the entity as subject. |
|
| 246 | - $as_subject = array_map( function ( $item ) use ( $uri ) { |
|
| 247 | - return Wordlift_Query_Builder |
|
| 248 | - ::new_instance() |
|
| 249 | - ->delete() |
|
| 250 | - ->statement( $uri, $item, '?o' ) |
|
| 251 | - ->build(); |
|
| 252 | - }, $this->schema_service->get_all_predicates() ); |
|
| 253 | - |
|
| 254 | - // Prepare the delete statements with the entity as object. |
|
| 255 | - $as_object = array_map( function ( $item ) use ( $uri ) { |
|
| 256 | - return Wordlift_Query_Builder |
|
| 257 | - ::new_instance() |
|
| 258 | - ->delete() |
|
| 259 | - ->statement( '?s', $item, $uri, Wordlift_Query_Builder::OBJECT_URI ) |
|
| 260 | - ->build(); |
|
| 261 | - }, $this->schema_service->get_all_predicates() ); |
|
| 262 | - |
|
| 263 | - // Merge the delete statements and return them. |
|
| 264 | - return array_merge( $as_subject, $as_object ); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Get the SPARQL insert tuples ( ?s ?p ?o ) for the specified {@link WP_Post}. |
|
| 269 | - * |
|
| 270 | - * @since 3.15.0 |
|
| 271 | - * |
|
| 272 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 273 | - * |
|
| 274 | - * @return array An array of insert tuples. |
|
| 275 | - */ |
|
| 276 | - private function get_insert_tuples( $post_id ) { |
|
| 277 | - |
|
| 278 | - // Get the entity type. |
|
| 279 | - $type = $this->entity_type_service->get( $post_id ); |
|
| 280 | - |
|
| 281 | - // Get the Linked Data properties. |
|
| 282 | - $properties = $type['linked_data']; |
|
| 283 | - |
|
| 284 | - // Accumulate the tuples. |
|
| 285 | - $tuples = array(); |
|
| 286 | - /** @var Wordlift_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */ |
|
| 287 | - foreach ( $properties as $property ) { |
|
| 288 | - foreach ( $property->get( $post_id ) as $tuple ) { |
|
| 289 | - $tuples[] = $tuple; |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // Finally return the tuples. |
|
| 294 | - return $tuples; |
|
| 295 | - } |
|
| 21 | + /** |
|
| 22 | + * A {@link Wordlift_Log_Service} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private $log; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $entity_service; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.0 |
|
| 43 | + * @access private |
|
| 44 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 45 | + */ |
|
| 46 | + private $entity_type_service; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The {@link Wordlift_Schema_Service} instance. |
|
| 50 | + * |
|
| 51 | + * @since 3.15.0 |
|
| 52 | + * @access private |
|
| 53 | + * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 54 | + */ |
|
| 55 | + private $schema_service; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The {@link Wordlift_Linked_Data_Service} singleton instance. |
|
| 59 | + * |
|
| 60 | + * @since 3.15.0 |
|
| 61 | + * @access private |
|
| 62 | + * @var \Wordlift_Linked_Data_Service $instance The {@link Wordlift_Linked_Data_Service} singleton instance. |
|
| 63 | + */ |
|
| 64 | + private static $instance; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The {@link Wordlift_Sparql_Service} instance. |
|
| 68 | + * |
|
| 69 | + * @since 3.15.0 |
|
| 70 | + * @access private |
|
| 71 | + * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 72 | + */ |
|
| 73 | + private $sparql_service; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Create a {@link Wordlift_Linked_Data_Service} instance. |
|
| 77 | + * |
|
| 78 | + * @since 3.15.0 |
|
| 79 | + * |
|
| 80 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 81 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 82 | + * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 83 | + * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
|
| 84 | + */ |
|
| 85 | + public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) { |
|
| 86 | + |
|
| 87 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' ); |
|
| 88 | + |
|
| 89 | + $this->entity_service = $entity_service; |
|
| 90 | + $this->entity_type_service = $entity_type_service; |
|
| 91 | + $this->schema_service = $schema_service; |
|
| 92 | + $this->sparql_service = $sparql_service; |
|
| 93 | + |
|
| 94 | + self::$instance = $this; |
|
| 95 | + |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get the singleton instance of {@link Wordlift_Linked_Data_Service}. |
|
| 100 | + * |
|
| 101 | + * @since 3.15.0 |
|
| 102 | + * |
|
| 103 | + * @return Wordlift_Linked_Data_Service The singleton instance of <a href='psi_element://Wordlift_Linked_Data_Service'>Wordlift_Linked_Data_Service</a>. |
|
| 104 | + */ |
|
| 105 | + public static function get_instance() { |
|
| 106 | + |
|
| 107 | + return self::$instance; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Push a {@link WP_Post} to the Linked Data store. |
|
| 112 | + * |
|
| 113 | + * If the {@link WP_Post} is an entity and it's not of the `Article` type, |
|
| 114 | + * then it is pushed to the remote Linked Data store. |
|
| 115 | + * |
|
| 116 | + * @since 3.15.0 |
|
| 117 | + * |
|
| 118 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 119 | + */ |
|
| 120 | + public function push( $post_id ) { |
|
| 121 | + |
|
| 122 | + $this->log->debug( "Pushing post $post_id..." ); |
|
| 123 | + |
|
| 124 | + // Bail out if it's not an entity: we do NOT publish non entities or |
|
| 125 | + // entities of type `Article`s. |
|
| 126 | + if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 127 | + $this->log->debug( "Post $post_id is not an entity." ); |
|
| 128 | + |
|
| 129 | + return; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Get the post and push it to the Linked Data store. |
|
| 133 | + $this->do_push( $post_id ); |
|
| 134 | + |
|
| 135 | + // Reindex the triple store if buffering is turned off. |
|
| 136 | + if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 137 | + wordlift_reindex_triple_store(); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Remove the specified {@link WP_Post} from the Linked Data. |
|
| 144 | + * |
|
| 145 | + * @since 3.15.0 |
|
| 146 | + * |
|
| 147 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 148 | + */ |
|
| 149 | + public function remove( $post_id ) { |
|
| 150 | + |
|
| 151 | + // Get the delete statements. |
|
| 152 | + $deletes = $this->get_delete_statements( $post_id ); |
|
| 153 | + $delete_query = implode( "\n", $deletes ); |
|
| 154 | + $this->sparql_service->execute( $delete_query ); |
|
| 155 | + |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Push an entity to the Linked Data store. |
|
| 160 | + * |
|
| 161 | + * @since 3.15.0 |
|
| 162 | + * |
|
| 163 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 164 | + */ |
|
| 165 | + private function do_push( $post_id ) { |
|
| 166 | + $this->log->debug( "Doing post $post_id push..." ); |
|
| 167 | + |
|
| 168 | + // Get the post. |
|
| 169 | + $post = get_post( $post_id ); |
|
| 170 | + |
|
| 171 | + // Bail out if the post isn't found. |
|
| 172 | + if ( null === $post ) { |
|
| 173 | + $this->log->warn( "Post $post_id not found." ); |
|
| 174 | + |
|
| 175 | + return; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // Bail out if the post isn't published. |
|
| 179 | + if ( 'publish' !== $post->post_status ) { |
|
| 180 | + $this->log->info( "Post $post_id not published." ); |
|
| 181 | + |
|
| 182 | + return; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // Bail out if the URI isn't valid. |
|
| 186 | + if ( ! $this->has_valid_uri( $post_id ) ) { |
|
| 187 | + $this->log->warn( "Post $post_id URI invalid." ); |
|
| 188 | + |
|
| 189 | + return; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // First remove the post data. |
|
| 193 | + $this->remove( $post_id ); |
|
| 194 | + |
|
| 195 | + // Get the insert statements. |
|
| 196 | + $insert_tuples = $this->get_insert_tuples( $post_id ); |
|
| 197 | + $insert_query_body = implode( "\n", $insert_tuples ); |
|
| 198 | + $insert_query = "INSERT DATA { $insert_query_body };"; |
|
| 199 | + $this->sparql_service->execute( $insert_query ); |
|
| 200 | + |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Check if an entity's {@link WP_Post} has a valid URI. |
|
| 205 | + * |
|
| 206 | + * @since 3.15.0 |
|
| 207 | + * |
|
| 208 | + * @param int $post_id The entity's {@link WP_Post}'s id. |
|
| 209 | + * |
|
| 210 | + * @return bool True if the URI is valid otherwise false. |
|
| 211 | + */ |
|
| 212 | + private function has_valid_uri( $post_id ) { |
|
| 213 | + |
|
| 214 | + // Get the entity's URI. |
|
| 215 | + $uri = $this->entity_service->get_uri( $post_id ); |
|
| 216 | + |
|
| 217 | + // If the URI isn't found, return false. |
|
| 218 | + if ( null === $uri ) { |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + // If the URI ends with a trailing slash, return false. |
|
| 223 | + if ( '/' === substr( $uri, - 1 ) ) { |
|
| 224 | + return false; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // URI is valid. |
|
| 228 | + return true; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Get the delete statements. |
|
| 233 | + * |
|
| 234 | + * @since 3.15.0 |
|
| 235 | + * |
|
| 236 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 237 | + * |
|
| 238 | + * @return array An array of delete statements. |
|
| 239 | + */ |
|
| 240 | + private function get_delete_statements( $post_id ) { |
|
| 241 | + |
|
| 242 | + // Get the entity URI. |
|
| 243 | + $uri = $this->entity_service->get_uri( $post_id ); |
|
| 244 | + |
|
| 245 | + // Prepare the delete statements with the entity as subject. |
|
| 246 | + $as_subject = array_map( function ( $item ) use ( $uri ) { |
|
| 247 | + return Wordlift_Query_Builder |
|
| 248 | + ::new_instance() |
|
| 249 | + ->delete() |
|
| 250 | + ->statement( $uri, $item, '?o' ) |
|
| 251 | + ->build(); |
|
| 252 | + }, $this->schema_service->get_all_predicates() ); |
|
| 253 | + |
|
| 254 | + // Prepare the delete statements with the entity as object. |
|
| 255 | + $as_object = array_map( function ( $item ) use ( $uri ) { |
|
| 256 | + return Wordlift_Query_Builder |
|
| 257 | + ::new_instance() |
|
| 258 | + ->delete() |
|
| 259 | + ->statement( '?s', $item, $uri, Wordlift_Query_Builder::OBJECT_URI ) |
|
| 260 | + ->build(); |
|
| 261 | + }, $this->schema_service->get_all_predicates() ); |
|
| 262 | + |
|
| 263 | + // Merge the delete statements and return them. |
|
| 264 | + return array_merge( $as_subject, $as_object ); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Get the SPARQL insert tuples ( ?s ?p ?o ) for the specified {@link WP_Post}. |
|
| 269 | + * |
|
| 270 | + * @since 3.15.0 |
|
| 271 | + * |
|
| 272 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 273 | + * |
|
| 274 | + * @return array An array of insert tuples. |
|
| 275 | + */ |
|
| 276 | + private function get_insert_tuples( $post_id ) { |
|
| 277 | + |
|
| 278 | + // Get the entity type. |
|
| 279 | + $type = $this->entity_type_service->get( $post_id ); |
|
| 280 | + |
|
| 281 | + // Get the Linked Data properties. |
|
| 282 | + $properties = $type['linked_data']; |
|
| 283 | + |
|
| 284 | + // Accumulate the tuples. |
|
| 285 | + $tuples = array(); |
|
| 286 | + /** @var Wordlift_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */ |
|
| 287 | + foreach ( $properties as $property ) { |
|
| 288 | + foreach ( $property->get( $post_id ) as $tuple ) { |
|
| 289 | + $tuples[] = $tuple; |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // Finally return the tuples. |
|
| 294 | + return $tuples; |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | 297 | } |
@@ -82,9 +82,9 @@ discard block |
||
| 82 | 82 | * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
| 83 | 83 | * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
| 84 | 84 | */ |
| 85 | - public function __construct( $entity_service, $entity_type_service, $schema_service, $sparql_service ) { |
|
| 85 | + public function __construct($entity_service, $entity_type_service, $schema_service, $sparql_service) { |
|
| 86 | 86 | |
| 87 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Linked_Data_Service' ); |
|
| 87 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Linked_Data_Service'); |
|
| 88 | 88 | |
| 89 | 89 | $this->entity_service = $entity_service; |
| 90 | 90 | $this->entity_type_service = $entity_type_service; |
@@ -117,23 +117,23 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * @param int $post_id The {@link WP_Post}'s id. |
| 119 | 119 | */ |
| 120 | - public function push( $post_id ) { |
|
| 120 | + public function push($post_id) { |
|
| 121 | 121 | |
| 122 | - $this->log->debug( "Pushing post $post_id..." ); |
|
| 122 | + $this->log->debug("Pushing post $post_id..."); |
|
| 123 | 123 | |
| 124 | 124 | // Bail out if it's not an entity: we do NOT publish non entities or |
| 125 | 125 | // entities of type `Article`s. |
| 126 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 127 | - $this->log->debug( "Post $post_id is not an entity." ); |
|
| 126 | + if ( ! $this->entity_service->is_entity($post_id)) { |
|
| 127 | + $this->log->debug("Post $post_id is not an entity."); |
|
| 128 | 128 | |
| 129 | 129 | return; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | // Get the post and push it to the Linked Data store. |
| 133 | - $this->do_push( $post_id ); |
|
| 133 | + $this->do_push($post_id); |
|
| 134 | 134 | |
| 135 | 135 | // Reindex the triple store if buffering is turned off. |
| 136 | - if ( false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 136 | + if (false === WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) { |
|
| 137 | 137 | wordlift_reindex_triple_store(); |
| 138 | 138 | } |
| 139 | 139 | |
@@ -146,12 +146,12 @@ discard block |
||
| 146 | 146 | * |
| 147 | 147 | * @param int $post_id The {@link WP_Post}'s id. |
| 148 | 148 | */ |
| 149 | - public function remove( $post_id ) { |
|
| 149 | + public function remove($post_id) { |
|
| 150 | 150 | |
| 151 | 151 | // Get the delete statements. |
| 152 | - $deletes = $this->get_delete_statements( $post_id ); |
|
| 153 | - $delete_query = implode( "\n", $deletes ); |
|
| 154 | - $this->sparql_service->execute( $delete_query ); |
|
| 152 | + $deletes = $this->get_delete_statements($post_id); |
|
| 153 | + $delete_query = implode("\n", $deletes); |
|
| 154 | + $this->sparql_service->execute($delete_query); |
|
| 155 | 155 | |
| 156 | 156 | } |
| 157 | 157 | |
@@ -162,41 +162,41 @@ discard block |
||
| 162 | 162 | * |
| 163 | 163 | * @param int $post_id The {@link WP_Post}'s id. |
| 164 | 164 | */ |
| 165 | - private function do_push( $post_id ) { |
|
| 166 | - $this->log->debug( "Doing post $post_id push..." ); |
|
| 165 | + private function do_push($post_id) { |
|
| 166 | + $this->log->debug("Doing post $post_id push..."); |
|
| 167 | 167 | |
| 168 | 168 | // Get the post. |
| 169 | - $post = get_post( $post_id ); |
|
| 169 | + $post = get_post($post_id); |
|
| 170 | 170 | |
| 171 | 171 | // Bail out if the post isn't found. |
| 172 | - if ( null === $post ) { |
|
| 173 | - $this->log->warn( "Post $post_id not found." ); |
|
| 172 | + if (null === $post) { |
|
| 173 | + $this->log->warn("Post $post_id not found."); |
|
| 174 | 174 | |
| 175 | 175 | return; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | // Bail out if the post isn't published. |
| 179 | - if ( 'publish' !== $post->post_status ) { |
|
| 180 | - $this->log->info( "Post $post_id not published." ); |
|
| 179 | + if ('publish' !== $post->post_status) { |
|
| 180 | + $this->log->info("Post $post_id not published."); |
|
| 181 | 181 | |
| 182 | 182 | return; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | // Bail out if the URI isn't valid. |
| 186 | - if ( ! $this->has_valid_uri( $post_id ) ) { |
|
| 187 | - $this->log->warn( "Post $post_id URI invalid." ); |
|
| 186 | + if ( ! $this->has_valid_uri($post_id)) { |
|
| 187 | + $this->log->warn("Post $post_id URI invalid."); |
|
| 188 | 188 | |
| 189 | 189 | return; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | // First remove the post data. |
| 193 | - $this->remove( $post_id ); |
|
| 193 | + $this->remove($post_id); |
|
| 194 | 194 | |
| 195 | 195 | // Get the insert statements. |
| 196 | - $insert_tuples = $this->get_insert_tuples( $post_id ); |
|
| 197 | - $insert_query_body = implode( "\n", $insert_tuples ); |
|
| 196 | + $insert_tuples = $this->get_insert_tuples($post_id); |
|
| 197 | + $insert_query_body = implode("\n", $insert_tuples); |
|
| 198 | 198 | $insert_query = "INSERT DATA { $insert_query_body };"; |
| 199 | - $this->sparql_service->execute( $insert_query ); |
|
| 199 | + $this->sparql_service->execute($insert_query); |
|
| 200 | 200 | |
| 201 | 201 | } |
| 202 | 202 | |
@@ -209,18 +209,18 @@ discard block |
||
| 209 | 209 | * |
| 210 | 210 | * @return bool True if the URI is valid otherwise false. |
| 211 | 211 | */ |
| 212 | - private function has_valid_uri( $post_id ) { |
|
| 212 | + private function has_valid_uri($post_id) { |
|
| 213 | 213 | |
| 214 | 214 | // Get the entity's URI. |
| 215 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 215 | + $uri = $this->entity_service->get_uri($post_id); |
|
| 216 | 216 | |
| 217 | 217 | // If the URI isn't found, return false. |
| 218 | - if ( null === $uri ) { |
|
| 218 | + if (null === $uri) { |
|
| 219 | 219 | return false; |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | // If the URI ends with a trailing slash, return false. |
| 223 | - if ( '/' === substr( $uri, - 1 ) ) { |
|
| 223 | + if ('/' === substr($uri, - 1)) { |
|
| 224 | 224 | return false; |
| 225 | 225 | } |
| 226 | 226 | |
@@ -237,31 +237,31 @@ discard block |
||
| 237 | 237 | * |
| 238 | 238 | * @return array An array of delete statements. |
| 239 | 239 | */ |
| 240 | - private function get_delete_statements( $post_id ) { |
|
| 240 | + private function get_delete_statements($post_id) { |
|
| 241 | 241 | |
| 242 | 242 | // Get the entity URI. |
| 243 | - $uri = $this->entity_service->get_uri( $post_id ); |
|
| 243 | + $uri = $this->entity_service->get_uri($post_id); |
|
| 244 | 244 | |
| 245 | 245 | // Prepare the delete statements with the entity as subject. |
| 246 | - $as_subject = array_map( function ( $item ) use ( $uri ) { |
|
| 246 | + $as_subject = array_map(function($item) use ($uri) { |
|
| 247 | 247 | return Wordlift_Query_Builder |
| 248 | 248 | ::new_instance() |
| 249 | 249 | ->delete() |
| 250 | - ->statement( $uri, $item, '?o' ) |
|
| 250 | + ->statement($uri, $item, '?o') |
|
| 251 | 251 | ->build(); |
| 252 | - }, $this->schema_service->get_all_predicates() ); |
|
| 252 | + }, $this->schema_service->get_all_predicates()); |
|
| 253 | 253 | |
| 254 | 254 | // Prepare the delete statements with the entity as object. |
| 255 | - $as_object = array_map( function ( $item ) use ( $uri ) { |
|
| 255 | + $as_object = array_map(function($item) use ($uri) { |
|
| 256 | 256 | return Wordlift_Query_Builder |
| 257 | 257 | ::new_instance() |
| 258 | 258 | ->delete() |
| 259 | - ->statement( '?s', $item, $uri, Wordlift_Query_Builder::OBJECT_URI ) |
|
| 259 | + ->statement('?s', $item, $uri, Wordlift_Query_Builder::OBJECT_URI) |
|
| 260 | 260 | ->build(); |
| 261 | - }, $this->schema_service->get_all_predicates() ); |
|
| 261 | + }, $this->schema_service->get_all_predicates()); |
|
| 262 | 262 | |
| 263 | 263 | // Merge the delete statements and return them. |
| 264 | - return array_merge( $as_subject, $as_object ); |
|
| 264 | + return array_merge($as_subject, $as_object); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -273,10 +273,10 @@ discard block |
||
| 273 | 273 | * |
| 274 | 274 | * @return array An array of insert tuples. |
| 275 | 275 | */ |
| 276 | - private function get_insert_tuples( $post_id ) { |
|
| 276 | + private function get_insert_tuples($post_id) { |
|
| 277 | 277 | |
| 278 | 278 | // Get the entity type. |
| 279 | - $type = $this->entity_type_service->get( $post_id ); |
|
| 279 | + $type = $this->entity_type_service->get($post_id); |
|
| 280 | 280 | |
| 281 | 281 | // Get the Linked Data properties. |
| 282 | 282 | $properties = $type['linked_data']; |
@@ -284,8 +284,8 @@ discard block |
||
| 284 | 284 | // Accumulate the tuples. |
| 285 | 285 | $tuples = array(); |
| 286 | 286 | /** @var Wordlift_Sparql_Tuple_Rendition $property A {@link Wordlift_Sparql_Tuple_Rendition} instance. */ |
| 287 | - foreach ( $properties as $property ) { |
|
| 288 | - foreach ( $property->get( $post_id ) as $tuple ) { |
|
| 287 | + foreach ($properties as $property) { |
|
| 288 | + foreach ($property->get($post_id) as $tuple) { |
|
| 289 | 289 | $tuples[] = $tuple; |
| 290 | 290 | } |
| 291 | 291 | } |
@@ -16,130 +16,130 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Timeline_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The Log service. |
|
| 21 | - * |
|
| 22 | - * @since 3.1.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The Entity service. |
|
| 30 | - * |
|
| 31 | - * @since 3.1.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 34 | - */ |
|
| 35 | - private $entity_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The number of words to use for the excerpt, set in the `to_json` function |
|
| 39 | - * and used by a filter. |
|
| 40 | - * |
|
| 41 | - * @since 3.7.0 |
|
| 42 | - * @access private |
|
| 43 | - * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 44 | - */ |
|
| 45 | - private $excerpt_length; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 49 | - * |
|
| 50 | - * @since 3.15.0 |
|
| 51 | - * @access private |
|
| 52 | - * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 53 | - */ |
|
| 54 | - private $entity_type_service; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * A singleton instance of the Timeline service (useful for unit tests). |
|
| 58 | - * |
|
| 59 | - * @since 3.1.0 |
|
| 60 | - * @access private |
|
| 61 | - * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 62 | - */ |
|
| 63 | - private static $instance; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Create a Wordlift_Timeline_Service instance. |
|
| 67 | - * |
|
| 68 | - * @since 3.1.0 |
|
| 69 | - * |
|
| 70 | - * @param \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 71 | - * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 72 | - */ |
|
| 73 | - public function __construct( $entity_service, $entity_type_service ) { |
|
| 74 | - |
|
| 75 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 76 | - |
|
| 77 | - $this->entity_service = $entity_service; |
|
| 78 | - $this->entity_type_service = $entity_type_service; |
|
| 79 | - |
|
| 80 | - self::$instance = $this; |
|
| 81 | - |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 86 | - * |
|
| 87 | - * @since 3.1.0 |
|
| 88 | - * |
|
| 89 | - * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 90 | - */ |
|
| 91 | - public static function get_instance() { |
|
| 92 | - |
|
| 93 | - return self::$instance; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Retrieve timeline events and output them in JSON. |
|
| 98 | - * |
|
| 99 | - * @since 3.1.0 |
|
| 100 | - */ |
|
| 101 | - public function ajax_timeline() { |
|
| 102 | - |
|
| 103 | - // Get the ID of the post who requested the timeline. |
|
| 104 | - $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 105 | - |
|
| 106 | - // Get the events and transform them for the JSON response, then send them to the client. |
|
| 107 | - wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 108 | - |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Retrieve timeline events. |
|
| 113 | - * |
|
| 114 | - * @since 3.1.0 |
|
| 115 | - * |
|
| 116 | - * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 117 | - * |
|
| 118 | - * @param int $post_id The post ID. |
|
| 119 | - * |
|
| 120 | - * @return array An array of event posts. |
|
| 121 | - */ |
|
| 122 | - public function get_events( $post_id = null ) { |
|
| 123 | - |
|
| 124 | - // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 125 | - // posts if no post has been specified. |
|
| 126 | - $ids = ( is_numeric( $post_id ) |
|
| 127 | - ? wl_core_get_related_entity_ids( $post_id ) |
|
| 128 | - : $this->get_all_related_to_last_50_published_posts() ); |
|
| 129 | - |
|
| 130 | - // Add the post itself if it's an entity. |
|
| 131 | - if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) { |
|
| 132 | - $ids[] = $post_id; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // If there's no entities, return an empty array right away. |
|
| 136 | - if ( 0 === sizeof( $ids ) ) { |
|
| 137 | - $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 138 | - |
|
| 139 | - return array(); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 19 | + /** |
|
| 20 | + * The Log service. |
|
| 21 | + * |
|
| 22 | + * @since 3.1.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Log_Service $log The Log service. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The Entity service. |
|
| 30 | + * |
|
| 31 | + * @since 3.1.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 34 | + */ |
|
| 35 | + private $entity_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The number of words to use for the excerpt, set in the `to_json` function |
|
| 39 | + * and used by a filter. |
|
| 40 | + * |
|
| 41 | + * @since 3.7.0 |
|
| 42 | + * @access private |
|
| 43 | + * @var int $excerpt_length The number of words to use for the excerpt. |
|
| 44 | + */ |
|
| 45 | + private $excerpt_length; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The {@link Wordlift_Entity_Type_Service} instance. |
|
| 49 | + * |
|
| 50 | + * @since 3.15.0 |
|
| 51 | + * @access private |
|
| 52 | + * @var \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 53 | + */ |
|
| 54 | + private $entity_type_service; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * A singleton instance of the Timeline service (useful for unit tests). |
|
| 58 | + * |
|
| 59 | + * @since 3.1.0 |
|
| 60 | + * @access private |
|
| 61 | + * @var \Wordlift_Timeline_Service $instance The singleton instance. |
|
| 62 | + */ |
|
| 63 | + private static $instance; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Create a Wordlift_Timeline_Service instance. |
|
| 67 | + * |
|
| 68 | + * @since 3.1.0 |
|
| 69 | + * |
|
| 70 | + * @param \Wordlift_Entity_Service $entity_service The Entity service. |
|
| 71 | + * @param \Wordlift_Entity_Type_Service $entity_type_service The {@link Wordlift_Entity_Type_Service} instance. |
|
| 72 | + */ |
|
| 73 | + public function __construct( $entity_service, $entity_type_service ) { |
|
| 74 | + |
|
| 75 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Service' ); |
|
| 76 | + |
|
| 77 | + $this->entity_service = $entity_service; |
|
| 78 | + $this->entity_type_service = $entity_type_service; |
|
| 79 | + |
|
| 80 | + self::$instance = $this; |
|
| 81 | + |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Get the singleton instance of the Wordlift_Timeline_Service |
|
| 86 | + * |
|
| 87 | + * @since 3.1.0 |
|
| 88 | + * |
|
| 89 | + * @return \Wordlift_Timeline_Service The singleton instance of the Wordlift_Timeline_Service. |
|
| 90 | + */ |
|
| 91 | + public static function get_instance() { |
|
| 92 | + |
|
| 93 | + return self::$instance; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Retrieve timeline events and output them in JSON. |
|
| 98 | + * |
|
| 99 | + * @since 3.1.0 |
|
| 100 | + */ |
|
| 101 | + public function ajax_timeline() { |
|
| 102 | + |
|
| 103 | + // Get the ID of the post who requested the timeline. |
|
| 104 | + $post_id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : null ); |
|
| 105 | + |
|
| 106 | + // Get the events and transform them for the JSON response, then send them to the client. |
|
| 107 | + wp_send_json( $this->to_json( $this->get_events( $post_id ) ) ); |
|
| 108 | + |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Retrieve timeline events. |
|
| 113 | + * |
|
| 114 | + * @since 3.1.0 |
|
| 115 | + * |
|
| 116 | + * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. |
|
| 117 | + * |
|
| 118 | + * @param int $post_id The post ID. |
|
| 119 | + * |
|
| 120 | + * @return array An array of event posts. |
|
| 121 | + */ |
|
| 122 | + public function get_events( $post_id = null ) { |
|
| 123 | + |
|
| 124 | + // Get the entity IDs either from the entities related to the specified post or from the last 50 published |
|
| 125 | + // posts if no post has been specified. |
|
| 126 | + $ids = ( is_numeric( $post_id ) |
|
| 127 | + ? wl_core_get_related_entity_ids( $post_id ) |
|
| 128 | + : $this->get_all_related_to_last_50_published_posts() ); |
|
| 129 | + |
|
| 130 | + // Add the post itself if it's an entity. |
|
| 131 | + if ( is_numeric( $post_id ) && $this->entity_service->is_entity( $post_id ) ) { |
|
| 132 | + $ids[] = $post_id; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // If there's no entities, return an empty array right away. |
|
| 136 | + if ( 0 === sizeof( $ids ) ) { |
|
| 137 | + $this->log->trace( "No events found [ post id :: $post_id ]" ); |
|
| 138 | + |
|
| 139 | + return array(); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $this->log->trace( 'Getting events [ entity ids :: ' . join( ', ', $ids ) . ' ]' ); |
|
| 143 | 143 | |
| 144 | 144 | // $entity_type_service = $this->entity_type_service; |
| 145 | 145 | |
@@ -151,237 +151,237 @@ discard block |
||
| 151 | 151 | // return $entity_type_service->has_entity_type( $item, 'http://schema.org/Event' ); |
| 152 | 152 | // } ) ); |
| 153 | 153 | |
| 154 | - $args = array( |
|
| 155 | - 'post__in' => $ids, |
|
| 156 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 157 | - 'post_status' => 'publish', |
|
| 158 | - 'posts_per_page' => - 1, |
|
| 159 | - 'meta_query' => array( |
|
| 160 | - 'relation' => 'AND', |
|
| 161 | - array( |
|
| 162 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 163 | - 'value' => null, |
|
| 164 | - 'compare' => '!=', |
|
| 165 | - ), |
|
| 166 | - array( |
|
| 167 | - 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 168 | - 'value' => null, |
|
| 169 | - 'compare' => '!=', |
|
| 170 | - ), |
|
| 171 | - ), |
|
| 172 | - 'tax_query' => array( |
|
| 173 | - array( |
|
| 174 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 175 | - 'field' => 'slug', |
|
| 176 | - 'terms' => 'event', |
|
| 177 | - ), |
|
| 178 | - ), |
|
| 179 | - ); |
|
| 180 | - |
|
| 181 | - return get_posts( $args ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Convert timeline events to JSON. This function sets the global post in order |
|
| 186 | - * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 187 | - * we're not taking care of restoring any previous post: there isn't any. |
|
| 188 | - * |
|
| 189 | - * @since 3.1.0 |
|
| 190 | - * |
|
| 191 | - * @param array $posts An array of posts. |
|
| 192 | - * |
|
| 193 | - * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 194 | - */ |
|
| 195 | - public function to_json( $posts ) { |
|
| 196 | - |
|
| 197 | - // If there are no events, return empty JSON |
|
| 198 | - if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 199 | - return ''; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 203 | - $display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media'; |
|
| 204 | - |
|
| 205 | - // The number of words for the excerpt (by default 55, as WordPress). |
|
| 206 | - $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 207 | - add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 208 | - |
|
| 209 | - // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 210 | - // a link to the post itself. |
|
| 211 | - add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 212 | - |
|
| 213 | - // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 214 | - $start_at_slide = 0; |
|
| 215 | - $event_index = - 1; |
|
| 216 | - $now = time(); |
|
| 217 | - |
|
| 218 | - // Prepare the timeline variable. |
|
| 219 | - $timeline = array(); |
|
| 220 | - |
|
| 221 | - // Populate the arrays. |
|
| 222 | - $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 223 | - |
|
| 224 | - // Get the start and end dates. |
|
| 225 | - $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ); |
|
| 226 | - $end_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ); |
|
| 227 | - |
|
| 228 | - // Set the starting slide. |
|
| 229 | - $event_index ++; |
|
| 230 | - if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 231 | - $start_at_slide = $event_index; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Load thumbnail |
|
| 235 | - if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $item->ID ) ) |
|
| 236 | - && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 237 | - ) { |
|
| 238 | - |
|
| 239 | - // Set the thumbnail URL. |
|
| 240 | - if ( 'background' === $display_images_as ) { |
|
| 241 | - $date['background'] = array( 'url' => $attachment[0] ); |
|
| 242 | - $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 243 | - } else { |
|
| 244 | - $date['media'] = array( |
|
| 245 | - 'url' => $attachment[0], |
|
| 246 | - 'thumbnail' => $attachment[0], |
|
| 247 | - ); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - // Set the start/end dates by converting them to TimelineJS required format. |
|
| 252 | - $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 253 | - $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 254 | - |
|
| 255 | - setup_postdata( $GLOBALS['post'] = $item ); |
|
| 256 | - |
|
| 257 | - $more_link_text = sprintf( |
|
| 258 | - '<span aria-label="%1$s">%2$s</span>', |
|
| 259 | - sprintf( |
|
| 260 | - /* translators: %s: Name of current post */ |
|
| 261 | - __( 'Continue reading %s' ), |
|
| 262 | - the_title_attribute( array( 'echo' => false ) ) |
|
| 263 | - ), |
|
| 264 | - __( '(more…)' ) |
|
| 265 | - ); |
|
| 266 | - |
|
| 267 | - // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 268 | - $date['text'] = array( |
|
| 269 | - 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 270 | - ); |
|
| 271 | - |
|
| 272 | - // If we have an excerpt, set it. |
|
| 273 | - if ( 0 < $excerpt_length ) { |
|
| 274 | - $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - return $date; |
|
| 278 | - |
|
| 279 | - }, $posts ); |
|
| 280 | - |
|
| 281 | - // Finally remove the excerpt filter. |
|
| 282 | - remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 283 | - |
|
| 284 | - // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 285 | - return array( |
|
| 286 | - 'timeline' => $timeline, |
|
| 287 | - 'start_at_slide' => $start_at_slide, |
|
| 288 | - ); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * This function filters {@link excerpt_more} by removing it, since we're |
|
| 293 | - * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 294 | - * |
|
| 295 | - * @since 3.7.0 |
|
| 296 | - * |
|
| 297 | - * @param string $excerpt_more The excerpt more preset. |
|
| 298 | - * |
|
| 299 | - * @return string An empty string. |
|
| 300 | - */ |
|
| 301 | - public function excerpt_more( $excerpt_more ) { |
|
| 302 | - |
|
| 303 | - return ''; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 308 | - * how many words to return according to the client setting. |
|
| 309 | - * |
|
| 310 | - * @since 3.7.0 |
|
| 311 | - * |
|
| 312 | - * @param int $length The preset number of words. |
|
| 313 | - * |
|
| 314 | - * @return int The number of words for the preset. |
|
| 315 | - */ |
|
| 316 | - public function excerpt_length( $length ) { |
|
| 317 | - |
|
| 318 | - return $this->excerpt_length; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Convert the date to a date array. |
|
| 324 | - * |
|
| 325 | - * @since 3.7.0 |
|
| 326 | - * |
|
| 327 | - * @param $value int A date value. |
|
| 328 | - * |
|
| 329 | - * @return array An array containing year, month and day values. |
|
| 330 | - */ |
|
| 331 | - public static function date( $value ) { |
|
| 332 | - |
|
| 333 | - return array( |
|
| 334 | - 'year' => (int) date( 'Y', $value ), |
|
| 335 | - 'month' => (int) date( 'm', $value ), |
|
| 336 | - 'day' => (int) date( 'd', $value ), |
|
| 337 | - |
|
| 338 | - ); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 343 | - * its specific function). |
|
| 344 | - * |
|
| 345 | - * @since 3.1.0 |
|
| 346 | - * |
|
| 347 | - * @return array An array of post IDs. |
|
| 348 | - */ |
|
| 349 | - public function get_all_related_to_last_50_published_posts() { |
|
| 350 | - |
|
| 351 | - // Global timeline. Get entities from the latest posts. |
|
| 352 | - $latest_posts_ids = get_posts( array( |
|
| 353 | - 'numberposts' => 50, |
|
| 354 | - 'fields' => 'ids', //only get post IDs |
|
| 355 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 356 | - 'tax_query' => array( |
|
| 357 | - 'relation' => 'OR', |
|
| 358 | - array( |
|
| 359 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 360 | - 'operator' => 'NOT EXISTS', |
|
| 361 | - ), |
|
| 362 | - array( |
|
| 363 | - 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 364 | - 'field' => 'slug', |
|
| 365 | - 'terms' => 'article', |
|
| 366 | - ), |
|
| 367 | - ), |
|
| 368 | - 'post_status' => 'publish', |
|
| 369 | - ) ); |
|
| 370 | - |
|
| 371 | - if ( empty( $latest_posts_ids ) ) { |
|
| 372 | - // There are no posts. |
|
| 373 | - return array(); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - // Collect entities related to latest posts |
|
| 377 | - $entity_ids = array(); |
|
| 378 | - foreach ( $latest_posts_ids as $id ) { |
|
| 379 | - $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 380 | - 'status' => 'publish', |
|
| 381 | - ) ) ); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - return $entity_ids; |
|
| 385 | - } |
|
| 154 | + $args = array( |
|
| 155 | + 'post__in' => $ids, |
|
| 156 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 157 | + 'post_status' => 'publish', |
|
| 158 | + 'posts_per_page' => - 1, |
|
| 159 | + 'meta_query' => array( |
|
| 160 | + 'relation' => 'AND', |
|
| 161 | + array( |
|
| 162 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
| 163 | + 'value' => null, |
|
| 164 | + 'compare' => '!=', |
|
| 165 | + ), |
|
| 166 | + array( |
|
| 167 | + 'key' => Wordlift_Schema_Service::FIELD_DATE_END, |
|
| 168 | + 'value' => null, |
|
| 169 | + 'compare' => '!=', |
|
| 170 | + ), |
|
| 171 | + ), |
|
| 172 | + 'tax_query' => array( |
|
| 173 | + array( |
|
| 174 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 175 | + 'field' => 'slug', |
|
| 176 | + 'terms' => 'event', |
|
| 177 | + ), |
|
| 178 | + ), |
|
| 179 | + ); |
|
| 180 | + |
|
| 181 | + return get_posts( $args ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Convert timeline events to JSON. This function sets the global post in order |
|
| 186 | + * to get an automatic excerpt. Since we're being called inside an AJAX request, |
|
| 187 | + * we're not taking care of restoring any previous post: there isn't any. |
|
| 188 | + * |
|
| 189 | + * @since 3.1.0 |
|
| 190 | + * |
|
| 191 | + * @param array $posts An array of posts. |
|
| 192 | + * |
|
| 193 | + * @return array|string An array of timeline events or an empty string if no posts are provided. |
|
| 194 | + */ |
|
| 195 | + public function to_json( $posts ) { |
|
| 196 | + |
|
| 197 | + // If there are no events, return empty JSON |
|
| 198 | + if ( empty( $posts ) || is_null( $posts ) ) { |
|
| 199 | + return ''; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // {media|thumbnail}: if set to 'media' the image is attached to the slide, if set to 'background' the image is set as background. |
|
| 203 | + $display_images_as = isset( $_REQUEST['display_images_as'] ) ? $_REQUEST['display_images_as'] : 'media'; |
|
| 204 | + |
|
| 205 | + // The number of words for the excerpt (by default 55, as WordPress). |
|
| 206 | + $this->excerpt_length = $excerpt_length = isset( $_REQUEST['excerpt_length'] ) && is_numeric( $_REQUEST['excerpt_length'] ) ? $_REQUEST['excerpt_length'] : 55; |
|
| 207 | + add_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 208 | + |
|
| 209 | + // Add a filter to remove the [...] after excerpts, since we're adding |
|
| 210 | + // a link to the post itself. |
|
| 211 | + add_filter( 'excerpt_more', array( $this, 'excerpt_more' ) ); |
|
| 212 | + |
|
| 213 | + // Prepare for the starting slide data. The starting slide will be the one where *now* is between *start/end* dates. |
|
| 214 | + $start_at_slide = 0; |
|
| 215 | + $event_index = - 1; |
|
| 216 | + $now = time(); |
|
| 217 | + |
|
| 218 | + // Prepare the timeline variable. |
|
| 219 | + $timeline = array(); |
|
| 220 | + |
|
| 221 | + // Populate the arrays. |
|
| 222 | + $timeline['events'] = array_map( function ( $item ) use ( &$timeline, &$event_index, &$start_at_slide, &$now, $display_images_as, $excerpt_length ) { |
|
| 223 | + |
|
| 224 | + // Get the start and end dates. |
|
| 225 | + $start_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_START, true ) ); |
|
| 226 | + $end_date = strtotime( get_post_meta( $item->ID, Wordlift_Schema_Service::FIELD_DATE_END, true ) ); |
|
| 227 | + |
|
| 228 | + // Set the starting slide. |
|
| 229 | + $event_index ++; |
|
| 230 | + if ( 0 === $start_at_slide && $now >= $start_date && $now <= $end_date ) { |
|
| 231 | + $start_at_slide = $event_index; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Load thumbnail |
|
| 235 | + if ( '' !== ( $thumbnail_id = get_post_thumbnail_id( $item->ID ) ) |
|
| 236 | + && false !== ( $attachment = wp_get_attachment_image_src( $thumbnail_id ) ) |
|
| 237 | + ) { |
|
| 238 | + |
|
| 239 | + // Set the thumbnail URL. |
|
| 240 | + if ( 'background' === $display_images_as ) { |
|
| 241 | + $date['background'] = array( 'url' => $attachment[0] ); |
|
| 242 | + $date['media'] = array( 'thumbnail' => $attachment[0] ); |
|
| 243 | + } else { |
|
| 244 | + $date['media'] = array( |
|
| 245 | + 'url' => $attachment[0], |
|
| 246 | + 'thumbnail' => $attachment[0], |
|
| 247 | + ); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + // Set the start/end dates by converting them to TimelineJS required format. |
|
| 252 | + $date['start_date'] = Wordlift_Timeline_Service::date( $start_date ); |
|
| 253 | + $date['end_date'] = Wordlift_Timeline_Service::date( $end_date ); |
|
| 254 | + |
|
| 255 | + setup_postdata( $GLOBALS['post'] = $item ); |
|
| 256 | + |
|
| 257 | + $more_link_text = sprintf( |
|
| 258 | + '<span aria-label="%1$s">%2$s</span>', |
|
| 259 | + sprintf( |
|
| 260 | + /* translators: %s: Name of current post */ |
|
| 261 | + __( 'Continue reading %s' ), |
|
| 262 | + the_title_attribute( array( 'echo' => false ) ) |
|
| 263 | + ), |
|
| 264 | + __( '(more…)' ) |
|
| 265 | + ); |
|
| 266 | + |
|
| 267 | + // Set the event text only with the headline (see https://github.com/insideout10/wordlift-plugin/issues/352). |
|
| 268 | + $date['text'] = array( |
|
| 269 | + 'headline' => '<a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a>', |
|
| 270 | + ); |
|
| 271 | + |
|
| 272 | + // If we have an excerpt, set it. |
|
| 273 | + if ( 0 < $excerpt_length ) { |
|
| 274 | + $date['text']['text'] = sprintf( '%s <a href="%s">%s</a>', get_the_excerpt(), get_permalink(), $more_link_text ); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + return $date; |
|
| 278 | + |
|
| 279 | + }, $posts ); |
|
| 280 | + |
|
| 281 | + // Finally remove the excerpt filter. |
|
| 282 | + remove_filter( 'excerpt_length', array( $this, 'excerpt_length' ) ); |
|
| 283 | + |
|
| 284 | + // The JSON format is defined here: https://timeline.knightlab.com/docs/json-format.html |
|
| 285 | + return array( |
|
| 286 | + 'timeline' => $timeline, |
|
| 287 | + 'start_at_slide' => $start_at_slide, |
|
| 288 | + ); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * This function filters {@link excerpt_more} by removing it, since we're |
|
| 293 | + * adding the 'read more' link. This filter is set by {@see to_json}. |
|
| 294 | + * |
|
| 295 | + * @since 3.7.0 |
|
| 296 | + * |
|
| 297 | + * @param string $excerpt_more The excerpt more preset. |
|
| 298 | + * |
|
| 299 | + * @return string An empty string. |
|
| 300 | + */ |
|
| 301 | + public function excerpt_more( $excerpt_more ) { |
|
| 302 | + |
|
| 303 | + return ''; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * A filter for the excerpt length, set by the `to_json` function, to tailor |
|
| 308 | + * how many words to return according to the client setting. |
|
| 309 | + * |
|
| 310 | + * @since 3.7.0 |
|
| 311 | + * |
|
| 312 | + * @param int $length The preset number of words. |
|
| 313 | + * |
|
| 314 | + * @return int The number of words for the preset. |
|
| 315 | + */ |
|
| 316 | + public function excerpt_length( $length ) { |
|
| 317 | + |
|
| 318 | + return $this->excerpt_length; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Convert the date to a date array. |
|
| 324 | + * |
|
| 325 | + * @since 3.7.0 |
|
| 326 | + * |
|
| 327 | + * @param $value int A date value. |
|
| 328 | + * |
|
| 329 | + * @return array An array containing year, month and day values. |
|
| 330 | + */ |
|
| 331 | + public static function date( $value ) { |
|
| 332 | + |
|
| 333 | + return array( |
|
| 334 | + 'year' => (int) date( 'Y', $value ), |
|
| 335 | + 'month' => (int) date( 'm', $value ), |
|
| 336 | + 'day' => (int) date( 'd', $value ), |
|
| 337 | + |
|
| 338 | + ); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Get the entities related to the last 50 posts published on this blog (we're keeping a long function name due to |
|
| 343 | + * its specific function). |
|
| 344 | + * |
|
| 345 | + * @since 3.1.0 |
|
| 346 | + * |
|
| 347 | + * @return array An array of post IDs. |
|
| 348 | + */ |
|
| 349 | + public function get_all_related_to_last_50_published_posts() { |
|
| 350 | + |
|
| 351 | + // Global timeline. Get entities from the latest posts. |
|
| 352 | + $latest_posts_ids = get_posts( array( |
|
| 353 | + 'numberposts' => 50, |
|
| 354 | + 'fields' => 'ids', //only get post IDs |
|
| 355 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 356 | + 'tax_query' => array( |
|
| 357 | + 'relation' => 'OR', |
|
| 358 | + array( |
|
| 359 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 360 | + 'operator' => 'NOT EXISTS', |
|
| 361 | + ), |
|
| 362 | + array( |
|
| 363 | + 'taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, |
|
| 364 | + 'field' => 'slug', |
|
| 365 | + 'terms' => 'article', |
|
| 366 | + ), |
|
| 367 | + ), |
|
| 368 | + 'post_status' => 'publish', |
|
| 369 | + ) ); |
|
| 370 | + |
|
| 371 | + if ( empty( $latest_posts_ids ) ) { |
|
| 372 | + // There are no posts. |
|
| 373 | + return array(); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + // Collect entities related to latest posts |
|
| 377 | + $entity_ids = array(); |
|
| 378 | + foreach ( $latest_posts_ids as $id ) { |
|
| 379 | + $entity_ids = array_merge( $entity_ids, wl_core_get_related_entity_ids( $id, array( |
|
| 380 | + 'status' => 'publish', |
|
| 381 | + ) ) ); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + return $entity_ids; |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | 387 | } |
@@ -19,295 +19,295 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Wordlift_Entity_Type_Service { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The {@link Wordlift_Schema_Service} instance. |
|
| 24 | - * |
|
| 25 | - * @since 3.7.0 |
|
| 26 | - * @access private |
|
| 27 | - * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 28 | - */ |
|
| 29 | - private $schema_service; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * A {@link Wordlift_Log_Service} instance. |
|
| 33 | - * |
|
| 34 | - * @since 3.8.0 |
|
| 35 | - * @access private |
|
| 36 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 37 | - */ |
|
| 38 | - private $log; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 42 | - * |
|
| 43 | - * @since 3.7.0 |
|
| 44 | - * @access private |
|
| 45 | - * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 46 | - */ |
|
| 47 | - private static $instance; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Wordlift_Entity_Type_Service constructor. |
|
| 51 | - * |
|
| 52 | - * @since 3.7.0 |
|
| 53 | - * |
|
| 54 | - * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 55 | - */ |
|
| 56 | - public function __construct( $schema_service ) { |
|
| 57 | - |
|
| 58 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
| 59 | - |
|
| 60 | - $this->schema_service = $schema_service; |
|
| 61 | - |
|
| 62 | - self::$instance = $this; |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 68 | - * |
|
| 69 | - * @since 3.7.0 |
|
| 70 | - * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 71 | - */ |
|
| 72 | - public static function get_instance() { |
|
| 73 | - |
|
| 74 | - return self::$instance; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the types associated with the specified entity post id. |
|
| 79 | - * |
|
| 80 | - * @since 3.7.0 |
|
| 81 | - * |
|
| 82 | - * @param int $post_id The post id. |
|
| 83 | - * |
|
| 84 | - * @return array|null { |
|
| 85 | - * An array of type properties or null if no term is associated |
|
| 86 | - * |
|
| 87 | - * @type string css_class The css class, e.g. `wl-thing`. |
|
| 88 | - * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
| 89 | - * @type array same_as An array of same as attributes. |
|
| 90 | - * @type array custom_fields An array of custom fields. |
|
| 91 | - * @type array linked_data An array of {@link Wordlift_Sparql_Tuple_Rendition}. |
|
| 92 | - * } |
|
| 93 | - */ |
|
| 94 | - public function get( $post_id ) { |
|
| 95 | - |
|
| 96 | - $this->log->trace( "Getting the post type for post $post_id..." ); |
|
| 97 | - |
|
| 98 | - $post_type = get_post_type( $post_id ); |
|
| 99 | - |
|
| 100 | - // If it's not an entity post type return `Thing` by default. |
|
| 101 | - if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
| 102 | - $this->log->info( "Returning `Thing` for post $post_id." ); |
|
| 103 | - |
|
| 104 | - return array( |
|
| 105 | - 'uri' => 'http://schema.org/Thing', |
|
| 106 | - 'css_class' => 'wl-thing', |
|
| 107 | - ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Get the type from the associated classification. |
|
| 111 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 112 | - |
|
| 113 | - if ( is_wp_error( $terms ) ) { |
|
| 114 | - $this->log->error( "An error occurred while getting the post type for post $post_id: " . $terms->get_error_message() ); |
|
| 115 | - |
|
| 116 | - // TODO: handle error |
|
| 117 | - return null; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // If there are not terms associated, default to article. |
|
| 121 | - if ( 0 === count( $terms ) ) { |
|
| 122 | - $this->log->info( "Post $post_id has no terms, returning `Article`." ); |
|
| 123 | - |
|
| 124 | - return array( |
|
| 125 | - 'uri' => 'http://schema.org/Article', |
|
| 126 | - 'css_class' => 'wl-post', |
|
| 127 | - ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $this->log->debug( "Found {$terms[0]->slug} term for post $post_id." ); |
|
| 131 | - |
|
| 132 | - // Return the entity type with the specified id. |
|
| 133 | - return $this->schema_service->get_schema( $terms[0]->slug ); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Set the main type for the specified entity post, given the type URI. |
|
| 138 | - * |
|
| 139 | - * @since 3.8.0 |
|
| 140 | - * |
|
| 141 | - * @param int $post_id The post id. |
|
| 142 | - * @param string $type_uri The type URI. |
|
| 143 | - */ |
|
| 144 | - public function set( $post_id, $type_uri ) { |
|
| 145 | - |
|
| 146 | - // If the type URI is empty we remove the type. |
|
| 147 | - if ( empty( $type_uri ) ) { |
|
| 148 | - $this->log->debug( "Removing entity type for post $post_id..." ); |
|
| 149 | - |
|
| 150 | - wp_set_object_terms( $post_id, null, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 151 | - |
|
| 152 | - return; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $this->log->debug( "Setting entity type for post $post_id..." ); |
|
| 156 | - |
|
| 157 | - // Get all the terms bound to the wl_entity_type taxonomy. |
|
| 158 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 159 | - 'hide_empty' => false, |
|
| 160 | - // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 161 | - // An issue has been opened with the AAM plugin author as well. |
|
| 162 | - // |
|
| 163 | - // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 164 | - // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 165 | - 'fields' => 'all', |
|
| 166 | - ) ); |
|
| 167 | - |
|
| 168 | - // Check which term matches the specified URI. |
|
| 169 | - foreach ( $terms as $term ) { |
|
| 170 | - |
|
| 171 | - $term_id = $term->term_id; |
|
| 172 | - $term_slug = $term->slug; |
|
| 173 | - |
|
| 174 | - $this->log->trace( "Parsing term {$term->slug}..." ); |
|
| 175 | - |
|
| 176 | - // Load the type data. |
|
| 177 | - $type = $this->schema_service->get_schema( $term_slug ); |
|
| 178 | - |
|
| 179 | - // Set the related term ID. |
|
| 180 | - if ( $type_uri === $type['uri'] || $type_uri === $type['css_class'] ) { |
|
| 181 | - |
|
| 182 | - $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term_id ][ term slug :: $term_slug ][ type uri :: {$type['uri']} ][ type css class :: {$type['css_class']} ]" ); |
|
| 183 | - |
|
| 184 | - wp_set_object_terms( $post_id, (int) $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 185 | - |
|
| 186 | - return; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $this->log->error( "Type not found [ post id :: $post_id ][ type uri :: $type_uri ]" ); |
|
| 191 | - |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Check whether an entity type is set for the {@link WP_Post} with the |
|
| 196 | - * specified id. |
|
| 197 | - * |
|
| 198 | - * @since 3.15.0 |
|
| 199 | - * |
|
| 200 | - * @param int $post_id The {@link WP_Post}'s `id`. |
|
| 201 | - * @param string $uri The entity type URI. |
|
| 202 | - * |
|
| 203 | - * @return bool True if an entity type is set otherwise false. |
|
| 204 | - */ |
|
| 205 | - public function has_entity_type( $post_id, $uri = null ) { |
|
| 206 | - |
|
| 207 | - $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
| 208 | - |
|
| 209 | - // If an URI hasn't been specified just check whether we have at least |
|
| 210 | - // one entity type. |
|
| 211 | - if ( null === $uri ) { |
|
| 212 | - |
|
| 213 | - // Get the post terms for the specified post ID. |
|
| 214 | - $terms = $this->get_post_terms( $post_id ); |
|
| 215 | - |
|
| 216 | - $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' ); |
|
| 217 | - |
|
| 218 | - // True if there's at least one term bound to the post. |
|
| 219 | - return ( 0 < count( $terms ) ); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - $has_entity_type = ( null !== $this->get_term_by_uri( $post_id, $uri ) ); |
|
| 223 | - |
|
| 224 | - $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
| 225 | - |
|
| 226 | - // Check whether the post has an entity type with that URI. |
|
| 227 | - return $has_entity_type; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get the list of entity types' terms for the specified {@link WP_Post}. |
|
| 232 | - * |
|
| 233 | - * @since 3.15.0 |
|
| 234 | - * |
|
| 235 | - * @param int $post_id The {@link WP_Post} id. |
|
| 236 | - * |
|
| 237 | - * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
|
| 238 | - */ |
|
| 239 | - private function get_post_terms( $post_id ) { |
|
| 240 | - |
|
| 241 | - return wp_get_post_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 242 | - 'hide_empty' => false, |
|
| 243 | - // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 244 | - // An issue has been opened with the AAM plugin author as well. |
|
| 245 | - // |
|
| 246 | - // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 247 | - // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 248 | - 'fields' => 'all', |
|
| 249 | - ) ); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * Get an entity type term given its URI. |
|
| 254 | - * |
|
| 255 | - * @since 3.15.0 |
|
| 256 | - * |
|
| 257 | - * @param int $post_id The {@link WP_Post} id. |
|
| 258 | - * @param string $uri The entity type URI. |
|
| 259 | - * |
|
| 260 | - * @return array|null { |
|
| 261 | - * An array of entity type properties or null if no term is associated |
|
| 262 | - * |
|
| 263 | - * @type string css_class The css class, e.g. `wl-thing`. |
|
| 264 | - * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
| 265 | - * @type array same_as An array of same as attributes. |
|
| 266 | - * @type array custom_fields An array of custom fields. |
|
| 267 | - * } |
|
| 268 | - */ |
|
| 269 | - private function get_term_by_uri( $post_id, $uri ) { |
|
| 270 | - |
|
| 271 | - // Get the post terms bound to the specified post. |
|
| 272 | - $terms = $this->get_post_terms( $post_id ); |
|
| 273 | - |
|
| 274 | - // Look for a term if the specified URI. |
|
| 275 | - foreach ( $terms as $term ) { |
|
| 276 | - // Get the schema by slug. |
|
| 277 | - $schema = $this->schema_service->get_schema( $term->slug ); |
|
| 278 | - |
|
| 279 | - // Continue to the next one, if a schema isn't found (or hasn't got |
|
| 280 | - // and URI. |
|
| 281 | - if ( null === $schema || ! isset( $schema['uri'] ) ) { |
|
| 282 | - continue; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - // Return the schema if the URI matches. |
|
| 286 | - if ( $uri === $schema['uri'] ) { |
|
| 287 | - return $schema; |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - // Return null. |
|
| 292 | - return null; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Determines whether a post type can be used for entities. |
|
| 298 | - * |
|
| 299 | - * Criteria is that the post type is public. The list of valid post types |
|
| 300 | - * can be overridden with a filter. |
|
| 301 | - * |
|
| 302 | - * @since 3.15.0 |
|
| 303 | - * |
|
| 304 | - * @param string $post_type A post type name. |
|
| 305 | - * |
|
| 306 | - * @return bool Return true if the post type can be used for entities, otherwise false. |
|
| 307 | - */ |
|
| 308 | - public static function is_valid_entity_post_type( $post_type ) { |
|
| 309 | - |
|
| 310 | - return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
| 311 | - } |
|
| 22 | + /** |
|
| 23 | + * The {@link Wordlift_Schema_Service} instance. |
|
| 24 | + * |
|
| 25 | + * @since 3.7.0 |
|
| 26 | + * @access private |
|
| 27 | + * @var \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 28 | + */ |
|
| 29 | + private $schema_service; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * A {@link Wordlift_Log_Service} instance. |
|
| 33 | + * |
|
| 34 | + * @since 3.8.0 |
|
| 35 | + * @access private |
|
| 36 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 37 | + */ |
|
| 38 | + private $log; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 42 | + * |
|
| 43 | + * @since 3.7.0 |
|
| 44 | + * @access private |
|
| 45 | + * @var \Wordlift_Entity_Type_Service $instance The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 46 | + */ |
|
| 47 | + private static $instance; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Wordlift_Entity_Type_Service constructor. |
|
| 51 | + * |
|
| 52 | + * @since 3.7.0 |
|
| 53 | + * |
|
| 54 | + * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
|
| 55 | + */ |
|
| 56 | + public function __construct( $schema_service ) { |
|
| 57 | + |
|
| 58 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
| 59 | + |
|
| 60 | + $this->schema_service = $schema_service; |
|
| 61 | + |
|
| 62 | + self::$instance = $this; |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 68 | + * |
|
| 69 | + * @since 3.7.0 |
|
| 70 | + * @return \Wordlift_Entity_Type_Service The {@link Wordlift_Entity_Type_Service} singleton instance. |
|
| 71 | + */ |
|
| 72 | + public static function get_instance() { |
|
| 73 | + |
|
| 74 | + return self::$instance; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the types associated with the specified entity post id. |
|
| 79 | + * |
|
| 80 | + * @since 3.7.0 |
|
| 81 | + * |
|
| 82 | + * @param int $post_id The post id. |
|
| 83 | + * |
|
| 84 | + * @return array|null { |
|
| 85 | + * An array of type properties or null if no term is associated |
|
| 86 | + * |
|
| 87 | + * @type string css_class The css class, e.g. `wl-thing`. |
|
| 88 | + * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
| 89 | + * @type array same_as An array of same as attributes. |
|
| 90 | + * @type array custom_fields An array of custom fields. |
|
| 91 | + * @type array linked_data An array of {@link Wordlift_Sparql_Tuple_Rendition}. |
|
| 92 | + * } |
|
| 93 | + */ |
|
| 94 | + public function get( $post_id ) { |
|
| 95 | + |
|
| 96 | + $this->log->trace( "Getting the post type for post $post_id..." ); |
|
| 97 | + |
|
| 98 | + $post_type = get_post_type( $post_id ); |
|
| 99 | + |
|
| 100 | + // If it's not an entity post type return `Thing` by default. |
|
| 101 | + if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
| 102 | + $this->log->info( "Returning `Thing` for post $post_id." ); |
|
| 103 | + |
|
| 104 | + return array( |
|
| 105 | + 'uri' => 'http://schema.org/Thing', |
|
| 106 | + 'css_class' => 'wl-thing', |
|
| 107 | + ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Get the type from the associated classification. |
|
| 111 | + $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 112 | + |
|
| 113 | + if ( is_wp_error( $terms ) ) { |
|
| 114 | + $this->log->error( "An error occurred while getting the post type for post $post_id: " . $terms->get_error_message() ); |
|
| 115 | + |
|
| 116 | + // TODO: handle error |
|
| 117 | + return null; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // If there are not terms associated, default to article. |
|
| 121 | + if ( 0 === count( $terms ) ) { |
|
| 122 | + $this->log->info( "Post $post_id has no terms, returning `Article`." ); |
|
| 123 | + |
|
| 124 | + return array( |
|
| 125 | + 'uri' => 'http://schema.org/Article', |
|
| 126 | + 'css_class' => 'wl-post', |
|
| 127 | + ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $this->log->debug( "Found {$terms[0]->slug} term for post $post_id." ); |
|
| 131 | + |
|
| 132 | + // Return the entity type with the specified id. |
|
| 133 | + return $this->schema_service->get_schema( $terms[0]->slug ); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Set the main type for the specified entity post, given the type URI. |
|
| 138 | + * |
|
| 139 | + * @since 3.8.0 |
|
| 140 | + * |
|
| 141 | + * @param int $post_id The post id. |
|
| 142 | + * @param string $type_uri The type URI. |
|
| 143 | + */ |
|
| 144 | + public function set( $post_id, $type_uri ) { |
|
| 145 | + |
|
| 146 | + // If the type URI is empty we remove the type. |
|
| 147 | + if ( empty( $type_uri ) ) { |
|
| 148 | + $this->log->debug( "Removing entity type for post $post_id..." ); |
|
| 149 | + |
|
| 150 | + wp_set_object_terms( $post_id, null, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 151 | + |
|
| 152 | + return; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $this->log->debug( "Setting entity type for post $post_id..." ); |
|
| 156 | + |
|
| 157 | + // Get all the terms bound to the wl_entity_type taxonomy. |
|
| 158 | + $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 159 | + 'hide_empty' => false, |
|
| 160 | + // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 161 | + // An issue has been opened with the AAM plugin author as well. |
|
| 162 | + // |
|
| 163 | + // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 164 | + // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 165 | + 'fields' => 'all', |
|
| 166 | + ) ); |
|
| 167 | + |
|
| 168 | + // Check which term matches the specified URI. |
|
| 169 | + foreach ( $terms as $term ) { |
|
| 170 | + |
|
| 171 | + $term_id = $term->term_id; |
|
| 172 | + $term_slug = $term->slug; |
|
| 173 | + |
|
| 174 | + $this->log->trace( "Parsing term {$term->slug}..." ); |
|
| 175 | + |
|
| 176 | + // Load the type data. |
|
| 177 | + $type = $this->schema_service->get_schema( $term_slug ); |
|
| 178 | + |
|
| 179 | + // Set the related term ID. |
|
| 180 | + if ( $type_uri === $type['uri'] || $type_uri === $type['css_class'] ) { |
|
| 181 | + |
|
| 182 | + $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term_id ][ term slug :: $term_slug ][ type uri :: {$type['uri']} ][ type css class :: {$type['css_class']} ]" ); |
|
| 183 | + |
|
| 184 | + wp_set_object_terms( $post_id, (int) $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 185 | + |
|
| 186 | + return; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + $this->log->error( "Type not found [ post id :: $post_id ][ type uri :: $type_uri ]" ); |
|
| 191 | + |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Check whether an entity type is set for the {@link WP_Post} with the |
|
| 196 | + * specified id. |
|
| 197 | + * |
|
| 198 | + * @since 3.15.0 |
|
| 199 | + * |
|
| 200 | + * @param int $post_id The {@link WP_Post}'s `id`. |
|
| 201 | + * @param string $uri The entity type URI. |
|
| 202 | + * |
|
| 203 | + * @return bool True if an entity type is set otherwise false. |
|
| 204 | + */ |
|
| 205 | + public function has_entity_type( $post_id, $uri = null ) { |
|
| 206 | + |
|
| 207 | + $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
| 208 | + |
|
| 209 | + // If an URI hasn't been specified just check whether we have at least |
|
| 210 | + // one entity type. |
|
| 211 | + if ( null === $uri ) { |
|
| 212 | + |
|
| 213 | + // Get the post terms for the specified post ID. |
|
| 214 | + $terms = $this->get_post_terms( $post_id ); |
|
| 215 | + |
|
| 216 | + $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' ); |
|
| 217 | + |
|
| 218 | + // True if there's at least one term bound to the post. |
|
| 219 | + return ( 0 < count( $terms ) ); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + $has_entity_type = ( null !== $this->get_term_by_uri( $post_id, $uri ) ); |
|
| 223 | + |
|
| 224 | + $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
| 225 | + |
|
| 226 | + // Check whether the post has an entity type with that URI. |
|
| 227 | + return $has_entity_type; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get the list of entity types' terms for the specified {@link WP_Post}. |
|
| 232 | + * |
|
| 233 | + * @since 3.15.0 |
|
| 234 | + * |
|
| 235 | + * @param int $post_id The {@link WP_Post} id. |
|
| 236 | + * |
|
| 237 | + * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
|
| 238 | + */ |
|
| 239 | + private function get_post_terms( $post_id ) { |
|
| 240 | + |
|
| 241 | + return wp_get_post_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 242 | + 'hide_empty' => false, |
|
| 243 | + // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 244 | + // An issue has been opened with the AAM plugin author as well. |
|
| 245 | + // |
|
| 246 | + // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 247 | + // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 248 | + 'fields' => 'all', |
|
| 249 | + ) ); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * Get an entity type term given its URI. |
|
| 254 | + * |
|
| 255 | + * @since 3.15.0 |
|
| 256 | + * |
|
| 257 | + * @param int $post_id The {@link WP_Post} id. |
|
| 258 | + * @param string $uri The entity type URI. |
|
| 259 | + * |
|
| 260 | + * @return array|null { |
|
| 261 | + * An array of entity type properties or null if no term is associated |
|
| 262 | + * |
|
| 263 | + * @type string css_class The css class, e.g. `wl-thing`. |
|
| 264 | + * @type string uri The schema.org class URI, e.g. `http://schema.org/Thing`. |
|
| 265 | + * @type array same_as An array of same as attributes. |
|
| 266 | + * @type array custom_fields An array of custom fields. |
|
| 267 | + * } |
|
| 268 | + */ |
|
| 269 | + private function get_term_by_uri( $post_id, $uri ) { |
|
| 270 | + |
|
| 271 | + // Get the post terms bound to the specified post. |
|
| 272 | + $terms = $this->get_post_terms( $post_id ); |
|
| 273 | + |
|
| 274 | + // Look for a term if the specified URI. |
|
| 275 | + foreach ( $terms as $term ) { |
|
| 276 | + // Get the schema by slug. |
|
| 277 | + $schema = $this->schema_service->get_schema( $term->slug ); |
|
| 278 | + |
|
| 279 | + // Continue to the next one, if a schema isn't found (or hasn't got |
|
| 280 | + // and URI. |
|
| 281 | + if ( null === $schema || ! isset( $schema['uri'] ) ) { |
|
| 282 | + continue; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + // Return the schema if the URI matches. |
|
| 286 | + if ( $uri === $schema['uri'] ) { |
|
| 287 | + return $schema; |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + // Return null. |
|
| 292 | + return null; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Determines whether a post type can be used for entities. |
|
| 298 | + * |
|
| 299 | + * Criteria is that the post type is public. The list of valid post types |
|
| 300 | + * can be overridden with a filter. |
|
| 301 | + * |
|
| 302 | + * @since 3.15.0 |
|
| 303 | + * |
|
| 304 | + * @param string $post_type A post type name. |
|
| 305 | + * |
|
| 306 | + * @return bool Return true if the post type can be used for entities, otherwise false. |
|
| 307 | + */ |
|
| 308 | + public static function is_valid_entity_post_type( $post_type ) { |
|
| 309 | + |
|
| 310 | + return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | 313 | } |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @param \Wordlift_Schema_Service $schema_service The {@link Wordlift_Schema_Service} instance. |
| 55 | 55 | */ |
| 56 | - public function __construct( $schema_service ) { |
|
| 56 | + public function __construct($schema_service) { |
|
| 57 | 57 | |
| 58 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Type_Service' ); |
|
| 58 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Type_Service'); |
|
| 59 | 59 | |
| 60 | 60 | $this->schema_service = $schema_service; |
| 61 | 61 | |
@@ -91,15 +91,15 @@ discard block |
||
| 91 | 91 | * @type array linked_data An array of {@link Wordlift_Sparql_Tuple_Rendition}. |
| 92 | 92 | * } |
| 93 | 93 | */ |
| 94 | - public function get( $post_id ) { |
|
| 94 | + public function get($post_id) { |
|
| 95 | 95 | |
| 96 | - $this->log->trace( "Getting the post type for post $post_id..." ); |
|
| 96 | + $this->log->trace("Getting the post type for post $post_id..."); |
|
| 97 | 97 | |
| 98 | - $post_type = get_post_type( $post_id ); |
|
| 98 | + $post_type = get_post_type($post_id); |
|
| 99 | 99 | |
| 100 | 100 | // If it's not an entity post type return `Thing` by default. |
| 101 | - if ( ! self::is_valid_entity_post_type( $post_type ) ) { |
|
| 102 | - $this->log->info( "Returning `Thing` for post $post_id." ); |
|
| 101 | + if ( ! self::is_valid_entity_post_type($post_type)) { |
|
| 102 | + $this->log->info("Returning `Thing` for post $post_id."); |
|
| 103 | 103 | |
| 104 | 104 | return array( |
| 105 | 105 | 'uri' => 'http://schema.org/Thing', |
@@ -108,18 +108,18 @@ discard block |
||
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // Get the type from the associated classification. |
| 111 | - $terms = wp_get_object_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 111 | + $terms = wp_get_object_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 112 | 112 | |
| 113 | - if ( is_wp_error( $terms ) ) { |
|
| 114 | - $this->log->error( "An error occurred while getting the post type for post $post_id: " . $terms->get_error_message() ); |
|
| 113 | + if (is_wp_error($terms)) { |
|
| 114 | + $this->log->error("An error occurred while getting the post type for post $post_id: ".$terms->get_error_message()); |
|
| 115 | 115 | |
| 116 | 116 | // TODO: handle error |
| 117 | 117 | return null; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // If there are not terms associated, default to article. |
| 121 | - if ( 0 === count( $terms ) ) { |
|
| 122 | - $this->log->info( "Post $post_id has no terms, returning `Article`." ); |
|
| 121 | + if (0 === count($terms)) { |
|
| 122 | + $this->log->info("Post $post_id has no terms, returning `Article`."); |
|
| 123 | 123 | |
| 124 | 124 | return array( |
| 125 | 125 | 'uri' => 'http://schema.org/Article', |
@@ -127,10 +127,10 @@ discard block |
||
| 127 | 127 | ); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - $this->log->debug( "Found {$terms[0]->slug} term for post $post_id." ); |
|
| 130 | + $this->log->debug("Found {$terms[0]->slug} term for post $post_id."); |
|
| 131 | 131 | |
| 132 | 132 | // Return the entity type with the specified id. |
| 133 | - return $this->schema_service->get_schema( $terms[0]->slug ); |
|
| 133 | + return $this->schema_service->get_schema($terms[0]->slug); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | /** |
@@ -141,21 +141,21 @@ discard block |
||
| 141 | 141 | * @param int $post_id The post id. |
| 142 | 142 | * @param string $type_uri The type URI. |
| 143 | 143 | */ |
| 144 | - public function set( $post_id, $type_uri ) { |
|
| 144 | + public function set($post_id, $type_uri) { |
|
| 145 | 145 | |
| 146 | 146 | // If the type URI is empty we remove the type. |
| 147 | - if ( empty( $type_uri ) ) { |
|
| 148 | - $this->log->debug( "Removing entity type for post $post_id..." ); |
|
| 147 | + if (empty($type_uri)) { |
|
| 148 | + $this->log->debug("Removing entity type for post $post_id..."); |
|
| 149 | 149 | |
| 150 | - wp_set_object_terms( $post_id, null, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 150 | + wp_set_object_terms($post_id, null, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 151 | 151 | |
| 152 | 152 | return; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - $this->log->debug( "Setting entity type for post $post_id..." ); |
|
| 155 | + $this->log->debug("Setting entity type for post $post_id..."); |
|
| 156 | 156 | |
| 157 | 157 | // Get all the terms bound to the wl_entity_type taxonomy. |
| 158 | - $terms = get_terms( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 158 | + $terms = get_terms(Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 159 | 159 | 'hide_empty' => false, |
| 160 | 160 | // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
| 161 | 161 | // An issue has been opened with the AAM plugin author as well. |
@@ -163,31 +163,31 @@ discard block |
||
| 163 | 163 | // see https://github.com/insideout10/wordlift-plugin/issues/334 |
| 164 | 164 | // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
| 165 | 165 | 'fields' => 'all', |
| 166 | - ) ); |
|
| 166 | + )); |
|
| 167 | 167 | |
| 168 | 168 | // Check which term matches the specified URI. |
| 169 | - foreach ( $terms as $term ) { |
|
| 169 | + foreach ($terms as $term) { |
|
| 170 | 170 | |
| 171 | 171 | $term_id = $term->term_id; |
| 172 | 172 | $term_slug = $term->slug; |
| 173 | 173 | |
| 174 | - $this->log->trace( "Parsing term {$term->slug}..." ); |
|
| 174 | + $this->log->trace("Parsing term {$term->slug}..."); |
|
| 175 | 175 | |
| 176 | 176 | // Load the type data. |
| 177 | - $type = $this->schema_service->get_schema( $term_slug ); |
|
| 177 | + $type = $this->schema_service->get_schema($term_slug); |
|
| 178 | 178 | |
| 179 | 179 | // Set the related term ID. |
| 180 | - if ( $type_uri === $type['uri'] || $type_uri === $type['css_class'] ) { |
|
| 180 | + if ($type_uri === $type['uri'] || $type_uri === $type['css_class']) { |
|
| 181 | 181 | |
| 182 | - $this->log->debug( "Setting entity type [ post id :: $post_id ][ term id :: $term_id ][ term slug :: $term_slug ][ type uri :: {$type['uri']} ][ type css class :: {$type['css_class']} ]" ); |
|
| 182 | + $this->log->debug("Setting entity type [ post id :: $post_id ][ term id :: $term_id ][ term slug :: $term_slug ][ type uri :: {$type['uri']} ][ type css class :: {$type['css_class']} ]"); |
|
| 183 | 183 | |
| 184 | - wp_set_object_terms( $post_id, (int) $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 184 | + wp_set_object_terms($post_id, (int) $term_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME); |
|
| 185 | 185 | |
| 186 | 186 | return; |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - $this->log->error( "Type not found [ post id :: $post_id ][ type uri :: $type_uri ]" ); |
|
| 190 | + $this->log->error("Type not found [ post id :: $post_id ][ type uri :: $type_uri ]"); |
|
| 191 | 191 | |
| 192 | 192 | } |
| 193 | 193 | |
@@ -202,26 +202,26 @@ discard block |
||
| 202 | 202 | * |
| 203 | 203 | * @return bool True if an entity type is set otherwise false. |
| 204 | 204 | */ |
| 205 | - public function has_entity_type( $post_id, $uri = null ) { |
|
| 205 | + public function has_entity_type($post_id, $uri = null) { |
|
| 206 | 206 | |
| 207 | - $this->log->debug( "Checking if post $post_id has an entity type [ $uri ]..." ); |
|
| 207 | + $this->log->debug("Checking if post $post_id has an entity type [ $uri ]..."); |
|
| 208 | 208 | |
| 209 | 209 | // If an URI hasn't been specified just check whether we have at least |
| 210 | 210 | // one entity type. |
| 211 | - if ( null === $uri ) { |
|
| 211 | + if (null === $uri) { |
|
| 212 | 212 | |
| 213 | 213 | // Get the post terms for the specified post ID. |
| 214 | - $terms = $this->get_post_terms( $post_id ); |
|
| 214 | + $terms = $this->get_post_terms($post_id); |
|
| 215 | 215 | |
| 216 | - $this->log->debug( "Post $post_id has " . count( $terms ) . ' type(s).' ); |
|
| 216 | + $this->log->debug("Post $post_id has ".count($terms).' type(s).'); |
|
| 217 | 217 | |
| 218 | 218 | // True if there's at least one term bound to the post. |
| 219 | - return ( 0 < count( $terms ) ); |
|
| 219 | + return (0 < count($terms)); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - $has_entity_type = ( null !== $this->get_term_by_uri( $post_id, $uri ) ); |
|
| 222 | + $has_entity_type = (null !== $this->get_term_by_uri($post_id, $uri)); |
|
| 223 | 223 | |
| 224 | - $this->log->debug( "Post $post_id has $uri type: " . ( $has_entity_type ? 'yes' : 'no' ) ); |
|
| 224 | + $this->log->debug("Post $post_id has $uri type: ".($has_entity_type ? 'yes' : 'no')); |
|
| 225 | 225 | |
| 226 | 226 | // Check whether the post has an entity type with that URI. |
| 227 | 227 | return $has_entity_type; |
@@ -236,9 +236,9 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @return array|WP_Error An array of entity types' terms or {@link WP_Error}. |
| 238 | 238 | */ |
| 239 | - private function get_post_terms( $post_id ) { |
|
| 239 | + private function get_post_terms($post_id) { |
|
| 240 | 240 | |
| 241 | - return wp_get_post_terms( $post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 241 | + return wp_get_post_terms($post_id, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array( |
|
| 242 | 242 | 'hide_empty' => false, |
| 243 | 243 | // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
| 244 | 244 | // An issue has been opened with the AAM plugin author as well. |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | // see https://github.com/insideout10/wordlift-plugin/issues/334 |
| 247 | 247 | // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
| 248 | 248 | 'fields' => 'all', |
| 249 | - ) ); |
|
| 249 | + )); |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | /** |
@@ -266,24 +266,24 @@ discard block |
||
| 266 | 266 | * @type array custom_fields An array of custom fields. |
| 267 | 267 | * } |
| 268 | 268 | */ |
| 269 | - private function get_term_by_uri( $post_id, $uri ) { |
|
| 269 | + private function get_term_by_uri($post_id, $uri) { |
|
| 270 | 270 | |
| 271 | 271 | // Get the post terms bound to the specified post. |
| 272 | - $terms = $this->get_post_terms( $post_id ); |
|
| 272 | + $terms = $this->get_post_terms($post_id); |
|
| 273 | 273 | |
| 274 | 274 | // Look for a term if the specified URI. |
| 275 | - foreach ( $terms as $term ) { |
|
| 275 | + foreach ($terms as $term) { |
|
| 276 | 276 | // Get the schema by slug. |
| 277 | - $schema = $this->schema_service->get_schema( $term->slug ); |
|
| 277 | + $schema = $this->schema_service->get_schema($term->slug); |
|
| 278 | 278 | |
| 279 | 279 | // Continue to the next one, if a schema isn't found (or hasn't got |
| 280 | 280 | // and URI. |
| 281 | - if ( null === $schema || ! isset( $schema['uri'] ) ) { |
|
| 281 | + if (null === $schema || ! isset($schema['uri'])) { |
|
| 282 | 282 | continue; |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | // Return the schema if the URI matches. |
| 286 | - if ( $uri === $schema['uri'] ) { |
|
| 286 | + if ($uri === $schema['uri']) { |
|
| 287 | 287 | return $schema; |
| 288 | 288 | } |
| 289 | 289 | } |
@@ -305,9 +305,9 @@ discard block |
||
| 305 | 305 | * |
| 306 | 306 | * @return bool Return true if the post type can be used for entities, otherwise false. |
| 307 | 307 | */ |
| 308 | - public static function is_valid_entity_post_type( $post_type ) { |
|
| 308 | + public static function is_valid_entity_post_type($post_type) { |
|
| 309 | 309 | |
| 310 | - return in_array( $post_type, Wordlift_Entity_Service::valid_entity_post_types(), true ); |
|
| 310 | + return in_array($post_type, Wordlift_Entity_Service::valid_entity_post_types(), true); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | } |
@@ -16,135 +16,135 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Http_Api { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A {@link Wordlift_Log_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.15.3 |
|
| 23 | - * |
|
| 24 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Create a {@link Wordlift_End_Point} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.15.3 |
|
| 32 | - */ |
|
| 33 | - public function __construct() { |
|
| 34 | - |
|
| 35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 36 | - |
|
| 37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 39 | - add_action( 'admin_post_wl_hello_world', array( $this, 'hello_world' ) ); |
|
| 40 | - add_action( 'admin_post_nopriv_wl_hello_world', array( $this, 'nopriv_hello_world' ) ); |
|
| 41 | - |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Add the `wl-api` rewrite end-point. |
|
| 46 | - * |
|
| 47 | - * @since 3.15.3 |
|
| 48 | - */ |
|
| 49 | - public function add_rewrite_endpoint() { |
|
| 50 | - |
|
| 51 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 52 | - $this->ensure_rewrite_rules_are_flushed(); |
|
| 53 | - |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Handle `template_redirect` hooks. |
|
| 58 | - * |
|
| 59 | - * @since 3.15.3 |
|
| 60 | - */ |
|
| 61 | - public function template_redirect() { |
|
| 62 | - |
|
| 63 | - global $wp_query; |
|
| 64 | - |
|
| 65 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 66 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 67 | - |
|
| 68 | - return; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $this->do_action( $_REQUEST['action'] ); |
|
| 72 | - |
|
| 73 | - exit; |
|
| 74 | - |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Do the requested action. |
|
| 79 | - * |
|
| 80 | - * @since 3.15.3 |
|
| 81 | - * |
|
| 82 | - * @param string $action The action to execute. |
|
| 83 | - */ |
|
| 84 | - private function do_action( $action ) { |
|
| 85 | - |
|
| 86 | - if ( empty( $action ) ) { |
|
| 87 | - return; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 91 | - /** |
|
| 92 | - * Fires on a non-authenticated admin post request for the given action. |
|
| 93 | - * |
|
| 94 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 95 | - * request action. |
|
| 96 | - * |
|
| 97 | - * @since 2.6.0 |
|
| 98 | - */ |
|
| 99 | - do_action( "admin_post_nopriv_{$action}" ); |
|
| 100 | - } else { |
|
| 101 | - /** |
|
| 102 | - * Fires on an authenticated admin post request for the given action. |
|
| 103 | - * |
|
| 104 | - * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 105 | - * request action. |
|
| 106 | - * |
|
| 107 | - * @since 2.6.0 |
|
| 108 | - */ |
|
| 109 | - do_action( "admin_post_{$action}" ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Test function, anonymous. |
|
| 116 | - * |
|
| 117 | - * @since 3.15.3 |
|
| 118 | - */ |
|
| 119 | - public function nopriv_hello_world() { |
|
| 120 | - |
|
| 121 | - wp_die( 'Hello World! (from anonymous)' ); |
|
| 122 | - |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Test function, authenticated. |
|
| 127 | - * |
|
| 128 | - * @since 3.15.3 |
|
| 129 | - */ |
|
| 130 | - public function hello_world() { |
|
| 131 | - |
|
| 132 | - wp_die( 'Hello World! (from authenticated)' ); |
|
| 133 | - |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Ensure that the rewrite rules are flushed the first time. |
|
| 138 | - * |
|
| 139 | - * @since 3.15.3 |
|
| 140 | - */ |
|
| 141 | - public static function ensure_rewrite_rules_are_flushed() { |
|
| 142 | - |
|
| 143 | - if ( 1 !== get_option( 'wl_http_api' ) ) { |
|
| 144 | - flush_rewrite_rules(); |
|
| 145 | - add_option( 'wl_http_api', 1 ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - } |
|
| 19 | + /** |
|
| 20 | + * A {@link Wordlift_Log_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.15.3 |
|
| 23 | + * |
|
| 24 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Create a {@link Wordlift_End_Point} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.15.3 |
|
| 32 | + */ |
|
| 33 | + public function __construct() { |
|
| 34 | + |
|
| 35 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 36 | + |
|
| 37 | + add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | + add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 39 | + add_action( 'admin_post_wl_hello_world', array( $this, 'hello_world' ) ); |
|
| 40 | + add_action( 'admin_post_nopriv_wl_hello_world', array( $this, 'nopriv_hello_world' ) ); |
|
| 41 | + |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Add the `wl-api` rewrite end-point. |
|
| 46 | + * |
|
| 47 | + * @since 3.15.3 |
|
| 48 | + */ |
|
| 49 | + public function add_rewrite_endpoint() { |
|
| 50 | + |
|
| 51 | + add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 52 | + $this->ensure_rewrite_rules_are_flushed(); |
|
| 53 | + |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Handle `template_redirect` hooks. |
|
| 58 | + * |
|
| 59 | + * @since 3.15.3 |
|
| 60 | + */ |
|
| 61 | + public function template_redirect() { |
|
| 62 | + |
|
| 63 | + global $wp_query; |
|
| 64 | + |
|
| 65 | + if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 66 | + $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 67 | + |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $this->do_action( $_REQUEST['action'] ); |
|
| 72 | + |
|
| 73 | + exit; |
|
| 74 | + |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Do the requested action. |
|
| 79 | + * |
|
| 80 | + * @since 3.15.3 |
|
| 81 | + * |
|
| 82 | + * @param string $action The action to execute. |
|
| 83 | + */ |
|
| 84 | + private function do_action( $action ) { |
|
| 85 | + |
|
| 86 | + if ( empty( $action ) ) { |
|
| 87 | + return; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 91 | + /** |
|
| 92 | + * Fires on a non-authenticated admin post request for the given action. |
|
| 93 | + * |
|
| 94 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 95 | + * request action. |
|
| 96 | + * |
|
| 97 | + * @since 2.6.0 |
|
| 98 | + */ |
|
| 99 | + do_action( "admin_post_nopriv_{$action}" ); |
|
| 100 | + } else { |
|
| 101 | + /** |
|
| 102 | + * Fires on an authenticated admin post request for the given action. |
|
| 103 | + * |
|
| 104 | + * The dynamic portion of the hook name, `$action`, refers to the given |
|
| 105 | + * request action. |
|
| 106 | + * |
|
| 107 | + * @since 2.6.0 |
|
| 108 | + */ |
|
| 109 | + do_action( "admin_post_{$action}" ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Test function, anonymous. |
|
| 116 | + * |
|
| 117 | + * @since 3.15.3 |
|
| 118 | + */ |
|
| 119 | + public function nopriv_hello_world() { |
|
| 120 | + |
|
| 121 | + wp_die( 'Hello World! (from anonymous)' ); |
|
| 122 | + |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Test function, authenticated. |
|
| 127 | + * |
|
| 128 | + * @since 3.15.3 |
|
| 129 | + */ |
|
| 130 | + public function hello_world() { |
|
| 131 | + |
|
| 132 | + wp_die( 'Hello World! (from authenticated)' ); |
|
| 133 | + |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Ensure that the rewrite rules are flushed the first time. |
|
| 138 | + * |
|
| 139 | + * @since 3.15.3 |
|
| 140 | + */ |
|
| 141 | + public static function ensure_rewrite_rules_are_flushed() { |
|
| 142 | + |
|
| 143 | + if ( 1 !== get_option( 'wl_http_api' ) ) { |
|
| 144 | + flush_rewrite_rules(); |
|
| 145 | + add_option( 'wl_http_api', 1 ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | 150 | } |
@@ -32,12 +32,12 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | public function __construct() { |
| 34 | 34 | |
| 35 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 35 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 36 | 36 | |
| 37 | - add_action( 'init', array( $this, 'add_rewrite_endpoint' ) ); |
|
| 38 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); |
|
| 39 | - add_action( 'admin_post_wl_hello_world', array( $this, 'hello_world' ) ); |
|
| 40 | - add_action( 'admin_post_nopriv_wl_hello_world', array( $this, 'nopriv_hello_world' ) ); |
|
| 37 | + add_action('init', array($this, 'add_rewrite_endpoint')); |
|
| 38 | + add_action('template_redirect', array($this, 'template_redirect')); |
|
| 39 | + add_action('admin_post_wl_hello_world', array($this, 'hello_world')); |
|
| 40 | + add_action('admin_post_nopriv_wl_hello_world', array($this, 'nopriv_hello_world')); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | public function add_rewrite_endpoint() { |
| 50 | 50 | |
| 51 | - add_rewrite_endpoint( 'wl-api', EP_ROOT ); |
|
| 51 | + add_rewrite_endpoint('wl-api', EP_ROOT); |
|
| 52 | 52 | $this->ensure_rewrite_rules_are_flushed(); |
| 53 | 53 | |
| 54 | 54 | } |
@@ -62,13 +62,13 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | global $wp_query; |
| 64 | 64 | |
| 65 | - if ( ! isset( $wp_query->query_vars['wl-api'] ) ) { |
|
| 66 | - $this->log->trace( 'Skipping, not a `wl-api` call.' ); |
|
| 65 | + if ( ! isset($wp_query->query_vars['wl-api'])) { |
|
| 66 | + $this->log->trace('Skipping, not a `wl-api` call.'); |
|
| 67 | 67 | |
| 68 | 68 | return; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - $this->do_action( $_REQUEST['action'] ); |
|
| 71 | + $this->do_action($_REQUEST['action']); |
|
| 72 | 72 | |
| 73 | 73 | exit; |
| 74 | 74 | |
@@ -81,13 +81,13 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @param string $action The action to execute. |
| 83 | 83 | */ |
| 84 | - private function do_action( $action ) { |
|
| 84 | + private function do_action($action) { |
|
| 85 | 85 | |
| 86 | - if ( empty( $action ) ) { |
|
| 86 | + if (empty($action)) { |
|
| 87 | 87 | return; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - if ( ! wp_validate_auth_cookie( '', 'logged_in' ) ) { |
|
| 90 | + if ( ! wp_validate_auth_cookie('', 'logged_in')) { |
|
| 91 | 91 | /** |
| 92 | 92 | * Fires on a non-authenticated admin post request for the given action. |
| 93 | 93 | * |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | * |
| 97 | 97 | * @since 2.6.0 |
| 98 | 98 | */ |
| 99 | - do_action( "admin_post_nopriv_{$action}" ); |
|
| 99 | + do_action("admin_post_nopriv_{$action}"); |
|
| 100 | 100 | } else { |
| 101 | 101 | /** |
| 102 | 102 | * Fires on an authenticated admin post request for the given action. |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | * |
| 107 | 107 | * @since 2.6.0 |
| 108 | 108 | */ |
| 109 | - do_action( "admin_post_{$action}" ); |
|
| 109 | + do_action("admin_post_{$action}"); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | } |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | public function nopriv_hello_world() { |
| 120 | 120 | |
| 121 | - wp_die( 'Hello World! (from anonymous)' ); |
|
| 121 | + wp_die('Hello World! (from anonymous)'); |
|
| 122 | 122 | |
| 123 | 123 | } |
| 124 | 124 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | public function hello_world() { |
| 131 | 131 | |
| 132 | - wp_die( 'Hello World! (from authenticated)' ); |
|
| 132 | + wp_die('Hello World! (from authenticated)'); |
|
| 133 | 133 | |
| 134 | 134 | } |
| 135 | 135 | |
@@ -140,9 +140,9 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | public static function ensure_rewrite_rules_are_flushed() { |
| 142 | 142 | |
| 143 | - if ( 1 !== get_option( 'wl_http_api' ) ) { |
|
| 143 | + if (1 !== get_option('wl_http_api')) { |
|
| 144 | 144 | flush_rewrite_rules(); |
| 145 | - add_option( 'wl_http_api', 1 ); |
|
| 145 | + add_option('wl_http_api', 1); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | } |