updates.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; |
| 4 | |
| 5 | class Shortcoder_Updates{ |
| 6 | |
| 7 | public static function init(){ |
| 8 | |
| 9 | add_action( 'admin_init', array( __CLASS__, 'do_update' ) ); |
| 10 | |
| 11 | } |
| 12 | |
| 13 | public static function do_update(){ |
| 14 | |
| 15 | $previous_version = self::get_previous_version(); |
| 16 | $current_version = SC_VERSION; |
| 17 | |
| 18 | if( version_compare( $previous_version, $current_version, '>=' ) ){ |
| 19 | return true; |
| 20 | } |
| 21 | |
| 22 | if ( get_transient( 'sc_upgrade' ) === 'yes' ) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | set_transient( 'sc_upgrade', 'yes', MINUTE_IN_SECONDS * 3 ); |
| 27 | |
| 28 | if( version_compare( $previous_version, '5.0', '<' ) ){ |
| 29 | if( !self::do_update_50() ){ |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | if( version_compare( $previous_version, '5.0', '>' ) && version_compare( $previous_version, '5.1', '<' ) ){ |
| 35 | if( !self::do_update_504() ){ |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Register roles every time plugin is updated. |
| 41 | self::register_roles(); |
| 42 | |
| 43 | delete_transient( 'sc_upgrade' ); |
| 44 | |
| 45 | update_option( 'shortcoder_version', $current_version ); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | public static function do_update_50(){ |
| 50 | |
| 51 | $o_scs = get_option( 'shortcoder_data' ); |
| 52 | $n_scs = Shortcoder::get_shortcodes(); |
| 53 | |
| 54 | if( empty( $o_scs ) ){ |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | // Remove kses filtering before the migration |
| 59 | remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); |
| 60 | remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); |
| 61 | |
| 62 | foreach( $o_scs as $o_name => $o_props ){ |
| 63 | |
| 64 | if( array_key_exists( 'post_id', $o_props ) || array_key_exists( $o_name, $n_scs ) ){ |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if( post_exists( $o_name, '', '', SC_POST_TYPE ) != 0 ){ |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | $status = ( isset( $o_props[ 'disabled' ] ) && $o_props[ 'disabled' ] == 0 ) ? 'no' : 'yes'; |
| 73 | $disable_admin = ( isset( $o_props[ 'hide_admin' ] ) && $o_props[ 'hide_admin' ] == 0 ) ? 'no' : 'yes'; |
| 74 | $content = isset( $o_props[ 'content' ] ) ? $o_props[ 'content' ] : ''; |
| 75 | $editor = isset( $o_props[ 'editor' ] ) ? $o_props[ 'editor' ] : 'code'; |
| 76 | $tags = isset( $o_props[ 'tags' ] ) ? $o_props[ 'tags' ] : array(); |
| 77 | $devices = isset( $o_props[ 'devices' ] ) ? $o_props[ 'devices' ] : 'all'; |
| 78 | |
| 79 | $post_id = wp_insert_post( array( |
| 80 | 'post_title' => $o_name, |
| 81 | 'post_name' => $o_name, |
| 82 | 'post_content' => $content, |
| 83 | 'post_type' => SC_POST_TYPE, |
| 84 | 'post_status' => 'publish', |
| 85 | 'tax_input' => array( |
| 86 | 'sc_tag' => $tags |
| 87 | ), |
| 88 | 'meta_input' => array( |
| 89 | '_sc_disable_sc' => $status, |
| 90 | '_sc_disable_admin' => $disable_admin, |
| 91 | '_sc_editor' => $editor, |
| 92 | '_sc_allowed_devices' => $devices, |
| 93 | '_sc_migrated' => 'yes' |
| 94 | ) |
| 95 | )); |
| 96 | |
| 97 | if( $post_id ){ |
| 98 | $o_scs[ $o_name ][ 'post_id' ] = $post_id; |
| 99 | }else{ |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | update_option( 'shortcoder_data', $o_scs ); |
| 106 | |
| 107 | // Enabling the filters back |
| 108 | add_filter( 'content_save_pre', 'wp_filter_post_kses' ); |
| 109 | add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); |
| 110 | |
| 111 | return true; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | public static function do_update_504(){ |
| 116 | |
| 117 | $o_scs = get_option( 'shortcoder_data' ); |
| 118 | |
| 119 | if( empty( $o_scs ) ){ |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | // Remove kses filtering before the migration |
| 124 | remove_filter( 'content_save_pre', 'wp_filter_post_kses' ); |
| 125 | remove_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); |
| 126 | |
| 127 | foreach( $o_scs as $o_name => $o_props ){ |
| 128 | |
| 129 | if( !array_key_exists( 'post_id', $o_props ) || !isset( $o_props[ 'content' ] ) ){ |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | if( array_key_exists( '_scc_fix', $o_props ) ){ |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | $sc_pid = $o_props[ 'post_id' ]; |
| 138 | $sc_content = $o_props[ 'content' ]; |
| 139 | |
| 140 | wp_insert_post( array( |
| 141 | 'ID' => $sc_pid, |
| 142 | 'post_content' => $sc_content, |
| 143 | 'post_type' => SC_POST_TYPE, |
| 144 | 'post_status' => 'publish' |
| 145 | )); |
| 146 | |
| 147 | $o_scs[ $o_name ][ '_scc_fix' ] = true; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | update_option( 'shortcoder_data', $o_scs ); |
| 152 | |
| 153 | // Enabling the filters back |
| 154 | add_filter( 'content_save_pre', 'wp_filter_post_kses' ); |
| 155 | add_filter( 'content_filtered_save_pre', 'wp_filter_post_kses' ); |
| 156 | |
| 157 | return true; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | public static function register_roles(){ |
| 162 | |
| 163 | $capability_type = 'shortcoder'; |
| 164 | |
| 165 | $capabilities = array( |
| 166 | "edit_{$capability_type}", |
| 167 | "read_{$capability_type}", |
| 168 | "delete_{$capability_type}", |
| 169 | "edit_{$capability_type}s", |
| 170 | "edit_others_{$capability_type}s", |
| 171 | "publish_{$capability_type}s", |
| 172 | "delete_{$capability_type}s", |
| 173 | "delete_published_{$capability_type}s", |
| 174 | "delete_others_{$capability_type}s", |
| 175 | "edit_published_{$capability_type}s", |
| 176 | ); |
| 177 | |
| 178 | $roles = array( 'administrator' ); |
| 179 | |
| 180 | foreach( $roles as $role_name ){ |
| 181 | |
| 182 | $role = get_role( $role_name ); |
| 183 | |
| 184 | foreach( $capabilities as $cap ){ |
| 185 | $role->add_cap( $cap ); |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
| 192 | public static function get_previous_version(){ |
| 193 | |
| 194 | $version = get_option( 'shortcoder_version' ); |
| 195 | |
| 196 | if( $version ){ |
| 197 | return $version; |
| 198 | } |
| 199 | |
| 200 | $sc_flags = get_option( 'shortcoder_flags' ); |
| 201 | |
| 202 | if( !$sc_flags ){ |
| 203 | return '0'; |
| 204 | } |
| 205 | |
| 206 | if( !is_array( $sc_flags ) || !array_key_exists( 'version', $sc_flags ) ){ |
| 207 | return '0'; |
| 208 | } |
| 209 | |
| 210 | return $sc_flags[ 'version' ]; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | Shortcoder_Updates::init(); |
| 217 | |
| 218 | ?> |