| 1 |
<?php |
| 2 |
/** |
| 3 |
* Migrations |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Migrations |
| 14 |
*/ |
| 15 |
class GhostKit_Migrations { |
| 16 |
/** |
| 17 |
* The version. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $version = GHOSTKIT_VERSION; |
| 22 |
|
| 23 |
/** |
| 24 |
* Initial version. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $initial_version = '2.25.0'; |
| 29 |
|
| 30 |
/** |
| 31 |
* GhostKit_Migrations constructor. |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
if ( is_admin() ) { |
| 35 |
add_action( 'admin_init', array( $this, 'init' ), 3 ); |
| 36 |
} else { |
| 37 |
add_action( 'wp', array( $this, 'init' ), 3 ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Init. |
| 43 |
*/ |
| 44 |
public function init() { |
| 45 |
// Migration code added after `$this->initial_version` plugin version. |
| 46 |
$saved_version = get_option( 'vpf_db_version', $this->initial_version ); |
| 47 |
$current_version = $this->version; |
| 48 |
|
| 49 |
foreach ( $this->get_migrations() as $migration ) { |
| 50 |
if ( version_compare( $saved_version, $migration['version'], '<' ) ) { |
| 51 |
call_user_func( $migration['cb'] ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ( version_compare( $saved_version, $current_version, '<' ) ) { |
| 56 |
update_option( 'vpf_db_version', $current_version ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get all available migrations. |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function get_migrations() { |
| 66 |
return array( |
| 67 |
array( |
| 68 |
'version' => '2.25.1', |
| 69 |
'cb' => array( $this, 'v_2_25_1' ), |
| 70 |
), |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Fix typography with google-fonts category. |
| 76 |
*/ |
| 77 |
public function v_2_25_1() { |
| 78 |
$current_typography_encode = get_option( 'ghostkit_typography', array() ); |
| 79 |
$typography_migrated = false; |
| 80 |
if ( ! empty( $current_typography_encode ) ) { |
| 81 |
$current_typography_decode = json_decode( $current_typography_encode['ghostkit_typography'], true ); |
| 82 |
$fonts_list = apply_filters( 'gkt_fonts_list', array() ); |
| 83 |
if ( ! empty( $current_typography_decode ) && ! empty( $fonts_list ) ) { |
| 84 |
foreach ( $current_typography_decode as &$typography ) { |
| 85 |
if ( |
| 86 |
isset( $typography['fontFamilyCategory'] ) && |
| 87 |
'default' === $typography['fontFamilyCategory'] && |
| 88 |
isset( $typography['fontFamily'] ) |
| 89 |
) { |
| 90 |
// Find typography font from fonts list default category. |
| 91 |
$found_default_font = null !== $fonts_list['default']['fonts'] ? array_search( $typography['fontFamily'], array_column( $fonts_list['default']['fonts'], 'name' ), true ) : false; |
| 92 |
// If not find, search font from google fonts category. |
| 93 |
if ( false === $found_default_font ) { |
| 94 |
$found_google_font = array_search( $typography['fontFamily'], array_column( $fonts_list['google-fonts']['fonts'], 'name' ), true ); |
| 95 |
if ( false !== $found_google_font ) { |
| 96 |
$typography['fontFamilyCategory'] = 'google-fonts'; |
| 97 |
$typography_migrated = true; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
if ( $typography_migrated ) { |
| 103 |
$current_typography_encode['ghostkit_typography'] = wp_json_encode( $current_typography_decode ); |
| 104 |
update_option( 'ghostkit_typography', $current_typography_encode ); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
new GhostKit_Migrations(); |
| 112 |
|