update
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace SmashBalloon\YouTubeFeed\Services\Upgrade;
|
||||
|
||||
use Smashballoon\Stubs\Services\ServiceProvider;
|
||||
use SmashBalloon\YouTubeFeed\Container;
|
||||
use SmashBalloon\YouTubeFeed\Services\Upgrade\Routines\UpgradeRoutine;
|
||||
use SmashBalloon\YouTubeFeed\Services\Upgrade\Routines\V2Routine;
|
||||
use SmashBalloon\YouTubeFeed\Services\Upgrade\Routines\OnboardingWizardRoutine;
|
||||
|
||||
class RoutineManagerService extends ServiceProvider {
|
||||
/**
|
||||
* a list of upgrade routines to be executed,
|
||||
* keep the correct order, newer is always at the end of the list.
|
||||
* @var UpgradeRoutine[]
|
||||
*/
|
||||
private $routines = [
|
||||
V2Routine::class,
|
||||
OnboardingWizardRoutine::class
|
||||
];
|
||||
|
||||
public function is_fresh_install() {
|
||||
$db_version = get_option( 'sby_db_version', false );
|
||||
return false === $db_version;
|
||||
}
|
||||
|
||||
public function register() {
|
||||
$container = Container::get_instance();
|
||||
$is_fresh_install = $this->is_fresh_install();
|
||||
|
||||
foreach ($this->routines as $routine) {
|
||||
$routine = $container->get($routine);
|
||||
$routine->set_is_fresh_install($is_fresh_install);
|
||||
$routine->register();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace SmashBalloon\YouTubeFeed\Services\Upgrade\Routines;
|
||||
|
||||
class OnboardingWizardRoutine extends UpgradeRoutine {
|
||||
protected $target_version = 2.3;
|
||||
|
||||
public function run() {
|
||||
$this->set_onboarding_wizard_flag();
|
||||
}
|
||||
|
||||
private function set_onboarding_wizard_flag() {
|
||||
if($this->is_fresh_install) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sby_statuses_option = get_option( 'sby_statuses', array() );
|
||||
if( !isset( $sby_statuses_option['wizard_dismissed'] ) || $sby_statuses_option['wizard_dismissed'] === false){
|
||||
$sby_statuses_option['wizard_dismissed'] = true;
|
||||
update_option( 'sby_statuses', $sby_statuses_option );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace SmashBalloon\YouTubeFeed\Services\Upgrade\Routines;
|
||||
|
||||
use Smashballoon\Stubs\Services\ServiceProvider;
|
||||
|
||||
class UpgradeRoutine extends ServiceProvider {
|
||||
protected $target_version = 0;
|
||||
protected $is_fresh_install = false;
|
||||
|
||||
public function register() {
|
||||
if ( $this->will_run() ) {
|
||||
$this->run();
|
||||
$this->update_db_version();
|
||||
}
|
||||
}
|
||||
|
||||
protected function will_run() {
|
||||
$current_schema = (float) get_option( 'sby_db_version', 0 );
|
||||
|
||||
return $current_schema < (float) $this->target_version;
|
||||
}
|
||||
|
||||
protected function update_db_version() {
|
||||
update_option('sby_db_version', $this->target_version);
|
||||
}
|
||||
|
||||
public function run() {
|
||||
//implement your own version
|
||||
}
|
||||
|
||||
/**
|
||||
* Set fresh install flag.
|
||||
*
|
||||
* @param $is_fresh_install
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set_is_fresh_install($is_fresh_install) {
|
||||
$this->is_fresh_install = $is_fresh_install;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace SmashBalloon\YouTubeFeed\Services\Upgrade\Routines;
|
||||
|
||||
use Smashballoon\Customizer\DB;
|
||||
use Smashballoon\Customizer\Feed_Saver;
|
||||
use Smashballoon\Customizer\Feed_Locator;
|
||||
|
||||
class V2Routine extends UpgradeRoutine {
|
||||
protected $target_version = 2.0;
|
||||
|
||||
/**
|
||||
* @var Feed_Saver
|
||||
*/
|
||||
private $feed_saver;
|
||||
|
||||
/**
|
||||
* @var Feed_Locator
|
||||
*/
|
||||
private $feed_locator;
|
||||
|
||||
/**
|
||||
* @var DB
|
||||
*/
|
||||
private $db;
|
||||
|
||||
public function __construct(Feed_Saver $feed_saver, DB $DB, Feed_Locator $feed_locator) {
|
||||
$this->feed_saver = $feed_saver;
|
||||
$this->feed_locator = $feed_locator;
|
||||
$this->db = $DB;
|
||||
}
|
||||
|
||||
public function run() {
|
||||
$this->create_tables();
|
||||
$this->migrate_legacy_feeds();
|
||||
$this->set_rating_notice_and_first_install_flags();
|
||||
}
|
||||
|
||||
private function create_tables() {
|
||||
$this->feed_locator->create_table();
|
||||
$this->db->create_tables(true, true);
|
||||
}
|
||||
|
||||
private function migrate_legacy_feeds() {
|
||||
$statuses_option = get_option( 'sby_statuses', array() );
|
||||
$args = array(
|
||||
'html_location' => array( 'header', 'footer', 'sidebar', 'content', 'unknown' ),
|
||||
'group_by' => 'shortcode_atts',
|
||||
'page' => 1
|
||||
);
|
||||
$feeds_data = $this->feed_locator->legacy_feed_locator_query($args);
|
||||
$legacy_count = count($feeds_data);
|
||||
$statuses_option['support_legacy_shortcode'] = false;
|
||||
|
||||
if($legacy_count > 0) {
|
||||
if($legacy_count > 1) {
|
||||
$statuses_option['legacy_onboarding'] = array(
|
||||
'active' => true,
|
||||
'type'=> 'multiple'
|
||||
);
|
||||
$statuses_option['support_legacy_shortcode'] = true;
|
||||
} else {
|
||||
$statuses_option['legacy_onboarding'] = array(
|
||||
'active' => true,
|
||||
'type'=> 'single'
|
||||
);
|
||||
|
||||
$shortcode_atts = ! empty($feeds_data[0] ) && $feeds_data[0]['shortcode_atts'] != '[""]' ? json_decode( $feeds_data[0]['shortcode_atts'], true ) : [];
|
||||
$shortcode_atts = is_array( $shortcode_atts ) ? $shortcode_atts : array();
|
||||
|
||||
$statuses_option['support_legacy_shortcode'] = true;
|
||||
|
||||
$shortcode_atts['from_update'] = true;
|
||||
|
||||
$this->feed_saver->set_data( $shortcode_atts );
|
||||
$this->feed_saver->set_feed_name( "Legacy feed" );
|
||||
|
||||
$new_feed_id = $this->feed_saver->update_or_insert();
|
||||
|
||||
$args = array(
|
||||
'new_feed_id' => $new_feed_id,
|
||||
'legacy_feed_id' => $feeds_data[0]['feed_id'],
|
||||
);
|
||||
|
||||
$this->feed_locator->update_legacy_to_builder( $args );
|
||||
}
|
||||
}
|
||||
|
||||
update_option( 'sby_statuses', $statuses_option, true );
|
||||
}
|
||||
|
||||
private function set_rating_notice_and_first_install_flags() {
|
||||
$sby_statuses_option = get_option( 'sby_statuses', array() );
|
||||
|
||||
if ( ! isset( $sby_statuses_option['first_install'] ) ) {
|
||||
|
||||
$options_set = get_option( 'sby_settings', false );
|
||||
|
||||
if ( $options_set ) {
|
||||
$sby_statuses_option['first_install'] = 'from_update';
|
||||
} else {
|
||||
$sby_statuses_option['first_install'] = time();
|
||||
}
|
||||
|
||||
$sby_rating_notice_option = get_option( 'sby_rating_notice', false );
|
||||
|
||||
if ( $sby_rating_notice_option === 'dismissed' ) {
|
||||
$sby_statuses_option['rating_notice_dismissed'] = time();
|
||||
}
|
||||
|
||||
$sby_rating_notice_waiting = get_transient( 'feeds_for_youtube_rating_notice_waiting' );
|
||||
|
||||
if ( $sby_rating_notice_waiting === false
|
||||
&& $sby_rating_notice_option === false ) {
|
||||
$time = 2 * WEEK_IN_SECONDS;
|
||||
set_transient( 'feeds_for_youtube_rating_notice_waiting', 'waiting', $time );
|
||||
update_option( 'sby_rating_notice', 'pending', false );
|
||||
}
|
||||
|
||||
update_option( 'sby_statuses', $sby_statuses_option, false );
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user