updates.php
158 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 * 2 ); |
| 27 | |
| 28 | if( version_compare( $previous_version, '5.0', '<' ) ){ |
| 29 | if( !self::do_update_50() ){ |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Register roles everytime plugin is updated. |
| 35 | self::register_roles(); |
| 36 | |
| 37 | delete_transient( 'sc_upgrade' ); |
| 38 | |
| 39 | update_option( 'shortcoder_version', $current_version ); |
| 40 | |
| 41 | } |
| 42 | |
| 43 | public static function do_update_50(){ |
| 44 | |
| 45 | $o_scs = get_option( 'shortcoder_data' ); |
| 46 | $n_scs = Shortcoder::get_shortcodes(); |
| 47 | |
| 48 | if( empty( $o_scs ) ){ |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | foreach( $o_scs as $o_name => $o_props ){ |
| 53 | |
| 54 | if( array_key_exists( 'post_id', $o_props ) || array_key_exists( $o_name, $n_scs ) ){ |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | if( post_exists( $o_name, '', '', SC_POST_TYPE ) != 0 ){ |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | $status = ( isset( $o_props[ 'disabled' ] ) && $o_props[ 'disabled' ] == 0 ) ? 'no' : 'yes'; |
| 63 | $disable_admin = ( isset( $o_props[ 'hide_admin' ] ) && $o_props[ 'hide_admin' ] == 0 ) ? 'no' : 'yes'; |
| 64 | $content = isset( $o_props[ 'content' ] ) ? $o_props[ 'content' ] : ''; |
| 65 | $editor = isset( $o_props[ 'editor' ] ) ? $o_props[ 'editor' ] : 'code'; |
| 66 | $tags = isset( $o_props[ 'tags' ] ) ? $o_props[ 'tags' ] : array(); |
| 67 | $devices = isset( $o_props[ 'devices' ] ) ? $o_props[ 'devices' ] : 'all'; |
| 68 | |
| 69 | $post_id = wp_insert_post( array( |
| 70 | 'post_title' => $o_name, |
| 71 | 'post_name' => $o_name, |
| 72 | 'post_content' => $content, |
| 73 | 'post_type' => SC_POST_TYPE, |
| 74 | 'post_status' => 'publish', |
| 75 | 'tax_input' => array( |
| 76 | 'sc_tag' => $tags |
| 77 | ), |
| 78 | 'meta_input' => array( |
| 79 | '_sc_disable_sc' => $status, |
| 80 | '_sc_disable_admin' => $disable_admin, |
| 81 | '_sc_editor' => $editor, |
| 82 | '_sc_allowed_devices' => $devices, |
| 83 | '_sc_migrated' => 'yes' |
| 84 | ) |
| 85 | )); |
| 86 | |
| 87 | if( $post_id ){ |
| 88 | $o_scs[ $o_name ][ 'post_id' ] = $post_id; |
| 89 | }else{ |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | update_option( 'shortcoder_data', $o_scs ); |
| 96 | |
| 97 | return true; |
| 98 | |
| 99 | } |
| 100 | |
| 101 | public static function register_roles(){ |
| 102 | |
| 103 | $capability_type = 'shortcoder'; |
| 104 | |
| 105 | $capabilities = array( |
| 106 | "edit_{$capability_type}", |
| 107 | "read_{$capability_type}", |
| 108 | "delete_{$capability_type}", |
| 109 | "edit_{$capability_type}s", |
| 110 | "edit_others_{$capability_type}s", |
| 111 | "publish_{$capability_type}s", |
| 112 | "delete_{$capability_type}s", |
| 113 | "delete_published_{$capability_type}s", |
| 114 | "delete_others_{$capability_type}s", |
| 115 | "edit_published_{$capability_type}s", |
| 116 | ); |
| 117 | |
| 118 | $roles = array( 'administrator' ); |
| 119 | |
| 120 | foreach( $roles as $role_name ){ |
| 121 | |
| 122 | $role = get_role( $role_name ); |
| 123 | |
| 124 | foreach( $capabilities as $cap ){ |
| 125 | $role->add_cap( $cap ); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | public static function get_previous_version(){ |
| 133 | |
| 134 | $version = get_option( 'shortcoder_version' ); |
| 135 | |
| 136 | if( $version ){ |
| 137 | return $version; |
| 138 | } |
| 139 | |
| 140 | $sc_flags = get_option( 'shortcoder_flags' ); |
| 141 | |
| 142 | if( !$sc_flags ){ |
| 143 | return '0'; |
| 144 | } |
| 145 | |
| 146 | if( !is_array( $sc_flags ) || !array_key_exists( 'version', $sc_flags ) ){ |
| 147 | return '0'; |
| 148 | } |
| 149 | |
| 150 | return $sc_flags[ 'version' ]; |
| 151 | |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |
| 156 | Shortcoder_Updates::init(); |
| 157 | |
| 158 | ?> |