PluginProbe
Knowledge Base documentation & wiki plugin – BasePress Docs / trunk
Knowledge Base documentation & wiki plugin – BasePress Docs vtrunk
basepress / update.php

update.php in Knowledge Base documentation & wiki plugin – BasePress Docs trunk, at update.php

257 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Runs Updates from previous versions
4 */
5
6 function basepress_update( $old_ver, $old_db_ver, $old_plan, $current_ver, $current_db_ver, $current_plan ) {
7 global $wpdb;
8
9 if( version_compare( $old_ver, '2.3.2', '<' ) ){
10 $wpdb->query("DELETE FROM {$wpdb->termmeta} WHERE {$wpdb->termmeta}.meta_key = 'basepress_restricted_articles'");
11 }
12
13 update_option( 'basepress_ver', $current_ver );
14 update_option( 'basepress_db_ver', $current_db_ver );
15 update_option( 'basepress_plan', $current_plan );
16
17 /**
18 * Sets articles menu_order to zero if on Free version and coming from version < 2.10.1
19 * The menu order was always set incrementally but it is not necessary anymore
20 */
21 if( 'lite' == $current_plan && version_compare( $old_ver, '2.10.1', '<' ) ){
22 $wpdb->query( "UPDATE {$wpdb->posts} SET menu_order = 0 WHERE {$wpdb->posts}.post_type = 'knowledgebase'" );
23 }
24
25 //Update settings if coming from older version or from free to premium
26 if ( ! empty( $old_ver )
27 && version_compare( $old_ver, $current_ver, '<' )
28 || ( $old_plan != 'premium' && 'premium' == $current_plan) ) {
29
30 $new_options = array();
31
32 //Upgrade settings from free to premium
33 if( ( $old_plan != 'premium' && 'premium' == $current_plan ) ){
34 $premium_options = include_once BASEPRESS_DIR . 'premium-options.php';
35 $new_options = array_merge( $new_options, $premium_options );
36 }
37 $new_options = array_merge( $new_options, basepress_update_1_9_0( $old_ver, $current_ver ) );
38
39 $current_options = get_option( 'basepress_settings' );
40 foreach ( $new_options as $new_option => $value ) {
41 if ( ! isset( $current_options[ $new_option ] ) ) {
42 $current_options[ $new_option ] = $value;
43 }
44 }
45
46 /**
47 * Replace old settings with new ones
48 */
49 //Up to version 2.9.2 we had a settings called "order_posts_alphabetically" if it is set we should set the new settings called "posts_orderby"
50 if( isset( $current_options['order_posts_alphabetically'] ) ){
51 $current_options['posts_orderby'] = 'alpha_asc';
52 }
53 if( isset( $current_options['kb_name'] ) ){
54 $current_options['breadcrumbs_kb_name'] = $current_options['kb_name'];
55 }
56
57 update_option( 'basepress_settings', $current_options );
58
59 }
60
61 //Flush rewrite rules on version update for possible changes to CPT
62 if( $old_ver != $current_ver && ( is_admin() || current_user_can( 'update_plugins' ) ) && ! wp_doing_ajax() ){
63 add_action( 'shutdown', 'basepress_update_flush_rewrite_rules' );
64 }
65 }
66
67
68 function basepress_update_flush_rewrite_rules(){
69 flush_rewrite_rules();
70 }
71
72
73 /**
74 * Returns an array of settings for updating to version 1.9.0
75 * @param $old_ver
76 * @param $current_ver
77 * @return array
78 */
79 function basepress_update_1_9_0( $old_ver, $current_ver ) {
80
81 if ( version_compare( $old_ver, '1.9.0', '<' ) ) {
82 $new_options = array(
83 'show_feedback_form' => 'always',
84 'feedback_form_lable' => 'Help us improve this article',
85 'feedback_submit_text' => 'Submit Feedback',
86 'feedback_submit_success_text' => 'Thanks for your feedback',
87 'feedback_submit_fail_text' => 'There was a problem sending your feedback. Please try again!',
88 );
89 } else {
90 $new_options = array();
91 }
92 return $new_options;
93 }
94
95 /**
96 * Add update notice in Admin and enqueues the necessary JS file
97 *
98 * @since 1.7.10
99 */
100 function basepress_database_update(){
101 add_action( 'admin_notices', 'basepress_update_notice' );
102 add_action( 'admin_enqueue_scripts', 'basepress_update_script' );
103 }
104
105 /**
106 * Registers the Ajax call for the update notice
107 *
108 * @since 1.7.10
109 */
110 add_action( 'wp_ajax_basepress_db_posts_update', 'basepress_db_posts_update' );
111
112 /**
113 * Enqueue update JS file
114 *
115 * @since 1.7.10
116 */
117 function basepress_update_script(){
118 wp_register_script( 'basepress-update-script', BASEPRESS_URI . 'admin/js/basepress-update-script.js', array('jquery'), BASEPRESS_VER, true );
119 $params = array(
120 'nonce' => wp_create_nonce( 'update_nonce' ),
121 );
122 wp_localize_script( 'basepress-update-script', 'update', $params );
123 wp_enqueue_script( 'basepress-update-script' );
124 }
125
126 /**
127 * Display admin notice to update DB
128 *
129 * @since 1.7.10
130 */
131 function basepress_update_notice(){
132 ?>
133 <div id="basepress-db-update-notice" class="notice notice-error">
134 <p>
135 <span style="font-size: 1.2em; font-weight: bold;">BasePress </span>
136 <span id="basepress-notice-text"><?php echo esc_html__( 'needs to update your database to take full advantage of its features!', 'basepress' ); ?></span>
137 <span id="basepress-notice-success"><?php echo esc_html__( 'your database has been updated!', 'basepress' ); ?></span>
138 <button id="basepress-update-db" class="button button-primary" style="margin-left:2em"><span id="basepress-update-spinner" class="dashicons dashicons-update"></span> <?php echo esc_html__( 'Update Database', 'basepress' ); ?></button>
139 </p>
140 </div>
141 <style>
142 #basepress-notice-success{
143 display: none;
144 }
145 #basepress-update-spinner{
146 line-height: inherit;
147 }
148 #basepress-update-spinner.spinning:before{
149 display: block;
150 animation: bp-spinner 1500ms infinite linear;
151 }
152 @keyframes bp-spinner{
153 from{ transform: rotate(0);}
154 to{ transform: rotate(359.99deg);}
155 }
156 </style>
157 <?php
158 }
159
160
161
162 /**
163 * Main Ajax call function for DB update
164 *
165 * @since 1.7.10
166 */
167 function basepress_db_posts_update(){
168
169 header('Content-Type: application/json');
170
171 check_ajax_referer( 'update_nonce', 'security' );
172 if ( ! current_user_can( 'update_plugins' ) ) {
173 wp_die();
174 }
175 $process = sanitize_text_field( wp_unslash( $_POST['process'] ) );
176 $data = sanitize_text_field( wp_unslash( $_POST['packet'] ) );
177 $transient = sanitize_text_field( wp_unslash( $_POST['transient'] ) );
178 $saved_transient = get_transient( 'basepress_db_update_started' );
179
180 if( $saved_transient && $saved_transient != $transient ){
181 echo json_encode('The update is already running!');
182 }
183 else{
184
185 switch( $process ){
186 case 'get_update_objects':
187 $items_query = new WP_Query(
188 array( 'post_type' => 'knowledgebase',
189 'posts_per_page' => -1,
190 'fields' => 'ids',
191 )
192 );
193
194 $transient = sprintf( '%.22F', microtime( true ) );
195 set_transient( 'basepress_db_update_started', $transient, 350 );
196 echo wp_json_encode( array( 'items' => $items_query->posts, 'transient' => $transient ) );
197
198 break;
199 case 'update_items':
200 if( $saved_transient == $transient ){
201 basepress_updates_process_post_data( $data, $transient );
202 }
203 else{
204 echo json_encode('There was a problem with the update. Please retry.');
205 delete_transient( 'basepress_db_update_started' );
206 }
207 break;
208 case 'process_finished':
209 delete_transient( 'basepress_db_update_started' );
210 update_site_option( 'basepress_db_ver', 2.1 );
211 }
212 }
213 wp_die();
214 }
215
216 /**
217 * Processes the passed IDs updating the postmeta in the DB
218 * Called by the main Ajax function basepress_db_posts_update()
219 *
220 * @since 1.7.10
221 *
222 * @param $post_ids
223 */
224 function basepress_updates_process_post_data( $post_ids ){
225 header('Content-Type: application/json');
226
227 $processed_items = array();
228
229 foreach( $post_ids as $post_id ){
230 //Get all meta for the post
231 $post_meta = get_post_meta( $post_id );
232
233 $votes = isset( $post_meta['basepress_votes'] ) ? maybe_unserialize( $post_meta['basepress_votes'][0] ) : array();
234 $votes = isset( $votes['like'] ) && isset( $votes['dislike'] ) ? $votes : array( 'like'=> 0, 'dislike'=> 0 );
235
236 //Set votes count
237 $votes_count = $votes['like'] + $votes['dislike'];
238
239 //Generate the score for the current post id
240 if( $votes['like'] > $votes['dislike'] ){
241 $score = round( ( $votes['like'] / ( $votes['like'] + $votes['dislike'] ) ) * 100 );
242 }else if( $votes['like'] < $votes['dislike'] ){
243 $score = -round( ( $votes['dislike'] / ( $votes['like'] + $votes['dislike'] ) ) * 100 );
244 }else{
245 $score = 0;
246 }
247
248 update_post_meta( $post_id, 'basepress_votes', $votes );
249 update_post_meta( $post_id, 'basepress_votes_count', $votes_count );
250 update_post_meta( $post_id, 'basepress_score', $score );
251
252 $processed_items[] = $post_id;
253 }
254 echo wp_json_encode( $processed_items );
255 wp_die();
256 }
257