PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / models / conversion / conversion-base.class.php

conversion-base.class.php in FV Player 8 trunk, at models/conversion/conversion-base.class.php

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