EoiNewCssMigration

The Mailchimp Forms by Optin Cat EoiNewCssMigration class.

Defined (1)

The class is defined in the following location(s).

/powerups/4_new_css/powerup.php  
  1. class EoiNewCssMigration { 
  2. private $layouts; 
  3. private $layout_ids; 
  4.  
  5. public function migrate( $from, $to ) { 
  6. foreach ( get_posts( array( 'post_type' => 'easy-opt-ins', 'posts_per_page' => -1 ) ) as $optin_form ) { 
  7. $this->migrate_form( $optin_form, $from, $to ); 
  8.  
  9. private function migrate_form( $optin_form, $from, $to ) { 
  10. $form_id = $optin_form->ID; 
  11. $fca_eoi = get_post_meta( $form_id, 'fca_eoi', true ); 
  12.  
  13. $fca_eoi_changed = false; 
  14. foreach ( $this->get_layout_ids() as $layout_id ) { 
  15. if ( $layout_id == 'layout_2' || $layout_id == 'postbox_2' || $layout_id == 'lightbox_2' ) { 
  16. $this->prepare_for_layout_2( $layout_id, $fca_eoi, $from ); 
  17.  
  18. if ( $this->migrate_layout( $layout_id, $fca_eoi, $from, $to ) ) { 
  19. $fca_eoi_changed = true; 
  20.  
  21. if ( $fca_eoi_changed ) { 
  22. delete_post_meta( $form_id, 'fca_eoi' ); 
  23. add_post_meta( $form_id, 'fca_eoi', $fca_eoi ); 
  24.  
  25. private function prepare_for_layout_2( $layout_id, &$fca_eoi, $from ) { 
  26. if ( empty( $fca_eoi[ $layout_id ] ) ) { 
  27. return; 
  28.  
  29. foreach ( $fca_eoi[ $layout_id ] as $main_selector => $attributes ) { 
  30. foreach ( $attributes as $sub_selector => $value ) { 
  31. if ( $from == 'old' && $sub_selector == 'border-top-color' ) { 
  32. $fca_eoi[ $layout_id ][ $main_selector ]['fill'] = $value; 
  33. unset( $fca_eoi[ $layout_id ][ $main_selector ][ $sub_selector ] ); 
  34. } elseif ( $from == 'new' && $sub_selector == 'fill' ) { 
  35. $fca_eoi[ $layout_id ][ $main_selector ]['border-top-color'] = $value; 
  36. unset( $fca_eoi[ $layout_id ][ $main_selector ][ $sub_selector ] ); 
  37.  
  38. private function migrate_layout( $layout_id, &$fca_eoi, $from, $to ) { 
  39. $layouts = $this->get_layouts(); 
  40. $fca_eoi_changed = false; 
  41.  
  42. if ( ! empty( $fca_eoi[ $layout_id ] ) ) { 
  43. $from_selectors = $layouts[ $layout_id ][ $from ]; 
  44. $to_selectors = $layouts[ $layout_id ][ $to ]; 
  45.  
  46. for ( $i = 0, $len = count( $from_selectors ); $i < $len; $i++ ) { 
  47. $from_selector = $from_selectors[ $i ]; 
  48. $to_selector = $to_selectors[ $i ]; 
  49.  
  50. if ( ! empty( $fca_eoi[ $layout_id ][ $from_selector ] ) ) { 
  51. $fca_eoi_changed == true ? '' : $fca_eoi_changed = true; 
  52.  
  53. $this->migrate_selector( $from_selector, $to_selector, $layout_id, $fca_eoi ); 
  54.  
  55. return $fca_eoi_changed; 
  56.  
  57. private function migrate_selector( $from_selector, $to_selector, $layout_id, &$fca_eoi ) { 
  58. $fca_eoi[ $layout_id ][ $to_selector ] = $fca_eoi[ $layout_id ][ $from_selector ]; 
  59. unset( $fca_eoi[ $layout_id ][ $from_selector ] ); 
  60.  
  61. private function get_layouts() { 
  62. if ( ! empty( $this->layouts ) ) { 
  63. return $this->layouts; 
  64.  
  65. $layouts = array(); 
  66.  
  67. foreach ( glob( FCA_EOI_PLUGIN_DIR . 'layouts/*/*/layout-new.php' ) as $path ) { 
  68. if ( strpos( $path, '/common/' ) !== false ) { 
  69. continue; 
  70.  
  71. $path_info = pathinfo( $path ); 
  72. $layout_id = basename( $path_info['dirname'] ); 
  73. $old_layout = $path_info['dirname'] . DIRECTORY_SEPARATOR . 'layout.php'; 
  74. $new_layout = $path; 
  75.  
  76. $layouts[ $layout_id ] = array( 
  77. 'old' => null,  
  78. 'new' => null 
  79. ); 
  80.  
  81. $layout = null; 
  82.  
  83. require $old_layout; 
  84. $layouts[ $layout_id ]['old'] = $this->extract_main_selectors_from_layout( $layout ); 
  85.  
  86. require $new_layout; 
  87. $layouts[ $layout_id ]['new'] = $this->extract_main_selectors_from_layout( $layout ); 
  88.  
  89. $this->layouts = $layouts; 
  90. return $layouts; 
  91.  
  92. private function extract_main_selectors_from_layout( $layout ) { 
  93. if ( empty( $layout['editables'] ) ) { 
  94. return array(); 
  95.  
  96. $selectors = array(); 
  97.  
  98. foreach ( $layout['editables'] as $parts ) { 
  99. $selectors = array_merge( $selectors, array_keys( $parts ) ); 
  100.  
  101. return $selectors; 
  102.  
  103. private function get_layout_ids() { 
  104. if ( empty( $this->layout_ids ) ) { 
  105. $this->layout_ids = array_keys( $this->get_layouts() ); 
  106. return $this->layout_ids;