diff --git a/src/classes/content/wordpress/class-abstract-wordpress-content-service.php b/src/classes/content/wordpress/class-abstract-wordpress-content-service.php index 983f62fc2..1c401ef46 100644 --- a/src/classes/content/wordpress/class-abstract-wordpress-content-service.php +++ b/src/classes/content/wordpress/class-abstract-wordpress-content-service.php @@ -2,6 +2,7 @@ namespace Wordlift\Content\Wordpress; +use Exception; use Wordlift\Assertions; use Wordlift\Content\Content_Service; use Wordlift\Object_Type_Enum; @@ -69,13 +70,24 @@ public function get_about_jsonld( $content_id ) { /** * @param Wordpress_Content_Id $content_id - * @param string $value + * @param string $value + * + * @throws Exception If the 'match_name' column does not exist in the database table. */ public function set_about_jsonld( $content_id, $value ) { global $wpdb; // Cleanup value. - $value = ( is_string( $value ) && strlen( $value ) > 2 ) ? $value : null; + $value = ( is_string( $value ) && strlen( $value ) > 2 ) ? $value : null; + $match_name = "NULL"; + + if ( $value ) { + // Check if the 'match_name' column exists in the database table + $columns = $wpdb->get_col_info( 'name', 0 ); + if ( in_array( 'match_name', $columns ) ) { + $match_name = $this->get_match_name( $value ); + } + } // This `hack` is necessary to ensure the entity exists in the entities table, but we // should revise how this works really. @@ -102,11 +114,10 @@ public function set_about_jsonld( $content_id, $value ) { if ( null === $value ) { return $wpdb->query( $wpdb->prepare( - " - UPDATE {$wpdb->prefix}wl_entities - SET about_jsonld = NULL - WHERE content_id = %d AND content_type = %d - ", + "UPDATE {$wpdb->prefix}wl_entities + SET about_jsonld = NULL, match_name = %s + WHERE content_id = %d AND content_type = %d", + $match_name, $content_id->get_id(), $content_id->get_type() ) @@ -115,16 +126,28 @@ public function set_about_jsonld( $content_id, $value ) { return $wpdb->query( $wpdb->prepare( - " - UPDATE {$wpdb->prefix}wl_entities - SET about_jsonld = %s - WHERE content_id = %d AND content_type = %d - ", + "UPDATE {$wpdb->prefix}wl_entities + SET about_jsonld = %s, match_name = %s + WHERE content_id = %d AND content_type = %d", $value, + $match_name, $content_id->get_id(), $content_id->get_type() ) ); } + /** + * @param $jsonld + * + * @return mixed|null + */ + public function get_match_name( $jsonld ) { + $data = json_decode( $jsonld, true ); + if ( ! $data || ! array_key_exists( 'name', $data ) ) { + return "NULL"; + } + + return $data['name']; + } } diff --git a/src/install/class-wordlift-install-3-49-1.php b/src/install/class-wordlift-install-3-49-1.php index aa846f2fb..fb3acb5ff 100644 --- a/src/install/class-wordlift-install-3-49-1.php +++ b/src/install/class-wordlift-install-3-49-1.php @@ -7,83 +7,85 @@ */ class Wordlift_Install_3_49_1 extends Wordlift_Install { - /** - * {@inheritdoc} - */ - protected static $version = '3.49.1'; + /** + * {@inheritdoc} + */ + protected static $version = '3.49.1'; - /** - * Is column exists - * - * @param $column_name - * @return mixed - */ - public static function is_column_exists( $column_name ) { - global $wpdb; + /** + * Is column exists + * + * @param $column_name + * + * @return mixed + */ + public static function is_column_exists( $column_name ) { + global $wpdb; - return $wpdb->get_results( - $wpdb->prepare( - "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s", - $column_name - ) - ); - } + return $wpdb->get_results( + $wpdb->prepare( + "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='{$wpdb->prefix}wl_relation_instances' AND column_name = %s", + $column_name + ) + ); + } - /** - * Install - * - * @return void - */ - public function install() { - global $wpdb; + /** + * Install + * + * @return void + */ + public function install() { + global $wpdb; - // Check if 'match_name' column exists - if ( self::is_column_exists( 'match_name' ) ) { - return; - } + // Check if 'match_name' column exists + if ( self::is_column_exists( 'match_name' ) ) { + return; + } - // Add new 'match_name' column - $wpdb->query( - "ALTER TABLE {$wpdb->prefix}wl_relation_instances + // Add new 'match_name' column + $wpdb->query( + "ALTER TABLE {$wpdb->prefix}wl_relation_instances ADD match_name VARCHAR(255) AFTER about_jsonld;" - ); + ); - // Get all rows with 'about_jsonld' - $results = $wpdb->get_results( - "SELECT id, about_jsonld FROM {$wpdb->prefix}wl_relation_instances WHERE about_jsonld IS NOT NULL", - ARRAY_A - ); + // Get all rows with 'about_jsonld' + $results = $wpdb->get_results( + "SELECT id, about_jsonld FROM {$wpdb->prefix}wl_relation_instances WHERE about_jsonld IS NOT NULL", + ARRAY_A + ); - // Update 'match_name' for each row - foreach ( $results as $row ) { - $match_name = $this->get_match_name( $row['about_jsonld'] ); + // Update 'match_name' for each row + foreach ( $results as $row ) { + $match_name = $this->get_match_name( $row['about_jsonld'] ); - if ( is_null($match_name) ) { - continue; - } + if ( is_null( $match_name ) ) { + continue; + } - $wpdb->update( - "{$wpdb->prefix}wl_relation_instances", - array( 'match_name' => $match_name ), - array( 'id' => $row['id'] ) - ); - } + $wpdb->update( + "{$wpdb->prefix}wl_relation_instances", + array( 'match_name' => $match_name ), + array( 'id' => $row['id'] ) + ); + } - Ttl_Cache::flush_all(); - } + Ttl_Cache::flush_all(); + } - /** - * Get match name - * - * @param $jsonld - * @return mixed|null - */ - public function get_match_name( $jsonld ) { - $data = json_decode( $jsonld, true ); - if ( ! $data || ! array_key_exists( 'name', $data ) ) { - return null; - } + /** + * Get match name + * + * @param $jsonld + * + * @return mixed|null + */ + public function get_match_name( $jsonld ) { + $data = json_decode( $jsonld, true ); + if ( ! $data || ! array_key_exists( 'name', $data ) ) { + return null; + } - return $data['name']; - } + return $data['name']; + } }