/wpsc-admin/ajax-and-init.php

  1. <?php 
  2.  
  3. /** 
  4. * WP eCommerce Admin AJAX functions 
  5. * 
  6. * These are the WPSC Admin AJAX functions 
  7. * 
  8. * @package wp-e-commerce 
  9. * @since 3.7 
  10. * 
  11. * @uses update_option() Updates option in the database given key and value 
  12. * @uses wp_delete_term() Removes term from the database 
  13. * @uses fetch_rss() DEPRECATED 
  14. * @uses wpsc_member_dedeactivate_subscriptions() @todo docs 
  15. * @uses wpsc_member_deactivate_subscriptions() @todo docs 
  16. * @uses wpsc_update_purchase_log_status() Updates the status of the logs for a purchase 
  17. * @uses transaction_results() Main function for creating purchase reports 
  18. * @uses wpsc_find_purchlog_status_name() Finds name of given status 
  19. */ 
  20. function wpsc_admin_ajax() { 
  21.  
  22. if ( ! wpsc_is_store_admin() ) { 
  23. return; 
  24.  
  25. global $wpdb; 
  26.  
  27. if ( isset( $_POST['action'] ) && $_POST['action'] == 'product-page-order' ) { 
  28. $current_order = get_option( 'wpsc_product_page_order' ); 
  29. $new_order = $_POST['order']; 
  30.  
  31. if ( isset( $new_order["advanced"] ) ) { 
  32. $current_order["advanced"] = array_unique( explode( ', ', $new_order["advanced"] ) ); 
  33. if ( isset( $new_order["side"] ) ) { 
  34. $current_order["side"] = array_unique( explode( ', ', $new_order["side"] ) ); 
  35.  
  36. update_option( 'wpsc_product_page_order', $current_order ); 
  37. exit( print_r( $order, 1 ) ); 
  38.  
  39.  
  40. if ( isset( $_POST['save_image_upload_state'] ) && $_POST['save_image_upload_state'] == 'true' && is_numeric( $_POST['image_upload_state'] ) ) { 
  41. $upload_state = (int)(bool)$_POST['image_upload_state']; 
  42. update_option( 'wpsc_use_flash_uploader', $upload_state ); 
  43. exit( "done" ); 
  44.  
  45. if ( isset( $_POST['remove_variation_value'] ) && $_POST['remove_variation_value'] == "true" && is_numeric( $_POST['variation_value_id'] ) ) { 
  46. $value_id = absint( $_GET['variation_value_id'] ); 
  47. echo wp_delete_term( $value_id, 'wpsc-variation' ); 
  48. exit(); 
  49.  
  50. if ( isset( $_REQUEST['log_state'] ) && $_REQUEST['log_state'] == "true" && is_numeric( $_POST['id'] ) && is_numeric( $_POST['value'] ) ) { 
  51. $newvalue = $_POST['value']; 
  52. if ( $_REQUEST['suspend'] == 'true' ) { 
  53. if ( $_REQUEST['value'] == 1 && function_exists('wpsc_member_dedeactivate_subscriptions')) 
  54. wpsc_member_dedeactivate_subscriptions( $_POST['id'] ); 
  55. elseif( function_exists('wpsc_member_deactivate_subscriptions')) 
  56. wpsc_member_deactivate_subscriptions( $_POST['id'] ); 
  57.  
  58. exit(); 
  59. } else { 
  60.  
  61. $log_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id` = '%d' LIMIT 1", $_POST['id'] ), ARRAY_A ); 
  62. if ( ($newvalue == 2) && function_exists( 'wpsc_member_activate_subscriptions' ) ) { 
  63. wpsc_member_activate_subscriptions( $_POST['id'] ); 
  64.  
  65. wpsc_update_purchase_log_status( $_POST['id'], $newvalue ); 
  66.  
  67. if ( ($newvalue > $log_data['processed']) && ($log_data['processed'] < 2) ) { 
  68. transaction_results( $log_data['sessionid'], false ); 
  69.  
  70. $status_name = wpsc_find_purchlog_status_name( $purchase['processed'] ); 
  71. echo "document.getElementById(\"form_group_" . absint( $_POST['id'] ) . "_text\").innerHTML = '" . $status_name . "';\n"; 
  72.  
  73.  
  74. $year = date( "Y" ); 
  75. $month = date( "m" ); 
  76. $start_timestamp = mktime( 0, 0, 0, $month, 1, $year ); 
  77. $end_timestamp = mktime( 0, 0, 0, ($month + 1 ), 0, $year ); 
  78.  
  79. echo "document.getElementById(\"log_total_month\").innerHTML = '" . addslashes( wpsc_currency_display( admin_display_total_price( $start_timestamp, $end_timestamp ) ) ) . "';\n"; 
  80. echo "document.getElementById(\"log_total_absolute\").innerHTML = '" . addslashes( wpsc_currency_display( admin_display_total_price() ) ) . "';\n"; 
  81. exit(); 
  82.  
  83. if ( isset( $_REQUEST['ajax'] ) && isset( $_REQUEST['admin'] ) && ($_REQUEST['ajax'] == "true") && ($_REQUEST['admin'] == "true") ) 
  84. add_action( 'admin_init', 'wpsc_admin_ajax' ); 
  85.  
  86. /** 
  87. * @todo docs 
  88. * @uses $wpdb WordPress database object for queries 
  89. */ 
  90. function wpsc_change_currency() { 
  91.  
  92. if ( ! wpsc_is_store_admin() ) { 
  93. return; 
  94.  
  95. if ( is_numeric( $_POST['currencyid'] ) ) { 
  96. $currency_data = $wpdb->get_results( $wpdb->prepare( "SELECT `symbol`, `symbol_html`, `code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id`=%d LIMIT 1", $_POST['currencyid'] ), ARRAY_A ); 
  97. $price_out = null; 
  98. if ( $currency_data[0]['symbol'] != '' ) { 
  99. $currency_sign = $currency_data[0]['symbol_html']; 
  100. } else { 
  101. $currency_sign = $currency_data[0]['code']; 
  102. echo $currency_sign; 
  103. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'change_currency') ) 
  104. add_action( 'admin_init', 'wpsc_change_currency' ); 
  105.  
  106. /** 
  107. * @todo docs 
  108. * @uses $wpdb WordPress database object for queries 
  109. */ 
  110. function wpsc_rearrange_images() { 
  111.  
  112. if ( ! wpsc_is_store_admin() ) { 
  113. return; 
  114.  
  115. global $wpdb; 
  116. $images = explode( ", ", $_POST['order'] ); 
  117. $product_id = absint( $_POST['product_id'] ); 
  118. $timestamp = time(); 
  119.  
  120. $new_main_image = null; 
  121. $have_set_first_item = false; 
  122. $i = 0; 
  123. foreach ( $images as $image ) { 
  124. if ( $image > 0 ) { 
  125. $wpdb->update( 
  126. $wpdb->posts,  
  127. array( 
  128. 'menu_order' => $i 
  129. ),  
  130. array( 
  131. 'ID' => $image 
  132. ),  
  133. '%d',  
  134. '%d' 
  135. ); 
  136. $i++; 
  137. $output = wpsc_main_product_image_menu( $product_id ); 
  138. echo "image_menu = '';\n\r"; 
  139. echo "image_id = '" . $new_main_image . "';\n\r"; 
  140. exit(); 
  141. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'rearrange_images') ) 
  142. add_action( 'admin_init', 'wpsc_rearrange_images' ); 
  143.  
  144. /** 
  145. * @todo docs 
  146. * 
  147. * @uses $wpdb WordPress database object for queries 
  148. * @uses $wp_rewrite Global variable instance of the WP_Rewrite Class 
  149. * @uses wp_get_referer() Retrieve referer from '_wp_http_referer' or HTTP referer. 
  150. * @uses add_query_arg() Retrieve a modified URL query string. 
  151. * @uses wp_redirect() Redirects to string given as argument 
  152. */ 
  153. function wpsc_clean_categories() { 
  154.  
  155. if ( ! wpsc_is_store_admin() ) { 
  156. return; 
  157.  
  158. global $wpdb, $wp_rewrite; 
  159. $sql_query = "SELECT `id`, `name`, `active` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "`"; 
  160. $sql_data = $wpdb->get_results( $sql_query, ARRAY_A ); 
  161. foreach ( (array)$sql_data as $datarow ) { 
  162. if ( $datarow['active'] == 1 ) { 
  163. $tidied_name = strtolower( trim( $datarow['name'] ) ); 
  164. $url_name = sanitize_title( $tidied_name ); 
  165. $similar_names = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS `count`, MAX(REPLACE(`nice-name`, '%s', '')) AS `max_number` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `nice-name` REGEXP '^( " . esc_sql( $url_name ) . " ) {1}(\d)*$' AND `id` NOT IN (%d) ", $url_name, $datarow['id'] ), ARRAY_A ); 
  166. $extension_number = ''; 
  167.  
  168. if ( $similar_names['count'] > 0 ) 
  169. $extension_number = (int)$similar_names['max_number'] + 2; 
  170.  
  171. $url_name .= $extension_number; 
  172.  
  173. $wpdb->update( 
  174. WPSC_TABLE_PRODUCT_CATEGORIES,  
  175. array( 
  176. 'nice-name' => $url_name 
  177. ),  
  178. array( 
  179. 'id' => $datarow['id'] 
  180. ),  
  181. '%s',  
  182. '%d' 
  183. ); 
  184.  
  185. } else if ( $datarow['active'] == 0 ) { 
  186. $wpdb->update( 
  187. WPSC_TABLE_PRODUCT_CATEGORIES,  
  188. array( 
  189. 'nice-name' => '' 
  190. ),  
  191. array( 
  192. 'id' => $datarow['id'] 
  193. ),  
  194. '%s',  
  195. '%d' 
  196. ); 
  197. $wp_rewrite->flush_rules(); 
  198. $sendback = wp_get_referer(); 
  199.  
  200. if ( isset( $updated ) ) { 
  201. $sendback = add_query_arg( 'updated', $updated, $sendback ); 
  202. if ( isset( $_SESSION['wpsc_settings_curr_page'] ) ) { 
  203. $sendback = add_query_arg( 'tab', $_SESSION['wpsc_settings_curr_page'], $sendback ); 
  204.  
  205. wp_redirect( esc_url_raw( $sendback ) ); 
  206.  
  207. exit(); 
  208. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'clean_categories') ) 
  209. add_action( 'admin_init', 'wpsc_clean_categories' ); 
.