| 1 |
<?php |
| 2 |
/** |
| 3 |
* Installation related functions and actions. |
| 4 |
* |
| 5 |
* @class FT_Install |
| 6 |
* @version 1.0.0 |
| 7 |
* @package FlashToolkit/Classes |
| 8 |
* @category Admin |
| 9 |
* @author ThemeGrill |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* FT_Install Class. |
| 18 |
*/ |
| 19 |
class FT_Install { |
| 20 |
|
| 21 |
/** @var array DB updates and callbacks that need to be run per version */ |
| 22 |
private static $db_updates = array( |
| 23 |
'1.0.0' => array( |
| 24 |
'flash_update_100_db_version', |
| 25 |
), |
| 26 |
); |
| 27 |
|
| 28 |
/** @var object Background update class */ |
| 29 |
private static $background_updater; |
| 30 |
|
| 31 |
/** |
| 32 |
* Hook in tabs. |
| 33 |
*/ |
| 34 |
public static function init() { |
| 35 |
add_action( 'init', array( __CLASS__, 'check_version' ), 5 ); |
| 36 |
add_action( 'init', array( __CLASS__, 'init_background_updater' ), 5 ); |
| 37 |
add_action( 'admin_init', array( __CLASS__, 'install_actions' ) ); |
| 38 |
add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Init background updates. |
| 43 |
*/ |
| 44 |
public static function init_background_updater() { |
| 45 |
include_once( dirname( __FILE__ ) . '/class-flash-background-updater.php' ); |
| 46 |
self::$background_updater = new FT_Background_Updater(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Check FlashToolkit version and run the updater is required. |
| 51 |
* |
| 52 |
* This check is done on all requests and runs if the versions do not match. |
| 53 |
*/ |
| 54 |
public static function check_version() { |
| 55 |
if ( ! defined( 'IFRAME_REQUEST' ) && get_option( 'flash_toolkit_version' ) !== FT()->version ) { |
| 56 |
self::install(); |
| 57 |
do_action( 'flash_toolkit_updated' ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Install actions when a update button is clicked within the admin area. |
| 63 |
* |
| 64 |
* This function is hooked into admin_init to affect admin only. |
| 65 |
*/ |
| 66 |
public static function install_actions() { |
| 67 |
if ( ! empty( $_GET['do_update_flash_toolkit'] ) ) { |
| 68 |
self::update(); |
| 69 |
FT_Admin_Notices::add_notice( 'update' ); |
| 70 |
} |
| 71 |
if ( ! empty( $_GET['force_update_flash_toolkit'] ) ) { |
| 72 |
do_action( 'wp_flash_updater_cron' ); |
| 73 |
wp_safe_redirect( admin_url( 'themes.php' ) ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Install FT. |
| 79 |
*/ |
| 80 |
public static function install() { |
| 81 |
global $wpdb; |
| 82 |
|
| 83 |
// Check if we are not already running this routine. |
| 84 |
if ( 'yes' === get_transient( 'ft_installing' ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
// If we made it till here nothing is running yet, lets set the transient now. |
| 89 |
set_transient( 'ft_installing', 'yes', MINUTE_IN_SECONDS * 10 ); |
| 90 |
|
| 91 |
if ( ! defined( 'FT_INSTALLING' ) ) { |
| 92 |
define( 'FT_INSTALLING', true ); |
| 93 |
} |
| 94 |
|
| 95 |
// Ensure needed classes are loaded. |
| 96 |
include_once( dirname( __FILE__ ) . '/admin/class-flash-admin-notices.php' ); |
| 97 |
|
| 98 |
self::create_roles(); |
| 99 |
|
| 100 |
// Register post types |
| 101 |
FT_Post_Types::register_post_types(); |
| 102 |
FT_Post_Types::register_taxonomies(); |
| 103 |
|
| 104 |
// Queue upgrades wizard |
| 105 |
$current_ft_version = get_option( 'flash_toolkit_version', null ); |
| 106 |
$current_db_version = get_option( 'flash_toolkit_db_version', null ); |
| 107 |
|
| 108 |
FT_Admin_Notices::remove_all_notices(); |
| 109 |
|
| 110 |
// No versions? This is a new install :) |
| 111 |
if ( is_null( $current_ft_version ) && is_null( $current_db_version ) && apply_filters( 'flash_toolkit_enable_setup_wizard', true ) ) { |
| 112 |
set_transient( '_flash_activation_redirect', 1, 30 ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! is_null( $current_db_version ) && version_compare( $current_db_version, max( array_keys( self::$db_updates ) ), '<' ) ) { |
| 116 |
FT_Admin_Notices::add_notice( 'update' ); |
| 117 |
} else { |
| 118 |
self::update_db_version(); |
| 119 |
} |
| 120 |
|
| 121 |
self::update_ft_version(); |
| 122 |
|
| 123 |
delete_transient( 'ft_installing' ); |
| 124 |
// Flush rules after install |
| 125 |
do_action( 'flash_toolkit_flush_rewrite_rules' ); |
| 126 |
|
| 127 |
/* |
| 128 |
* Deletes all expired transients. The multi-table delete syntax is used |
| 129 |
* to delete the transient record from table a, and the corresponding |
| 130 |
* transient_timeout record from table b. |
| 131 |
* |
| 132 |
* Based on code inside core's upgrade_network() function. |
| 133 |
*/ |
| 134 |
$sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b |
| 135 |
WHERE a.option_name LIKE %s |
| 136 |
AND a.option_name NOT LIKE %s |
| 137 |
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) |
| 138 |
AND b.option_value < %d"; |
| 139 |
$wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', time() ) ); |
| 140 |
|
| 141 |
// Trigger action. |
| 142 |
do_action( 'flash_toolkit_installed' ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Removes Pro notice data from database. |
| 147 |
* |
| 148 |
* @since 1.2.0 |
| 149 |
*/ |
| 150 |
public static function deactivate() { |
| 151 |
|
| 152 |
$users = get_users(); |
| 153 |
|
| 154 |
foreach ( $users as $user ) { |
| 155 |
if ( get_user_meta( $user->ID, 'flash_pro_notice_temporary_ignore', true ) ) { |
| 156 |
delete_user_meta( $user->ID, 'flash_pro_notice_temporary_ignore' ); |
| 157 |
} |
| 158 |
|
| 159 |
if ( get_user_meta( $user->ID, 'flash_pro_notice_permanent_ignore', true ) ) { |
| 160 |
delete_user_meta( $user->ID, 'flash_pro_notice_permanent_ignore' ); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if ( get_option( 'flash_pro_notice_start_time' ) ) { |
| 165 |
delete_option( 'flash_pro_notice_start_time' ); |
| 166 |
} |
| 167 |
|
| 168 |
// Trigger action. |
| 169 |
do_action( 'flash_toolkit_deactivate' ); |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Update FT version to current. |
| 175 |
*/ |
| 176 |
private static function update_ft_version() { |
| 177 |
delete_option( 'flash_toolkit_version' ); |
| 178 |
add_option( 'flash_toolkit_version', FT()->version ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Push all needed DB updates to the queue for processing. |
| 183 |
*/ |
| 184 |
private static function update() { |
| 185 |
$current_db_version = get_option( 'flash_toolkit_db_version' ); |
| 186 |
$update_queued = false; |
| 187 |
|
| 188 |
foreach ( self::$db_updates as $version => $update_callbacks ) { |
| 189 |
if ( version_compare( $current_db_version, $version, '<' ) ) { |
| 190 |
foreach ( $update_callbacks as $update_callback ) { |
| 191 |
self::$background_updater->push_to_queue( $update_callback ); |
| 192 |
$update_queued = true; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if ( $update_queued ) { |
| 198 |
self::$background_updater->save()->dispatch(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Update DB version to current. |
| 204 |
* |
| 205 |
* @param string $version |
| 206 |
*/ |
| 207 |
public static function update_db_version( $version = null ) { |
| 208 |
delete_option( 'flash_toolkit_db_version' ); |
| 209 |
add_option( 'flash_toolkit_db_version', is_null( $version ) ? FT()->version : $version ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Create roles and capabilities. |
| 214 |
*/ |
| 215 |
public static function create_roles() { |
| 216 |
global $wp_roles; |
| 217 |
|
| 218 |
if ( ! class_exists( 'WP_Roles' ) ) { |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
if ( ! isset( $wp_roles ) ) { |
| 223 |
$wp_roles = new WP_Roles(); |
| 224 |
} |
| 225 |
|
| 226 |
$capabilities = self::get_core_capabilities(); |
| 227 |
|
| 228 |
foreach ( $capabilities as $cap_group ) { |
| 229 |
foreach ( $cap_group as $cap ) { |
| 230 |
$wp_roles->add_cap( 'administrator', $cap ); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Get capabilities for FlashToolkit. |
| 237 |
* |
| 238 |
* @return array |
| 239 |
*/ |
| 240 |
private static function get_core_capabilities() { |
| 241 |
$capabilities = array(); |
| 242 |
|
| 243 |
$capabilities['core'] = array( |
| 244 |
'manage_flash_toolkit', |
| 245 |
); |
| 246 |
|
| 247 |
$capability_types = array( 'portfolio' ); |
| 248 |
|
| 249 |
foreach ( $capability_types as $capability_type ) { |
| 250 |
|
| 251 |
$capabilities[ $capability_type ] = array( |
| 252 |
// Post type |
| 253 |
"edit_{$capability_type}", |
| 254 |
"read_{$capability_type}", |
| 255 |
"delete_{$capability_type}", |
| 256 |
"edit_{$capability_type}s", |
| 257 |
"edit_others_{$capability_type}s", |
| 258 |
"publish_{$capability_type}s", |
| 259 |
"read_private_{$capability_type}s", |
| 260 |
"delete_{$capability_type}s", |
| 261 |
"delete_private_{$capability_type}s", |
| 262 |
"delete_published_{$capability_type}s", |
| 263 |
"delete_others_{$capability_type}s", |
| 264 |
"edit_private_{$capability_type}s", |
| 265 |
"edit_published_{$capability_type}s", |
| 266 |
|
| 267 |
// Terms |
| 268 |
"manage_{$capability_type}_terms", |
| 269 |
"edit_{$capability_type}_terms", |
| 270 |
"delete_{$capability_type}_terms", |
| 271 |
"assign_{$capability_type}_terms", |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
return $capabilities; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Remove roles and capabilities. |
| 280 |
*/ |
| 281 |
public static function remove_roles() { |
| 282 |
global $wp_roles; |
| 283 |
|
| 284 |
if ( ! class_exists( 'WP_Roles' ) ) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
if ( ! isset( $wp_roles ) ) { |
| 289 |
$wp_roles = new WP_Roles(); |
| 290 |
} |
| 291 |
|
| 292 |
$capabilities = self::get_core_capabilities(); |
| 293 |
|
| 294 |
foreach ( $capabilities as $cap_group ) { |
| 295 |
foreach ( $cap_group as $cap ) { |
| 296 |
$wp_roles->remove_cap( 'administrator', $cap ); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Display row meta in the Plugins list table. |
| 303 |
* |
| 304 |
* @param array $plugin_meta |
| 305 |
* @param string $plugin_file |
| 306 |
* |
| 307 |
* @return array |
| 308 |
*/ |
| 309 |
public static function plugin_row_meta( $plugin_meta, $plugin_file ) { |
| 310 |
if ( $plugin_file == FT_PLUGIN_BASENAME ) { |
| 311 |
$new_plugin_meta = array( |
| 312 |
'docs' => '<a href="' . esc_url( apply_filters( 'flash_toolkit_docs_url', 'http://docs.themegrill.com/flash/' ) ) . '" title="' . esc_attr( __( 'View Flash Toolkit Documentation', 'flash-toolkit' ) ) . '">' . __( 'Docs', 'flash-toolkit' ) . '</a>', |
| 313 |
'support' => '<a href="' . esc_url( apply_filters( 'flash_toolkit_support_url', 'http://themegrill.com/support-forum/' ) ) . '" title="' . esc_attr( __( 'Visit Free Customer Support Forum', 'flash-toolkit' ) ) . '">' . __( 'Free Support', 'flash-toolkit' ) . '</a>', |
| 314 |
); |
| 315 |
|
| 316 |
return array_merge( $plugin_meta, $new_plugin_meta ); |
| 317 |
} |
| 318 |
|
| 319 |
return (array) $plugin_meta; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
FT_Install::init(); |
| 324 |
|