Skip to content

Commit

Permalink
Add updater code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikola committed Oct 5, 2017
1 parent 69a25ee commit 81bfab8
Show file tree
Hide file tree
Showing 15 changed files with 1,116 additions and 203 deletions.
2 changes: 1 addition & 1 deletion includes/CMB2/includes/CMB2_Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static function array_insert( &$array, $new, $position ) {
*/
public static function url( $path = '' ) {
// SM: Seems like CMB2 doesn't return a correct URL. This should fix it.
return str_replace( get_site_url(), '', SERMON_MANAGER_URL ) . 'includes/CMB2/' . $path;
return str_replace( get_site_url(), '', SM_URL ) . 'includes/CMB2/' . $path;

if ( self::$url ) {
return self::$url . $path;
Expand Down
107 changes: 107 additions & 0 deletions includes/class-sm-background-updater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
defined( 'ABSPATH' ) or die; // exit if accessed directly

if ( ! class_exists( 'WP_Async_Request', false ) ) {
include_once 'libraries/wp-async-request.php';
}

if ( ! class_exists( 'WP_Background_Process', false ) ) {
include_once 'libraries/wp-background-process.php';
}

/**
* @since 2.8
*/
class SM_Background_Updater extends WP_Background_Process {

/**
* @var string
*/
protected $action = 'sm_updater';

/**
* Dispatch updater.
*
* Updater will still run via cron job if this fails for any reason.
*/
public function dispatch() {
parent::dispatch();
}

/**
* Handle cron healthcheck
*
* Restart the background process if not already running
* and data exists in the queue.
*/
public function handle_cron_healthcheck() {
if ( $this->is_process_running() ) {
// Background process already running.
return;
}

if ( $this->is_queue_empty() ) {
// No data to process.
$this->clear_scheduled_event();

return;
}

$this->handle();
}

/**
* Schedule fallback event.
*/
protected function schedule_event() {
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier );
}
}

/**
* Is the updater running?
*
* @return boolean
*/
public function is_updating() {
return false === $this->is_queue_empty();
}

/**
* Task
*
* Override this method to perform any actions required on each
* queue item. Return the modified item for further processing
* in the next pass through. Or, return false to remove the
* item from the queue.
*
* @param string $callback Update callback function
*
* @return mixed
*/
protected function task( $callback ) {
if ( ! defined( 'SM_UPDATING' ) ) {
define( 'SM_UPDATING', true );
}

include_once( dirname( __FILE__ ) . '/sm-update-functions.php' );

if ( is_callable( $callback ) ) {
call_user_func( $callback );
}

return false;
}

/**
* Complete
*
* Override if applicable, but ensure that the below actions are
* performed, or, call parent::complete().
*/
protected function complete() {
SM_Install::update_db_version();
parent::complete();
}
}
6 changes: 4 additions & 2 deletions includes/class-sm-dates-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ public static function update_series_date() {
}
}

arsort( $dates );
update_term_meta( $term->term_id, 'sermon_date', $dates[0] );
if ( ! empty( $dates ) ) {
arsort( $dates );
update_term_meta( $term->term_id, 'sermon_date', $dates[0] );
}
}
}
}
Expand Down
16 changes: 6 additions & 10 deletions includes/class-sm-error-recovery.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
defined( 'ABSPATH' ) or die; // exit if accessed directly

/**
* After leaving many websites temporarily unusable (mostly because of usage on old and outdated PHP
Expand Down Expand Up @@ -26,7 +27,7 @@ class SM_Error_Recovery {
* @var string Name of constant that has "__FILE__" magic constant of main plugin file
* @access private
*/
private static $_plugin_main_file = 'SM___FILE__';
private static $_plugin_main_file = 'SM_PLUGIN_FILE';

