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