| 1 |
<?php |
| 2 |
|
| 3 |
abstract class FV_Player_Conversion_Base { |
| 4 |
|
| 5 |
/** |
| 6 |
* If set to true, it will convert data to db |
| 7 |
* |
| 8 |
* @var boolean |
| 9 |
*/ |
| 10 |
public $set_live = false; |
| 11 |
|
| 12 |
protected $title = 'Conversion'; |
| 13 |
protected $slug = 'conversion-slug'; |
| 14 |
protected $screen; |
| 15 |
protected $help; |
| 16 |
protected $screen_fields = array(); |
| 17 |
protected $start_warning_text = 'This will convert your data to new format. Please make sure you have a backup of your database before continuing.'; |
| 18 |
protected $conversion_done_details =false; |
| 19 |
protected $conversion_limit = 1; |
| 20 |
protected $make_chages_button = true; |
| 21 |
|
| 22 |
abstract function convert_one($post); |
| 23 |
|
| 24 |
abstract function get_items( $offset, $limit ); |
| 25 |
|
| 26 |
abstract function conversion_button(); |
| 27 |
|
| 28 |
abstract function iterate_data( $data ); |
| 29 |
|
| 30 |
abstract function build_output_html( $data , $percent_done); |
| 31 |
|
| 32 |
function __construct( $args ) { |
| 33 |
$this->title = $args['title']; |
| 34 |
$this->help = $args['help']; |
| 35 |
$this->slug = $args['slug']; |
| 36 |
$this->screen = 'fv_player_conversion_' . $this->slug; |
| 37 |
|
| 38 |
add_action( 'admin_menu', array( $this, 'admin_page' ) ); |
| 39 |
add_action( 'wp_ajax_'. $this->screen, array( $this, 'ajax_convert') ); |
| 40 |
add_action( 'fv_player_conversion_buttons', array( $this, 'conversion_button') ); |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
function admin_page() { |
| 45 |
add_submenu_page( 'fv_player', 'FV Player Migration', 'FV Player Migration', 'install_plugins', $this->screen, array($this, 'conversion_screen') ); |
| 46 |
remove_submenu_page( 'fv_player', $this->screen ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Count old data |
| 51 |
* |
| 52 |
* @return int $count |
| 53 |
*/ |
| 54 |
function get_count() { |
| 55 |
global $wpdb; |
| 56 |
return $wpdb->get_var( "SELECT FOUND_ROWS()" ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Convert data to new format using ajax |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
function ajax_convert() { |
| 65 |
if ( current_user_can( 'install_plugins' ) && check_ajax_referer( $this->screen ) ) { |
| 66 |
if( function_exists( 'FV_Player_Pro' ) ) { |
| 67 |
// takes too long to save if not removed |
| 68 |
remove_filter( 'content_save_pre', array( FV_Player_Pro(), 'save_post' ), 10 ); |
| 69 |
} |
| 70 |
|
| 71 |
$convert_error = false; |
| 72 |
|
| 73 |
$offset = intval($_POST['offset']); |
| 74 |
$offset = 0 + intval($_POST['offset2']) + $offset; |
| 75 |
$limit = intval($_POST['limit']); |
| 76 |
|
| 77 |
$items = $this->get_items( $offset, $limit ); |
| 78 |
|
| 79 |
$total = $this->get_count(); |
| 80 |
|
| 81 |
// iterate data |
| 82 |
$result = $this->iterate_data( $items ); |
| 83 |
|
| 84 |
$convert_error = $result['convert_error']; |
| 85 |
$conversions_output = $result['conversions_output']; |
| 86 |
|
| 87 |
$percent_done = $total > 0 ? round ( (($offset + $limit) / $total) * 100 ) : 0; |
| 88 |
$left = $total - ($offset + $limit); |
| 89 |
|
| 90 |
// build html output |
| 91 |
$html = $this->build_output_html( $conversions_output, $percent_done ); |
| 92 |
|
| 93 |
// check if we are done |
| 94 |
if( $percent_done >= 100 ) { |
| 95 |
// store conversion status |
| 96 |
$options = get_option( 'fvwpflowplayer' ); |
| 97 |
|
| 98 |
if( !isset( $options['conversion'] ) ) { |
| 99 |
$options['conversion'] = array(); |
| 100 |
} |
| 101 |
|
| 102 |
// store current time as finished time |
| 103 |
$options['conversion'][$this->slug] = array( 'date' => gmdate('Y-m-d H:i:s'), 'did_cleanup' => false ); |
| 104 |
update_option( 'fvwpflowplayer', $options ); |
| 105 |
} |
| 106 |
|
| 107 |
// response |
| 108 |
echo wp_json_encode( |
| 109 |
array( |
| 110 |
'table_rows' => implode( "\n", $html ), |
| 111 |
'percent_done' => $percent_done, |
| 112 |
'left' => $left, |
| 113 |
'total' => $total, |
| 114 |
'convert_error' => $convert_error, |
| 115 |
'memory_used_peak_mb' => round( memory_get_peak_usage() / 1024 / 1024, 2 ), |
| 116 |
'memory_used_mb' => round( memory_get_usage() / 1024 / 1024, 2 ), |
| 117 |
) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
die(); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Create admin page for conversion screen |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
function conversion_screen() { |
| 130 |
global $fv_wp_flowplayer_ver; |
| 131 |
wp_enqueue_script('fv-player-convertor', flowplayer::get_plugin_url().'/js/admin-convertor.js', array('jquery'), filemtime( dirname(__FILE__).'/../../js/admin-convertor.js' ) ); |
| 132 |
|
| 133 |
?> |
| 134 |
<style> |
| 135 |
#wrapper { |
| 136 |
border: 1px solid gray; |
| 137 |
position: relative; |
| 138 |
height: 1em; |
| 139 |
margin-bottom: 1em; |
| 140 |
} |
| 141 |
#progress { |
| 142 |
border-right: 1px solid gray; |
| 143 |
background-color: #800; |
| 144 |
position: absolute; |
| 145 |
left: 0; |
| 146 |
top: 0; |
| 147 |
bottom: 0; |
| 148 |
} |
| 149 |
</style> |
| 150 |
<div class="wrap"> |
| 151 |
<h1><?php echo esc_html( $this->title ); ?></h1> |
| 152 |
|
| 153 |
<?php echo wpautop($this->help); ?> |
| 154 |
<p> |
| 155 |
<input type="hidden" name="action" value="rebuild" /> |
| 156 |
|
| 157 |
<?php if( $this->make_chages_button ): // This checkbox shows the JS confirmation box when clicked to enable ?> |
| 158 |
<input type="checkbox" name="make-changes" id="make-changes" value="1" onclick="if( this.checked ) return confirm('<?php echo esc_attr( $this->start_warning_text ); ?>') " /> <label for="make-changes">Make changes</label> |
| 159 |
<?php endif; ?> |
| 160 |
|
| 161 |
<input class="button-primary" type="submit" name="convert" value="Start" /> |
| 162 |
|
| 163 |
<img id="loading" width="16" height="16" src="<?php echo esc_attr( site_url('wp-includes/images/wpspin-2x.gif') ); ?>" style="display: none" /> |
| 164 |
</p> |
| 165 |
|
| 166 |
<p> |
| 167 |
<a id="export" href="<?php echo admin_url('admin.php?page=' . $this->screen .'&fv-conversion-export=1');?>" style="display: none" >Export errors to csv file</a> |
| 168 |
</p> |
| 169 |
|
| 170 |
<div id="wrapper" style="display: none"><div id="progress"></div></div> |
| 171 |
|
| 172 |
<div class="conversion-done" style="display: none;" style="max-width: 30em; background: white; padding: .5em 1em; margin: 5em auto; box-shadow: 0px 0px 10px 5px rgba(0,0,0,.5)"> |
| 173 |
<h1>Conversion done!</h1> |
| 174 |
|
| 175 |
<?php if( $this->conversion_done_details ): ?> |
| 176 |
<p> |
| 177 |
<?php echo esc_html( $this->conversion_done_details ); ?> |
| 178 |
</p> |
| 179 |
<?php endif; ?> |
| 180 |
</div> |
| 181 |
|
| 182 |
<table class="wp-list-table widefat fixed striped table-view-list posts"> |
| 183 |
<thead> |
| 184 |
<tr> |
| 185 |
<?php foreach( $this->screen_fields as $field ) : ?> |
| 186 |
<th><?php echo esc_html( $field ); ?></th> |
| 187 |
<?php endforeach; ?> |
| 188 |
</tr> |
| 189 |
</thead> |
| 190 |
<tbody id="output"></tbody> |
| 191 |
</table> |
| 192 |
</div> |
| 193 |
|
| 194 |
<script type="text/javascript" charset="utf-8"> |
| 195 |
jQuery(function() { |
| 196 |
jQuery('#wrapper').Progressor( { |
| 197 |
action: '<?php echo esc_attr( $this->screen ); ?>', |
| 198 |
start: jQuery('input[name=convert]'), |
| 199 |
cancel: '<?php echo 'Cancel'; ?>', |
| 200 |
url: '<?php echo admin_url('admin-ajax.php') ?>', |
| 201 |
nonce: '<?php echo wp_create_nonce($this->screen)?>', |
| 202 |
finished: '<?php echo 'Finished'; ?>', |
| 203 |
limit: '<?php echo intval( $this->conversion_limit ); ?>' |
| 204 |
}); |
| 205 |
}); |
| 206 |
</script> |
| 207 |
<?php |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get live status |
| 212 |
* |
| 213 |
* @return boolean |
| 214 |
*/ |
| 215 |
function is_live() { |
| 216 |
return (!empty($_POST['make-changes']) && sanitize_key( $_POST['make-changes'] ) == 'true') || $this->set_live ; |
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
|