Complete refactoring and add style settings
This commit is contained in:
509
includes/class-sp-install.php
Normal file
509
includes/class-sp-install.php
Normal file
@@ -0,0 +1,509 @@
|
||||
<?php
|
||||
/**
|
||||
* Installation related functions and actions.
|
||||
*
|
||||
* @author ThemeBoy
|
||||
* @category Admin
|
||||
* @package SportsPress/Classes
|
||||
* @version 0.7
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
if ( ! class_exists( 'SP_Install' ) ) :
|
||||
|
||||
/**
|
||||
* SP_Install Class
|
||||
*/
|
||||
class SP_Install {
|
||||
|
||||
/**
|
||||
* Hook in tabs.
|
||||
*/
|
||||
public function __construct() {
|
||||
register_activation_hook( SP_PLUGIN_FILE, array( $this, 'install' ) );
|
||||
|
||||
add_action( 'admin_init', array( $this, 'install_actions' ) );
|
||||
add_action( 'admin_init', array( $this, 'check_version' ), 5 );
|
||||
add_action( 'in_plugin_update_message-sportspress/sportspress.php', array( $this, 'in_plugin_update_message' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* check_version function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function check_version() {
|
||||
if ( ! defined( 'IFRAME_REQUEST' ) && get_option( 'sportspress_version' ) != SP()->version ) {
|
||||
$this->install();
|
||||
|
||||
do_action( 'sportspress_updated' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Install actions such as installing pages when a button is clicked.
|
||||
*/
|
||||
public function install_actions() {
|
||||
// Install - Add pages button
|
||||
if ( ! empty( $_GET['install_sportspress_pages'] ) ) {
|
||||
|
||||
// We no longer need to install pages
|
||||
delete_option( '_sp_needs_pages' );
|
||||
delete_transient( '_sp_activation_redirect' );
|
||||
|
||||
// What's new redirect
|
||||
wp_redirect( admin_url( 'index.php?page=sp-about&sp-installed=true' ) );
|
||||
exit;
|
||||
|
||||
// Skip button
|
||||
} elseif ( ! empty( $_GET['skip_install_sportspress_pages'] ) ) {
|
||||
|
||||
// We no longer need to install pages
|
||||
delete_option( '_sp_needs_pages' );
|
||||
delete_transient( '_sp_activation_redirect' );
|
||||
|
||||
// What's new redirect
|
||||
wp_redirect( admin_url( 'index.php?page=sp-about' ) );
|
||||
exit;
|
||||
|
||||
// Update button
|
||||
} elseif ( ! empty( $_GET['do_update_sportspress'] ) ) {
|
||||
|
||||
$this->update();
|
||||
|
||||
// Update complete
|
||||
delete_option( '_sp_needs_pages' );
|
||||
delete_option( '_sp_needs_update' );
|
||||
delete_transient( '_sp_activation_redirect' );
|
||||
|
||||
// What's new redirect
|
||||
wp_redirect( admin_url( 'index.php?page=sp-about&sp-updated=true' ) );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Install SP
|
||||
*/
|
||||
public function install() {
|
||||
$this->create_options();
|
||||
$this->create_roles();
|
||||
|
||||
// Register post types
|
||||
include_once( 'class-sp-post-types.php' );
|
||||
SP_Post_types::register_post_types();
|
||||
SP_Post_types::register_taxonomies();
|
||||
|
||||
// Queue upgrades
|
||||
$current_version = get_option( 'sportspress_version', null );
|
||||
$current_db_version = get_option( 'sportspress_db_version', null );
|
||||
|
||||
if ( version_compare( $current_db_version, '2.1.0', '<' ) && null !== $current_db_version ) {
|
||||
update_option( '_sp_needs_update', 1 );
|
||||
} else {
|
||||
update_option( 'sportspress_db_version', SP()->version );
|
||||
}
|
||||
|
||||
// Update version
|
||||
update_option( 'sportspress_version', SP()->version );
|
||||
|
||||
// Flush rules after install
|
||||
flush_rewrite_rules();
|
||||
|
||||
// Redirect to welcome screen
|
||||
set_transient( '_sp_activation_redirect', 1, 60 * 60 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle updates
|
||||
*/
|
||||
public function update() {
|
||||
// Do updates
|
||||
$current_db_version = get_option( 'sportspress_db_version' );
|
||||
|
||||
if ( version_compare( $current_db_version, '1.4', '<' ) ) {
|
||||
include( 'updates/sportspress-update-1.4.php' );
|
||||
update_option( 'sportspress_db_version', '1.4' );
|
||||
}
|
||||
|
||||
if ( version_compare( $current_db_version, '1.5', '<' ) ) {
|
||||
include( 'updates/sportspress-update-1.5.php' );
|
||||
update_option( 'sportspress_db_version', '1.5' );
|
||||
}
|
||||
|
||||
if ( version_compare( $current_db_version, '2.0', '<' ) ) {
|
||||
include( 'updates/sportspress-update-2.0.php' );
|
||||
update_option( 'sportspress_db_version', '2.0' );
|
||||
}
|
||||
|
||||
if ( version_compare( $current_db_version, '2.0.9', '<' ) ) {
|
||||
include( 'updates/sportspress-update-2.0.9.php' );
|
||||
update_option( 'sportspress_db_version', '2.0.9' );
|
||||
}
|
||||
|
||||
if ( version_compare( $current_db_version, '2.0.14', '<' ) ) {
|
||||
if ( 'HU' == get_option( 'sportspress_default_country' ) ) {
|
||||
update_option( 'sportspress_default_country', 'HU:BU' );
|
||||
}
|
||||
|
||||
update_option( 'sportspress_db_version', '2.0.14' );
|
||||
}
|
||||
|
||||
if ( version_compare( $current_db_version, '2.1.0', '<' ) || SP_VERSION == '2.1-bleeding' ) {
|
||||
include( 'updates/sportspress-update-2.1.php' );
|
||||
update_option( 'sportspress_db_version', '2.1.0' );
|
||||
}
|
||||
|
||||
update_option( 'sportspress_db_version', SP()->version );
|
||||
}
|
||||
|
||||
/**
|
||||
* Default options
|
||||
*
|
||||
* Sets up the default options used on the settings page
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function create_options() {
|
||||
// Include settings so that we can run through defaults
|
||||
include_once( 'admin/class-sp-admin-settings.php' );
|
||||
|
||||
$settings = SP_Admin_Settings::get_settings_pages();
|
||||
|
||||
foreach ( $settings as $section ) {
|
||||
foreach ( $section->get_settings() as $value ) {
|
||||
if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
|
||||
$autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true;
|
||||
add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create roles and capabilities
|
||||
*/
|
||||
public function create_roles() {
|
||||
global $wp_roles;
|
||||
|
||||
if ( class_exists( 'WP_Roles' ) ) {
|
||||
if ( ! isset( $wp_roles ) ) {
|
||||
$wp_roles = new WP_Roles();
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_object( $wp_roles ) ) {
|
||||
|
||||
add_role(
|
||||
'sp_player',
|
||||
__( 'Player', 'sportspress' ),
|
||||
array(
|
||||
'read' => true,
|
||||
'edit_posts' => true,
|
||||
'delete_posts' => true,
|
||||
'upload_files' => true,
|
||||
|
||||
'edit_sp_player' => true,
|
||||
'edit_sp_players' => true,
|
||||
'read_sp_players' => true,
|
||||
'delete_sp_players' => true,
|
||||
)
|
||||
);
|
||||
|
||||
add_role(
|
||||
'sp_staff',
|
||||
__( 'Staff', 'sportspress' ),
|
||||
array(
|
||||
'read' => true,
|
||||
'edit_posts' => true,
|
||||
'delete_posts' => true,
|
||||
'upload_files' => true,
|
||||
|
||||
'edit_sp_staff' => true,
|
||||
'edit_sp_staffs' => true,
|
||||
'read_sp_staffs' => true,
|
||||
'delete_sp_staffs' => true,
|
||||
)
|
||||
);
|
||||
|
||||
add_role(
|
||||
'sp_team_manager',
|
||||
__( 'Team Manager', 'sportspress' ),
|
||||
array(
|
||||
'read' => true,
|
||||
'edit_posts' => true,
|
||||
'delete_posts' => true,
|
||||
'upload_files' => true,
|
||||
|
||||
'edit_sp_player' => true,
|
||||
'edit_sp_players' => true,
|
||||
'edit_private_sp_players' => true,
|
||||
'edit_published_sp_players' => true,
|
||||
'read_sp_players' => true,
|
||||
'read_private_sp_players' => true,
|
||||
'publish_sp_players' => true,
|
||||
'delete_sp_players' => true,
|
||||
'delete_private_sp_players' => true,
|
||||
'delete_published_sp_players' => true,
|
||||
|
||||
'edit_sp_staff' => true,
|
||||
'edit_sp_staffs' => true,
|
||||
'edit_private_sp_staffs' => true,
|
||||
'edit_published_sp_staffs' => true,
|
||||
'read_sp_staffs' => true,
|
||||
'read_private_sp_staffs' => true,
|
||||
'publish_sp_staffs' => true,
|
||||
'delete_sp_staffs' => true,
|
||||
'delete_private_sp_staffs' => true,
|
||||
'delete_published_sp_staffs' => true,
|
||||
|
||||
'edit_sp_team' => true,
|
||||
'edit_sp_teams' => true,
|
||||
'read_sp_teams' => true,
|
||||
'delete_sp_teams' => true,
|
||||
|
||||
'edit_sp_list' => true,
|
||||
'edit_sp_lists' => true,
|
||||
'read_sp_lists' => true,
|
||||
'delete_sp_lists' => true,
|
||||
)
|
||||
);
|
||||
|
||||
// League manager role
|
||||
add_role( 'sp_league_manager', __( 'League Manager', 'sportspress' ), array(
|
||||
'level_9' => true,
|
||||
'level_8' => true,
|
||||
'level_7' => true,
|
||||
'level_6' => true,
|
||||
'level_5' => true,
|
||||
'level_4' => true,
|
||||
'level_3' => true,
|
||||
'level_2' => true,
|
||||
'level_1' => true,
|
||||
'level_0' => true,
|
||||
'read' => true,
|
||||
'read_private_pages' => true,
|
||||
'read_private_posts' => true,
|
||||
'edit_users' => true,
|
||||
'edit_posts' => true,
|
||||
'edit_pages' => true,
|
||||
'edit_published_posts' => true,
|
||||
'edit_published_pages' => true,
|
||||
'edit_private_pages' => true,
|
||||
'edit_private_posts' => true,
|
||||
'edit_others_posts' => true,
|
||||
'edit_others_pages' => true,
|
||||
'publish_posts' => true,
|
||||
'publish_pages' => true,
|
||||
'delete_posts' => true,
|
||||
'delete_pages' => true,
|
||||
'delete_private_pages' => true,
|
||||
'delete_private_posts' => true,
|
||||
'delete_published_pages' => true,
|
||||
'delete_published_posts' => true,
|
||||
'delete_others_posts' => true,
|
||||
'delete_others_pages' => true,
|
||||
'manage_categories' => true,
|
||||
'manage_links' => true,
|
||||
'moderate_comments' => true,
|
||||
'unfiltered_html' => true,
|
||||
'upload_files' => true,
|
||||
'export' => true,
|
||||
'import' => true,
|
||||
'list_users' => true
|
||||
) );
|
||||
|
||||
$capabilities = $this->get_core_capabilities();
|
||||
|
||||
foreach ( $capabilities as $cap_group ) {
|
||||
foreach ( $cap_group as $cap ) {
|
||||
$wp_roles->add_cap( 'sp_league_manager', $cap );
|
||||
$wp_roles->add_cap( 'administrator', $cap );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get capabilities for SportsPress - these are assigned to admin/shop manager during installation or reset
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_core_capabilities() {
|
||||
$capabilities = array();
|
||||
|
||||
$capabilities['core'] = array(
|
||||
'manage_sportspress',
|
||||
'view_sportspress_reports'
|
||||
);
|
||||
|
||||
$capability_types = array( 'sp_config', 'sp_event', 'sp_calendar', 'sp_team', 'sp_table', 'sp_player', 'sp_list', 'sp_staff' );
|
||||
|
||||
foreach ( $capability_types as $capability_type ) {
|
||||
|
||||
$capabilities[ $capability_type ] = array(
|
||||
// Post type
|
||||
"edit_{$capability_type}",
|
||||
"read_{$capability_type}",
|
||||
"delete_{$capability_type}",
|
||||
"edit_{$capability_type}s",
|
||||
"edit_others_{$capability_type}s",
|
||||
"publish_{$capability_type}s",
|
||||
"read_private_{$capability_type}s",
|
||||
"delete_{$capability_type}s",
|
||||
"delete_private_{$capability_type}s",
|
||||
"delete_published_{$capability_type}s",
|
||||
"delete_others_{$capability_type}s",
|
||||
"edit_private_{$capability_type}s",
|
||||
"edit_published_{$capability_type}s",
|
||||
|
||||
// Terms
|
||||
"manage_{$capability_type}_terms",
|
||||
"edit_{$capability_type}_terms",
|
||||
"delete_{$capability_type}_terms",
|
||||
"assign_{$capability_type}_terms"
|
||||
);
|
||||
}
|
||||
|
||||
return $capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* sportspress_remove_roles function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function remove_roles() {
|
||||
global $wp_roles;
|
||||
|
||||
if ( class_exists( 'WP_Roles' ) ) {
|
||||
if ( ! isset( $wp_roles ) ) {
|
||||
$wp_roles = new WP_Roles();
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_object( $wp_roles ) ) {
|
||||
|
||||
$capabilities = $this->get_core_capabilities();
|
||||
|
||||
foreach ( $capabilities as $cap_group ) {
|
||||
foreach ( $cap_group as $cap ) {
|
||||
$wp_roles->remove_cap( 'sp_player', $cap );
|
||||
$wp_roles->remove_cap( 'sp_staff', $cap );
|
||||
$wp_roles->remove_cap( 'sp_team_manager', $cap );
|
||||
$wp_roles->remove_cap( 'sp_league_manager', $cap );
|
||||
$wp_roles->remove_cap( 'administrator', $cap );
|
||||
}
|
||||
}
|
||||
|
||||
remove_role( 'sp_player' );
|
||||
remove_role( 'sp_staff' );
|
||||
remove_role( 'sp_team_manager' );
|
||||
remove_role( 'sp_league_manager' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Active plugins pre update option filter
|
||||
*
|
||||
* @param string $new_value
|
||||
* @return string
|
||||
*/
|
||||
function pre_update_option_active_plugins( $new_value ) {
|
||||
$old_value = (array) get_option( 'active_plugins' );
|
||||
|
||||
if ( $new_value !== $old_value && in_array( W3TC_FILE, (array) $new_value ) && in_array( W3TC_FILE, (array) $old_value ) ) {
|
||||
$this->_config->set( 'notes.plugins_updated', true );
|
||||
try {
|
||||
$this->_config->save();
|
||||
} catch( Exception $ex ) {}
|
||||
}
|
||||
|
||||
return $new_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show plugin changes. Code adapted from W3 Total Cache.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function in_plugin_update_message() {
|
||||
$response = wp_remote_get( 'https://plugins.svn.wordpress.org/sportspress/trunk/readme.txt' );
|
||||
|
||||
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
|
||||
|
||||
// Output Upgrade Notice
|
||||
$matches = null;
|
||||
$regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( SP_VERSION ) . '\s*=|$)~Uis';
|
||||
|
||||
if ( preg_match( $regexp, $response['body'], $matches ) ) {
|
||||
$version = trim( $matches[1] );
|
||||
$notices = (array) preg_split('~[\r\n]+~', trim( $matches[2] ) );
|
||||
|
||||
if ( version_compare( SP_VERSION, $version, '<' ) ) {
|
||||
|
||||
echo '<div style="font-weight: normal; background: #cc99c2; color: #fff !important; border: 1px solid #b76ca9; padding: 9px; margin: 9px 0;">';
|
||||
|
||||
foreach ( $notices as $index => $line ) {
|
||||
echo '<p style="margin: 0; font-size: 1.1em; color: #fff; text-shadow: 0 1px 1px #b574a8;">' . wp_kses_post( preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line ) ) . '</p>';
|
||||
}
|
||||
|
||||
echo '</div> ';
|
||||
}
|
||||
}
|
||||
|
||||
// Output Changelog
|
||||
$matches = null;
|
||||
$regexp = '~==\s*Changelog\s*==\s*=\s*[0-9.]+\s*-(.*)=(.*)(=\s*' . preg_quote( SP_VERSION ) . '\s*-(.*)=|$)~Uis';
|
||||
|
||||
if ( preg_match( $regexp, $response['body'], $matches ) ) {
|
||||
$changelog = (array) preg_split( '~[\r\n]+~', trim( $matches[2] ) );
|
||||
|
||||
echo __( 'What\'s new:', 'sportspress' ) . '<div style="font-weight: normal;">';
|
||||
|
||||
$ul = false;
|
||||
|
||||
foreach ( $changelog as $index => $line ) {
|
||||
if ( preg_match('~^\s*\*\s*~', $line ) ) {
|
||||
if ( ! $ul ) {
|
||||
echo '<ul style="list-style: disc inside; margin: 9px 0 9px 20px; overflow:hidden; zoom: 1;">';
|
||||
$ul = true;
|
||||
}
|
||||
|
||||
$line = preg_replace( '~^\s*\*\s*~', '', htmlspecialchars( $line ) );
|
||||
|
||||
echo '<li style="width: 50%; margin: 0; float: left; ' . ( $index % 2 == 0 ? 'clear: left;' : '' ) . '">' . esc_html( $line ) . '</li>';
|
||||
} else {
|
||||
|
||||
$version = trim( current( explode( '-', str_replace( '=', '', $line ) ) ) );
|
||||
|
||||
if ( version_compare( SP_VERSION, $version, '>=' ) ) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ( $ul ) {
|
||||
echo '</ul>';
|
||||
$ul = false;
|
||||
}
|
||||
|
||||
echo '<p style="margin: 9px 0;">' . esc_html( htmlspecialchars( $line ) ) . '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $ul ) {
|
||||
echo '</ul>';
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
||||
|
||||
return new SP_Install();
|
||||
Reference in New Issue
Block a user