|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Database Class |
| 4 | + * |
| 5 | + * @package Webmention |
| 6 | + */ |
| 7 | +namespace Webmention; |
| 8 | + |
| 9 | +/** |
| 10 | + * Database Class |
| 11 | + * |
| 12 | + * @package Webmention |
| 13 | + */ |
| 14 | +class DB { |
| 15 | + /** |
| 16 | + * Which internal datastructure version we are running on. |
| 17 | + * |
| 18 | + * @var int |
| 19 | + */ |
| 20 | + private static $target_version = '1.0.1'; |
| 21 | + |
| 22 | + public static function get_target_version() { |
| 23 | + return self::$target_version; |
| 24 | + } |
| 25 | + |
| 26 | + public static function get_version() { |
| 27 | + return get_option( 'webmention_db_version', 0 ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Whether the database structure is up to date. |
| 32 | + * |
| 33 | + * @return bool |
| 34 | + */ |
| 35 | + public static function is_latest_version() { |
| 36 | + return (bool) version_compare( |
| 37 | + self::get_version(), |
| 38 | + self::get_target_version(), |
| 39 | + '==' |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Updates the database structure if necessary. |
| 45 | + */ |
| 46 | + public static function update_database() { |
| 47 | + if ( self::is_latest_version() ) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $version_from_db = self::get_version(); |
| 52 | + |
| 53 | + if ( version_compare( $version_from_db, '1.0.0', '<' ) ) { |
| 54 | + self::migrate_to_1_0_0(); |
| 55 | + } |
| 56 | + if ( version_compare( $version_from_db, '1.0.1', '<' ) ) { |
| 57 | + self::migrate_to_1_0_1(); |
| 58 | + } |
| 59 | + |
| 60 | + update_option( 'webmention_db_version', self::$target_version ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Rename meta keys. |
| 65 | + * |
| 66 | + * @param string $old The old commentmeta key |
| 67 | + * @param string $new The new commentmeta key |
| 68 | + */ |
| 69 | + public static function update_commentmeta_key( $old, $new ) { |
| 70 | + global $wpdb; |
| 71 | + |
| 72 | + $wpdb->update( |
| 73 | + $wpdb->commentmeta, |
| 74 | + array( 'meta_key' => $new ), |
| 75 | + array( 'meta_key' => $old ), |
| 76 | + array( '%s' ), |
| 77 | + array( '%s' ) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Rename option keys. |
| 83 | + * |
| 84 | + * @param string $old The old option key |
| 85 | + * @param string $new The new option key |
| 86 | + */ |
| 87 | + public static function update_options_key( $old, $new ) { |
| 88 | + global $wpdb; |
| 89 | + |
| 90 | + $wpdb->update( |
| 91 | + $wpdb->options, |
| 92 | + array( 'options_name' => $new ), |
| 93 | + array( 'options_name' => $old ), |
| 94 | + array( '%s' ), |
| 95 | + array( '%s' ) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * The Migration for Plugin Version 5.0.0 and DB Version 1.0.0 |
| 101 | + * |
| 102 | + * @since 5.0.0 |
| 103 | + * |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + public static function migrate_to_1_0_0() { |
| 107 | + wp_cache_flush(); |
| 108 | + |
| 109 | + // 1. rename comment meta |
| 110 | + self::update_commentmeta_key( 'semantic_linkbacks_source', 'webmention_source_url' ); |
| 111 | + self::update_commentmeta_key( 'semantic_linkbacks_avatar', 'avatar' ); |
| 112 | + self::update_commentmeta_key( 'semantic_linkbacks_canonical', 'url' ); |
| 113 | + // 2. migrate comment type |
| 114 | + global $wpdb; |
| 115 | + |
| 116 | + //Migrate Webmentions to comment types. |
| 117 | + $wpdb->query( |
| 118 | + "UPDATE {$wpdb->comments} comment SET comment_type = ( SELECT meta_value FROM {$wpdb->commentmeta} WHERE comment_id = comment.comment_ID AND meta_key = 'semantic_linkbacks_type' LIMIT 1 ) WHERE comment_type = 'webmention'" |
| 119 | + ); |
| 120 | + |
| 121 | + // Add protocol designation for Webmentions. |
| 122 | + $wpdb->query( |
| 123 | + "UPDATE {$wpdb->commentmeta} SET meta_key = 'protocol', meta_value = 'webmention' WHERE meta_key = 'semantic_linkbacks_type' OR meta_key = 'webmention_type'" |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Migrate to version 1.0.1 |
| 129 | + * |
| 130 | + * @since 5.1.0 |
| 131 | + * |
| 132 | + * @return void |
| 133 | + */ |
| 134 | + public static function migrate_to_1_0_1() { |
| 135 | + wp_cache_flush(); |
| 136 | + |
| 137 | + $comments = get_comments( |
| 138 | + array( |
| 139 | + 'fields' => 'ids', |
| 140 | + 'meta_query' => array( |
| 141 | + array( |
| 142 | + 'key' => 'mf2_author', |
| 143 | + 'compare' => 'EXISTS', |
| 144 | + ), |
| 145 | + array( |
| 146 | + 'key' => 'protocol', |
| 147 | + 'value' => 'webmention', |
| 148 | + ), |
| 149 | + ), |
| 150 | + ) |
| 151 | + ); |
| 152 | + |
| 153 | + foreach ( $comments as $comment_id ) { |
| 154 | + $author = get_comment_meta( $comment_id, 'mf2_author', true ); |
| 155 | + $source = get_comment_meta( $comment_id, 'webmention_source_url', true ); |
| 156 | + if ( is_array( $author ) ) { |
| 157 | + if ( array_key_exists( 'url', $author ) && ( $source !== $author['url'] ) ) { |
| 158 | + $comment = get_comment( $comment_id, ARRAY_A ); |
| 159 | + $comment['comment_author_url'] = $author['url']; |
| 160 | + wp_update_comment( $comment ); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments