@@ -16,127 +16,127 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Schemaorg_Sync_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * A {@link Wordlift_Log_Service} instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.20.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 25 | - */ |
|
| 26 | - private $log; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The singleton instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.20.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var \Wordlift_Schemaorg_Sync_Service $instance The singleton instance. |
|
| 34 | - */ |
|
| 35 | - private static $instance; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Create a {@link Wordlift_Schemaorg_Sync_Service} instance. |
|
| 39 | - * |
|
| 40 | - * @since 3.20.0 |
|
| 41 | - */ |
|
| 42 | - public function __construct() { |
|
| 43 | - |
|
| 44 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 45 | - |
|
| 46 | - // Hook the `wl_sync_schemaorg` ajax action. |
|
| 47 | - add_action( 'wp_ajax_wl_sync_schemaorg', array( $this, 'load' ) ); |
|
| 48 | - |
|
| 49 | - self::$instance = $this; |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Get the singleton instance. |
|
| 55 | - * |
|
| 56 | - * @since 3.20.0 |
|
| 57 | - * |
|
| 58 | - * @return \Wordlift_Schemaorg_Sync_Service The singleton instance. |
|
| 59 | - */ |
|
| 60 | - public static function get_instance() { |
|
| 61 | - |
|
| 62 | - return self::$instance; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Load the Schema.org classes from a file. |
|
| 67 | - * |
|
| 68 | - * @since 3.20.0 |
|
| 69 | - * |
|
| 70 | - * @return bool True if successful otherwise false. |
|
| 71 | - */ |
|
| 72 | - public function load_from_file() { |
|
| 73 | - |
|
| 74 | - // Load the file contents. |
|
| 75 | - $contents = file_get_contents( dirname( __FILE__ ) . '/schema-classes.json' ); |
|
| 76 | - |
|
| 77 | - // Load the file contents. |
|
| 78 | - return $this->load( $contents ); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Load the Schema.org classes from the provided contents. |
|
| 83 | - * |
|
| 84 | - * @param $contents |
|
| 85 | - * |
|
| 86 | - * @return bool |
|
| 87 | - */ |
|
| 88 | - private function load( $contents ) { |
|
| 89 | - |
|
| 90 | - // Decode the JSON contents. |
|
| 91 | - $json = json_decode( $contents, true ); |
|
| 92 | - |
|
| 93 | - if ( null === $json ) { |
|
| 94 | - $this->log->error( 'Invalid json.' ); |
|
| 95 | - |
|
| 96 | - // Error: invalid body. |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - if ( ! isset( $json['schemaClasses'] ) ) { |
|
| 101 | - $this->log->error( '`schemaClasses` missing from json.' ); |
|
| 102 | - |
|
| 103 | - // Error: invalid json. |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - // Load the Schema.org classes. |
|
| 108 | - foreach ( $json['schemaClasses'] as $schema_class ) { |
|
| 109 | - $slug = $schema_class['dashname']; |
|
| 110 | - $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 111 | - |
|
| 112 | - $args = array( |
|
| 113 | - 'parent' => 0, |
|
| 114 | - 'description' => $schema_class['description'], |
|
| 115 | - 'slug' => $schema_class['dashname'], |
|
| 116 | - ); |
|
| 117 | - if ( null !== $term ) { |
|
| 118 | - wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 119 | - } else { |
|
| 120 | - $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - // Update the parents/children relationship. |
|
| 124 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
| 125 | - foreach ( $schema_class['children'] as $child ) { |
|
| 126 | - add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // Update the term name. |
|
| 130 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
| 131 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
| 132 | - |
|
| 133 | - // Update the term URI. |
|
| 134 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
| 135 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
| 136 | - |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return true; |
|
| 140 | - } |
|
| 19 | + /** |
|
| 20 | + * A {@link Wordlift_Log_Service} instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.20.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 25 | + */ |
|
| 26 | + private $log; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The singleton instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.20.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var \Wordlift_Schemaorg_Sync_Service $instance The singleton instance. |
|
| 34 | + */ |
|
| 35 | + private static $instance; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Create a {@link Wordlift_Schemaorg_Sync_Service} instance. |
|
| 39 | + * |
|
| 40 | + * @since 3.20.0 |
|
| 41 | + */ |
|
| 42 | + public function __construct() { |
|
| 43 | + |
|
| 44 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 45 | + |
|
| 46 | + // Hook the `wl_sync_schemaorg` ajax action. |
|
| 47 | + add_action( 'wp_ajax_wl_sync_schemaorg', array( $this, 'load' ) ); |
|
| 48 | + |
|
| 49 | + self::$instance = $this; |
|
| 50 | + |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Get the singleton instance. |
|
| 55 | + * |
|
| 56 | + * @since 3.20.0 |
|
| 57 | + * |
|
| 58 | + * @return \Wordlift_Schemaorg_Sync_Service The singleton instance. |
|
| 59 | + */ |
|
| 60 | + public static function get_instance() { |
|
| 61 | + |
|
| 62 | + return self::$instance; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Load the Schema.org classes from a file. |
|
| 67 | + * |
|
| 68 | + * @since 3.20.0 |
|
| 69 | + * |
|
| 70 | + * @return bool True if successful otherwise false. |
|
| 71 | + */ |
|
| 72 | + public function load_from_file() { |
|
| 73 | + |
|
| 74 | + // Load the file contents. |
|
| 75 | + $contents = file_get_contents( dirname( __FILE__ ) . '/schema-classes.json' ); |
|
| 76 | + |
|
| 77 | + // Load the file contents. |
|
| 78 | + return $this->load( $contents ); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Load the Schema.org classes from the provided contents. |
|
| 83 | + * |
|
| 84 | + * @param $contents |
|
| 85 | + * |
|
| 86 | + * @return bool |
|
| 87 | + */ |
|
| 88 | + private function load( $contents ) { |
|
| 89 | + |
|
| 90 | + // Decode the JSON contents. |
|
| 91 | + $json = json_decode( $contents, true ); |
|
| 92 | + |
|
| 93 | + if ( null === $json ) { |
|
| 94 | + $this->log->error( 'Invalid json.' ); |
|
| 95 | + |
|
| 96 | + // Error: invalid body. |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + if ( ! isset( $json['schemaClasses'] ) ) { |
|
| 101 | + $this->log->error( '`schemaClasses` missing from json.' ); |
|
| 102 | + |
|
| 103 | + // Error: invalid json. |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + // Load the Schema.org classes. |
|
| 108 | + foreach ( $json['schemaClasses'] as $schema_class ) { |
|
| 109 | + $slug = $schema_class['dashname']; |
|
| 110 | + $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 111 | + |
|
| 112 | + $args = array( |
|
| 113 | + 'parent' => 0, |
|
| 114 | + 'description' => $schema_class['description'], |
|
| 115 | + 'slug' => $schema_class['dashname'], |
|
| 116 | + ); |
|
| 117 | + if ( null !== $term ) { |
|
| 118 | + wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 119 | + } else { |
|
| 120 | + $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + // Update the parents/children relationship. |
|
| 124 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
| 125 | + foreach ( $schema_class['children'] as $child ) { |
|
| 126 | + add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // Update the term name. |
|
| 130 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
| 131 | + update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
| 132 | + |
|
| 133 | + // Update the term URI. |
|
| 134 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
| 135 | + update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
| 136 | + |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return true; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | } |
@@ -41,10 +41,10 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public function __construct() { |
| 43 | 43 | |
| 44 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 44 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 45 | 45 | |
| 46 | 46 | // Hook the `wl_sync_schemaorg` ajax action. |
| 47 | - add_action( 'wp_ajax_wl_sync_schemaorg', array( $this, 'load' ) ); |
|
| 47 | + add_action('wp_ajax_wl_sync_schemaorg', array($this, 'load')); |
|
| 48 | 48 | |
| 49 | 49 | self::$instance = $this; |
| 50 | 50 | |
@@ -72,10 +72,10 @@ discard block |
||
| 72 | 72 | public function load_from_file() { |
| 73 | 73 | |
| 74 | 74 | // Load the file contents. |
| 75 | - $contents = file_get_contents( dirname( __FILE__ ) . '/schema-classes.json' ); |
|
| 75 | + $contents = file_get_contents(dirname(__FILE__).'/schema-classes.json'); |
|
| 76 | 76 | |
| 77 | 77 | // Load the file contents. |
| 78 | - return $this->load( $contents ); |
|
| 78 | + return $this->load($contents); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
@@ -85,54 +85,54 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @return bool |
| 87 | 87 | */ |
| 88 | - private function load( $contents ) { |
|
| 88 | + private function load($contents) { |
|
| 89 | 89 | |
| 90 | 90 | // Decode the JSON contents. |
| 91 | - $json = json_decode( $contents, true ); |
|
| 91 | + $json = json_decode($contents, true); |
|
| 92 | 92 | |
| 93 | - if ( null === $json ) { |
|
| 94 | - $this->log->error( 'Invalid json.' ); |
|
| 93 | + if (null === $json) { |
|
| 94 | + $this->log->error('Invalid json.'); |
|
| 95 | 95 | |
| 96 | 96 | // Error: invalid body. |
| 97 | 97 | return false; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - if ( ! isset( $json['schemaClasses'] ) ) { |
|
| 101 | - $this->log->error( '`schemaClasses` missing from json.' ); |
|
| 100 | + if ( ! isset($json['schemaClasses'])) { |
|
| 101 | + $this->log->error('`schemaClasses` missing from json.'); |
|
| 102 | 102 | |
| 103 | 103 | // Error: invalid json. |
| 104 | 104 | return false; |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | // Load the Schema.org classes. |
| 108 | - foreach ( $json['schemaClasses'] as $schema_class ) { |
|
| 108 | + foreach ($json['schemaClasses'] as $schema_class) { |
|
| 109 | 109 | $slug = $schema_class['dashname']; |
| 110 | - $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 110 | + $term = term_exists($slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 111 | 111 | |
| 112 | 112 | $args = array( |
| 113 | 113 | 'parent' => 0, |
| 114 | 114 | 'description' => $schema_class['description'], |
| 115 | 115 | 'slug' => $schema_class['dashname'], |
| 116 | 116 | ); |
| 117 | - if ( null !== $term ) { |
|
| 118 | - wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 117 | + if (null !== $term) { |
|
| 118 | + wp_update_term($term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args); |
|
| 119 | 119 | } else { |
| 120 | - $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 120 | + $term = wp_insert_term($schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | // Update the parents/children relationship. |
| 124 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
| 125 | - foreach ( $schema_class['children'] as $child ) { |
|
| 126 | - add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
| 124 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY); |
|
| 125 | + foreach ($schema_class['children'] as $child) { |
|
| 126 | + add_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname']); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | // Update the term name. |
| 130 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
| 131 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
| 130 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY); |
|
| 131 | + update_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name']); |
|
| 132 | 132 | |
| 133 | 133 | // Update the term URI. |
| 134 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
| 135 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
| 134 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY); |
|
| 135 | + update_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}"); |
|
| 136 | 136 | |
| 137 | 137 | } |
| 138 | 138 | |
@@ -16,123 +16,123 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Schemaorg_Sync_Batch_Operation implements Wordlift_Batch_Operation_Interface { |
| 18 | 18 | |
| 19 | - private static $instance; |
|
| 20 | - |
|
| 21 | - public function __construct() { |
|
| 22 | - |
|
| 23 | - self::$instance = $this; |
|
| 24 | - |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public static function get_instance() { |
|
| 28 | - |
|
| 29 | - return self::$instance; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Process the batch operation starting from the specified offset. |
|
| 34 | - * |
|
| 35 | - * @since 3.20.0 |
|
| 36 | - * |
|
| 37 | - * @param int $offset Start from the specified offset (or 0 if not specified). |
|
| 38 | - * @param int $limit Process the specified amount of items per call (or 10 if not specified). |
|
| 39 | - * |
|
| 40 | - * @return array { |
|
| 41 | - * The operation result. |
|
| 42 | - * |
|
| 43 | - * @type int $next The next offset. |
|
| 44 | - * @type int $limit The amount of items to process per call. |
|
| 45 | - * @type int $remaining The remaining number of elements to process. |
|
| 46 | - * } |
|
| 47 | - */ |
|
| 48 | - public function process( $offset = 0, $limit = 10 ) { |
|
| 49 | - |
|
| 50 | - // Get the schema classes. |
|
| 51 | - $all_schema_classes = $this->get_schema_classes(); |
|
| 52 | - |
|
| 53 | - // Get only the part that we need to process. |
|
| 54 | - $schema_classes = array_slice( $all_schema_classes, $offset, $limit ); |
|
| 55 | - |
|
| 56 | - // Load the Schema.org classes. |
|
| 57 | - foreach ( $schema_classes as $schema_class ) { |
|
| 58 | - $slug = $schema_class['dashname']; |
|
| 59 | - $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 60 | - |
|
| 61 | - $args = array( |
|
| 62 | - 'parent' => 0, |
|
| 63 | - 'description' => $schema_class['description'], |
|
| 64 | - 'slug' => $schema_class['dashname'], |
|
| 65 | - ); |
|
| 66 | - if ( null !== $term ) { |
|
| 67 | - wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 68 | - } else { |
|
| 69 | - $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // Update the parents/children relationship. |
|
| 73 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
| 74 | - foreach ( $schema_class['children'] as $child ) { |
|
| 75 | - add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // Update the term name. |
|
| 79 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
| 80 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
| 81 | - |
|
| 82 | - // Update the term URI. |
|
| 83 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
| 84 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
| 85 | - |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - // Calculate the return values. |
|
| 89 | - $next = $offset + $limit; |
|
| 90 | - $remaining = $this->count(); |
|
| 91 | - |
|
| 92 | - return array( |
|
| 93 | - 'next' => $next, |
|
| 94 | - 'limit' => $limit, |
|
| 95 | - 'complete' => ( 0 === $remaining ), |
|
| 96 | - 'remaining' => $remaining, |
|
| 97 | - ); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Count the number of elements that would be affected by the operation. |
|
| 102 | - * |
|
| 103 | - * @since 3.20.0 |
|
| 104 | - * |
|
| 105 | - * @return int The number of elements that would be affected. |
|
| 106 | - */ |
|
| 107 | - public function count() { |
|
| 108 | - |
|
| 109 | - // Schema Classes count. |
|
| 110 | - $schema_classes_count = count( $this->get_schema_classes() ); |
|
| 111 | - |
|
| 112 | - // Terms count. |
|
| 113 | - $terms_count = wp_count_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 114 | - |
|
| 115 | - // Return the difference. |
|
| 116 | - return $schema_classes_count - $terms_count; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Get the schema.org classes from the JSON file. |
|
| 121 | - * |
|
| 122 | - * @since 3.20.0 |
|
| 123 | - * |
|
| 124 | - * @return array An array of schema classes. |
|
| 125 | - */ |
|
| 126 | - private function get_schema_classes() { |
|
| 127 | - |
|
| 128 | - // Load the file contents. |
|
| 129 | - $contents = file_get_contents( dirname( __FILE__ ) . '/schema-classes.json' ); |
|
| 130 | - |
|
| 131 | - // Decode the JSON contents. |
|
| 132 | - $json = json_decode( $contents, true ); |
|
| 133 | - |
|
| 134 | - // Return the schema classes or an empty array. |
|
| 135 | - return isset( $json['schemaClasses'] ) ? $json['schemaClasses'] : array(); |
|
| 136 | - } |
|
| 19 | + private static $instance; |
|
| 20 | + |
|
| 21 | + public function __construct() { |
|
| 22 | + |
|
| 23 | + self::$instance = $this; |
|
| 24 | + |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public static function get_instance() { |
|
| 28 | + |
|
| 29 | + return self::$instance; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Process the batch operation starting from the specified offset. |
|
| 34 | + * |
|
| 35 | + * @since 3.20.0 |
|
| 36 | + * |
|
| 37 | + * @param int $offset Start from the specified offset (or 0 if not specified). |
|
| 38 | + * @param int $limit Process the specified amount of items per call (or 10 if not specified). |
|
| 39 | + * |
|
| 40 | + * @return array { |
|
| 41 | + * The operation result. |
|
| 42 | + * |
|
| 43 | + * @type int $next The next offset. |
|
| 44 | + * @type int $limit The amount of items to process per call. |
|
| 45 | + * @type int $remaining The remaining number of elements to process. |
|
| 46 | + * } |
|
| 47 | + */ |
|
| 48 | + public function process( $offset = 0, $limit = 10 ) { |
|
| 49 | + |
|
| 50 | + // Get the schema classes. |
|
| 51 | + $all_schema_classes = $this->get_schema_classes(); |
|
| 52 | + |
|
| 53 | + // Get only the part that we need to process. |
|
| 54 | + $schema_classes = array_slice( $all_schema_classes, $offset, $limit ); |
|
| 55 | + |
|
| 56 | + // Load the Schema.org classes. |
|
| 57 | + foreach ( $schema_classes as $schema_class ) { |
|
| 58 | + $slug = $schema_class['dashname']; |
|
| 59 | + $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 60 | + |
|
| 61 | + $args = array( |
|
| 62 | + 'parent' => 0, |
|
| 63 | + 'description' => $schema_class['description'], |
|
| 64 | + 'slug' => $schema_class['dashname'], |
|
| 65 | + ); |
|
| 66 | + if ( null !== $term ) { |
|
| 67 | + wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 68 | + } else { |
|
| 69 | + $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // Update the parents/children relationship. |
|
| 73 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
| 74 | + foreach ( $schema_class['children'] as $child ) { |
|
| 75 | + add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // Update the term name. |
|
| 79 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
| 80 | + update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
| 81 | + |
|
| 82 | + // Update the term URI. |
|
| 83 | + delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
| 84 | + update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
| 85 | + |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + // Calculate the return values. |
|
| 89 | + $next = $offset + $limit; |
|
| 90 | + $remaining = $this->count(); |
|
| 91 | + |
|
| 92 | + return array( |
|
| 93 | + 'next' => $next, |
|
| 94 | + 'limit' => $limit, |
|
| 95 | + 'complete' => ( 0 === $remaining ), |
|
| 96 | + 'remaining' => $remaining, |
|
| 97 | + ); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Count the number of elements that would be affected by the operation. |
|
| 102 | + * |
|
| 103 | + * @since 3.20.0 |
|
| 104 | + * |
|
| 105 | + * @return int The number of elements that would be affected. |
|
| 106 | + */ |
|
| 107 | + public function count() { |
|
| 108 | + |
|
| 109 | + // Schema Classes count. |
|
| 110 | + $schema_classes_count = count( $this->get_schema_classes() ); |
|
| 111 | + |
|
| 112 | + // Terms count. |
|
| 113 | + $terms_count = wp_count_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 114 | + |
|
| 115 | + // Return the difference. |
|
| 116 | + return $schema_classes_count - $terms_count; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Get the schema.org classes from the JSON file. |
|
| 121 | + * |
|
| 122 | + * @since 3.20.0 |
|
| 123 | + * |
|
| 124 | + * @return array An array of schema classes. |
|
| 125 | + */ |
|
| 126 | + private function get_schema_classes() { |
|
| 127 | + |
|
| 128 | + // Load the file contents. |
|
| 129 | + $contents = file_get_contents( dirname( __FILE__ ) . '/schema-classes.json' ); |
|
| 130 | + |
|
| 131 | + // Decode the JSON contents. |
|
| 132 | + $json = json_decode( $contents, true ); |
|
| 133 | + |
|
| 134 | + // Return the schema classes or an empty array. |
|
| 135 | + return isset( $json['schemaClasses'] ) ? $json['schemaClasses'] : array(); |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | } |
@@ -45,43 +45,43 @@ discard block |
||
| 45 | 45 | * @type int $remaining The remaining number of elements to process. |
| 46 | 46 | * } |
| 47 | 47 | */ |
| 48 | - public function process( $offset = 0, $limit = 10 ) { |
|
| 48 | + public function process($offset = 0, $limit = 10) { |
|
| 49 | 49 | |
| 50 | 50 | // Get the schema classes. |
| 51 | 51 | $all_schema_classes = $this->get_schema_classes(); |
| 52 | 52 | |
| 53 | 53 | // Get only the part that we need to process. |
| 54 | - $schema_classes = array_slice( $all_schema_classes, $offset, $limit ); |
|
| 54 | + $schema_classes = array_slice($all_schema_classes, $offset, $limit); |
|
| 55 | 55 | |
| 56 | 56 | // Load the Schema.org classes. |
| 57 | - foreach ( $schema_classes as $schema_class ) { |
|
| 57 | + foreach ($schema_classes as $schema_class) { |
|
| 58 | 58 | $slug = $schema_class['dashname']; |
| 59 | - $term = term_exists( $slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 59 | + $term = term_exists($slug, Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 60 | 60 | |
| 61 | 61 | $args = array( |
| 62 | 62 | 'parent' => 0, |
| 63 | 63 | 'description' => $schema_class['description'], |
| 64 | 64 | 'slug' => $schema_class['dashname'], |
| 65 | 65 | ); |
| 66 | - if ( null !== $term ) { |
|
| 67 | - wp_update_term( $term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 66 | + if (null !== $term) { |
|
| 67 | + wp_update_term($term['term_id'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args); |
|
| 68 | 68 | } else { |
| 69 | - $term = wp_insert_term( $schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args ); |
|
| 69 | + $term = wp_insert_term($schema_class['name'], Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, $args); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | // Update the parents/children relationship. |
| 73 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY ); |
|
| 74 | - foreach ( $schema_class['children'] as $child ) { |
|
| 75 | - add_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname'] ); |
|
| 73 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY); |
|
| 74 | + foreach ($schema_class['children'] as $child) { |
|
| 75 | + add_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::PARENT_OF_META_KEY, $child['dashname']); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // Update the term name. |
| 79 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY ); |
|
| 80 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name'] ); |
|
| 79 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY); |
|
| 80 | + update_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::NAME_META_KEY, $schema_class['name']); |
|
| 81 | 81 | |
| 82 | 82 | // Update the term URI. |
| 83 | - delete_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY ); |
|
| 84 | - update_term_meta( $term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}" ); |
|
| 83 | + delete_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY); |
|
| 84 | + update_term_meta($term['term_id'], Wordlift_Schemaorg_Class_Service::URI_META_KEY, "http://schema.org/{$schema_class['name']}"); |
|
| 85 | 85 | |
| 86 | 86 | } |
| 87 | 87 | |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | return array( |
| 93 | 93 | 'next' => $next, |
| 94 | 94 | 'limit' => $limit, |
| 95 | - 'complete' => ( 0 === $remaining ), |
|
| 95 | + 'complete' => (0 === $remaining), |
|
| 96 | 96 | 'remaining' => $remaining, |
| 97 | 97 | ); |
| 98 | 98 | } |
@@ -107,10 +107,10 @@ discard block |
||
| 107 | 107 | public function count() { |
| 108 | 108 | |
| 109 | 109 | // Schema Classes count. |
| 110 | - $schema_classes_count = count( $this->get_schema_classes() ); |
|
| 110 | + $schema_classes_count = count($this->get_schema_classes()); |
|
| 111 | 111 | |
| 112 | 112 | // Terms count. |
| 113 | - $terms_count = wp_count_terms( Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME ); |
|
| 113 | + $terms_count = wp_count_terms(Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME); |
|
| 114 | 114 | |
| 115 | 115 | // Return the difference. |
| 116 | 116 | return $schema_classes_count - $terms_count; |
@@ -126,13 +126,13 @@ discard block |
||
| 126 | 126 | private function get_schema_classes() { |
| 127 | 127 | |
| 128 | 128 | // Load the file contents. |
| 129 | - $contents = file_get_contents( dirname( __FILE__ ) . '/schema-classes.json' ); |
|
| 129 | + $contents = file_get_contents(dirname(__FILE__).'/schema-classes.json'); |
|
| 130 | 130 | |
| 131 | 131 | // Decode the JSON contents. |
| 132 | - $json = json_decode( $contents, true ); |
|
| 132 | + $json = json_decode($contents, true); |
|
| 133 | 133 | |
| 134 | 134 | // Return the schema classes or an empty array. |
| 135 | - return isset( $json['schemaClasses'] ) ? $json['schemaClasses'] : array(); |
|
| 135 | + return isset($json['schemaClasses']) ? $json['schemaClasses'] : array(); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | } |
@@ -15,32 +15,32 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | interface Wordlift_Batch_Operation_Interface { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Process the batch operation starting from the specified offset. |
|
| 20 | - * |
|
| 21 | - * @since 3.20.0 |
|
| 22 | - * |
|
| 23 | - * @param int $offset Start from the specified offset (or 0 if not specified). |
|
| 24 | - * @param int $limit Process the specified amount of items per call (or 10 if not specified). |
|
| 25 | - * |
|
| 26 | - * @return array { |
|
| 27 | - * The operation result. |
|
| 28 | - * |
|
| 29 | - * @type int $next The next offset. |
|
| 30 | - * @type int $limit The amount of items to process per call. |
|
| 31 | - * @type int $remaining The remaining number of elements to process. |
|
| 32 | - * @type bool $complete Whether the operation completed. |
|
| 33 | - * } |
|
| 34 | - */ |
|
| 35 | - public function process( $offset = 0, $limit = 10 ); |
|
| 18 | + /** |
|
| 19 | + * Process the batch operation starting from the specified offset. |
|
| 20 | + * |
|
| 21 | + * @since 3.20.0 |
|
| 22 | + * |
|
| 23 | + * @param int $offset Start from the specified offset (or 0 if not specified). |
|
| 24 | + * @param int $limit Process the specified amount of items per call (or 10 if not specified). |
|
| 25 | + * |
|
| 26 | + * @return array { |
|
| 27 | + * The operation result. |
|
| 28 | + * |
|
| 29 | + * @type int $next The next offset. |
|
| 30 | + * @type int $limit The amount of items to process per call. |
|
| 31 | + * @type int $remaining The remaining number of elements to process. |
|
| 32 | + * @type bool $complete Whether the operation completed. |
|
| 33 | + * } |
|
| 34 | + */ |
|
| 35 | + public function process( $offset = 0, $limit = 10 ); |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Count the number of elements that would be affected by the operation. |
|
| 39 | - * |
|
| 40 | - * @since 3.20.0 |
|
| 41 | - * |
|
| 42 | - * @return int The number of elements that would be affected. |
|
| 43 | - */ |
|
| 44 | - public function count(); |
|
| 37 | + /** |
|
| 38 | + * Count the number of elements that would be affected by the operation. |
|
| 39 | + * |
|
| 40 | + * @since 3.20.0 |
|
| 41 | + * |
|
| 42 | + * @return int The number of elements that would be affected. |
|
| 43 | + */ |
|
| 44 | + public function count(); |
|
| 45 | 45 | |
| 46 | 46 | } |
@@ -32,7 +32,7 @@ |
||
| 32 | 32 | * @type bool $complete Whether the operation completed. |
| 33 | 33 | * } |
| 34 | 34 | */ |
| 35 | - public function process( $offset = 0, $limit = 10 ); |
|
| 35 | + public function process($offset = 0, $limit = 10); |
|
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * Count the number of elements that would be affected by the operation. |
@@ -18,34 +18,34 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Post_Schema_Class_Storage extends Wordlift_Storage { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Get the schema class for the specified {@link WP_Post}. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * |
|
| 26 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 27 | - * |
|
| 28 | - * @return string|array An array of schema classes. |
|
| 29 | - */ |
|
| 30 | - public function get( $post_id ) { |
|
| 31 | - |
|
| 32 | - // Get the type names (CamelCase). |
|
| 33 | - $names = Wordlift_Entity_Type_Service::get_instance()->get_names( $post_id ); |
|
| 34 | - |
|
| 35 | - // If we don't find any type use the legacy function to get the URI. |
|
| 36 | - if ( empty( $names ) ) { |
|
| 37 | - $type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 38 | - |
|
| 39 | - return $type['uri']; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - // Prepend the `schema.org` base URI. |
|
| 43 | - $uris = array_map( function( $item ) { |
|
| 44 | - return "http://schema.org/$item"; |
|
| 45 | - }, $names ); |
|
| 46 | - |
|
| 47 | - // Finally return the schema uri. |
|
| 48 | - return 1 === count( $uris ) ? $uris[0] : $uris; |
|
| 49 | - } |
|
| 21 | + /** |
|
| 22 | + * Get the schema class for the specified {@link WP_Post}. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * |
|
| 26 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 27 | + * |
|
| 28 | + * @return string|array An array of schema classes. |
|
| 29 | + */ |
|
| 30 | + public function get( $post_id ) { |
|
| 31 | + |
|
| 32 | + // Get the type names (CamelCase). |
|
| 33 | + $names = Wordlift_Entity_Type_Service::get_instance()->get_names( $post_id ); |
|
| 34 | + |
|
| 35 | + // If we don't find any type use the legacy function to get the URI. |
|
| 36 | + if ( empty( $names ) ) { |
|
| 37 | + $type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 38 | + |
|
| 39 | + return $type['uri']; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + // Prepend the `schema.org` base URI. |
|
| 43 | + $uris = array_map( function( $item ) { |
|
| 44 | + return "http://schema.org/$item"; |
|
| 45 | + }, $names ); |
|
| 46 | + |
|
| 47 | + // Finally return the schema uri. |
|
| 48 | + return 1 === count( $uris ) ? $uris[0] : $uris; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | } |
@@ -27,25 +27,25 @@ |
||
| 27 | 27 | * |
| 28 | 28 | * @return string|array An array of schema classes. |
| 29 | 29 | */ |
| 30 | - public function get( $post_id ) { |
|
| 30 | + public function get($post_id) { |
|
| 31 | 31 | |
| 32 | 32 | // Get the type names (CamelCase). |
| 33 | - $names = Wordlift_Entity_Type_Service::get_instance()->get_names( $post_id ); |
|
| 33 | + $names = Wordlift_Entity_Type_Service::get_instance()->get_names($post_id); |
|
| 34 | 34 | |
| 35 | 35 | // If we don't find any type use the legacy function to get the URI. |
| 36 | - if ( empty( $names ) ) { |
|
| 37 | - $type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 36 | + if (empty($names)) { |
|
| 37 | + $type = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 38 | 38 | |
| 39 | 39 | return $type['uri']; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | // Prepend the `schema.org` base URI. |
| 43 | - $uris = array_map( function( $item ) { |
|
| 43 | + $uris = array_map(function($item) { |
|
| 44 | 44 | return "http://schema.org/$item"; |
| 45 | - }, $names ); |
|
| 45 | + }, $names); |
|
| 46 | 46 | |
| 47 | 47 | // Finally return the schema uri. |
| 48 | - return 1 === count( $uris ) ? $uris[0] : $uris; |
|
| 48 | + return 1 === count($uris) ? $uris[0] : $uris; |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | } |
@@ -18,314 +18,314 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Sparql_Service { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * A {@link Wordlift_Log_Service} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.6.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private static $log; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The {@link Wordlift_Sparql_Service} singleton instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.6.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_Sparql_Service $instance The {@link Wordlift_Sparql_Service} singleton instance. |
|
| 36 | - */ |
|
| 37 | - private static $instance; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Create a {@link Wordlift_Sparql_Service} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.6.0 |
|
| 43 | - */ |
|
| 44 | - public function __construct() { |
|
| 45 | - |
|
| 46 | - self::$log = Wordlift_Log_Service::get_logger( 'Wordlift_Sparql_Service' ); |
|
| 47 | - |
|
| 48 | - self::$instance = $this; |
|
| 49 | - |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Get the singleton instance of the {@link Wordlift_Sparql_Service}. |
|
| 54 | - * |
|
| 55 | - * @since 3.6.0 |
|
| 56 | - * @return \Wordlift_Sparql_Service |
|
| 57 | - */ |
|
| 58 | - public static function get_instance() { |
|
| 59 | - |
|
| 60 | - return self::$instance; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Queue a SPARQL statement for execution. |
|
| 65 | - * |
|
| 66 | - * @since 3.6.0 |
|
| 67 | - * |
|
| 68 | - * @param string $stmt The SPARQL statement. |
|
| 69 | - * @param bool $queue Whether to queue the statement for asynchronous |
|
| 70 | - * execution. |
|
| 71 | - */ |
|
| 72 | - public function execute( $stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 73 | - |
|
| 74 | - rl_execute_sparql_update_query( $stmt, $queue ); |
|
| 75 | - |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Run the SPARQL queries buffered for the specified request id. |
|
| 80 | - * |
|
| 81 | - * @since 3.13.2 |
|
| 82 | - * |
|
| 83 | - * @param string $request_id A unique request id. |
|
| 84 | - */ |
|
| 85 | - public function run_sparql_query( $request_id ) { |
|
| 86 | - |
|
| 87 | - self::$log->debug( "Running SPARQL queries..." ); |
|
| 88 | - |
|
| 89 | - // Look for a free temporary filename. |
|
| 90 | - for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) { |
|
| 91 | - $filename = WL_TEMP_DIR . $request_id . "-$index.sparql"; |
|
| 92 | - |
|
| 93 | - // Bail out if there are no files left. |
|
| 94 | - if ( ! file_exists( $filename ) ) { |
|
| 95 | - self::$log->trace( "$filename not found." ); |
|
| 96 | - |
|
| 97 | - break; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - self::$log->debug( "Running SPARQL from $filename..." ); |
|
| 101 | - |
|
| 102 | - // Get the query saved in the file. |
|
| 103 | - $query = file_get_contents( $filename ); |
|
| 104 | - |
|
| 105 | - // Execute the SPARQL query. |
|
| 106 | - rl_execute_sparql_update_query( $query, false ); |
|
| 107 | - |
|
| 108 | - // Delete the temporary file. |
|
| 109 | - unlink( $filename ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // Reindex the triple store. |
|
| 113 | - wordlift_reindex_triple_store(); |
|
| 114 | - |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Queue a SPARQL statement for asynchronous execution. |
|
| 119 | - * |
|
| 120 | - * @since 3.13.2 |
|
| 121 | - * |
|
| 122 | - * @param string $stmt The SPARQL statement. |
|
| 123 | - * |
|
| 124 | - * @throws Exception |
|
| 125 | - */ |
|
| 126 | - public function queue( $stmt ) { |
|
| 127 | - |
|
| 128 | - // Get a temporary filename. |
|
| 129 | - $filename = $this->get_temporary_file_for_sparql(); |
|
| 130 | - |
|
| 131 | - self::$log->debug( "Buffering SPARQL to file $filename..." ); |
|
| 132 | - |
|
| 133 | - // Write the contents to the temporary filename. |
|
| 134 | - @file_put_contents( $filename, $stmt . "\n", FILE_APPEND ); |
|
| 135 | - |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Get a temporary filename where to store SPARQL queries. |
|
| 140 | - * |
|
| 141 | - * @since 3.13.2 |
|
| 142 | - * |
|
| 143 | - * @return string The filename. |
|
| 144 | - * @throws Exception An exception is thrown if there are already 1.000 |
|
| 145 | - * temporary files for this request. |
|
| 146 | - */ |
|
| 147 | - private function get_temporary_file_for_sparql() { |
|
| 148 | - |
|
| 149 | - // Look for a free temporary filename. |
|
| 150 | - for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) { |
|
| 151 | - $filename = WL_TEMP_DIR . WL_REQUEST_ID . "-$index.sparql"; |
|
| 152 | - |
|
| 153 | - if ( ! file_exists( $filename ) ) { |
|
| 154 | - |
|
| 155 | - // Only if this it the first buffered SPARQL, then launch the |
|
| 156 | - // action which will be handled by the Async Task. The Async |
|
| 157 | - // Task will take care of all the buffered files _on shutdown_. |
|
| 158 | - if ( 1 === $index ) { |
|
| 159 | - do_action( 'wl_run_sparql_query', WL_REQUEST_ID ); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - // Return the temporary filename. |
|
| 163 | - return $filename; |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - throw new Exception( 'Cannot create a temporary file [ ' . WL_TEMP_DIR . WL_REQUEST_ID . ' ].' ); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Execute the SELECT query. |
|
| 172 | - * |
|
| 173 | - * @since 3.12.2 |
|
| 174 | - * |
|
| 175 | - * @param string $query The SELECT query to execute. |
|
| 176 | - * |
|
| 177 | - * @return WP_Error|array The response or WP_Error on failure. |
|
| 178 | - */ |
|
| 179 | - public function select( $query ) { |
|
| 180 | - |
|
| 181 | - // Prepare the SPARQL statement by prepending the default namespaces. |
|
| 182 | - $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 183 | - |
|
| 184 | - // Get the SPARQL SELECT URL. |
|
| 185 | - $url = wl_configuration_get_query_select_url() . urlencode( $sparql ); |
|
| 186 | - |
|
| 187 | - $http_options = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Filter: 'wl_sparql_select_http_args' - Allow third parties to hook and add additional HTTP args. |
|
| 191 | - * |
|
| 192 | - * @since 3.17.0 |
|
| 193 | - * |
|
| 194 | - * @param array $http_options Current http options. |
|
| 195 | - */ |
|
| 196 | - $args = apply_filters( 'wl_sparql_select_http_args', $http_options ); |
|
| 197 | - |
|
| 198 | - return wp_remote_get( $url, $args ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Formats the provided value according to the specified type in order to |
|
| 203 | - * insert the value using SPARQL. The value is also escaped. |
|
| 204 | - * |
|
| 205 | - * @since 3.6.0 |
|
| 206 | - * |
|
| 207 | - * @param string $value The value. |
|
| 208 | - * @param string $type The value type. |
|
| 209 | - * @param string|null $language The language tag or null if not set. |
|
| 210 | - * |
|
| 211 | - * @return string The formatted value for SPARQL statements. |
|
| 212 | - */ |
|
| 213 | - public static function format( $value, $type = null, $language = null ) { |
|
| 214 | - |
|
| 215 | - // see https://www.w3.org/TR/sparql11-query/. |
|
| 216 | - |
|
| 217 | - switch ( $type ) { |
|
| 218 | - |
|
| 219 | - case Wordlift_Schema_Service::DATA_TYPE_BOOLEAN: |
|
| 220 | - // SPARQL supports 'true' and 'false', so we evaluate the $value |
|
| 221 | - // and return true/false accordingly. |
|
| 222 | - return $value ? 'true' : 'false'; |
|
| 223 | - |
|
| 224 | - case Wordlift_Schema_Service::DATA_TYPE_DATE: |
|
| 225 | - $date = date_create_from_format( 'Y/m/d', $value ); |
|
| 226 | - $date_value = date_format( $date, 'Y-m-d' ); |
|
| 227 | - |
|
| 228 | - return sprintf( '"%s"^^xsd:date', self::escape( $date_value ) ); |
|
| 229 | - |
|
| 230 | - case Wordlift_Schema_Service::DATA_TYPE_DATE_TIME: |
|
| 231 | - $date = date_create_from_format( 'Y/m/d H:i', $value ); |
|
| 232 | - $date_value = date_format( $date, 'Y-m-d\TH:i:00' ); |
|
| 233 | - |
|
| 234 | - return sprintf( '"%s"^^xsd:dateTime', self::escape( $date_value ) ); |
|
| 235 | - |
|
| 236 | - case Wordlift_Schema_Service::DATA_TYPE_DURATION: |
|
| 237 | - $time = date_create_from_format( 'H:i', $value ); |
|
| 238 | - $time_value = sprintf( 'PT%dH%dM', date_format( $time, 'H' ), intval( date_format( $time, 'i' ) ) ); |
|
| 239 | - |
|
| 240 | - return sprintf( '"%s"^^xsd:duration', self::escape( $time_value ) ); |
|
| 241 | - |
|
| 242 | - case Wordlift_Schema_Service::DATA_TYPE_DOUBLE: |
|
| 243 | - return sprintf( '"%s"^^xsd:double', self::escape( $value ) ); |
|
| 244 | - |
|
| 245 | - case Wordlift_Schema_Service::DATA_TYPE_INTEGER: |
|
| 246 | - return sprintf( '"%s"^^xsd:integer', self::escape( $value ) ); |
|
| 247 | - |
|
| 248 | - case Wordlift_Schema_Service::DATA_TYPE_STRING: |
|
| 249 | - return sprintf( '"%s"^^xsd:string', self::escape( $value ) ); |
|
| 250 | - |
|
| 251 | - case Wordlift_Schema_Service::DATA_TYPE_URI: |
|
| 252 | - /** |
|
| 253 | - * Allow 3rd parties to change the uri. |
|
| 254 | - * |
|
| 255 | - * @since 3.20.0 |
|
| 256 | - * |
|
| 257 | - * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 258 | - * |
|
| 259 | - * @param string $uri The uri. |
|
| 260 | - */ |
|
| 261 | - return sprintf( '<%s>', self::escape_uri( apply_filters( 'wl_production_uri', $value ) ) ); |
|
| 262 | - |
|
| 263 | - case null: |
|
| 264 | - $language_tag = ( null !== $language ? "@$language" : '' ); |
|
| 265 | - |
|
| 266 | - return sprintf( '"%s"%s', self::escape( $value ), $language_tag ); |
|
| 267 | - |
|
| 268 | - default: |
|
| 269 | - |
|
| 270 | - self::$log->warn( "Unknown data type [ type :: $type ]" ); |
|
| 271 | - |
|
| 272 | - // Try to insert the value anyway. |
|
| 273 | - return sprintf( '"%s"', self::escape( $value ) ); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Escapes an URI for a SPARQL statement. |
|
| 280 | - * |
|
| 281 | - * @since 3.6.0 |
|
| 282 | - * |
|
| 283 | - * @param string $uri The URI to escape. |
|
| 284 | - * |
|
| 285 | - * @return string The escaped URI. |
|
| 286 | - */ |
|
| 287 | - public static function escape_uri( $uri ) { |
|
| 288 | - |
|
| 289 | - // Should we validate the IRI? |
|
| 290 | - // http://www.w3.org/TR/sparql11-query/#QSynIRI |
|
| 291 | - |
|
| 292 | - $uri = str_replace( '<', '\<', $uri ); |
|
| 293 | - $uri = str_replace( '>', '\>', $uri ); |
|
| 294 | - |
|
| 295 | - return $uri; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Escapes a string for a SPARQL statement. |
|
| 300 | - * |
|
| 301 | - * @since 3.6.0 |
|
| 302 | - * |
|
| 303 | - * @param string $string The string to escape. |
|
| 304 | - * |
|
| 305 | - * @return string The escaped string. |
|
| 306 | - */ |
|
| 307 | - public static function escape( $string ) { |
|
| 308 | - |
|
| 309 | - // see http://www.w3.org/TR/rdf-sparql-query/ |
|
| 310 | - // '\t' U+0009 (tab) |
|
| 311 | - // '\n' U+000A (line feed) |
|
| 312 | - // '\r' U+000D (carriage return) |
|
| 313 | - // '\b' U+0008 (backspace) |
|
| 314 | - // '\f' U+000C (form feed) |
|
| 315 | - // '\"' U+0022 (quotation mark, double quote mark) |
|
| 316 | - // "\'" U+0027 (apostrophe-quote, single quote mark) |
|
| 317 | - // '\\' U+005C (backslash) |
|
| 318 | - |
|
| 319 | - $string = str_replace( '\\', '\\\\', $string ); |
|
| 320 | - $string = str_replace( '\'', '\\\'', $string ); |
|
| 321 | - $string = str_replace( '"', '\\"', $string ); |
|
| 322 | - $string = str_replace( "\f", '\\f', $string ); |
|
| 323 | - $string = str_replace( "\b", '\\b', $string ); |
|
| 324 | - $string = str_replace( "\r", '\\r', $string ); |
|
| 325 | - $string = str_replace( "\n", '\\n', $string ); |
|
| 326 | - $string = str_replace( "\t", '\\t', $string ); |
|
| 327 | - |
|
| 328 | - return $string; |
|
| 329 | - } |
|
| 21 | + /** |
|
| 22 | + * A {@link Wordlift_Log_Service} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.6.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private static $log; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The {@link Wordlift_Sparql_Service} singleton instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.6.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_Sparql_Service $instance The {@link Wordlift_Sparql_Service} singleton instance. |
|
| 36 | + */ |
|
| 37 | + private static $instance; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Create a {@link Wordlift_Sparql_Service} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.6.0 |
|
| 43 | + */ |
|
| 44 | + public function __construct() { |
|
| 45 | + |
|
| 46 | + self::$log = Wordlift_Log_Service::get_logger( 'Wordlift_Sparql_Service' ); |
|
| 47 | + |
|
| 48 | + self::$instance = $this; |
|
| 49 | + |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Get the singleton instance of the {@link Wordlift_Sparql_Service}. |
|
| 54 | + * |
|
| 55 | + * @since 3.6.0 |
|
| 56 | + * @return \Wordlift_Sparql_Service |
|
| 57 | + */ |
|
| 58 | + public static function get_instance() { |
|
| 59 | + |
|
| 60 | + return self::$instance; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Queue a SPARQL statement for execution. |
|
| 65 | + * |
|
| 66 | + * @since 3.6.0 |
|
| 67 | + * |
|
| 68 | + * @param string $stmt The SPARQL statement. |
|
| 69 | + * @param bool $queue Whether to queue the statement for asynchronous |
|
| 70 | + * execution. |
|
| 71 | + */ |
|
| 72 | + public function execute( $stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 73 | + |
|
| 74 | + rl_execute_sparql_update_query( $stmt, $queue ); |
|
| 75 | + |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Run the SPARQL queries buffered for the specified request id. |
|
| 80 | + * |
|
| 81 | + * @since 3.13.2 |
|
| 82 | + * |
|
| 83 | + * @param string $request_id A unique request id. |
|
| 84 | + */ |
|
| 85 | + public function run_sparql_query( $request_id ) { |
|
| 86 | + |
|
| 87 | + self::$log->debug( "Running SPARQL queries..." ); |
|
| 88 | + |
|
| 89 | + // Look for a free temporary filename. |
|
| 90 | + for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) { |
|
| 91 | + $filename = WL_TEMP_DIR . $request_id . "-$index.sparql"; |
|
| 92 | + |
|
| 93 | + // Bail out if there are no files left. |
|
| 94 | + if ( ! file_exists( $filename ) ) { |
|
| 95 | + self::$log->trace( "$filename not found." ); |
|
| 96 | + |
|
| 97 | + break; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + self::$log->debug( "Running SPARQL from $filename..." ); |
|
| 101 | + |
|
| 102 | + // Get the query saved in the file. |
|
| 103 | + $query = file_get_contents( $filename ); |
|
| 104 | + |
|
| 105 | + // Execute the SPARQL query. |
|
| 106 | + rl_execute_sparql_update_query( $query, false ); |
|
| 107 | + |
|
| 108 | + // Delete the temporary file. |
|
| 109 | + unlink( $filename ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // Reindex the triple store. |
|
| 113 | + wordlift_reindex_triple_store(); |
|
| 114 | + |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Queue a SPARQL statement for asynchronous execution. |
|
| 119 | + * |
|
| 120 | + * @since 3.13.2 |
|
| 121 | + * |
|
| 122 | + * @param string $stmt The SPARQL statement. |
|
| 123 | + * |
|
| 124 | + * @throws Exception |
|
| 125 | + */ |
|
| 126 | + public function queue( $stmt ) { |
|
| 127 | + |
|
| 128 | + // Get a temporary filename. |
|
| 129 | + $filename = $this->get_temporary_file_for_sparql(); |
|
| 130 | + |
|
| 131 | + self::$log->debug( "Buffering SPARQL to file $filename..." ); |
|
| 132 | + |
|
| 133 | + // Write the contents to the temporary filename. |
|
| 134 | + @file_put_contents( $filename, $stmt . "\n", FILE_APPEND ); |
|
| 135 | + |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Get a temporary filename where to store SPARQL queries. |
|
| 140 | + * |
|
| 141 | + * @since 3.13.2 |
|
| 142 | + * |
|
| 143 | + * @return string The filename. |
|
| 144 | + * @throws Exception An exception is thrown if there are already 1.000 |
|
| 145 | + * temporary files for this request. |
|
| 146 | + */ |
|
| 147 | + private function get_temporary_file_for_sparql() { |
|
| 148 | + |
|
| 149 | + // Look for a free temporary filename. |
|
| 150 | + for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) { |
|
| 151 | + $filename = WL_TEMP_DIR . WL_REQUEST_ID . "-$index.sparql"; |
|
| 152 | + |
|
| 153 | + if ( ! file_exists( $filename ) ) { |
|
| 154 | + |
|
| 155 | + // Only if this it the first buffered SPARQL, then launch the |
|
| 156 | + // action which will be handled by the Async Task. The Async |
|
| 157 | + // Task will take care of all the buffered files _on shutdown_. |
|
| 158 | + if ( 1 === $index ) { |
|
| 159 | + do_action( 'wl_run_sparql_query', WL_REQUEST_ID ); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + // Return the temporary filename. |
|
| 163 | + return $filename; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + throw new Exception( 'Cannot create a temporary file [ ' . WL_TEMP_DIR . WL_REQUEST_ID . ' ].' ); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Execute the SELECT query. |
|
| 172 | + * |
|
| 173 | + * @since 3.12.2 |
|
| 174 | + * |
|
| 175 | + * @param string $query The SELECT query to execute. |
|
| 176 | + * |
|
| 177 | + * @return WP_Error|array The response or WP_Error on failure. |
|
| 178 | + */ |
|
| 179 | + public function select( $query ) { |
|
| 180 | + |
|
| 181 | + // Prepare the SPARQL statement by prepending the default namespaces. |
|
| 182 | + $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 183 | + |
|
| 184 | + // Get the SPARQL SELECT URL. |
|
| 185 | + $url = wl_configuration_get_query_select_url() . urlencode( $sparql ); |
|
| 186 | + |
|
| 187 | + $http_options = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Filter: 'wl_sparql_select_http_args' - Allow third parties to hook and add additional HTTP args. |
|
| 191 | + * |
|
| 192 | + * @since 3.17.0 |
|
| 193 | + * |
|
| 194 | + * @param array $http_options Current http options. |
|
| 195 | + */ |
|
| 196 | + $args = apply_filters( 'wl_sparql_select_http_args', $http_options ); |
|
| 197 | + |
|
| 198 | + return wp_remote_get( $url, $args ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Formats the provided value according to the specified type in order to |
|
| 203 | + * insert the value using SPARQL. The value is also escaped. |
|
| 204 | + * |
|
| 205 | + * @since 3.6.0 |
|
| 206 | + * |
|
| 207 | + * @param string $value The value. |
|
| 208 | + * @param string $type The value type. |
|
| 209 | + * @param string|null $language The language tag or null if not set. |
|
| 210 | + * |
|
| 211 | + * @return string The formatted value for SPARQL statements. |
|
| 212 | + */ |
|
| 213 | + public static function format( $value, $type = null, $language = null ) { |
|
| 214 | + |
|
| 215 | + // see https://www.w3.org/TR/sparql11-query/. |
|
| 216 | + |
|
| 217 | + switch ( $type ) { |
|
| 218 | + |
|
| 219 | + case Wordlift_Schema_Service::DATA_TYPE_BOOLEAN: |
|
| 220 | + // SPARQL supports 'true' and 'false', so we evaluate the $value |
|
| 221 | + // and return true/false accordingly. |
|
| 222 | + return $value ? 'true' : 'false'; |
|
| 223 | + |
|
| 224 | + case Wordlift_Schema_Service::DATA_TYPE_DATE: |
|
| 225 | + $date = date_create_from_format( 'Y/m/d', $value ); |
|
| 226 | + $date_value = date_format( $date, 'Y-m-d' ); |
|
| 227 | + |
|
| 228 | + return sprintf( '"%s"^^xsd:date', self::escape( $date_value ) ); |
|
| 229 | + |
|
| 230 | + case Wordlift_Schema_Service::DATA_TYPE_DATE_TIME: |
|
| 231 | + $date = date_create_from_format( 'Y/m/d H:i', $value ); |
|
| 232 | + $date_value = date_format( $date, 'Y-m-d\TH:i:00' ); |
|
| 233 | + |
|
| 234 | + return sprintf( '"%s"^^xsd:dateTime', self::escape( $date_value ) ); |
|
| 235 | + |
|
| 236 | + case Wordlift_Schema_Service::DATA_TYPE_DURATION: |
|
| 237 | + $time = date_create_from_format( 'H:i', $value ); |
|
| 238 | + $time_value = sprintf( 'PT%dH%dM', date_format( $time, 'H' ), intval( date_format( $time, 'i' ) ) ); |
|
| 239 | + |
|
| 240 | + return sprintf( '"%s"^^xsd:duration', self::escape( $time_value ) ); |
|
| 241 | + |
|
| 242 | + case Wordlift_Schema_Service::DATA_TYPE_DOUBLE: |
|
| 243 | + return sprintf( '"%s"^^xsd:double', self::escape( $value ) ); |
|
| 244 | + |
|
| 245 | + case Wordlift_Schema_Service::DATA_TYPE_INTEGER: |
|
| 246 | + return sprintf( '"%s"^^xsd:integer', self::escape( $value ) ); |
|
| 247 | + |
|
| 248 | + case Wordlift_Schema_Service::DATA_TYPE_STRING: |
|
| 249 | + return sprintf( '"%s"^^xsd:string', self::escape( $value ) ); |
|
| 250 | + |
|
| 251 | + case Wordlift_Schema_Service::DATA_TYPE_URI: |
|
| 252 | + /** |
|
| 253 | + * Allow 3rd parties to change the uri. |
|
| 254 | + * |
|
| 255 | + * @since 3.20.0 |
|
| 256 | + * |
|
| 257 | + * @see https://github.com/insideout10/wordlift-plugin/issues/850 |
|
| 258 | + * |
|
| 259 | + * @param string $uri The uri. |
|
| 260 | + */ |
|
| 261 | + return sprintf( '<%s>', self::escape_uri( apply_filters( 'wl_production_uri', $value ) ) ); |
|
| 262 | + |
|
| 263 | + case null: |
|
| 264 | + $language_tag = ( null !== $language ? "@$language" : '' ); |
|
| 265 | + |
|
| 266 | + return sprintf( '"%s"%s', self::escape( $value ), $language_tag ); |
|
| 267 | + |
|
| 268 | + default: |
|
| 269 | + |
|
| 270 | + self::$log->warn( "Unknown data type [ type :: $type ]" ); |
|
| 271 | + |
|
| 272 | + // Try to insert the value anyway. |
|
| 273 | + return sprintf( '"%s"', self::escape( $value ) ); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Escapes an URI for a SPARQL statement. |
|
| 280 | + * |
|
| 281 | + * @since 3.6.0 |
|
| 282 | + * |
|
| 283 | + * @param string $uri The URI to escape. |
|
| 284 | + * |
|
| 285 | + * @return string The escaped URI. |
|
| 286 | + */ |
|
| 287 | + public static function escape_uri( $uri ) { |
|
| 288 | + |
|
| 289 | + // Should we validate the IRI? |
|
| 290 | + // http://www.w3.org/TR/sparql11-query/#QSynIRI |
|
| 291 | + |
|
| 292 | + $uri = str_replace( '<', '\<', $uri ); |
|
| 293 | + $uri = str_replace( '>', '\>', $uri ); |
|
| 294 | + |
|
| 295 | + return $uri; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Escapes a string for a SPARQL statement. |
|
| 300 | + * |
|
| 301 | + * @since 3.6.0 |
|
| 302 | + * |
|
| 303 | + * @param string $string The string to escape. |
|
| 304 | + * |
|
| 305 | + * @return string The escaped string. |
|
| 306 | + */ |
|
| 307 | + public static function escape( $string ) { |
|
| 308 | + |
|
| 309 | + // see http://www.w3.org/TR/rdf-sparql-query/ |
|
| 310 | + // '\t' U+0009 (tab) |
|
| 311 | + // '\n' U+000A (line feed) |
|
| 312 | + // '\r' U+000D (carriage return) |
|
| 313 | + // '\b' U+0008 (backspace) |
|
| 314 | + // '\f' U+000C (form feed) |
|
| 315 | + // '\"' U+0022 (quotation mark, double quote mark) |
|
| 316 | + // "\'" U+0027 (apostrophe-quote, single quote mark) |
|
| 317 | + // '\\' U+005C (backslash) |
|
| 318 | + |
|
| 319 | + $string = str_replace( '\\', '\\\\', $string ); |
|
| 320 | + $string = str_replace( '\'', '\\\'', $string ); |
|
| 321 | + $string = str_replace( '"', '\\"', $string ); |
|
| 322 | + $string = str_replace( "\f", '\\f', $string ); |
|
| 323 | + $string = str_replace( "\b", '\\b', $string ); |
|
| 324 | + $string = str_replace( "\r", '\\r', $string ); |
|
| 325 | + $string = str_replace( "\n", '\\n', $string ); |
|
| 326 | + $string = str_replace( "\t", '\\t', $string ); |
|
| 327 | + |
|
| 328 | + return $string; |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | 331 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function __construct() { |
| 45 | 45 | |
| 46 | - self::$log = Wordlift_Log_Service::get_logger( 'Wordlift_Sparql_Service' ); |
|
| 46 | + self::$log = Wordlift_Log_Service::get_logger('Wordlift_Sparql_Service'); |
|
| 47 | 47 | |
| 48 | 48 | self::$instance = $this; |
| 49 | 49 | |
@@ -69,9 +69,9 @@ discard block |
||
| 69 | 69 | * @param bool $queue Whether to queue the statement for asynchronous |
| 70 | 70 | * execution. |
| 71 | 71 | */ |
| 72 | - public function execute( $stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING ) { |
|
| 72 | + public function execute($stmt, $queue = WL_ENABLE_SPARQL_UPDATE_QUERIES_BUFFERING) { |
|
| 73 | 73 | |
| 74 | - rl_execute_sparql_update_query( $stmt, $queue ); |
|
| 74 | + rl_execute_sparql_update_query($stmt, $queue); |
|
| 75 | 75 | |
| 76 | 76 | } |
| 77 | 77 | |
@@ -82,31 +82,31 @@ discard block |
||
| 82 | 82 | * |
| 83 | 83 | * @param string $request_id A unique request id. |
| 84 | 84 | */ |
| 85 | - public function run_sparql_query( $request_id ) { |
|
| 85 | + public function run_sparql_query($request_id) { |
|
| 86 | 86 | |
| 87 | - self::$log->debug( "Running SPARQL queries..." ); |
|
| 87 | + self::$log->debug("Running SPARQL queries..."); |
|
| 88 | 88 | |
| 89 | 89 | // Look for a free temporary filename. |
| 90 | - for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) { |
|
| 91 | - $filename = WL_TEMP_DIR . $request_id . "-$index.sparql"; |
|
| 90 | + for ($index = 1; $index < PHP_INT_MAX; $index++) { |
|
| 91 | + $filename = WL_TEMP_DIR.$request_id."-$index.sparql"; |
|
| 92 | 92 | |
| 93 | 93 | // Bail out if there are no files left. |
| 94 | - if ( ! file_exists( $filename ) ) { |
|
| 95 | - self::$log->trace( "$filename not found." ); |
|
| 94 | + if ( ! file_exists($filename)) { |
|
| 95 | + self::$log->trace("$filename not found."); |
|
| 96 | 96 | |
| 97 | 97 | break; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - self::$log->debug( "Running SPARQL from $filename..." ); |
|
| 100 | + self::$log->debug("Running SPARQL from $filename..."); |
|
| 101 | 101 | |
| 102 | 102 | // Get the query saved in the file. |
| 103 | - $query = file_get_contents( $filename ); |
|
| 103 | + $query = file_get_contents($filename); |
|
| 104 | 104 | |
| 105 | 105 | // Execute the SPARQL query. |
| 106 | - rl_execute_sparql_update_query( $query, false ); |
|
| 106 | + rl_execute_sparql_update_query($query, false); |
|
| 107 | 107 | |
| 108 | 108 | // Delete the temporary file. |
| 109 | - unlink( $filename ); |
|
| 109 | + unlink($filename); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // Reindex the triple store. |
@@ -123,15 +123,15 @@ discard block |
||
| 123 | 123 | * |
| 124 | 124 | * @throws Exception |
| 125 | 125 | */ |
| 126 | - public function queue( $stmt ) { |
|
| 126 | + public function queue($stmt) { |
|
| 127 | 127 | |
| 128 | 128 | // Get a temporary filename. |
| 129 | 129 | $filename = $this->get_temporary_file_for_sparql(); |
| 130 | 130 | |
| 131 | - self::$log->debug( "Buffering SPARQL to file $filename..." ); |
|
| 131 | + self::$log->debug("Buffering SPARQL to file $filename..."); |
|
| 132 | 132 | |
| 133 | 133 | // Write the contents to the temporary filename. |
| 134 | - @file_put_contents( $filename, $stmt . "\n", FILE_APPEND ); |
|
| 134 | + @file_put_contents($filename, $stmt."\n", FILE_APPEND); |
|
| 135 | 135 | |
| 136 | 136 | } |
| 137 | 137 | |
@@ -147,16 +147,16 @@ discard block |
||
| 147 | 147 | private function get_temporary_file_for_sparql() { |
| 148 | 148 | |
| 149 | 149 | // Look for a free temporary filename. |
| 150 | - for ( $index = 1; $index < PHP_INT_MAX; $index ++ ) { |
|
| 151 | - $filename = WL_TEMP_DIR . WL_REQUEST_ID . "-$index.sparql"; |
|
| 150 | + for ($index = 1; $index < PHP_INT_MAX; $index++) { |
|
| 151 | + $filename = WL_TEMP_DIR.WL_REQUEST_ID."-$index.sparql"; |
|
| 152 | 152 | |
| 153 | - if ( ! file_exists( $filename ) ) { |
|
| 153 | + if ( ! file_exists($filename)) { |
|
| 154 | 154 | |
| 155 | 155 | // Only if this it the first buffered SPARQL, then launch the |
| 156 | 156 | // action which will be handled by the Async Task. The Async |
| 157 | 157 | // Task will take care of all the buffered files _on shutdown_. |
| 158 | - if ( 1 === $index ) { |
|
| 159 | - do_action( 'wl_run_sparql_query', WL_REQUEST_ID ); |
|
| 158 | + if (1 === $index) { |
|
| 159 | + do_action('wl_run_sparql_query', WL_REQUEST_ID); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | // Return the temporary filename. |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | } |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - throw new Exception( 'Cannot create a temporary file [ ' . WL_TEMP_DIR . WL_REQUEST_ID . ' ].' ); |
|
| 167 | + throw new Exception('Cannot create a temporary file [ '.WL_TEMP_DIR.WL_REQUEST_ID.' ].'); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -176,15 +176,15 @@ discard block |
||
| 176 | 176 | * |
| 177 | 177 | * @return WP_Error|array The response or WP_Error on failure. |
| 178 | 178 | */ |
| 179 | - public function select( $query ) { |
|
| 179 | + public function select($query) { |
|
| 180 | 180 | |
| 181 | 181 | // Prepare the SPARQL statement by prepending the default namespaces. |
| 182 | - $sparql = rl_sparql_prefixes() . "\n" . $query; |
|
| 182 | + $sparql = rl_sparql_prefixes()."\n".$query; |
|
| 183 | 183 | |
| 184 | 184 | // Get the SPARQL SELECT URL. |
| 185 | - $url = wl_configuration_get_query_select_url() . urlencode( $sparql ); |
|
| 185 | + $url = wl_configuration_get_query_select_url().urlencode($sparql); |
|
| 186 | 186 | |
| 187 | - $http_options = unserialize( WL_REDLINK_API_HTTP_OPTIONS ); |
|
| 187 | + $http_options = unserialize(WL_REDLINK_API_HTTP_OPTIONS); |
|
| 188 | 188 | |
| 189 | 189 | /** |
| 190 | 190 | * Filter: 'wl_sparql_select_http_args' - Allow third parties to hook and add additional HTTP args. |
@@ -193,9 +193,9 @@ discard block |
||
| 193 | 193 | * |
| 194 | 194 | * @param array $http_options Current http options. |
| 195 | 195 | */ |
| 196 | - $args = apply_filters( 'wl_sparql_select_http_args', $http_options ); |
|
| 196 | + $args = apply_filters('wl_sparql_select_http_args', $http_options); |
|
| 197 | 197 | |
| 198 | - return wp_remote_get( $url, $args ); |
|
| 198 | + return wp_remote_get($url, $args); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | /** |
@@ -210,11 +210,11 @@ discard block |
||
| 210 | 210 | * |
| 211 | 211 | * @return string The formatted value for SPARQL statements. |
| 212 | 212 | */ |
| 213 | - public static function format( $value, $type = null, $language = null ) { |
|
| 213 | + public static function format($value, $type = null, $language = null) { |
|
| 214 | 214 | |
| 215 | 215 | // see https://www.w3.org/TR/sparql11-query/. |
| 216 | 216 | |
| 217 | - switch ( $type ) { |
|
| 217 | + switch ($type) { |
|
| 218 | 218 | |
| 219 | 219 | case Wordlift_Schema_Service::DATA_TYPE_BOOLEAN: |
| 220 | 220 | // SPARQL supports 'true' and 'false', so we evaluate the $value |
@@ -222,31 +222,31 @@ discard block |
||
| 222 | 222 | return $value ? 'true' : 'false'; |
| 223 | 223 | |
| 224 | 224 | case Wordlift_Schema_Service::DATA_TYPE_DATE: |
| 225 | - $date = date_create_from_format( 'Y/m/d', $value ); |
|
| 226 | - $date_value = date_format( $date, 'Y-m-d' ); |
|
| 225 | + $date = date_create_from_format('Y/m/d', $value); |
|
| 226 | + $date_value = date_format($date, 'Y-m-d'); |
|
| 227 | 227 | |
| 228 | - return sprintf( '"%s"^^xsd:date', self::escape( $date_value ) ); |
|
| 228 | + return sprintf('"%s"^^xsd:date', self::escape($date_value)); |
|
| 229 | 229 | |
| 230 | 230 | case Wordlift_Schema_Service::DATA_TYPE_DATE_TIME: |
| 231 | - $date = date_create_from_format( 'Y/m/d H:i', $value ); |
|
| 232 | - $date_value = date_format( $date, 'Y-m-d\TH:i:00' ); |
|
| 231 | + $date = date_create_from_format('Y/m/d H:i', $value); |
|
| 232 | + $date_value = date_format($date, 'Y-m-d\TH:i:00'); |
|
| 233 | 233 | |
| 234 | - return sprintf( '"%s"^^xsd:dateTime', self::escape( $date_value ) ); |
|
| 234 | + return sprintf('"%s"^^xsd:dateTime', self::escape($date_value)); |
|
| 235 | 235 | |
| 236 | 236 | case Wordlift_Schema_Service::DATA_TYPE_DURATION: |
| 237 | - $time = date_create_from_format( 'H:i', $value ); |
|
| 238 | - $time_value = sprintf( 'PT%dH%dM', date_format( $time, 'H' ), intval( date_format( $time, 'i' ) ) ); |
|
| 237 | + $time = date_create_from_format('H:i', $value); |
|
| 238 | + $time_value = sprintf('PT%dH%dM', date_format($time, 'H'), intval(date_format($time, 'i'))); |
|
| 239 | 239 | |
| 240 | - return sprintf( '"%s"^^xsd:duration', self::escape( $time_value ) ); |
|
| 240 | + return sprintf('"%s"^^xsd:duration', self::escape($time_value)); |
|
| 241 | 241 | |
| 242 | 242 | case Wordlift_Schema_Service::DATA_TYPE_DOUBLE: |
| 243 | - return sprintf( '"%s"^^xsd:double', self::escape( $value ) ); |
|
| 243 | + return sprintf('"%s"^^xsd:double', self::escape($value)); |
|
| 244 | 244 | |
| 245 | 245 | case Wordlift_Schema_Service::DATA_TYPE_INTEGER: |
| 246 | - return sprintf( '"%s"^^xsd:integer', self::escape( $value ) ); |
|
| 246 | + return sprintf('"%s"^^xsd:integer', self::escape($value)); |
|
| 247 | 247 | |
| 248 | 248 | case Wordlift_Schema_Service::DATA_TYPE_STRING: |
| 249 | - return sprintf( '"%s"^^xsd:string', self::escape( $value ) ); |
|
| 249 | + return sprintf('"%s"^^xsd:string', self::escape($value)); |
|
| 250 | 250 | |
| 251 | 251 | case Wordlift_Schema_Service::DATA_TYPE_URI: |
| 252 | 252 | /** |
@@ -258,19 +258,19 @@ discard block |
||
| 258 | 258 | * |
| 259 | 259 | * @param string $uri The uri. |
| 260 | 260 | */ |
| 261 | - return sprintf( '<%s>', self::escape_uri( apply_filters( 'wl_production_uri', $value ) ) ); |
|
| 261 | + return sprintf('<%s>', self::escape_uri(apply_filters('wl_production_uri', $value))); |
|
| 262 | 262 | |
| 263 | 263 | case null: |
| 264 | - $language_tag = ( null !== $language ? "@$language" : '' ); |
|
| 264 | + $language_tag = (null !== $language ? "@$language" : ''); |
|
| 265 | 265 | |
| 266 | - return sprintf( '"%s"%s', self::escape( $value ), $language_tag ); |
|
| 266 | + return sprintf('"%s"%s', self::escape($value), $language_tag); |
|
| 267 | 267 | |
| 268 | 268 | default: |
| 269 | 269 | |
| 270 | - self::$log->warn( "Unknown data type [ type :: $type ]" ); |
|
| 270 | + self::$log->warn("Unknown data type [ type :: $type ]"); |
|
| 271 | 271 | |
| 272 | 272 | // Try to insert the value anyway. |
| 273 | - return sprintf( '"%s"', self::escape( $value ) ); |
|
| 273 | + return sprintf('"%s"', self::escape($value)); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | } |
@@ -284,13 +284,13 @@ discard block |
||
| 284 | 284 | * |
| 285 | 285 | * @return string The escaped URI. |
| 286 | 286 | */ |
| 287 | - public static function escape_uri( $uri ) { |
|
| 287 | + public static function escape_uri($uri) { |
|
| 288 | 288 | |
| 289 | 289 | // Should we validate the IRI? |
| 290 | 290 | // http://www.w3.org/TR/sparql11-query/#QSynIRI |
| 291 | 291 | |
| 292 | - $uri = str_replace( '<', '\<', $uri ); |
|
| 293 | - $uri = str_replace( '>', '\>', $uri ); |
|
| 292 | + $uri = str_replace('<', '\<', $uri); |
|
| 293 | + $uri = str_replace('>', '\>', $uri); |
|
| 294 | 294 | |
| 295 | 295 | return $uri; |
| 296 | 296 | } |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | * |
| 305 | 305 | * @return string The escaped string. |
| 306 | 306 | */ |
| 307 | - public static function escape( $string ) { |
|
| 307 | + public static function escape($string) { |
|
| 308 | 308 | |
| 309 | 309 | // see http://www.w3.org/TR/rdf-sparql-query/ |
| 310 | 310 | // '\t' U+0009 (tab) |
@@ -316,14 +316,14 @@ discard block |
||
| 316 | 316 | // "\'" U+0027 (apostrophe-quote, single quote mark) |
| 317 | 317 | // '\\' U+005C (backslash) |
| 318 | 318 | |
| 319 | - $string = str_replace( '\\', '\\\\', $string ); |
|
| 320 | - $string = str_replace( '\'', '\\\'', $string ); |
|
| 321 | - $string = str_replace( '"', '\\"', $string ); |
|
| 322 | - $string = str_replace( "\f", '\\f', $string ); |
|
| 323 | - $string = str_replace( "\b", '\\b', $string ); |
|
| 324 | - $string = str_replace( "\r", '\\r', $string ); |
|
| 325 | - $string = str_replace( "\n", '\\n', $string ); |
|
| 326 | - $string = str_replace( "\t", '\\t', $string ); |
|
| 319 | + $string = str_replace('\\', '\\\\', $string); |
|
| 320 | + $string = str_replace('\'', '\\\'', $string); |
|
| 321 | + $string = str_replace('"', '\\"', $string); |
|
| 322 | + $string = str_replace("\f", '\\f', $string); |
|
| 323 | + $string = str_replace("\b", '\\b', $string); |
|
| 324 | + $string = str_replace("\r", '\\r', $string); |
|
| 325 | + $string = str_replace("\n", '\\n', $string); |
|
| 326 | + $string = str_replace("\t", '\\t', $string); |
|
| 327 | 327 | |
| 328 | 328 | return $string; |
| 329 | 329 | } |
@@ -14,55 +14,55 @@ |
||
| 14 | 14 | // Mapping options / validations rules used by wl_core_get_posts to perform validation on args |
| 15 | 15 | // The array is serialized because array constants are only from php 5.6 on. |
| 16 | 16 | define( 'WL_CORE_GET_POSTS_VALIDATION_RULES', serialize( array( |
| 17 | - 'get' => array( 'posts', 'post_ids' ), |
|
| 18 | - 'as' => array( 'object', 'subject' ), |
|
| 19 | - 'post_type' => array( 'post', 'entity' ), |
|
| 20 | - 'post_status' => array( 'draft', 'trash', 'publish' ), |
|
| 21 | - 'with_predicate' => array( |
|
| 22 | - WL_WHAT_RELATION, |
|
| 23 | - WL_WHEN_RELATION, |
|
| 24 | - WL_WHERE_RELATION, |
|
| 25 | - WL_WHO_RELATION, |
|
| 26 | - ), |
|
| 17 | + 'get' => array( 'posts', 'post_ids' ), |
|
| 18 | + 'as' => array( 'object', 'subject' ), |
|
| 19 | + 'post_type' => array( 'post', 'entity' ), |
|
| 20 | + 'post_status' => array( 'draft', 'trash', 'publish' ), |
|
| 21 | + 'with_predicate' => array( |
|
| 22 | + WL_WHAT_RELATION, |
|
| 23 | + WL_WHEN_RELATION, |
|
| 24 | + WL_WHERE_RELATION, |
|
| 25 | + WL_WHO_RELATION, |
|
| 26 | + ), |
|
| 27 | 27 | ) ) ); |
| 28 | 28 | |
| 29 | 29 | // Classification boxes configuration for angularjs edit-post widget |
| 30 | 30 | // The array is serialized because array constants are only from php 5.6 on. |
| 31 | 31 | |
| 32 | 32 | define( 'WL_CORE_POST_CLASSIFICATION_BOXES', serialize( array( |
| 33 | - array( |
|
| 34 | - 'id' => WL_WHAT_RELATION, |
|
| 35 | - 'label' => 'What', |
|
| 36 | - 'registeredTypes' => array( |
|
| 37 | - 'thing', |
|
| 38 | - 'creative-work', |
|
| 39 | - 'recipe', |
|
| 40 | - ), |
|
| 41 | - 'selectedEntities' => array(), |
|
| 42 | - ), |
|
| 43 | - array( |
|
| 44 | - 'id' => WL_WHO_RELATION, |
|
| 45 | - 'label' => 'Who', |
|
| 46 | - 'registeredTypes' => array( |
|
| 47 | - 'organization', |
|
| 48 | - 'person', |
|
| 49 | - 'local-business', |
|
| 50 | - 'localbusiness', |
|
| 51 | - ), |
|
| 52 | - 'selectedEntities' => array(), |
|
| 53 | - ), |
|
| 54 | - array( |
|
| 55 | - 'id' => WL_WHERE_RELATION, |
|
| 56 | - 'label' => 'Where', |
|
| 57 | - 'registeredTypes' => array( 'place' ), |
|
| 58 | - 'selectedEntities' => array(), |
|
| 59 | - ), |
|
| 60 | - array( |
|
| 61 | - 'id' => WL_WHEN_RELATION, |
|
| 62 | - 'label' => 'When', |
|
| 63 | - 'registeredTypes' => array( 'event' ), |
|
| 64 | - 'selectedEntities' => array(), |
|
| 65 | - ), |
|
| 33 | + array( |
|
| 34 | + 'id' => WL_WHAT_RELATION, |
|
| 35 | + 'label' => 'What', |
|
| 36 | + 'registeredTypes' => array( |
|
| 37 | + 'thing', |
|
| 38 | + 'creative-work', |
|
| 39 | + 'recipe', |
|
| 40 | + ), |
|
| 41 | + 'selectedEntities' => array(), |
|
| 42 | + ), |
|
| 43 | + array( |
|
| 44 | + 'id' => WL_WHO_RELATION, |
|
| 45 | + 'label' => 'Who', |
|
| 46 | + 'registeredTypes' => array( |
|
| 47 | + 'organization', |
|
| 48 | + 'person', |
|
| 49 | + 'local-business', |
|
| 50 | + 'localbusiness', |
|
| 51 | + ), |
|
| 52 | + 'selectedEntities' => array(), |
|
| 53 | + ), |
|
| 54 | + array( |
|
| 55 | + 'id' => WL_WHERE_RELATION, |
|
| 56 | + 'label' => 'Where', |
|
| 57 | + 'registeredTypes' => array( 'place' ), |
|
| 58 | + 'selectedEntities' => array(), |
|
| 59 | + ), |
|
| 60 | + array( |
|
| 61 | + 'id' => WL_WHEN_RELATION, |
|
| 62 | + 'label' => 'When', |
|
| 63 | + 'registeredTypes' => array( 'event' ), |
|
| 64 | + 'selectedEntities' => array(), |
|
| 65 | + ), |
|
| 66 | 66 | ) ) ); |
| 67 | 67 | |
| 68 | 68 | // Default namespace for wp-json |
@@ -1,63 +1,63 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -define( 'WL_DEFAULT_THUMBNAIL_PATH', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/public/images/missing-image-150x150.png' ); |
|
| 4 | -define( 'WL_DEFAULT_PATH', dirname( dirname( plugin_dir_url( __FILE__ ) ) ) . '/' ); |
|
| 3 | +define('WL_DEFAULT_THUMBNAIL_PATH', dirname(dirname(plugin_dir_url(__FILE__))).'/public/images/missing-image-150x150.png'); |
|
| 4 | +define('WL_DEFAULT_PATH', dirname(dirname(plugin_dir_url(__FILE__))).'/'); |
|
| 5 | 5 | |
| 6 | 6 | // Custom table name |
| 7 | -define( 'WL_DB_RELATION_INSTANCES_TABLE_NAME', 'wl_relation_instances' ); |
|
| 7 | +define('WL_DB_RELATION_INSTANCES_TABLE_NAME', 'wl_relation_instances'); |
|
| 8 | 8 | |
| 9 | -define( 'WL_WHAT_RELATION', 'what' ); |
|
| 10 | -define( 'WL_WHO_RELATION', 'who' ); |
|
| 11 | -define( 'WL_WHERE_RELATION', 'where' ); |
|
| 12 | -define( 'WL_WHEN_RELATION', 'when' ); |
|
| 9 | +define('WL_WHAT_RELATION', 'what'); |
|
| 10 | +define('WL_WHO_RELATION', 'who'); |
|
| 11 | +define('WL_WHERE_RELATION', 'where'); |
|
| 12 | +define('WL_WHEN_RELATION', 'when'); |
|
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * Define wl_mapping_table_name |
| 16 | 16 | * |
| 17 | 17 | * @since 3.25.0 |
| 18 | 18 | */ |
| 19 | -define( 'WL_MAPPING_TABLE_NAME', 'wl_mapping' ); |
|
| 19 | +define('WL_MAPPING_TABLE_NAME', 'wl_mapping'); |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * Define wl_rule_group_table_name |
| 23 | 23 | * |
| 24 | 24 | * @since 3.25.0 |
| 25 | 25 | */ |
| 26 | -define( 'WL_RULE_GROUP_TABLE_NAME', 'wl_mapping_rule_group' ); |
|
| 26 | +define('WL_RULE_GROUP_TABLE_NAME', 'wl_mapping_rule_group'); |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * Define wl_rule_table_name |
| 30 | 30 | * |
| 31 | 31 | * @since 3.25.0 |
| 32 | 32 | */ |
| 33 | -define( 'WL_RULE_TABLE_NAME', 'wl_mapping_rule' ); |
|
| 33 | +define('WL_RULE_TABLE_NAME', 'wl_mapping_rule'); |
|
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | 36 | * Define wl_property_table_name |
| 37 | 37 | * |
| 38 | 38 | * @since 3.25.0 |
| 39 | 39 | */ |
| 40 | -define( 'WL_PROPERTY_TABLE_NAME', 'wl_mapping_property' ); |
|
| 40 | +define('WL_PROPERTY_TABLE_NAME', 'wl_mapping_property'); |
|
| 41 | 41 | |
| 42 | 42 | // Mapping options / validations rules used by wl_core_get_posts to perform validation on args |
| 43 | 43 | // The array is serialized because array constants are only from php 5.6 on. |
| 44 | -define( 'WL_CORE_GET_POSTS_VALIDATION_RULES', serialize( array( |
|
| 45 | - 'get' => array( 'posts', 'post_ids' ), |
|
| 46 | - 'as' => array( 'object', 'subject' ), |
|
| 47 | - 'post_type' => array( 'post', 'entity' ), |
|
| 48 | - 'post_status' => array( 'draft', 'trash', 'publish' ), |
|
| 44 | +define('WL_CORE_GET_POSTS_VALIDATION_RULES', serialize(array( |
|
| 45 | + 'get' => array('posts', 'post_ids'), |
|
| 46 | + 'as' => array('object', 'subject'), |
|
| 47 | + 'post_type' => array('post', 'entity'), |
|
| 48 | + 'post_status' => array('draft', 'trash', 'publish'), |
|
| 49 | 49 | 'with_predicate' => array( |
| 50 | 50 | WL_WHAT_RELATION, |
| 51 | 51 | WL_WHEN_RELATION, |
| 52 | 52 | WL_WHERE_RELATION, |
| 53 | 53 | WL_WHO_RELATION, |
| 54 | 54 | ), |
| 55 | -) ) ); |
|
| 55 | +))); |
|
| 56 | 56 | |
| 57 | 57 | // Classification boxes configuration for angularjs edit-post widget |
| 58 | 58 | // The array is serialized because array constants are only from php 5.6 on. |
| 59 | 59 | |
| 60 | -define( 'WL_CORE_POST_CLASSIFICATION_BOXES', serialize( array( |
|
| 60 | +define('WL_CORE_POST_CLASSIFICATION_BOXES', serialize(array( |
|
| 61 | 61 | array( |
| 62 | 62 | 'id' => WL_WHAT_RELATION, |
| 63 | 63 | 'label' => 'What', |
@@ -82,16 +82,16 @@ discard block |
||
| 82 | 82 | array( |
| 83 | 83 | 'id' => WL_WHERE_RELATION, |
| 84 | 84 | 'label' => 'Where', |
| 85 | - 'registeredTypes' => array( 'place' ), |
|
| 85 | + 'registeredTypes' => array('place'), |
|
| 86 | 86 | 'selectedEntities' => array(), |
| 87 | 87 | ), |
| 88 | 88 | array( |
| 89 | 89 | 'id' => WL_WHEN_RELATION, |
| 90 | 90 | 'label' => 'When', |
| 91 | - 'registeredTypes' => array( 'event' ), |
|
| 91 | + 'registeredTypes' => array('event'), |
|
| 92 | 92 | 'selectedEntities' => array(), |
| 93 | 93 | ), |
| 94 | -) ) ); |
|
| 94 | +))); |
|
| 95 | 95 | |
| 96 | 96 | // Default namespace for wp-json |
| 97 | -define( 'WL_REST_ROUTE_DEFAULT_NAMESPACE', 'wordlift/v1' ); |
|
| 97 | +define('WL_REST_ROUTE_DEFAULT_NAMESPACE', 'wordlift/v1'); |
|
@@ -21,75 +21,75 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_AMP_Service { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The {@link \Wordlift_Jsonld_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.19.1 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Jsonld_Service $jsonld_service The {@link \Wordlift_Jsonld_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $jsonld_service; |
|
| 24 | + /** |
|
| 25 | + * The {@link \Wordlift_Jsonld_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.19.1 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Jsonld_Service $jsonld_service The {@link \Wordlift_Jsonld_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $jsonld_service; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Create a {@link Wordlift_AMP_Service} instance. |
|
| 35 | - * @since 3.19.1 |
|
| 36 | - * |
|
| 37 | - * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 38 | - */ |
|
| 39 | - function __construct( $jsonld_service ) { |
|
| 33 | + /** |
|
| 34 | + * Create a {@link Wordlift_AMP_Service} instance. |
|
| 35 | + * @since 3.19.1 |
|
| 36 | + * |
|
| 37 | + * @param \Wordlift_Jsonld_Service $jsonld_service |
|
| 38 | + */ |
|
| 39 | + function __construct( $jsonld_service ) { |
|
| 40 | 40 | |
| 41 | - $this->jsonld_service = $jsonld_service; |
|
| 41 | + $this->jsonld_service = $jsonld_service; |
|
| 42 | 42 | |
| 43 | - add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin', ) ); |
|
| 44 | - add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata', ), 99, 2 ); |
|
| 43 | + add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin', ) ); |
|
| 44 | + add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata', ), 99, 2 ); |
|
| 45 | 45 | |
| 46 | - } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Register the `entity` post type with the AMP plugin. |
|
| 50 | - * |
|
| 51 | - * @since 3.12.0 |
|
| 52 | - */ |
|
| 53 | - function register_entity_cpt_with_amp_plugin() { |
|
| 48 | + /** |
|
| 49 | + * Register the `entity` post type with the AMP plugin. |
|
| 50 | + * |
|
| 51 | + * @since 3.12.0 |
|
| 52 | + */ |
|
| 53 | + function register_entity_cpt_with_amp_plugin() { |
|
| 54 | 54 | |
| 55 | - if ( ! defined( 'AMP_QUERY_VAR' ) ) { |
|
| 56 | - return; |
|
| 57 | - } |
|
| 55 | + if ( ! defined( 'AMP_QUERY_VAR' ) ) { |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) { |
|
| 60 | - // Do not change anything for posts and pages. |
|
| 61 | - if ( 'post' === $post_type || 'page' === $post_type ) { |
|
| 62 | - continue; |
|
| 63 | - } |
|
| 64 | - add_post_type_support( $post_type, AMP_QUERY_VAR ); |
|
| 65 | - } |
|
| 59 | + foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) { |
|
| 60 | + // Do not change anything for posts and pages. |
|
| 61 | + if ( 'post' === $post_type || 'page' === $post_type ) { |
|
| 62 | + continue; |
|
| 63 | + } |
|
| 64 | + add_post_type_support( $post_type, AMP_QUERY_VAR ); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Filters Schema.org metadata for a post. |
|
| 71 | - * |
|
| 72 | - * @since 3.19.1 |
|
| 73 | - * |
|
| 74 | - * @param array $metadata Metadata. |
|
| 75 | - * @param WP_Post $post Post. |
|
| 76 | - * |
|
| 77 | - * @return array Return WordLift's generated JSON-LD. |
|
| 78 | - */ |
|
| 79 | - function amp_post_template_metadata( $metadata, $post ) { |
|
| 69 | + /** |
|
| 70 | + * Filters Schema.org metadata for a post. |
|
| 71 | + * |
|
| 72 | + * @since 3.19.1 |
|
| 73 | + * |
|
| 74 | + * @param array $metadata Metadata. |
|
| 75 | + * @param WP_Post $post Post. |
|
| 76 | + * |
|
| 77 | + * @return array Return WordLift's generated JSON-LD. |
|
| 78 | + */ |
|
| 79 | + function amp_post_template_metadata( $metadata, $post ) { |
|
| 80 | 80 | |
| 81 | - return $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 82 | - } |
|
| 81 | + return $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Check if current page is amp endpoint. |
|
| 86 | - * |
|
| 87 | - * @since 3.20.0 |
|
| 88 | - * |
|
| 89 | - * @return bool |
|
| 90 | - */ |
|
| 91 | - public static function is_amp_endpoint() { |
|
| 92 | - return function_exists('is_amp_endpoint') && is_amp_endpoint(); |
|
| 93 | - } |
|
| 84 | + /** |
|
| 85 | + * Check if current page is amp endpoint. |
|
| 86 | + * |
|
| 87 | + * @since 3.20.0 |
|
| 88 | + * |
|
| 89 | + * @return bool |
|
| 90 | + */ |
|
| 91 | + public static function is_amp_endpoint() { |
|
| 92 | + return function_exists('is_amp_endpoint') && is_amp_endpoint(); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | } |
@@ -36,12 +36,12 @@ discard block |
||
| 36 | 36 | * |
| 37 | 37 | * @param \Wordlift_Jsonld_Service $jsonld_service |
| 38 | 38 | */ |
| 39 | - function __construct( $jsonld_service ) { |
|
| 39 | + function __construct($jsonld_service) { |
|
| 40 | 40 | |
| 41 | 41 | $this->jsonld_service = $jsonld_service; |
| 42 | 42 | |
| 43 | - add_action( 'amp_init', array( $this, 'register_entity_cpt_with_amp_plugin', ) ); |
|
| 44 | - add_filter( 'amp_post_template_metadata', array( $this, 'amp_post_template_metadata', ), 99, 2 ); |
|
| 43 | + add_action('amp_init', array($this, 'register_entity_cpt_with_amp_plugin',)); |
|
| 44 | + add_filter('amp_post_template_metadata', array($this, 'amp_post_template_metadata',), 99, 2); |
|
| 45 | 45 | |
| 46 | 46 | } |
| 47 | 47 | |
@@ -52,16 +52,16 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | function register_entity_cpt_with_amp_plugin() { |
| 54 | 54 | |
| 55 | - if ( ! defined( 'AMP_QUERY_VAR' ) ) { |
|
| 55 | + if ( ! defined('AMP_QUERY_VAR')) { |
|
| 56 | 56 | return; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - foreach ( Wordlift_Entity_Service::valid_entity_post_types() as $post_type ) { |
|
| 59 | + foreach (Wordlift_Entity_Service::valid_entity_post_types() as $post_type) { |
|
| 60 | 60 | // Do not change anything for posts and pages. |
| 61 | - if ( 'post' === $post_type || 'page' === $post_type ) { |
|
| 61 | + if ('post' === $post_type || 'page' === $post_type) { |
|
| 62 | 62 | continue; |
| 63 | 63 | } |
| 64 | - add_post_type_support( $post_type, AMP_QUERY_VAR ); |
|
| 64 | + add_post_type_support($post_type, AMP_QUERY_VAR); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | } |
@@ -76,9 +76,9 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @return array Return WordLift's generated JSON-LD. |
| 78 | 78 | */ |
| 79 | - function amp_post_template_metadata( $metadata, $post ) { |
|
| 79 | + function amp_post_template_metadata($metadata, $post) { |
|
| 80 | 80 | |
| 81 | - return $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
| 81 | + return $this->jsonld_service->get_jsonld(false, $post->ID); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -25,219 +25,219 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class Wordlift_Entity_Link_Service { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The entity type service. |
|
| 30 | - * |
|
| 31 | - * @since 3.6.0 |
|
| 32 | - * @access private |
|
| 33 | - * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service. |
|
| 34 | - */ |
|
| 35 | - private $entity_type_service; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The entity post type slug. |
|
| 39 | - * |
|
| 40 | - * @since 3.6.0 |
|
| 41 | - * @access private |
|
| 42 | - * @var string $slug The entity post type slug. |
|
| 43 | - */ |
|
| 44 | - private $slug; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * A logger instance. |
|
| 48 | - * |
|
| 49 | - * @since 3.6.0 |
|
| 50 | - * @access private |
|
| 51 | - * @var Wordlift_Log_Service |
|
| 52 | - */ |
|
| 53 | - private $log; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Wordlift_Entity_Link_Service constructor. |
|
| 57 | - * |
|
| 58 | - * @since 3.6.0 |
|
| 59 | - * |
|
| 60 | - * @param Wordlift_Entity_Post_Type_Service $entity_type_service |
|
| 61 | - * @param string $slug The entity post type slug. |
|
| 62 | - */ |
|
| 63 | - public function __construct( $entity_type_service, $slug ) { |
|
| 64 | - |
|
| 65 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' ); |
|
| 66 | - |
|
| 67 | - $this->entity_type_service = $entity_type_service; |
|
| 68 | - $this->slug = $slug; |
|
| 69 | - |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Intercept link generation to posts in order to customize links to entities. |
|
| 74 | - * |
|
| 75 | - * @since 3.6.0 |
|
| 76 | - * |
|
| 77 | - * @param string $post_link The post's permalink. |
|
| 78 | - * @param WP_Post $post The post in question. |
|
| 79 | - * @param bool $leavename Whether to keep the post name. |
|
| 80 | - * @param bool $sample Is it a sample permalink. |
|
| 81 | - * |
|
| 82 | - * @return string The link to the post. |
|
| 83 | - */ |
|
| 84 | - public function post_type_link( $post_link, $post, $leavename, $sample ) { |
|
| 85 | - |
|
| 86 | - // Return the post link if this is not our post type. |
|
| 87 | - if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) { |
|
| 88 | - return $post_link; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Replace /slug/post_name/ with /post_name/ |
|
| 92 | - // The slug comes from the Entity Type Service since that service is responsible for registering the default |
|
| 93 | - // slug. |
|
| 94 | - return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Alter the query to look for our own custom type. |
|
| 99 | - * |
|
| 100 | - * @since 3.6.0 |
|
| 101 | - * |
|
| 102 | - * @param WP_Query $query |
|
| 103 | - */ |
|
| 104 | - public function pre_get_posts( $query ) { |
|
| 105 | - |
|
| 106 | - // If a slug has been set, we don't need to alter the query. |
|
| 107 | - if ( ! empty( $this->slug ) ) { |
|
| 108 | - return; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - // Check if it's a query we should extend with our own custom post type. |
|
| 112 | - // |
|
| 113 | - // The `$query->query` count could be > 2 if the preview parameter is passed too. |
|
| 114 | - // |
|
| 115 | - // See https://github.com/insideout10/wordlift-plugin/issues/439 |
|
| 116 | - if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) { |
|
| 117 | - return; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // Add our own post type to the query. |
|
| 121 | - $post_types = '' === $query->get( 'post_type' ) |
|
| 122 | - ? Wordlift_Entity_Service::valid_entity_post_types() |
|
| 123 | - : array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() ); |
|
| 124 | - $query->set( 'post_type', $post_types ); |
|
| 125 | - |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved. |
|
| 130 | - * |
|
| 131 | - * @since 3.6.0 |
|
| 132 | - * |
|
| 133 | - * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
| 134 | - * @param string $slug The post slug. |
|
| 135 | - * @param string $post_type Post type. |
|
| 136 | - * |
|
| 137 | - * @return bool Whether the slug is bad. |
|
| 138 | - */ |
|
| 139 | - public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) { |
|
| 140 | - |
|
| 141 | - // The list of post types that might have conflicting slugs. |
|
| 142 | - $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 143 | - |
|
| 144 | - // Ignore post types different from the ones we need to check. |
|
| 145 | - if ( ! in_array( $post_type, $post_types ) ) { |
|
| 146 | - return $bad_slug; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // We remove the request post type since WordPress is already checking that the slug doesn't conflict. |
|
| 150 | - $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) ); |
|
| 151 | - |
|
| 152 | - $this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' ); |
|
| 153 | - |
|
| 154 | - return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Hook to WordPress' wp_unique_post_slug_is_bad_hierarchical_slug filter. This is called when a page is saved. |
|
| 159 | - * |
|
| 160 | - * @since 3.6.0 |
|
| 161 | - * |
|
| 162 | - * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
| 163 | - * @param string $slug The post slug. |
|
| 164 | - * @param string $post_type Post type. |
|
| 165 | - * @param int $post_parent |
|
| 166 | - * |
|
| 167 | - * @return bool Whether the slug is bad. |
|
| 168 | - */ |
|
| 169 | - public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) { |
|
| 170 | - |
|
| 171 | - // We only care about pages here. |
|
| 172 | - if ( 'page' !== $post_type ) { |
|
| 173 | - return $bad_slug; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue |
|
| 177 | - // about overlapping slugs among pages and posts: |
|
| 178 | - // https://core.trac.wordpress.org/ticket/13459 |
|
| 179 | - return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Check whether a slug exists already for the specified post types. |
|
| 184 | - * |
|
| 185 | - * @since 3.6.0 |
|
| 186 | - * |
|
| 187 | - * @param string $slug The slug. |
|
| 188 | - * @param array $post_types An array of post types. |
|
| 189 | - * |
|
| 190 | - * @return bool True if the slug exists, otherwise false. |
|
| 191 | - */ |
|
| 192 | - private function slug_exists( $slug, $post_types ) { |
|
| 193 | - global $wpdb; |
|
| 194 | - |
|
| 195 | - // Loop through all post types and check |
|
| 196 | - // whether they have archive pages and if |
|
| 197 | - // the archive slug matches the post slug. |
|
| 198 | - // |
|
| 199 | - // Note that the condition below checks only post types used by WordLift. |
|
| 200 | - // We don't check other post types for archive pages, |
|
| 201 | - // because this is a job of WordPress. |
|
| 202 | - // |
|
| 203 | - // There is a open ticket that should solve this, when it's merged: |
|
| 204 | - // https://core.trac.wordpress.org/ticket/13459 |
|
| 205 | - $all_post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 206 | - foreach ( $all_post_types as $post_type ) { |
|
| 207 | - |
|
| 208 | - // Get the post type object for current post type. |
|
| 209 | - $post_type_object = get_post_type_object( $post_type ); |
|
| 210 | - |
|
| 211 | - if ( |
|
| 212 | - // Check whether the post type object is not empty. |
|
| 213 | - ! empty( $post_type_object ) && |
|
| 214 | - // And the post type has archive page. |
|
| 215 | - $post_type_object->has_archive && |
|
| 216 | - // And `rewrite` options exists.. |
|
| 217 | - ! empty( $post_type_object->rewrite ) && |
|
| 218 | - // And the `rewrite` slug property is not empty. |
|
| 219 | - ! empty( $post_type_object->rewrite['slug'] ) && |
|
| 220 | - // And if the rewrite slug equals to the slug. |
|
| 221 | - $post_type_object->rewrite['slug'] === $slug |
|
| 222 | - ) { |
|
| 223 | - // Return true which means that the slug is already in use. |
|
| 224 | - return true; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - // Post slugs must be unique across all posts. |
|
| 230 | - $check_sql = $wpdb->prepare( |
|
| 231 | - "SELECT post_name |
|
| 28 | + /** |
|
| 29 | + * The entity type service. |
|
| 30 | + * |
|
| 31 | + * @since 3.6.0 |
|
| 32 | + * @access private |
|
| 33 | + * @var Wordlift_Entity_Post_Type_Service $entity_type_service The entity type service. |
|
| 34 | + */ |
|
| 35 | + private $entity_type_service; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The entity post type slug. |
|
| 39 | + * |
|
| 40 | + * @since 3.6.0 |
|
| 41 | + * @access private |
|
| 42 | + * @var string $slug The entity post type slug. |
|
| 43 | + */ |
|
| 44 | + private $slug; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * A logger instance. |
|
| 48 | + * |
|
| 49 | + * @since 3.6.0 |
|
| 50 | + * @access private |
|
| 51 | + * @var Wordlift_Log_Service |
|
| 52 | + */ |
|
| 53 | + private $log; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Wordlift_Entity_Link_Service constructor. |
|
| 57 | + * |
|
| 58 | + * @since 3.6.0 |
|
| 59 | + * |
|
| 60 | + * @param Wordlift_Entity_Post_Type_Service $entity_type_service |
|
| 61 | + * @param string $slug The entity post type slug. |
|
| 62 | + */ |
|
| 63 | + public function __construct( $entity_type_service, $slug ) { |
|
| 64 | + |
|
| 65 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' ); |
|
| 66 | + |
|
| 67 | + $this->entity_type_service = $entity_type_service; |
|
| 68 | + $this->slug = $slug; |
|
| 69 | + |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Intercept link generation to posts in order to customize links to entities. |
|
| 74 | + * |
|
| 75 | + * @since 3.6.0 |
|
| 76 | + * |
|
| 77 | + * @param string $post_link The post's permalink. |
|
| 78 | + * @param WP_Post $post The post in question. |
|
| 79 | + * @param bool $leavename Whether to keep the post name. |
|
| 80 | + * @param bool $sample Is it a sample permalink. |
|
| 81 | + * |
|
| 82 | + * @return string The link to the post. |
|
| 83 | + */ |
|
| 84 | + public function post_type_link( $post_link, $post, $leavename, $sample ) { |
|
| 85 | + |
|
| 86 | + // Return the post link if this is not our post type. |
|
| 87 | + if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) { |
|
| 88 | + return $post_link; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Replace /slug/post_name/ with /post_name/ |
|
| 92 | + // The slug comes from the Entity Type Service since that service is responsible for registering the default |
|
| 93 | + // slug. |
|
| 94 | + return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Alter the query to look for our own custom type. |
|
| 99 | + * |
|
| 100 | + * @since 3.6.0 |
|
| 101 | + * |
|
| 102 | + * @param WP_Query $query |
|
| 103 | + */ |
|
| 104 | + public function pre_get_posts( $query ) { |
|
| 105 | + |
|
| 106 | + // If a slug has been set, we don't need to alter the query. |
|
| 107 | + if ( ! empty( $this->slug ) ) { |
|
| 108 | + return; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + // Check if it's a query we should extend with our own custom post type. |
|
| 112 | + // |
|
| 113 | + // The `$query->query` count could be > 2 if the preview parameter is passed too. |
|
| 114 | + // |
|
| 115 | + // See https://github.com/insideout10/wordlift-plugin/issues/439 |
|
| 116 | + if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) { |
|
| 117 | + return; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // Add our own post type to the query. |
|
| 121 | + $post_types = '' === $query->get( 'post_type' ) |
|
| 122 | + ? Wordlift_Entity_Service::valid_entity_post_types() |
|
| 123 | + : array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() ); |
|
| 124 | + $query->set( 'post_type', $post_types ); |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Hook to WordPress' wp_unique_post_slug_is_bad_flat_slug filter. This is called when a page is saved. |
|
| 130 | + * |
|
| 131 | + * @since 3.6.0 |
|
| 132 | + * |
|
| 133 | + * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
| 134 | + * @param string $slug The post slug. |
|
| 135 | + * @param string $post_type Post type. |
|
| 136 | + * |
|
| 137 | + * @return bool Whether the slug is bad. |
|
| 138 | + */ |
|
| 139 | + public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) { |
|
| 140 | + |
|
| 141 | + // The list of post types that might have conflicting slugs. |
|
| 142 | + $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 143 | + |
|
| 144 | + // Ignore post types different from the ones we need to check. |
|
| 145 | + if ( ! in_array( $post_type, $post_types ) ) { |
|
| 146 | + return $bad_slug; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // We remove the request post type since WordPress is already checking that the slug doesn't conflict. |
|
| 150 | + $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) ); |
|
| 151 | + |
|
| 152 | + $this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' ); |
|
| 153 | + |
|
| 154 | + return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Hook to WordPress' wp_unique_post_slug_is_bad_hierarchical_slug filter. This is called when a page is saved. |
|
| 159 | + * |
|
| 160 | + * @since 3.6.0 |
|
| 161 | + * |
|
| 162 | + * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
| 163 | + * @param string $slug The post slug. |
|
| 164 | + * @param string $post_type Post type. |
|
| 165 | + * @param int $post_parent |
|
| 166 | + * |
|
| 167 | + * @return bool Whether the slug is bad. |
|
| 168 | + */ |
|
| 169 | + public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) { |
|
| 170 | + |
|
| 171 | + // We only care about pages here. |
|
| 172 | + if ( 'page' !== $post_type ) { |
|
| 173 | + return $bad_slug; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue |
|
| 177 | + // about overlapping slugs among pages and posts: |
|
| 178 | + // https://core.trac.wordpress.org/ticket/13459 |
|
| 179 | + return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Check whether a slug exists already for the specified post types. |
|
| 184 | + * |
|
| 185 | + * @since 3.6.0 |
|
| 186 | + * |
|
| 187 | + * @param string $slug The slug. |
|
| 188 | + * @param array $post_types An array of post types. |
|
| 189 | + * |
|
| 190 | + * @return bool True if the slug exists, otherwise false. |
|
| 191 | + */ |
|
| 192 | + private function slug_exists( $slug, $post_types ) { |
|
| 193 | + global $wpdb; |
|
| 194 | + |
|
| 195 | + // Loop through all post types and check |
|
| 196 | + // whether they have archive pages and if |
|
| 197 | + // the archive slug matches the post slug. |
|
| 198 | + // |
|
| 199 | + // Note that the condition below checks only post types used by WordLift. |
|
| 200 | + // We don't check other post types for archive pages, |
|
| 201 | + // because this is a job of WordPress. |
|
| 202 | + // |
|
| 203 | + // There is a open ticket that should solve this, when it's merged: |
|
| 204 | + // https://core.trac.wordpress.org/ticket/13459 |
|
| 205 | + $all_post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
|
| 206 | + foreach ( $all_post_types as $post_type ) { |
|
| 207 | + |
|
| 208 | + // Get the post type object for current post type. |
|
| 209 | + $post_type_object = get_post_type_object( $post_type ); |
|
| 210 | + |
|
| 211 | + if ( |
|
| 212 | + // Check whether the post type object is not empty. |
|
| 213 | + ! empty( $post_type_object ) && |
|
| 214 | + // And the post type has archive page. |
|
| 215 | + $post_type_object->has_archive && |
|
| 216 | + // And `rewrite` options exists.. |
|
| 217 | + ! empty( $post_type_object->rewrite ) && |
|
| 218 | + // And the `rewrite` slug property is not empty. |
|
| 219 | + ! empty( $post_type_object->rewrite['slug'] ) && |
|
| 220 | + // And if the rewrite slug equals to the slug. |
|
| 221 | + $post_type_object->rewrite['slug'] === $slug |
|
| 222 | + ) { |
|
| 223 | + // Return true which means that the slug is already in use. |
|
| 224 | + return true; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + // Post slugs must be unique across all posts. |
|
| 230 | + $check_sql = $wpdb->prepare( |
|
| 231 | + "SELECT post_name |
|
| 232 | 232 | FROM $wpdb->posts |
| 233 | 233 | WHERE post_name = %s |
| 234 | 234 | AND post_type IN ('" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "') |
| 235 | 235 | LIMIT 1 |
| 236 | 236 | ", |
| 237 | - $slug |
|
| 238 | - ); |
|
| 237 | + $slug |
|
| 238 | + ); |
|
| 239 | 239 | |
| 240 | - return null !== $wpdb->get_var( $check_sql ); |
|
| 241 | - } |
|
| 240 | + return null !== $wpdb->get_var( $check_sql ); |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | 243 | } |
@@ -60,9 +60,9 @@ discard block |
||
| 60 | 60 | * @param Wordlift_Entity_Post_Type_Service $entity_type_service |
| 61 | 61 | * @param string $slug The entity post type slug. |
| 62 | 62 | */ |
| 63 | - public function __construct( $entity_type_service, $slug ) { |
|
| 63 | + public function __construct($entity_type_service, $slug) { |
|
| 64 | 64 | |
| 65 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Entity_Link_Service' ); |
|
| 65 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Entity_Link_Service'); |
|
| 66 | 66 | |
| 67 | 67 | $this->entity_type_service = $entity_type_service; |
| 68 | 68 | $this->slug = $slug; |
@@ -81,17 +81,17 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @return string The link to the post. |
| 83 | 83 | */ |
| 84 | - public function post_type_link( $post_link, $post, $leavename, $sample ) { |
|
| 84 | + public function post_type_link($post_link, $post, $leavename, $sample) { |
|
| 85 | 85 | |
| 86 | 86 | // Return the post link if this is not our post type. |
| 87 | - if ( ! empty( $this->slug ) || $this->entity_type_service->get_post_type() !== get_post_type( $post ) ) { |
|
| 87 | + if ( ! empty($this->slug) || $this->entity_type_service->get_post_type() !== get_post_type($post)) { |
|
| 88 | 88 | return $post_link; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | // Replace /slug/post_name/ with /post_name/ |
| 92 | 92 | // The slug comes from the Entity Type Service since that service is responsible for registering the default |
| 93 | 93 | // slug. |
| 94 | - return str_replace( "/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link ); |
|
| 94 | + return str_replace("/{$this->entity_type_service->get_slug()}/$post->post_name/", "/$post->post_name/", $post_link); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -101,10 +101,10 @@ discard block |
||
| 101 | 101 | * |
| 102 | 102 | * @param WP_Query $query |
| 103 | 103 | */ |
| 104 | - public function pre_get_posts( $query ) { |
|
| 104 | + public function pre_get_posts($query) { |
|
| 105 | 105 | |
| 106 | 106 | // If a slug has been set, we don't need to alter the query. |
| 107 | - if ( ! empty( $this->slug ) ) { |
|
| 107 | + if ( ! empty($this->slug)) { |
|
| 108 | 108 | return; |
| 109 | 109 | } |
| 110 | 110 | |
@@ -113,15 +113,15 @@ discard block |
||
| 113 | 113 | // The `$query->query` count could be > 2 if the preview parameter is passed too. |
| 114 | 114 | // |
| 115 | 115 | // See https://github.com/insideout10/wordlift-plugin/issues/439 |
| 116 | - if ( ! $query->is_main_query() || 2 > count( $query->query ) || ! isset( $query->query['page'] ) || empty( $query->query['name'] ) ) { |
|
| 116 | + if ( ! $query->is_main_query() || 2 > count($query->query) || ! isset($query->query['page']) || empty($query->query['name'])) { |
|
| 117 | 117 | return; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // Add our own post type to the query. |
| 121 | - $post_types = '' === $query->get( 'post_type' ) |
|
| 121 | + $post_types = '' === $query->get('post_type') |
|
| 122 | 122 | ? Wordlift_Entity_Service::valid_entity_post_types() |
| 123 | - : array_merge( (array) $query->get( 'post_type' ), (array) $this->entity_type_service->get_post_type() ); |
|
| 124 | - $query->set( 'post_type', $post_types ); |
|
| 123 | + : array_merge((array) $query->get('post_type'), (array) $this->entity_type_service->get_post_type()); |
|
| 124 | + $query->set('post_type', $post_types); |
|
| 125 | 125 | |
| 126 | 126 | } |
| 127 | 127 | |
@@ -136,22 +136,22 @@ discard block |
||
| 136 | 136 | * |
| 137 | 137 | * @return bool Whether the slug is bad. |
| 138 | 138 | */ |
| 139 | - public function wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ) { |
|
| 139 | + public function wp_unique_post_slug_is_bad_flat_slug($bad_slug, $slug, $post_type) { |
|
| 140 | 140 | |
| 141 | 141 | // The list of post types that might have conflicting slugs. |
| 142 | 142 | $post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 143 | 143 | |
| 144 | 144 | // Ignore post types different from the ones we need to check. |
| 145 | - if ( ! in_array( $post_type, $post_types ) ) { |
|
| 145 | + if ( ! in_array($post_type, $post_types)) { |
|
| 146 | 146 | return $bad_slug; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | // We remove the request post type since WordPress is already checking that the slug doesn't conflict. |
| 150 | - $exists = $this->slug_exists( $slug, array_diff( $post_types, array( $post_type ) ) ); |
|
| 150 | + $exists = $this->slug_exists($slug, array_diff($post_types, array($post_type))); |
|
| 151 | 151 | |
| 152 | - $this->log->debug( "Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: " . ( $exists ? 'yes' : 'no' ) . ' ]' ); |
|
| 152 | + $this->log->debug("Checking if a slug exists [ post type :: $post_type ][ slug :: $slug ][ exists :: ".($exists ? 'yes' : 'no').' ]'); |
|
| 153 | 153 | |
| 154 | - return apply_filters( 'wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type ); |
|
| 154 | + return apply_filters('wl_unique_post_slug_is_bad_flat_slug', $exists, $bad_slug, $slug, $post_type); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -166,17 +166,17 @@ discard block |
||
| 166 | 166 | * |
| 167 | 167 | * @return bool Whether the slug is bad. |
| 168 | 168 | */ |
| 169 | - public function wp_unique_post_slug_is_bad_hierarchical_slug( $bad_slug, $slug, $post_type, $post_parent ) { |
|
| 169 | + public function wp_unique_post_slug_is_bad_hierarchical_slug($bad_slug, $slug, $post_type, $post_parent) { |
|
| 170 | 170 | |
| 171 | 171 | // We only care about pages here. |
| 172 | - if ( 'page' !== $post_type ) { |
|
| 172 | + if ('page' !== $post_type) { |
|
| 173 | 173 | return $bad_slug; |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | // We redirect the call to the flat hook, this means that this check is going to solve also the 6-years old issue |
| 177 | 177 | // about overlapping slugs among pages and posts: |
| 178 | 178 | // https://core.trac.wordpress.org/ticket/13459 |
| 179 | - return $this->wp_unique_post_slug_is_bad_flat_slug( $bad_slug, $slug, $post_type ); |
|
| 179 | + return $this->wp_unique_post_slug_is_bad_flat_slug($bad_slug, $slug, $post_type); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | * |
| 190 | 190 | * @return bool True if the slug exists, otherwise false. |
| 191 | 191 | */ |
| 192 | - private function slug_exists( $slug, $post_types ) { |
|
| 192 | + private function slug_exists($slug, $post_types) { |
|
| 193 | 193 | global $wpdb; |
| 194 | 194 | |
| 195 | 195 | // Loop through all post types and check |
@@ -203,20 +203,20 @@ discard block |
||
| 203 | 203 | // There is a open ticket that should solve this, when it's merged: |
| 204 | 204 | // https://core.trac.wordpress.org/ticket/13459 |
| 205 | 205 | $all_post_types = Wordlift_Entity_Service::valid_entity_post_types(); |
| 206 | - foreach ( $all_post_types as $post_type ) { |
|
| 206 | + foreach ($all_post_types as $post_type) { |
|
| 207 | 207 | |
| 208 | 208 | // Get the post type object for current post type. |
| 209 | - $post_type_object = get_post_type_object( $post_type ); |
|
| 209 | + $post_type_object = get_post_type_object($post_type); |
|
| 210 | 210 | |
| 211 | 211 | if ( |
| 212 | 212 | // Check whether the post type object is not empty. |
| 213 | - ! empty( $post_type_object ) && |
|
| 213 | + ! empty($post_type_object) && |
|
| 214 | 214 | // And the post type has archive page. |
| 215 | 215 | $post_type_object->has_archive && |
| 216 | 216 | // And `rewrite` options exists.. |
| 217 | - ! empty( $post_type_object->rewrite ) && |
|
| 217 | + ! empty($post_type_object->rewrite) && |
|
| 218 | 218 | // And the `rewrite` slug property is not empty. |
| 219 | - ! empty( $post_type_object->rewrite['slug'] ) && |
|
| 219 | + ! empty($post_type_object->rewrite['slug']) && |
|
| 220 | 220 | // And if the rewrite slug equals to the slug. |
| 221 | 221 | $post_type_object->rewrite['slug'] === $slug |
| 222 | 222 | ) { |
@@ -231,13 +231,13 @@ discard block |
||
| 231 | 231 | "SELECT post_name |
| 232 | 232 | FROM $wpdb->posts |
| 233 | 233 | WHERE post_name = %s |
| 234 | - AND post_type IN ('" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "') |
|
| 234 | + AND post_type IN ('".implode("', '", array_map('esc_sql', $post_types))."') |
|
| 235 | 235 | LIMIT 1 |
| 236 | 236 | ", |
| 237 | 237 | $slug |
| 238 | 238 | ); |
| 239 | 239 | |
| 240 | - return null !== $wpdb->get_var( $check_sql ); |
|
| 240 | + return null !== $wpdb->get_var($check_sql); |
|
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | } |
@@ -18,75 +18,75 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Post_Property_Storage extends Wordlift_Storage { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The `post_title` property. |
|
| 23 | - */ |
|
| 24 | - const TITLE = 'title'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The `post_content` property stripped of tags and shortcodes. |
|
| 28 | - */ |
|
| 29 | - const DESCRIPTION_NO_TAGS_NO_SHORTCODES = 'description_no_tags_no_shortcodes'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The `post_author` property. |
|
| 33 | - */ |
|
| 34 | - const AUTHOR = 'author'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The {@link WP_Post} property to retrieve. |
|
| 38 | - * |
|
| 39 | - * @since 3.15.0 |
|
| 40 | - * @access private |
|
| 41 | - * @var string $property The {@link WP_Post} property to retrieve. |
|
| 42 | - */ |
|
| 43 | - private $property; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Create a {@link Wordlift_Post_Property_Storage} instance. |
|
| 47 | - * |
|
| 48 | - * @since 3.15.0 |
|
| 49 | - * |
|
| 50 | - * @param string $property A post property. |
|
| 51 | - */ |
|
| 52 | - public function __construct( $property ) { |
|
| 53 | - |
|
| 54 | - $this->property = $property; |
|
| 55 | - |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Get the property value. |
|
| 60 | - * |
|
| 61 | - * @since 3.15.0 |
|
| 62 | - * |
|
| 63 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 64 | - * |
|
| 65 | - * @return array|string|null A single string, or an array of values or null |
|
| 66 | - * if the property isn't recognized. |
|
| 67 | - */ |
|
| 68 | - public function get( $post_id ) { |
|
| 69 | - |
|
| 70 | - // Get the post. |
|
| 71 | - $post = get_post( $post_id ); |
|
| 72 | - |
|
| 73 | - // Switch according to the selected property. |
|
| 74 | - switch ( $this->property ) { |
|
| 75 | - |
|
| 76 | - // Title. |
|
| 77 | - case self::TITLE: |
|
| 78 | - return $post->post_title; |
|
| 79 | - |
|
| 80 | - // Description. |
|
| 81 | - case self::DESCRIPTION_NO_TAGS_NO_SHORTCODES: |
|
| 82 | - return wp_strip_all_tags( preg_replace( '/\[[^]]+\]/', '', do_shortcode( $post->post_content ) ) ); |
|
| 83 | - |
|
| 84 | - // Author. |
|
| 85 | - case self::AUTHOR: |
|
| 86 | - return $post->post_author; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - return null; |
|
| 90 | - } |
|
| 21 | + /** |
|
| 22 | + * The `post_title` property. |
|
| 23 | + */ |
|
| 24 | + const TITLE = 'title'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The `post_content` property stripped of tags and shortcodes. |
|
| 28 | + */ |
|
| 29 | + const DESCRIPTION_NO_TAGS_NO_SHORTCODES = 'description_no_tags_no_shortcodes'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The `post_author` property. |
|
| 33 | + */ |
|
| 34 | + const AUTHOR = 'author'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The {@link WP_Post} property to retrieve. |
|
| 38 | + * |
|
| 39 | + * @since 3.15.0 |
|
| 40 | + * @access private |
|
| 41 | + * @var string $property The {@link WP_Post} property to retrieve. |
|
| 42 | + */ |
|
| 43 | + private $property; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Create a {@link Wordlift_Post_Property_Storage} instance. |
|
| 47 | + * |
|
| 48 | + * @since 3.15.0 |
|
| 49 | + * |
|
| 50 | + * @param string $property A post property. |
|
| 51 | + */ |
|
| 52 | + public function __construct( $property ) { |
|
| 53 | + |
|
| 54 | + $this->property = $property; |
|
| 55 | + |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Get the property value. |
|
| 60 | + * |
|
| 61 | + * @since 3.15.0 |
|
| 62 | + * |
|
| 63 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 64 | + * |
|
| 65 | + * @return array|string|null A single string, or an array of values or null |
|
| 66 | + * if the property isn't recognized. |
|
| 67 | + */ |
|
| 68 | + public function get( $post_id ) { |
|
| 69 | + |
|
| 70 | + // Get the post. |
|
| 71 | + $post = get_post( $post_id ); |
|
| 72 | + |
|
| 73 | + // Switch according to the selected property. |
|
| 74 | + switch ( $this->property ) { |
|
| 75 | + |
|
| 76 | + // Title. |
|
| 77 | + case self::TITLE: |
|
| 78 | + return $post->post_title; |
|
| 79 | + |
|
| 80 | + // Description. |
|
| 81 | + case self::DESCRIPTION_NO_TAGS_NO_SHORTCODES: |
|
| 82 | + return wp_strip_all_tags( preg_replace( '/\[[^]]+\]/', '', do_shortcode( $post->post_content ) ) ); |
|
| 83 | + |
|
| 84 | + // Author. |
|
| 85 | + case self::AUTHOR: |
|
| 86 | + return $post->post_author; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + return null; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | } |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * |
| 50 | 50 | * @param string $property A post property. |
| 51 | 51 | */ |
| 52 | - public function __construct( $property ) { |
|
| 52 | + public function __construct($property) { |
|
| 53 | 53 | |
| 54 | 54 | $this->property = $property; |
| 55 | 55 | |
@@ -65,13 +65,13 @@ discard block |
||
| 65 | 65 | * @return array|string|null A single string, or an array of values or null |
| 66 | 66 | * if the property isn't recognized. |
| 67 | 67 | */ |
| 68 | - public function get( $post_id ) { |
|
| 68 | + public function get($post_id) { |
|
| 69 | 69 | |
| 70 | 70 | // Get the post. |
| 71 | - $post = get_post( $post_id ); |
|
| 71 | + $post = get_post($post_id); |
|
| 72 | 72 | |
| 73 | 73 | // Switch according to the selected property. |
| 74 | - switch ( $this->property ) { |
|
| 74 | + switch ($this->property) { |
|
| 75 | 75 | |
| 76 | 76 | // Title. |
| 77 | 77 | case self::TITLE: |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | // Description. |
| 81 | 81 | case self::DESCRIPTION_NO_TAGS_NO_SHORTCODES: |
| 82 | - return wp_strip_all_tags( preg_replace( '/\[[^]]+\]/', '', do_shortcode( $post->post_content ) ) ); |
|
| 82 | + return wp_strip_all_tags(preg_replace('/\[[^]]+\]/', '', do_shortcode($post->post_content))); |
|
| 83 | 83 | |
| 84 | 84 | // Author. |
| 85 | 85 | case self::AUTHOR: |