| 1 |
<?php |
| 2 |
|
| 3 |
function powerup_new_css_is_active() { |
| 4 |
$paf = get_option( 'paf' ); |
| 5 |
$key = 'eoi_powerup_new_css'; |
| 6 |
|
| 7 |
return ! empty( $paf ) |
| 8 |
&& is_array( $paf ) |
| 9 |
&& ! empty( $paf[ $key ] ) |
| 10 |
&& is_array( $paf[ $key ] ) |
| 11 |
&& in_array( 'on', $paf[ $key ] ); |
| 12 |
} |
| 13 |
|
| 14 |
function powerup_new_css_set_active( $active ) { |
| 15 |
$paf = get_option( 'paf' ); |
| 16 |
$key = 'eoi_powerup_new_css'; |
| 17 |
|
| 18 |
if ( $active ) { |
| 19 |
$paf[ $key ] = array( 'on' ); |
| 20 |
powerup_new_css_on_activate(); |
| 21 |
} else { |
| 22 |
$paf[ $key ] = array(); |
| 23 |
powerup_new_css_on_deactivate(); |
| 24 |
} |
| 25 |
|
| 26 |
update_option( 'paf', $paf ); |
| 27 |
} |
| 28 |
|
| 29 |
function powerup_new_css_on_activate() { |
| 30 |
$new_css_obj = new EoiNewCssMigration(); |
| 31 |
$new_css_obj->migrate( 'old', 'new' ); |
| 32 |
} |
| 33 |
|
| 34 |
function powerup_new_css_on_deactivate() { |
| 35 |
$new_css_obj = new EoiNewCssMigration(); |
| 36 |
$new_css_obj->migrate( 'new', 'old' ); |
| 37 |
} |
| 38 |
|
| 39 |
class EoiNewCssMigration { |
| 40 |
private $layouts; |
| 41 |
private $layout_ids; |
| 42 |
|
| 43 |
public function migrate( $from, $to ) { |
| 44 |
foreach ( get_posts( array( 'post_type' => 'easy-opt-ins', 'posts_per_page' => - 1 ) ) as $optin_form ) { |
| 45 |
$this->migrate_form( $optin_form, $from, $to ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
private function migrate_form( $optin_form, $from, $to ) { |
| 50 |
$form_id = $optin_form->ID; |
| 51 |
$fca_eoi = get_post_meta( $form_id, 'fca_eoi', true ); |
| 52 |
|
| 53 |
$fca_eoi_changed = false; |
| 54 |
foreach ( $this->get_layout_ids() as $layout_id ) { |
| 55 |
if ( $layout_id == 'layout_2' || $layout_id == 'postbox_2' || $layout_id == 'lightbox_2' ) { |
| 56 |
$this->prepare_for_layout_2( $layout_id, $fca_eoi, $from ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( $this->migrate_layout( $layout_id, $fca_eoi, $from, $to ) ) { |
| 60 |
$fca_eoi_changed = true; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if ( $fca_eoi_changed ) { |
| 65 |
delete_post_meta( $form_id, 'fca_eoi' ); |
| 66 |
add_post_meta( $form_id, 'fca_eoi', $fca_eoi ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
private function prepare_for_layout_2( $layout_id, &$fca_eoi, $from ) { |
| 71 |
if ( empty( $fca_eoi[ $layout_id ] ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
foreach ( $fca_eoi[ $layout_id ] as $main_selector => $attributes ) { |
| 76 |
foreach ( $attributes as $sub_selector => $value ) { |
| 77 |
if ( $from == 'old' && $sub_selector == 'border-top-color' ) { |
| 78 |
$fca_eoi[ $layout_id ][ $main_selector ]['fill'] = $value; |
| 79 |
unset( $fca_eoi[ $layout_id ][ $main_selector ][ $sub_selector ] ); |
| 80 |
} elseif ( $from == 'new' && $sub_selector == 'fill' ) { |
| 81 |
$fca_eoi[ $layout_id ][ $main_selector ]['border-top-color'] = $value; |
| 82 |
unset( $fca_eoi[ $layout_id ][ $main_selector ][ $sub_selector ] ); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
private function migrate_layout( $layout_id, &$fca_eoi, $from, $to ) { |
| 89 |
$layouts = $this->get_layouts(); |
| 90 |
$fca_eoi_changed = false; |
| 91 |
|
| 92 |
if ( ! empty( $fca_eoi[ $layout_id ] ) ) { |
| 93 |
$from_selectors = $layouts[ $layout_id ][ $from ]; |
| 94 |
$to_selectors = $layouts[ $layout_id ][ $to ]; |
| 95 |
|
| 96 |
for ( $i = 0, $len = count( $from_selectors ); $i < $len; $i++ ) { |
| 97 |
$from_selector = $from_selectors[ $i ]; |
| 98 |
$to_selector = $to_selectors[ $i ]; |
| 99 |
|
| 100 |
if ( ! empty( $fca_eoi[ $layout_id ][ $from_selector ] ) ) { |
| 101 |
$fca_eoi_changed == true ? '' : $fca_eoi_changed = true; |
| 102 |
|
| 103 |
$this->migrate_selector( $from_selector, $to_selector, $layout_id, $fca_eoi ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return $fca_eoi_changed; |
| 109 |
} |
| 110 |
|
| 111 |
private function migrate_selector( $from_selector, $to_selector, $layout_id, &$fca_eoi ) { |
| 112 |
$fca_eoi[ $layout_id ][ $to_selector ] = $fca_eoi[ $layout_id ][ $from_selector ]; |
| 113 |
unset( $fca_eoi[ $layout_id ][ $from_selector ] ); |
| 114 |
} |
| 115 |
|
| 116 |
private function get_layouts() { |
| 117 |
if ( ! empty( $this->layouts ) ) { |
| 118 |
return $this->layouts; |
| 119 |
} |
| 120 |
|
| 121 |
$layouts = array(); |
| 122 |
|
| 123 |
foreach ( glob( FCA_EOI_PLUGIN_DIR . 'layouts/*/*/layout-new.php' ) as $path ) { |
| 124 |
if ( strpos( $path, '/common/' ) !== false ) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
$path_info = pathinfo( $path ); |
| 129 |
$layout_id = basename( $path_info['dirname'] ); |
| 130 |
$old_layout = $path_info['dirname'] . DIRECTORY_SEPARATOR . 'layout.php'; |
| 131 |
$new_layout = $path; |
| 132 |
|
| 133 |
$layouts[ $layout_id ] = array( |
| 134 |
'old' => null, |
| 135 |
'new' => null |
| 136 |
); |
| 137 |
|
| 138 |
$layout = null; |
| 139 |
|
| 140 |
require $old_layout; |
| 141 |
$layouts[ $layout_id ]['old'] = $this->extract_main_selectors_from_layout( $layout ); |
| 142 |
|
| 143 |
require $new_layout; |
| 144 |
$layouts[ $layout_id ]['new'] = $this->extract_main_selectors_from_layout( $layout ); |
| 145 |
} |
| 146 |
|
| 147 |
$this->layouts = $layouts; |
| 148 |
return $layouts; |
| 149 |
} |
| 150 |
|
| 151 |
private function extract_main_selectors_from_layout( $layout ) { |
| 152 |
if ( empty( $layout['editables'] ) ) { |
| 153 |
return array(); |
| 154 |
} |
| 155 |
|
| 156 |
$selectors = array(); |
| 157 |
|
| 158 |
foreach ( $layout['editables'] as $parts ) { |
| 159 |
$selectors = array_merge( $selectors, array_keys( $parts ) ); |
| 160 |
} |
| 161 |
|
| 162 |
return $selectors; |
| 163 |
} |
| 164 |
|
| 165 |
private function get_layout_ids() { |
| 166 |
if ( empty( $this->layout_ids ) ) { |
| 167 |
$this->layout_ids = array_keys( $this->get_layouts() ); |
| 168 |
} |
| 169 |
return $this->layout_ids; |
| 170 |
} |
| 171 |
} |
| 172 |
|