/**
* @var array Errors to catch
Expand Down Expand Up @@ -256,14 +257,14 @@ public static function enqueue_scripts_styles() {
$plugin_data = get_plugin_data( constant( self::$_plugin_main_file ) );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-dialog' );
wp_enqueue_script( 'sm-error-recovery', SERMON_MANAGER_URL . 'assets/js/admin/error-recovery.js', array(), SERMON_MANAGER_VERSION );
wp_enqueue_script( 'sm-error-recovery', SM_URL . 'assets/js/admin/error-recovery.js', array(), SM_VERSION );
wp_localize_script( 'sm-error-recovery', 'sm_error_recovery_data', array(
'stacktrace' => urlencode( str_replace( ABSPATH, '~/', get_option( '_sm_recovery_last_fatal_error' ) ) ),
'environment_info' => 'WordPress: ' . $GLOBALS['wp_version'] . '; Server: ' . ( function_exists( 'apache_get_version' ) ? apache_get_version() : 'N/A' ) . '; PHP: ' . PHP_VERSION . '; Sermon Manager:' . SERMON_MANAGER_VERSION . ';',
'environment_info' => 'WordPress: ' . $GLOBALS['wp_version'] . '; Server: ' . ( function_exists( 'apache_get_version' ) ? apache_get_version() : 'N/A' ) . '; PHP: ' . PHP_VERSION . '; Sermon Manager:' . SM_VERSION . ';',
'plugin_name' => $plugin_data['Name'],

) );
wp_enqueue_style( 'sm-error-recovery', SERMON_MANAGER_URL . 'assets/css/error-recovery.css', array(), SERMON_MANAGER_VERSION );
wp_enqueue_style( 'sm-error-recovery', SM_URL . 'assets/css/error-recovery.css', array(), SM_VERSION );
}

/**
Expand All @@ -281,14 +282,9 @@ public static function disable_send_report_button() {
*/
public static function upgrade_check() {
$db_version = get_option( 'sm_version' );
if ( empty( $db_version ) || $db_version != SERMON_MANAGER_VERSION ) {
if ( empty( $db_version ) || $db_version != SM_VERSION ) {
update_option( '_sm_recovery_do_not_catch', 0 );
update_option( '_sm_recovery_disable', 0 );
update_option( 'sm_version', SERMON_MANAGER_VERSION );

// Flush rewrite rules after update
add_action( 'sm_after_register_post_type', array( 'SM_Post_Types', 'flush_rewrite_rules_hard' ) );
add_action( 'sm_after_register_taxonomy', array( 'SM_Post_Types', 'flush_rewrite_rules_hard' ) );
}
}

Expand Down
195 changes: 195 additions & 0 deletions includes/class-sm-install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php
defined( 'ABSPATH' ) or die; // exit if accessed directly

/**
* Used on installation/update
*
* @since 2.8
*/
class SM_Install {
/** @var array DB updates and callbacks that need to be run per version */
private static $db_updates = array(
'2.8' => array(
'sm_update_28_revert_old_dates',
'sm_update_28_convert_dates_to_unix',
'sm_update_28_fill_out_empty_dates',
'sm_update_28_fill_out_series_dates',
'sm_update_28_save_sermon_render_into_post_content',
'sm_update_28_reset_recovery',
)
);

/** @var object Background update class */
private static $background_updater;

public static function init() {
add_action( 'init', array( __CLASS__, 'init_background_updater' ), 3 );
add_action( 'init', array( __CLASS__, 'check_version' ), 8 );
add_filter( 'plugin_action_links_' . SM_BASENAME, array( __CLASS__, 'plugin_action_links' ) );
add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 );
add_filter( 'cron_schedules', array( __CLASS__, 'cron_schedules' ) );
}

/**
* Check Sermon Manager version and run the updater is required
*
* This check is done on all requests and runs if the versions do not match
*/
public static function check_version() {
if ( ! defined( 'IFRAME_REQUEST' ) && get_option( 'sm_version' ) !== SM_VERSION ) {
self::_install();
do_action( 'sm_updated' );
}
}

/**
* Install Sermon Manager
*/
private static function _install() {
global $wpdb;

if ( ! is_blog_installed() ) {
return;
}

if ( ! defined( 'SM_INSTALLING' ) ) {
define( 'SM_INSTALLING', true );
}

//self::_create_options(); todo: will be done in future versions, every option will have its own field in the database, so we will just use `add_option()` - it won't overwrite the field
//self::_create_roles(); todo: will be done in future versions

// Register post types
SM_Post_types::register_post_types();
SM_Post_types::register_taxonomies();

// do update
self::_update();

// Update version just in case
self::update_db_version();

// Flush rules after install
do_action( 'sm_flush_rewrite_rules' );

/*
* Deletes all expired transients. The multi-table delete syntax is used
* to delete the transient record from table a, and the corresponding
* transient_timeout record from table b.
*
* Based on code inside core's upgrade_network() function.
*/
/** @noinspection SqlNoDataSourceInspection */
$sql = "DELETE a, b FROM $wpdb->options a, $wpdb->options b
WHERE a.option_name LIKE %s
AND a.option_name NOT LIKE %s
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
AND b.option_value < %d";
$wpdb->query( $wpdb->prepare( $sql, $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', time() ) );

// Trigger action
do_action( 'sm_installed' );
}

/**
* Push all needed DB updates to the queue for processing.
*/
private static function _update() {
$current_db_version = get_option( 'sm_version' );
$update_queued = false;

foreach ( self::_get_db_update_callbacks() as $version => $update_callbacks ) {
if ( version_compare( $current_db_version, $version, '<' ) ) {
foreach ( $update_callbacks as $update_callback ) {
self::$background_updater->push_to_queue( $update_callback );
$update_queued = true;
}
}
}

if ( $update_queued ) {
self::$background_updater->save()->dispatch();
}
}

/**
* Get list of DB update callbacks.
*
* @return array
*/
private static function _get_db_update_callbacks() {
return self::$db_updates;
}

/**
* Init background updates
*/
public static function init_background_updater() {
include_once 'class-sm-background-updater.php';
self::$background_updater = new SM_Background_Updater();
}

/**
* Update DB version to current.
*
* @param string $version (optional)
*/
public static function update_db_version( $version = null ) {
delete_option( 'sm_version' );
add_option( 'sm_version', is_null( $version ) ? SM_VERSION : $version );
}

/**
* Add more cron schedules
*
* @param array $schedules
*
* @return array
*/
public static function cron_schedules( $schedules ) {
$schedules['monthly'] = array(
'interval' => 2635200,
'display' => __( 'Monthly', 'sermon-manager-for-wordpress' ),
);

return $schedules;
}

/**
* Show action links on the plugin screen.
*
* @param mixed $links Plugin Action links
*
* @return array
*/
public static function plugin_action_links( $links ) {
$action_links = array(
'settings' => '<a href="' . admin_url( 'edit.php?post_type=wpfc_sermon&page=Sermon-Manager%2Fincludes%2Foptions.php' ) . '" aria-label="' . esc_attr__( 'View Sermon Manager settings', 'sermon-manager-for-wordpress' ) . '">' . esc_html__( 'Settings' ) . '</a>',
);

return array_merge( $action_links, $links );
}

/**
* Show row meta on the plugin screen.
*
* @param mixed $links Plugin Row Meta
* @param mixed $file Plugin Base file
*
* @return array
*/
public static function plugin_row_meta( $links, $file ) {
/** @noinspection PhpUndefinedConstantInspection */
if ( SM_BASENAME == $file ) {
$row_meta = array(
'support' => '<a href="' . esc_url( 'https://wpforchurch.com/my/submitticket.php' ) . '" aria-label="' . esc_attr__( 'Visit premium customer support', 'sermon-manager-for-wordpress' ) . '">' . esc_html__( 'Premium support', 'sermon-manager-for-wordpress' ) . '</a>',
);

return array_merge( $links, $row_meta );
}

return (array) $links;
}
}

SM_Install::init();
Loading

0 comments on commit 81bfab8

Please sign in to comment.