PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wp-e-commerce/wpsc-admin/ajax-and-init.php

https://github.com/AaronFernandes/aquestionof
PHP | 1826 lines | 1451 code | 282 blank | 93 comment | 380 complexity | d6a11d68467375166bb31640a6d12350 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * WP eCommerce Admin AJAX functions
  4. *
  5. * These are the WPSC Admin AJAX functions
  6. *
  7. * @package wp-e-commerce
  8. * @since 3.7
  9. */
  10. function wpsc_ajax_add_tracking() {
  11. global $wpdb;
  12. foreach ( $_POST as $key => $value ) {
  13. $parts = preg_split( '/^wpsc_trackingid/', $key );
  14. if ( count( $parts ) > '1' ) {
  15. $id = $parts[1];
  16. $trackingid = $value;
  17. $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `track_id`='" . $trackingid . "' WHERE `id`=" . $id;
  18. $wpdb->query( $sql );
  19. }
  20. }
  21. }
  22. if ( isset( $_REQUEST['submit'] ) && ($_REQUEST['submit'] == 'Add Tracking ID') ) {
  23. add_action( 'admin_init', 'wpsc_ajax_add_tracking' );
  24. }
  25. function wpsc_purchlog_email_trackid() {
  26. global $wpdb;
  27. $id = absint( $_POST['purchlog_id'] );
  28. $trackingid = $wpdb->get_var( "SELECT `track_id` FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE `id`={$id} LIMIT 1" );
  29. $message = get_option( 'wpsc_trackingid_message' );
  30. $message = str_replace( '%trackid%', $trackingid, $message );
  31. $message = str_replace( '%shop_name%', get_option( 'blogname' ), $message );
  32. $email_form_field = $wpdb->get_var( "SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type` IN ('email') AND `active` = '1' ORDER BY `checkout_order` ASC LIMIT 1" );
  33. $email = $wpdb->get_var( "SELECT `value` FROM `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` WHERE `log_id`=" . $id . " AND `form_id` = '$email_form_field' LIMIT 1" );
  34. $subject = get_option( 'wpsc_trackingid_subject' );
  35. $subject = str_replace( '%shop_name%', get_option( 'blogname' ), $subject );
  36. add_filter( 'wp_mail_from', 'wpsc_replace_reply_address', 0 );
  37. add_filter( 'wp_mail_from_name', 'wpsc_replace_reply_name', 0 );
  38. wp_mail( $email, $subject, $message);
  39. remove_filter( 'wp_mail_from_name', 'wpsc_replace_reply_name' );
  40. remove_filter( 'wp_mail_from', 'wpsc_replace_reply_address' );
  41. exit( true );
  42. }
  43. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'purchlog_email_trackid') ) {
  44. add_action( 'admin_init', 'wpsc_purchlog_email_trackid' );
  45. }
  46. function wpsc_ajax_sales_quarterly() {
  47. $lastdate = $_POST['add_start'];
  48. $date = preg_split( '/-/', $lastdate );
  49. if ( !isset( $date[0] ) )
  50. $date[0] = 0;
  51. if ( !isset( $date[1] ) )
  52. $date[1] = 0;
  53. if ( !isset( $date[2] ) )
  54. $date[2] = 0;
  55. $lastquart = mktime( 0, 0, 0, $date[1], $date[2], $date[0] );
  56. if ( $lastquart != get_option( 'wpsc_last_quarter' ) ) {
  57. update_option( 'wpsc_last_date', $lastdate );
  58. update_option( 'wpsc_fourth_quart', $lastquart );
  59. $thirdquart = mktime( 0, 0, 0, $date[1] - 3, $date[2], $date[0] );
  60. update_option( 'wpsc_third_quart', $thirdquart );
  61. $secondquart = mktime( 0, 0, 0, $date[1] - 6, $date[2], $date[0] );
  62. update_option( 'wpsc_second_quart', $secondquart );
  63. $firstquart = mktime( 0, 0, 0, $date[1] - 9, $date[2], $date[0] );
  64. update_option( 'wpsc_first_quart', $firstquart );
  65. $finalquart = mktime( 0, 0, 0, $date[1], $date[2], $date[0] - 1 );
  66. update_option( 'wpsc_final_quart', $finalquart );
  67. }
  68. }
  69. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'wpsc_quarterly') ) {
  70. add_action( 'admin_init', 'wpsc_ajax_sales_quarterly' );
  71. }
  72. function wpsc_delete_file() {
  73. global $wpdb;
  74. $output = 0;
  75. $row_number = absint( $_GET['row_number'] );
  76. $product_id = absint( $_GET['product_id'] );
  77. $file_name = basename( $_GET['file_name'] );
  78. check_admin_referer( 'delete_file_' . $file_name );
  79. $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_parent = %d AND post_type ='wpsc-product-file'", $file_name, $product_id );
  80. $product_id_to_delete = $wpdb->get_var( $sql );
  81. wp_delete_post( $product_id_to_delete, true );
  82. if ( $_POST['ajax'] !== 'true' ) {
  83. $sendback = wp_get_referer();
  84. wp_redirect( $sendback );
  85. }
  86. echo "jQuery('#select_product_file_row_$row_number').fadeOut('fast',function() {\n";
  87. echo " jQuery(this).remove();\n";
  88. echo " jQuery('div.select_product_file p:even').removeClass('alt');\n";
  89. echo " jQuery('div.select_product_file p:odd').addClass('alt');\n";
  90. echo "});\n";
  91. exit( "" );
  92. }
  93. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'delete_file') ) {
  94. add_action( 'admin_init', 'wpsc_delete_file' );
  95. }
  96. /**
  97. Function and action for publishing or unpublishing single products
  98. */
  99. function wpsc_ajax_toggle_published() {
  100. $product_id = absint( $_GET['product'] );
  101. check_admin_referer( 'toggle_publish_' . $product_id );
  102. $status = (wpsc_toggle_publish_status( $product_id )) ? ('true') : ('false');
  103. $sendback = add_query_arg( 'flipped', "1", wp_get_referer() );
  104. wp_redirect( $sendback );
  105. exit();
  106. }
  107. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'toggle_publish') ) {
  108. add_action( 'admin_init', 'wpsc_ajax_toggle_published' );
  109. }
  110. /**
  111. Function and action for duplicating products,
  112. Refactored for 3.8
  113. * Purposely not duplicating stick post status (logically, products are most often duplicated because they share many attributes, where products are generally 'featured' uniquely.)
  114. */
  115. function wpsc_duplicate_product() {
  116. // Get the original post
  117. $id = absint( $_GET['product'] );
  118. $post = wpsc_duplicate_this_dangit( $id );
  119. // Copy the post and insert it
  120. if ( isset( $post ) && $post != null ) {
  121. $new_id = wpsc_duplicate_product_process( $post );
  122. $duplicated = true;
  123. $sendback = wp_get_referer();
  124. $sendback = add_query_arg( 'duplicated', (int)$duplicated, $sendback );
  125. wp_redirect( $sendback );
  126. exit();
  127. } else {
  128. wp_die( __( 'Sorry, for some reason, we couldn\'t duplicate this product because it could not be found in the database, check there for this ID: ' ) . $id );
  129. }
  130. }
  131. function wpsc_duplicate_this_dangit( $id ) {
  132. $post = get_post($id);
  133. return $post;
  134. }
  135. function wpsc_duplicate_product_process( $post ) {
  136. $new_post_date = $post->post_date;
  137. $new_post_date_gmt = get_gmt_from_date( $new_post_date );
  138. $new_post_type = $post->post_type;
  139. $post_content = str_replace( "'", "''", $post->post_content );
  140. $post_content_filtered = str_replace( "'", "''", $post->post_content_filtered );
  141. $post_excerpt = str_replace( "'", "''", $post->post_excerpt );
  142. $post_title = str_replace( "'", "''", $post->post_title ) . " (Duplicate)";
  143. $post_name = str_replace( "'", "''", $post->post_name );
  144. $comment_status = str_replace( "'", "''", $post->comment_status );
  145. $ping_status = str_replace( "'", "''", $post->ping_status );
  146. $defaults = array(
  147. 'post_status' => $post->post_status,
  148. 'post_type' => $new_post_type,
  149. 'ping_status' => $ping_status,
  150. 'post_parent' => $post->post_parent,
  151. 'menu_order' => $post->menu_order,
  152. 'to_ping' => $post->to_ping,
  153. 'pinged' => $post->pinged,
  154. 'post_excerpt' => $post_excerpt,
  155. 'post_title' => $post_title,
  156. 'post_content' => $post_content,
  157. 'post_content_filtered' => $post_content_filtered,
  158. 'import_id' => 0
  159. );
  160. // Insert the new template in the post table
  161. $new_post_id = wp_insert_post($defaults);
  162. // Copy the taxonomies
  163. wpsc_duplicate_taxonomies( $post->ID, $new_post_id, $post->post_type );
  164. // Copy the meta information
  165. wpsc_duplicate_product_meta( $post->ID, $new_post_id );
  166. // Finds children (Which includes product files AND product images), their meta values, and duplicates them.
  167. wpsc_duplicate_children( $post->ID, $new_post_id );
  168. return $new_post_id;
  169. }
  170. /**
  171. * Copy the taxonomies of a post to another post
  172. */
  173. function wpsc_duplicate_taxonomies( $id, $new_id, $post_type ) {
  174. $taxonomies = get_object_taxonomies( $post_type ); //array("category", "post_tag");
  175. foreach ( $taxonomies as $taxonomy ) {
  176. $post_terms = wp_get_object_terms( $id, $taxonomy );
  177. for ( $i = 0; $i < count( $post_terms ); $i++ ) {
  178. wp_set_object_terms( $new_id, $post_terms[$i]->slug, $taxonomy, true );
  179. }
  180. }
  181. }
  182. /**
  183. * Copy the meta information of a post to another post
  184. */
  185. function wpsc_duplicate_product_meta( $id, $new_id ) {
  186. global $wpdb;
  187. $post_meta_infos = $wpdb->get_results( "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$id" );
  188. if ( count( $post_meta_infos ) != 0 ) {
  189. $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
  190. foreach ( $post_meta_infos as $meta_info ) {
  191. $meta_key = $meta_info->meta_key;
  192. $meta_value = addslashes( $meta_info->meta_value );
  193. $sql_query_sel[] = "SELECT $new_id, '$meta_key', '$meta_value'";
  194. }
  195. $sql_query.= implode( " UNION ALL ", $sql_query_sel );
  196. $wpdb->query( $sql_query );
  197. }
  198. }
  199. /**
  200. * Duplicates children product and children meta
  201. */
  202. function wpsc_duplicate_children( $old_parent_id, $new_parent_id ) {
  203. global $wpdb;
  204. //Get children products and duplicate them
  205. $child_posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_parent = $old_parent_id" );
  206. foreach ( $child_posts as $child_post ) {
  207. $new_post_date = $child_post->post_date;
  208. $new_post_date_gmt = get_gmt_from_date( $new_post_date );
  209. $new_post_type = $child_post->post_type;
  210. $post_content = str_replace( "'", "''", $child_post->post_content );
  211. $post_content_filtered = str_replace( "'", "''", $child_post->post_content_filtered );
  212. $post_excerpt = str_replace( "'", "''", $child_post->post_excerpt );
  213. $post_title = str_replace( "'", "''", $child_post->post_title );
  214. $post_name = str_replace( "'", "''", $child_post->post_name );
  215. $comment_status = str_replace( "'", "''", $child_post->comment_status );
  216. $ping_status = str_replace( "'", "''", $child_post->ping_status );
  217. $wpdb->query(
  218. "INSERT INTO $wpdb->posts
  219. (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
  220. VALUES
  221. ('$child_post->post_author', '$new_post_date', '$new_post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$child_post->post_status', '$new_post_type', '$comment_status', '$ping_status', '$child_post->post_password', '$child_post->to_ping', '$child_post->pinged', '$new_post_date', '$new_post_date_gmt', '$new_parent_id', '$child_post->menu_order', '$child_post->post_mime_type')" );
  222. $old_post_id = $child_post->ID;
  223. $new_post_id = $wpdb->insert_id;
  224. $child_meta = $wpdb->get_results( "SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = $old_post_id" );
  225. foreach ( $child_meta as $child_meta ) {
  226. $wpdb->query(
  227. "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value)
  228. VALUES('$new_post_id', '$child_meta->meta_key', '$child_meta->meta_value')"
  229. );
  230. }
  231. }
  232. }
  233. if ( isset( $_GET['wpsc_admin_action'] ) && ($_GET['wpsc_admin_action'] == 'duplicate_product') ) {
  234. add_action( 'admin_init', 'wpsc_duplicate_product' );
  235. }
  236. function wpsc_purchase_log_csv() {
  237. global $wpdb, $wpsc_gateways;
  238. get_currentuserinfo();
  239. $count = 0;
  240. if ( ($_GET['rss_key'] == 'key') && is_numeric( $_GET['start_timestamp'] ) && is_numeric( $_GET['end_timestamp'] ) && current_user_can( 'manage_options' ) ) {
  241. $form_sql = "SELECT * FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `active` = '1' AND `type` != 'heading' ORDER BY `checkout_order` DESC;";
  242. $form_data = $wpdb->get_results( $form_sql, ARRAY_A );
  243. $start_timestamp = $_GET['start_timestamp'];
  244. $end_timestamp = $_GET['end_timestamp'];
  245. $data = $wpdb->get_results( "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `date` BETWEEN '$start_timestamp' AND '$end_timestamp' ORDER BY `date` DESC", ARRAY_A );
  246. $csv = 'Purchase ID, Price, Firstname, Lastname, Email, Order Status, Data, ';
  247. header( 'Content-Type: text/csv' );
  248. header( 'Content-Disposition: inline; filename="Purchase Log ' . date( "M-d-Y", $start_timestamp ) . ' to ' . date( "M-d-Y", $end_timestamp ) . '.csv"' );
  249. $headers = "\"Purchase ID\",\"Purchase Total\","; //capture the headers
  250. $headers2 ="\"Payment Gateway\",";
  251. $headers2 .="\"Payment Status\",\"Purchase Date\",";
  252. foreach ( (array)$data as $purchase ) {
  253. $form_headers = '';
  254. $output .= "\"" . $purchase['id'] . "\","; //Purchase ID
  255. $output .= "\"" . $purchase['totalprice'] . "\","; //Purchase Total
  256. foreach ( (array)$form_data as $form_field ) {
  257. $form_headers .="\"".$form_field['unique_name']."\",";
  258. $collected_data_sql = "SELECT * FROM `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` WHERE `log_id` = '" . $purchase['id'] . "' AND `form_id` = '" . $form_field['id'] . "' LIMIT 1";
  259. $collected_data = $wpdb->get_results( $collected_data_sql, ARRAY_A );
  260. $collected_data = $collected_data[0];
  261. $output .= "\"" . $collected_data['value'] . "\","; // get form fields
  262. }
  263. $output .= "\"" . $wpsc_gateways[$purchase['gateway']]['display_name'] . "\","; //get gateway name
  264. $status_name = wpsc_find_purchlog_status_name( $purchase['processed'] );
  265. $output .= "\"" . $status_name . "\","; //get purchase status
  266. $output .= "\"" . date( "jS M Y", $purchase['date'] ) . "\","; //date
  267. $cartsql = "SELECT `prodid`, `quantity`, `name` FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`=" . $purchase['id'] . "";
  268. $cart = $wpdb->get_results( $cartsql, ARRAY_A );
  269. if($count < count($cart))
  270. $count = count($cart);
  271. // Go through all products in cart and display quantity and sku
  272. foreach ( (array)$cart as $item ) {
  273. $skuvalue = get_product_meta($item['prodid'], 'sku', true);
  274. if(empty($skuvalue)) $skuvalue = __('N/A', 'wpsc');
  275. $output .= "\"" . $item['quantity'] . " x " . str_replace( '"', '\"', $item['name'] ) . "\"";
  276. $output .= "," . $skuvalue."," ;
  277. }
  278. $output .= "\n"; // terminates the row/line in the CSV file
  279. }
  280. // Get the most number of products and create a header for them
  281. $headers3 = "";
  282. for($i = 0; $i < $count ;$i++){
  283. $headers3 .= "\"Quantity - Product Name \", \" SKU \"";
  284. if($i < ($count-1))
  285. $headers3 .= ",";
  286. }
  287. echo $headers . $form_headers . $headers2 . $headers3 . "\n". $output;
  288. exit();
  289. }
  290. }
  291. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'wpsc_downloadcsv') ) {
  292. add_action( 'admin_init', 'wpsc_purchase_log_csv' );
  293. }
  294. function wpsc_admin_ajax() {
  295. global $wpdb;
  296. if ( isset( $_POST['action'] ) && $_POST['action'] == 'product-page-order' ) {
  297. $current_order = get_option( 'wpsc_product_page_order' );
  298. $new_order = $_POST['order'];
  299. if ( isset( $new_order["advanced"] ) ) {
  300. $current_order["advanced"] = array_unique( explode( ',', $new_order["advanced"] ) );
  301. }
  302. if ( isset( $new_order["side"] ) ) {
  303. $current_order["side"] = array_unique( explode( ',', $new_order["side"] ) );
  304. }
  305. update_option( 'wpsc_product_page_order', $current_order );
  306. exit( print_r( $order, 1 ) );
  307. }
  308. if ( isset( $_POST['save_image_upload_state'] ) && $_POST['save_image_upload_state'] == 'true' && is_numeric( $_POST['image_upload_state'] ) ) {
  309. $upload_state = (int)(bool)$_POST['image_upload_state'];
  310. update_option( 'wpsc_use_flash_uploader', $upload_state );
  311. exit( "done" );
  312. }
  313. if ( isset( $_POST['remove_variation_value'] ) && $_POST['remove_variation_value'] == "true" && is_numeric( $_POST['variation_value_id'] ) ) {
  314. $value_id = absint( $_GET['variation_value_id'] );
  315. echo wp_delete_term( $value_id, 'wpsc-variation' );
  316. exit();
  317. }
  318. if ( isset( $_POST['remove_form_field'] ) && $_POST['remove_form_field'] == "true" && is_numeric( $_POST['form_id'] ) ) {
  319. if ( current_user_can( 'manage_options' ) ) {
  320. $wpdb->query( $wpdb->prepare( "UPDATE `" . WPSC_TABLE_CHECKOUT_FORMS . "` SET `active` = '0' WHERE `id` = %d LIMIT 1 ;", $_POST['form_id'] ) );
  321. exit( ' ' );
  322. }
  323. }
  324. if ( isset( $_POST['hide_ecom_dashboard'] ) && $_POST['hide_ecom_dashboard'] == 'true' ) {
  325. require_once (ABSPATH . WPINC . '/rss.php');
  326. $rss = fetch_rss( 'http://www.instinct.co.nz/feed/' );
  327. $rss->items = array_slice( $rss->items, 0, 5 );
  328. $rss_hash = sha1( serialize( $rss->items ) );
  329. update_option( 'wpsc_ecom_news_hash', $rss_hash );
  330. exit( 1 );
  331. }
  332. if ( isset( $_POST['remove_meta'] ) && $_POST['remove_meta'] == 'true' && is_numeric( $_POST['meta_id'] ) ) {
  333. $meta_id = (int)$_POST['meta_id'];
  334. if ( delete_meta( $meta_id ) ) {
  335. echo $meta_id;
  336. exit();
  337. }
  338. echo 0;
  339. exit();
  340. }
  341. if ( isset( $_REQUEST['log_state'] ) && $_REQUEST['log_state'] == "true" && is_numeric( $_POST['id'] ) && is_numeric( $_POST['value'] ) ) {
  342. $newvalue = $_POST['value'];
  343. if ( $_REQUEST['suspend'] == 'true' ) {
  344. if ( $_REQUEST['value'] == 1 && function_exists('wpsc_member_dedeactivate_subscriptions'))
  345. wpsc_member_dedeactivate_subscriptions( $_POST['id'] );
  346. elseif( function_exists('wpsc_member_deactivate_subscriptions'))
  347. wpsc_member_deactivate_subscriptions( $_POST['id'] );
  348. exit();
  349. } else {
  350. $log_data = $wpdb->get_row( "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id` = '" . $_POST['id'] . "' LIMIT 1", ARRAY_A );
  351. if ( ($newvalue == 2) && function_exists( 'wpsc_member_activate_subscriptions' ) ) {
  352. wpsc_member_activate_subscriptions( $_POST['id'] );
  353. }
  354. $update_sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `processed` = '" . $newvalue . "' WHERE `id` = '" . $_POST['id'] . "' LIMIT 1";
  355. $wpdb->query( $update_sql );
  356. if ( ($newvalue > $log_data['processed']) && ($log_data['processed'] < 2) ) {
  357. transaction_results( $log_data['sessionid'], false );
  358. }
  359. $status_name = wpsc_find_purchlog_status_name( $purchase['processed'] );
  360. echo "document.getElementById(\"form_group_" . $_POST['id'] . "_text\").innerHTML = '" . $status_name . "';\n";
  361. $year = date( "Y" );
  362. $month = date( "m" );
  363. $start_timestamp = mktime( 0, 0, 0, $month, 1, $year );
  364. $end_timestamp = mktime( 0, 0, 0, ($month + 1 ), 0, $year );
  365. echo "document.getElementById(\"log_total_month\").innerHTML = '" . addslashes( wpsc_currency_display( admin_display_total_price( $start_timestamp, $end_timestamp ) ) ) . "';\n";
  366. echo "document.getElementById(\"log_total_absolute\").innerHTML = '" . addslashes( wpsc_currency_display( admin_display_total_price() ) ) . "';\n";
  367. exit();
  368. }
  369. }
  370. }
  371. function wpsc_admin_sale_rss() {
  372. global $wpdb;
  373. if ( ($_GET['rss'] == "true") && ($_GET['rss_key'] == 'key') && ($_GET['action'] == "purchase_log") ) {
  374. $sql = "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `date`!='' ORDER BY `date` DESC";
  375. $purchase_log = $wpdb->get_results( $sql, ARRAY_A );
  376. header( "Content-Type: application/xml; charset=UTF-8" );
  377. header( 'Content-Disposition: inline; filename="WP_E-Commerce_Purchase_Log.rss"' );
  378. $output = '';
  379. $output .= "<?xml version='1.0'?>\n\r";
  380. $output .= "<rss version='2.0'>\n\r";
  381. $output .= " <channel>\n\r";
  382. $output .= " <title>WP e-Commerce Product Log</title>\n\r";
  383. $output .= " <link>" . get_option( 'siteurl' ) . "/wp-admin/admin.php?page=" . WPSC_DIR_NAME . "/display-log.php</link>\n\r";
  384. $output .= " <description>This is the WP e-Commerce Product Log RSS feed</description>\n\r";
  385. $output .= " <generator>WP e-Commerce Plugin</generator>\n\r";
  386. foreach ( (array)$purchase_log as $purchase ) {
  387. $purchase_link = get_option( 'siteurl' ) . "/wp-admin/admin.php?page=" . WPSC_DIR_NAME . "/display-log.php&amp;purchaseid=" . $purchase['id'];
  388. $output .= " <item>\n\r";
  389. $output .= " <title>Purchase # " . $purchase['id'] . "</title>\n\r";
  390. $output .= " <link>$purchase_link</link>\n\r";
  391. $output .= " <description>This is an entry in the purchase log.</description>\n\r";
  392. $output .= " <pubDate>" . date( "r", $purchase['date'] ) . "</pubDate>\n\r";
  393. $output .= " <guid>$purchase_link</guid>\n\r";
  394. $output .= " </item>\n\r";
  395. }
  396. $output .= " </channel>\n\r";
  397. $output .= "</rss>";
  398. echo $output;
  399. exit();
  400. }
  401. }
  402. function wpsc_display_invoice() {
  403. $purchase_id = (int)$_GET['purchaselog_id'];
  404. add_action('wpsc_packing_slip', 'wpsc_packing_slip');
  405. do_action('wpsc_before_packing_slip', $purchase_id);
  406. do_action('wpsc_packing_slip', $purchase_id);
  407. exit();
  408. }
  409. //other actions are here
  410. if ( isset( $_GET['display_invoice'] ) && ( 'true' == $_GET['display_invoice'] ) )
  411. add_action( 'admin_init', 'wpsc_display_invoice', 0 );
  412. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ( 'wpsc_display_invoice' == $_REQUEST['wpsc_admin_action'] ) )
  413. add_action( 'admin_init', 'wpsc_display_invoice' );
  414. /**
  415. * Purchase log ajax code starts here
  416. */
  417. function wpsc_purchlog_resend_email() {
  418. global $wpdb;
  419. $log_id = $_GET['email_buyer_id'];
  420. $wpec_taxes_controller = new wpec_taxes_controller();
  421. if ( is_numeric( $log_id ) ) {
  422. $selectsql = "SELECT `sessionid` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id`= " . $log_id . " LIMIT 1";
  423. $purchase_log = $wpdb->get_var( $selectsql );
  424. transaction_results( $purchase_log, false);
  425. $sent = true;
  426. }
  427. $sendback = wp_get_referer();
  428. if ( isset( $sent ) ) {
  429. $sendback = add_query_arg( 'sent', $sent, $sendback );
  430. }
  431. wp_redirect( $sendback );
  432. exit();
  433. }
  434. if ( isset( $_REQUEST['email_buyer_id'] ) && is_numeric( $_REQUEST['email_buyer_id'] ) ) {
  435. add_action( 'admin_init', 'wpsc_purchlog_resend_email' );
  436. }
  437. function wpsc_purchlog_clear_download_items() {
  438. global $wpdb;
  439. if ( is_numeric( $_GET['purchaselog_id'] ) ) {
  440. $purchase_id = (int)$_GET['purchaselog_id'];
  441. $downloadable_items = $wpdb->get_results( "SELECT * FROM `" . WPSC_TABLE_DOWNLOAD_STATUS . "` WHERE `purchid` IN ('$purchase_id')", ARRAY_A );
  442. $clear_locks_sql = "UPDATE`" . WPSC_TABLE_DOWNLOAD_STATUS . "` SET `ip_number` = '' WHERE `purchid` IN ('$purchase_id')";
  443. $wpdb->query( $clear_locks_sql );
  444. $cleared = true;
  445. $email_form_field = $wpdb->get_var( "SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type` IN ('email') AND `active` = '1' ORDER BY `checkout_order` ASC LIMIT 1" );
  446. $email_address = $wpdb->get_var( "SELECT `value` FROM `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` WHERE `log_id`='{$purchase_id}' AND `form_id` = '{$email_form_field}' LIMIT 1" );
  447. foreach ( (array)$downloadable_items as $downloadable_item ) {
  448. $download_links .= $siteurl . "?downloadid=" . $downloadable_item['uniqueid'] . "\n";
  449. }
  450. wp_mail( $email_address, __( 'The administrator has unlocked your file', 'wpsc' ), str_replace( "[download_links]", $download_links, __( 'Dear CustomerWe are pleased to advise you that your order has been updated and your downloads are now active.Please download your purchase using the links provided below.[download_links]Thank you for your custom.', 'wpsc' ) ), "From: " . get_option( 'return_email' ) . "" );
  451. $sendback = wp_get_referer();
  452. if ( isset( $cleared ) ) {
  453. $sendback = add_query_arg( 'cleared', $cleared, $sendback );
  454. }
  455. wp_redirect( $sendback );
  456. exit();
  457. }
  458. }
  459. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'clear_locks') ) {
  460. add_action( 'admin_init', 'wpsc_purchlog_clear_download_items' );
  461. }
  462. //call to change view for purchase log
  463. function wpsc_purchlog_filter_by() {
  464. wpsc_change_purchlog_view( $_POST['view_purchlogs_by'], $_POST['view_purchlogs_by_status'] );
  465. }
  466. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'purchlog_filter_by') ) {
  467. add_action( 'admin_init', 'wpsc_purchlog_filter_by' );
  468. }
  469. //bulk actions for purchase log
  470. function wpsc_purchlog_bulk_modify() {
  471. if ( $_POST['purchlog_multiple_status_change'] != -1 ) {
  472. if ( is_numeric( $_POST['purchlog_multiple_status_change'] ) && $_POST['purchlog_multiple_status_change'] != 'delete' ) {
  473. foreach ( (array)$_POST['purchlogids'] as $purchlogid ) {
  474. wpsc_purchlog_edit_status( $purchlogid, $_POST['purchlog_multiple_status_change'] );
  475. $updated++;
  476. }
  477. } elseif ( $_POST['purchlog_multiple_status_change'] == 'delete' ) {
  478. foreach ( (array)$_POST['purchlogids'] as $purchlogid ) {
  479. wpsc_delete_purchlog( $purchlogid );
  480. $deleted++;
  481. }
  482. }
  483. }
  484. $sendback = wp_get_referer();
  485. if ( isset( $updated ) ) {
  486. $sendback = add_query_arg( 'updated', $updated, $sendback );
  487. }
  488. if ( isset( $deleted ) ) {
  489. $sendback = add_query_arg( 'deleted', $deleted, $sendback );
  490. }
  491. if ( isset( $_POST['view_purchlogs_by'] ) ) {
  492. $sendback = add_query_arg( 'view_purchlogs_by', $_POST['view_purchlogs_by'], $sendback );
  493. }
  494. if ( isset( $_POST['view_purchlogs_by_status'] ) ) {
  495. $sendback = add_query_arg( 'view_purchlogs_by_status', $_POST['view_purchlogs_by_status'], $sendback );
  496. }
  497. wp_redirect( $sendback );
  498. exit();
  499. }
  500. if ( isset( $_REQUEST['wpsc_admin_action2'] ) && ($_REQUEST['wpsc_admin_action2'] == 'purchlog_bulk_modify') ) {
  501. add_action( 'admin_init', 'wpsc_purchlog_bulk_modify' );
  502. }
  503. //edit purchase log status function
  504. function wpsc_purchlog_edit_status( $purchlog_id='', $purchlog_status='' ) {
  505. global $wpdb;
  506. if ( empty($purchlog_id) && empty($purchlog_status) ) {
  507. $purchlog_id = absint( $_POST['purchlog_id'] );
  508. $purchlog_status = absint( $_POST['purchlog_status'] );
  509. }
  510. $log_data = $wpdb->get_row( "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id` = '{$purchlog_id}' LIMIT 1", ARRAY_A );
  511. $is_transaction = wpsc_check_purchase_processed($log_data['processed']);
  512. if ( $is_transaction && function_exists('wpsc_member_activate_subscriptions')) {
  513. wpsc_member_activate_subscriptions( $_POST['id'] );
  514. }
  515. //in the future when everyone is using the 2.0 merchant api, we should use the merchant class to update the staus,
  516. // then you can get rid of this hook and have each person overwrite the method that updates the status.
  517. do_action('wpsc_edit_order_status', array('purchlog_id'=>$purchlog_id, 'purchlog_data'=>$log_data, 'new_status'=>$purchlog_status));
  518. $wpdb->query( "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET processed='{$purchlog_status}' WHERE id='{$purchlog_id}'" );
  519. wpsc_clear_stock_claims();
  520. wpsc_decrement_claimed_stock($purchlog_id);
  521. if ( $purchlog_status == 3 )
  522. transaction_results($log_data['sessionid'],false,null);
  523. }
  524. add_action( 'wp_ajax_purchlog_edit_status', 'wpsc_purchlog_edit_status' );
  525. function wpsc_save_product_order() {
  526. global $wpdb;
  527. $products = array( );
  528. foreach ( $_POST['post'] as $product ) {
  529. $products[] = absint( $product );
  530. }
  531. print_r( $products );
  532. foreach ( $products as $order => $product_id ) {
  533. $wpdb->query( $wpdb->prepare( "UPDATE `{$wpdb->posts}` SET `menu_order`='%d' WHERE `ID`='%d' LIMIT 1", $order, $product_id ) );
  534. }
  535. $success = true;
  536. exit( (string)$success );
  537. }
  538. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'save_product_order') ) {
  539. add_action( 'admin_init', 'wpsc_save_product_order' );
  540. }
  541. function wpsc_save_checkout_order() {
  542. global $wpdb;
  543. $checkoutfields = $_POST['checkout'];
  544. $order = 1;
  545. foreach ( $checkoutfields as $checkoutfield ) {
  546. $checkoutfield = absint( $checkoutfield );
  547. $wpdb->query( "UPDATE `" . WPSC_TABLE_CHECKOUT_FORMS . "` SET `checkout_order` = '" . $order . "' WHERE `id`=" . $checkoutfield );
  548. $order++;
  549. }
  550. $success = true;
  551. exit( (string)$success );
  552. }
  553. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'save_checkout_order') )
  554. add_action( 'admin_init', 'wpsc_save_checkout_order' );
  555. /* Start Order Notes (by Ben) */
  556. function wpsc_purchlogs_update_notes( $purchlog_id = '', $purchlog_notes = '' ) {
  557. global $wpdb;
  558. if ( wp_verify_nonce( $_POST['wpsc_purchlogs_update_notes_nonce'], 'wpsc_purchlogs_update_notes' ) ) {
  559. if ( ($purchlog_id == '') && ($purchlog_notes == '') ) {
  560. $purchlog_id = absint( $_POST['purchlog_id'] );
  561. $purchlog_notes = $wpdb->escape( $_POST['purchlog_notes'] );
  562. }
  563. $wpdb->query( "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET notes='{$purchlog_notes}' WHERE id='{$purchlog_id}'" );
  564. }
  565. }
  566. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'purchlogs_update_notes' ) )
  567. add_action( 'admin_init', 'wpsc_purchlogs_update_notes' );
  568. /* End Order Notes (by Ben) */
  569. //delete a purchase log
  570. function wpsc_delete_purchlog( $purchlog_id='' ) {
  571. global $wpdb;
  572. $deleted = 0;
  573. if ( $purchlog_id == '' ) {
  574. $purchlog_id = absint( $_GET['purchlog_id'] );
  575. check_admin_referer( 'delete_purchlog_' . $purchlog_id );
  576. }
  577. if ( is_numeric( $purchlog_id ) ) {
  578. $delete_log_form_sql = "SELECT * FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`='$purchlog_id'";
  579. $cart_content = $wpdb->get_results( $delete_log_form_sql, ARRAY_A );
  580. }
  581. $purchlog_status = $wpdb->get_var( "SELECT `processed` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id`=" . $purchlog_id );
  582. if ( $purchlog_status == 5 || $purchlog_status == 1 ) {
  583. $wpdb->query( "DELETE FROM `" . WPSC_TABLE_CLAIMED_STOCK . "` WHERE `cart_id` = '{$purchlog_id}' AND `cart_submitted` = '1'" );
  584. }
  585. $wpdb->query( "DELETE FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`='$purchlog_id'" );
  586. $wpdb->query( "DELETE FROM `" . WPSC_TABLE_SUBMITED_FORM_DATA . "` WHERE `log_id` IN ('$purchlog_id')" );
  587. $wpdb->query( "DELETE FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id`='$purchlog_id' LIMIT 1" );
  588. $deleted = 1;
  589. if ( is_numeric( $_GET['purchlog_id'] ) ) {
  590. $sendback = wp_get_referer();
  591. $sendback = remove_query_arg( 'purchaselog_id', $sendback );
  592. if ( isset( $deleted ) ) {
  593. $sendback = add_query_arg( 'deleted', $deleted, $sendback );
  594. }
  595. wp_redirect( $sendback );
  596. exit();
  597. }
  598. }
  599. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'delete_purchlog') ) {
  600. add_action( 'admin_init', 'wpsc_delete_purchlog' );
  601. }
  602. /*
  603. * Get Shipping Form ajax call
  604. */
  605. function wpsc_ajax_get_shipping_form() {
  606. $shippingname = $_REQUEST['shippingname'];
  607. $_SESSION['previous_shipping_name'] = $shippingname;
  608. $shipping_data = wpsc_get_shipping_form( $shippingname );
  609. $html_shipping_name = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $shipping_data['name'] ) );
  610. $shipping_form = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $shipping_data['form_fields'] ) );
  611. echo "shipping_name_html = '$html_shipping_name'; \n\r";
  612. echo "shipping_form_html = '$shipping_form'; \n\r";
  613. echo "has_submit_button = '{$shipping_data['has_submit_button']}'; \n\r";
  614. exit();
  615. }
  616. function wpsc_ajax_get_payment_form() {
  617. $paymentname = $_REQUEST['paymentname'];
  618. $_SESSION['previous_payment_name'] = $paymentname;
  619. $payment_data = wpsc_get_payment_form( $paymentname );
  620. $html_payment_name = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $payment_data['name'] ) );
  621. $payment_form = str_replace( Array( "\n", "\r" ), Array( "\\n", "\\r" ), addslashes( $payment_data['form_fields'] ) );
  622. echo "payment_name_html = '$html_payment_name'; \n\r";
  623. echo "payment_form_html = '$payment_form'; \n\r";
  624. echo "has_submit_button = '{$payment_data['has_submit_button']}'; \n\r";
  625. exit();
  626. }
  627. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'get_shipping_form') )
  628. add_action( 'admin_init', 'wpsc_ajax_get_shipping_form' );
  629. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'get_payment_form') )
  630. add_action( 'admin_init', 'wpsc_ajax_get_payment_form' );
  631. /*
  632. * Submit Options from Settings Pages,
  633. * takes an array of options checks to see whether it is empty or the same as the exisiting values
  634. * and if its not it updates them.
  635. */
  636. function wpsc_submit_options( $selected='' ) {
  637. global $wpdb, $wpsc_gateways;
  638. $updated = 0;
  639. //This is to change the Overall target market selection
  640. check_admin_referer( 'update-options', 'wpsc-update-options' );
  641. if ( isset( $_POST['change-settings'] ) ) {
  642. if ( isset( $_POST['wpsc_also_bought'] ) && $_POST['wpsc_also_bought'] == 'on' )
  643. update_option( 'wpsc_also_bought', 1 );
  644. else
  645. update_option( 'wpsc_also_bought', 0 );
  646. if ( isset( $_POST['display_find_us'] ) && $_POST['display_find_us'] == 'on' )
  647. update_option( 'display_find_us', 1 );
  648. else
  649. update_option( 'display_find_us', 0 );
  650. if ( isset( $_POST['wpsc_share_this'] ) && $_POST['wpsc_share_this'] == 'on' )
  651. update_option( 'wpsc_share_this', 1 );
  652. else
  653. update_option( 'wpsc_share_this', 0 );
  654. }
  655. if (empty($_POST['countrylist2']) && !empty($_POST['wpsc_options']['currency_sign_location']))
  656. $selected = 'none';
  657. if ( !isset( $_POST['countrylist2'] ) )
  658. $_POST['countrylist2'] = '';
  659. if ( !isset( $_POST['country_id'] ) )
  660. $_POST['country_id'] = '';
  661. if ( !isset( $_POST['country_tax'] ) )
  662. $_POST['country_tax'] = '';
  663. if ( $_POST['countrylist2'] != null || !empty($selected) ) {
  664. $AllSelected = false;
  665. if ( $selected == 'all' ) {
  666. $wpdb->query( "UPDATE `" . WPSC_TABLE_CURRENCY_LIST . "` SET visible = '1'" );
  667. $AllSelected = true;
  668. }
  669. if ( $selected == 'none' ) {
  670. $wpdb->query( "UPDATE `" . WPSC_TABLE_CURRENCY_LIST . "` SET visible = '0'" );
  671. $AllSelected = true;
  672. }
  673. if ( $AllSelected != true ) {
  674. $countrylist = $wpdb->get_col( "SELECT id FROM `" . WPSC_TABLE_CURRENCY_LIST . "` ORDER BY country ASC " );
  675. //find the countries not selected
  676. $unselectedCountries = array_diff( $countrylist, $_POST['countrylist2'] );
  677. foreach ( $unselectedCountries as $unselected ) {
  678. $wpdb->query( "UPDATE `" . WPSC_TABLE_CURRENCY_LIST . "` SET visible = 0 WHERE id = '" . $unselected . "' LIMIT 1" );
  679. }
  680. //find the countries that are selected
  681. $selectedCountries = array_intersect( $countrylist, $_POST['countrylist2'] );
  682. foreach ( $selectedCountries as $selected ) {
  683. $wpdb->query( "UPDATE `" . WPSC_TABLE_CURRENCY_LIST . "` SET visible = 1 WHERE id = '" . $selected . "' LIMIT 1" );
  684. }
  685. }
  686. }
  687. $previous_currency = get_option( 'currency_type' );
  688. $regenerate = false;
  689. $regenerate_options = array('single_view_image_height', 'single_view_image_width','wpsc_gallery_image_width','wpsc_gallery_image_height', 'wpsc_crop_thumbnails','product_image_width','product_image_height');
  690. //To update options
  691. if ( isset( $_POST['wpsc_options'] ) ) {
  692. foreach ( $_POST['wpsc_options'] as $key => $value ) {
  693. if ( in_array( $key, $regenerate_options ) && $value != get_option( $key ) ) {
  694. $regenerate = true;
  695. }
  696. if ( $value != get_option( $key ) ) {
  697. update_option( $key, $value );
  698. $updated++;
  699. }
  700. }
  701. }
  702. if ( $previous_currency != get_option( 'currency_type' ) ) {
  703. $currency_code = $wpdb->get_var( "SELECT `code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id` IN ('" . absint( get_option( 'currency_type' ) ) . "')" );
  704. $selected_gateways = get_option( 'custom_gateway_options' );
  705. $already_changed = array( );
  706. foreach ( $selected_gateways as $selected_gateway ) {
  707. if ( isset( $wpsc_gateways[$selected_gateway]['supported_currencies'] ) ) {
  708. if ( in_array( $currency_code, $wpsc_gateways[$selected_gateway]['supported_currencies']['currency_list'] ) ) {
  709. $option_name = $wpsc_gateways[$selected_gateway]['supported_currencies']['option_name'];
  710. if ( !in_array( $option_name, $already_changed ) ) {
  711. update_option( $option_name, $currency_code );
  712. $already_changed[] = $option_name;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. foreach ( $GLOBALS['wpsc_shipping_modules'] as $shipping ) {
  719. if ( is_object( $shipping ) )
  720. $shipping->submit_form();
  721. }
  722. //This is for submitting shipping details to the shipping module
  723. if ( !isset( $_POST['update_gateways'] ) )
  724. $_POST['update_gateways'] = '';
  725. if ( !isset( $_POST['custom_shipping_options'] ) )
  726. $_POST['custom_shipping_options'] = null;
  727. if ( $_POST['update_gateways'] == 'true' ) {
  728. update_option( 'custom_shipping_options', $_POST['custom_shipping_options'] );
  729. $shipadd = 0;
  730. foreach ( $GLOBALS['wpsc_shipping_modules'] as $shipping ) {
  731. foreach ( (array)$_POST['custom_shipping_options'] as $shippingoption ) {
  732. if ( $shipping->internal_name == $shippingoption ) {
  733. $shipadd++;
  734. }
  735. }
  736. }
  737. }
  738. $sendback = wp_get_referer();
  739. if ( $regenerate ) {
  740. $sendback = add_query_arg( array('regenerate' => 'true', 'updated' => $updated), $sendback );
  741. }
  742. if ( isset( $updated ) ) {
  743. $sendback = add_query_arg( 'updated', $updated, $sendback );
  744. }
  745. if ( isset( $shipadd ) ) {
  746. $sendback = add_query_arg( 'shipadd', $shipadd, $sendback );
  747. }
  748. if ( !isset( $_SESSION['wpsc_settings_curr_page'] ) )
  749. $_SESSION['wpsc_settings_curr_page'] = '';
  750. if ( !isset( $_POST['page_title'] ) )
  751. $_POST['page_title'] = '';
  752. if ( isset( $_SESSION['wpsc_settings_curr_page'] ) ) {
  753. $sendback = add_query_arg( 'tab', $_SESSION['wpsc_settings_curr_page'], $sendback );
  754. }
  755. $sendback = add_query_arg( 'page', 'wpsc-settings', $sendback );
  756. wp_redirect( $sendback );
  757. exit();
  758. }
  759. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'submit_options') )
  760. add_action( 'admin_init', 'wpsc_submit_options' );
  761. function wpsc_change_currency() {
  762. if ( is_numeric( $_POST['currencyid'] ) ) {
  763. $currency_data = $wpdb->get_results( "SELECT `symbol`,`symbol_html`,`code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id`='" . $_POST['currencyid'] . "' LIMIT 1", ARRAY_A );
  764. $price_out = null;
  765. if ( $currency_data[0]['symbol'] != '' ) {
  766. $currency_sign = $currency_data[0]['symbol_html'];
  767. } else {
  768. $currency_sign = $currency_data[0]['code'];
  769. }
  770. echo $currency_sign;
  771. }
  772. }
  773. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'change_currency') )
  774. add_action( 'admin_init', 'wpsc_change_currency' );
  775. function wpsc_rearrange_images() {
  776. global $wpdb;
  777. $images = explode( ",", $_POST['order'] );
  778. $product_id = absint( $_POST['product_id'] );
  779. $timestamp = time();
  780. $new_main_image = null;
  781. $have_set_first_item = false;
  782. $i = 0;
  783. foreach ( $images as $image ) {
  784. if ( $image > 0 ) {
  785. $wpdb->query( $wpdb->prepare( "UPDATE `{$wpdb->posts}` SET `menu_order`='%d' WHERE `ID`='%d' LIMIT 1", $i, $image ) );
  786. $i++;
  787. }
  788. }
  789. $output = wpsc_main_product_image_menu( $product_id );
  790. echo "image_menu = '';\n\r";
  791. echo "image_id = '" . $new_main_image . "';\n\r";
  792. exit();
  793. }
  794. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'rearrange_images') )
  795. add_action( 'admin_init', 'wpsc_rearrange_images' );
  796. /**
  797. * wpsc_update_page_urls gets the permalinks for products pages and stores them in the options for quick reference
  798. * @public
  799. *
  800. * @since 3.6
  801. * @param $auto (Boolean) true if coming from WordPress Permalink Page, false otherwise
  802. * @return nothing
  803. */
  804. function wpsc_update_page_urls($auto = false) {
  805. global $wpdb;
  806. $wpsc_pageurl_option['product_list_url'] = '[productspage]';
  807. $wpsc_pageurl_option['shopping_cart_url'] = '[shoppingcart]';
  808. $check_chekout = $wpdb->get_var( "SELECT `guid` FROM `{$wpdb->posts}` WHERE `post_content` LIKE '%[checkout]%' LIMIT 1" );
  809. if ( $check_chekout != null ) {
  810. $wpsc_pageurl_option['checkout_url'] = '[checkout]';
  811. } else {
  812. $wpsc_pageurl_option['checkout_url'] = '[checkout]';
  813. }
  814. $wpsc_pageurl_option['transact_url'] = '[transactionresults]';
  815. $wpsc_pageurl_option['user_account_url'] = '[userlog]';
  816. $changes_made = false;
  817. foreach ( $wpsc_pageurl_option as $option_key => $page_string ) {
  818. $post_id = $wpdb->get_var( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` IN('page','post') AND `post_content` LIKE '%$page_string%' LIMIT 1" );
  819. if ( ! $post_id )
  820. continue;
  821. $the_new_link = _get_page_link( $post_id );
  822. if ( stristr( get_option( $option_key ), "https://" ) ) {
  823. $the_new_link = str_replace( 'http://', "https://", $the_new_link );
  824. }
  825. update_option( $option_key, $the_new_link );
  826. }
  827. if(!$auto){
  828. $sendback = wp_get_referer();
  829. if ( isset( $updated ) )
  830. $sendback = add_query_arg( 'updated', $updated, $sendback );
  831. if ( isset( $_SESSION['wpsc_settings_curr_page'] ) )
  832. $sendback = add_query_arg( 'tab', $_SESSION['wpsc_settings_curr_page'], $sendback );
  833. wp_redirect( $sendback );
  834. exit();
  835. }
  836. }
  837. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'update_page_urls') )
  838. add_action( 'admin_init', 'wpsc_update_page_urls' );
  839. function wpsc_clean_categories() {
  840. global $wpdb, $wp_rewrite;
  841. $sql_query = "SELECT `id`, `name`, `active` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "`";
  842. $sql_data = $wpdb->get_results( $sql_query, ARRAY_A );
  843. foreach ( (array)$sql_data as $datarow ) {
  844. if ( $datarow['active'] == 1 ) {
  845. $tidied_name = trim( $datarow['name'] );
  846. $tidied_name = strtolower( $tidied_name );
  847. $url_name = sanitize_title( $tidied_name );
  848. $similar_names = $wpdb->get_row( "SELECT COUNT(*) AS `count`, MAX(REPLACE(`nice-name`, '$url_name', '')) AS `max_number` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `nice-name` REGEXP '^($url_name){1}(\d)*$' AND `id` NOT IN ('{$datarow['id']}') ", ARRAY_A );
  849. $extension_number = '';
  850. if ( $similar_names['count'] > 0 ) {
  851. $extension_number = (int)$similar_names['max_number'] + 2;
  852. }
  853. $url_name .= $extension_number;
  854. $wpdb->query( "UPDATE `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` SET `nice-name` = '$url_name' WHERE `id` = '{$datarow['id']}' LIMIT 1 ;" );
  855. $updated;
  856. } else if ( $datarow['active'] == 0 ) {
  857. $wpdb->query( "UPDATE `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` SET `nice-name` = '' WHERE `id` = '{$datarow['id']}' LIMIT 1 ;" );
  858. $updated;
  859. }
  860. }
  861. $wp_rewrite->flush_rules();
  862. $sendback = wp_get_referer();
  863. if ( isset( $updated ) ) {
  864. $sendback = add_query_arg( 'updated', $updated, $sendback );
  865. }
  866. if ( isset( $_SESSION['wpsc_settings_curr_page'] ) ) {
  867. $sendback = add_query_arg( 'tab', $_SESSION['wpsc_settings_curr_page'], $sendback );
  868. }
  869. wp_redirect( $sendback );
  870. exit();
  871. }
  872. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'clean_categories') )
  873. add_action( 'admin_init', 'wpsc_clean_categories' );
  874. //change the regions tax settings
  875. function wpsc_change_region_tax() {
  876. global $wpdb;
  877. if ( is_array( $_POST['region_tax'] ) ) {
  878. foreach ( $_POST['region_tax'] as $region_id => $tax ) {
  879. if ( is_numeric( $region_id ) && is_numeric( $tax ) ) {
  880. $previous_tax = $wpdb->get_var( "SELECT `tax` FROM `" . WPSC_TABLE_REGION_TAX . "` WHERE `id` = '$region_id' LIMIT 1" );
  881. if ( $tax != $previous_tax ) {
  882. $wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `tax` = '$tax' WHERE `id` = '$region_id' LIMIT 1" );
  883. $changes_made = true;
  884. }
  885. }
  886. }
  887. $sendback = wp_get_referer();
  888. wp_redirect( $sendback );
  889. }
  890. }
  891. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'change_region_tax') )
  892. add_action( 'admin_init', 'wpsc_change_region_tax' );
  893. function wpsc_product_files_existing() {
  894. //List all product_files, with checkboxes
  895. $product_id = absint( $_GET["product_id"] );
  896. $file_list = wpsc_uploaded_files();
  897. $args = array(
  898. 'post_type' => 'wpsc-product-file',
  899. 'post_parent' => $product_id,
  900. 'numberposts' => -1,
  901. 'post_status' => 'all'
  902. );
  903. $attached_files = (array)get_posts( $args );
  904. foreach ( $attached_files as $key => $attached_file ) {
  905. $attached_files_by_file[$attached_file->post_title] = & $attached_files[$key];
  906. }
  907. $output = "<span class='admin_product_notes select_product_note '>" . __( 'Choose a downloadable file for this product:', 'wpsc' ) . "</span><br>";
  908. $output .= "<form method='post' class='product_upload'>";
  909. $output .= "<div class='ui-widget-content multiple-select select_product_file'>";
  910. $num = 0;
  911. foreach ( (array)$file_list as $file ) {
  912. $num++;
  913. $checked_curr_file = "";
  914. if ( isset( $attached_files_by_file[$file['display_filename']] ) ) {
  915. $checked_curr_file = "checked='checked'";
  916. }
  917. $output .= "<p " . ((($num % 2) > 0) ? '' : "class='alt'") . " id='select_product_file_row_$num'>\n";
  918. $output .= " <input type='checkbox' name='select_product_file[]' value='" . $file['real_filename'] . "' id='select_product_file_$num' " . $checked_curr_file . " />\n";
  919. $output .= " <label for='select_product_file_$num'>" . $file['display_filename'] . "</label>\n";
  920. $output .= "</p>\n";
  921. }
  922. $output .= "</div>";
  923. $output .= "<input type='hidden' id='hidden_id' value='$product_id' />";
  924. $output .= "<input type='submit' name='save' name='product_files_submit' class='button-primary prdfil' value='Save Product Files' />";
  925. $output .= "</form>";
  926. $output .= "<div class='" . ((is_numeric( $product_id )) ? "edit_" : "") . "select_product_handle'><div></div></div>";
  927. $output .= "<script type='text/javascript'>\n\r";
  928. $output .= "var select_min_height = " . (25 * 3) . ";\n\r";
  929. $output .= "var select_max_height = " . (25 * ($num + 1)) . ";\n\r";
  930. $output .= "</script>";
  931. echo $output;
  932. }
  933. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'product_files_existing') )
  934. add_action( 'admin_init', 'wpsc_product_files_existing' );
  935. function prod_upload() {
  936. global $wpdb;
  937. $product_id = absint( $_POST["product_id"] );
  938. $output = '';
  939. foreach ( $_POST["select_product_file"] as $selected_file ) {
  940. // if we already use this file, there is no point doing anything more.
  941. $sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = 'wpsc-product-file' AND post_title = %s", $selected_file ); // TODO it's safer to select by post ID, in that case we will use get_posts()
  942. $file_post_data = $wpdb->get_row( $sql, ARRAY_A );
  943. $selected_file_path = WPSC_FILE_DIR . basename( $selected_file );
  944. if ( empty( $file_post_data ) ) {
  945. $type = wpsc_get_mimetype( $selected_file_path );
  946. $attachment = array(
  947. 'post_mime_type' => $type,
  948. 'post_parent' => $product_id,
  949. 'post_title' => $selected_file,
  950. 'post_content' => '',
  951. 'post_type' => "wpsc-product-file",
  952. 'post_status' => 'inherit'
  953. );
  954. $id = wp_insert_post( $attachment );
  955. } else {
  956. // already attached
  957. if ( $file_post_data['post_parent'] == $product_id )
  958. continue;
  959. $type = $file_post_data["post_mime_type"];
  960. $url = $file_post_data["guid"];
  961. $title = $file_post_data["post_title"];
  962. $content = $file_post_data["post_content"];
  963. // Construct the attachment
  964. $attachment = array(
  965. 'post_mime_type' => $type,
  966. 'guid' => $url,
  967. 'post_parent' => absint( $product_id ),
  968. 'post_title' => $title,
  969. 'post_content' => $content,
  970. 'post_type' => "wpsc-product-file",
  971. 'post_status' => 'inherit'
  972. );
  973. // Save the data
  974. $id = wp_insert_post( $attachment );
  975. }
  976. $deletion_url = wp_nonce_url( "admin.php?wpsc_admin_action=delete_file&amp;file_name={$attachment['post_title']}&amp;product_id={$product_id}", 'delete_file_' . $attachment['post_title'] );
  977. $output .= "<p id='select_product_file_row_id_" . $id . "'>\n";
  978. $output .= " <a class='file_delete_button' href='{$deletion_url}' >\n";
  979. $output .= " <img src='" . WPSC_CORE_IMAGES_URL . "/cross.png' />\n";
  980. $output .= " </a>\n";
  981. $output .= " <label for='select_product_file_row_id_" . $id . "'>" . $attachment['post_title'] . "</label>\n";
  982. $output .= "</p>\n";
  983. }
  984. echo $output;
  985. }
  986. if ( isset( $_GET['wpsc_admin_action'] ) && ($_GET['wpsc_admin_action'] == 'product_files_upload') )
  987. add_action( 'admin_init', 'prod_upload' );
  988. //change the gateway settings
  989. function wpsc_gateway_settings() {
  990. //To update options
  991. if ( isset( $_POST['wpsc_options'] ) ) {
  992. foreach ( $_POST['wpsc_options'] as $key => $value ) {
  993. if ( $value != get_option( $key ) ) {
  994. update_option( $key, $value );
  995. }
  996. }
  997. unset( $_POST['wpsc_options'] );
  998. }
  999. if ( isset( $_POST['user_defined_name'] ) && is_array( $_POST['user_defined_name'] ) ) {
  1000. $payment_gateway_names = get_option( 'payment_gateway_names' );
  1001. if ( !is_array( $payment_gateway_names ) ) {
  1002. $payment_gateway_names = array( );
  1003. }
  1004. $payment_gateway_names = array_merge( $payment_gateway_names, (array)$_POST['user_defined_name'] );
  1005. update_option( 'payment_gateway_names', $payment_gateway_names );
  1006. }
  1007. $custom_gateways = get_option( 'custom_gateway_options' );
  1008. $nzshpcrt_gateways = nzshpcrt_get_gateways();
  1009. foreach ( $nzshpcrt_gateways as $gateway ) {
  1010. if ( in_array( $gateway['internalname'], $custom_gateways ) ) {
  1011. if ( isset( $gateway['submit_function'] ) ) {
  1012. call_user_func_array( $gateway['submit_function'], array( ) );
  1013. $changes_made = true;
  1014. }
  1015. }
  1016. }
  1017. if ( (isset( $_POST['payment_gw'] ) && $_POST['payment_gw'] != null ) ) {
  1018. update_option( 'payment_gateway', $_POST['payment_gw'] );
  1019. }
  1020. $sendback = wp_get_referer();
  1021. if ( isset( $updated ) ) {
  1022. $sendback = add_query_arg( 'updated', $updated, $sendback );
  1023. }
  1024. if ( isset( $_SESSION['wpsc_settings_curr_page'] ) ) {
  1025. $sendback = add_query_arg( 'page', 'wpsc-settings', $sendback );
  1026. $sendback = add_query_arg( 'tab', $_SESSION['wpsc_settings_curr_page'], $sendback );
  1027. }
  1028. wp_redirect( $sendback );
  1029. exit();
  1030. }
  1031. if ( isset( $_REQUEST['wpsc_gateway_settings'] ) && ($_REQUEST['wpsc_gateway_settings'] == 'gateway_settings') )
  1032. add_action( 'admin_init', 'wpsc_gateway_settings' );
  1033. function wpsc_check_form_options() {
  1034. global $wpdb;
  1035. $id = $wpdb->escape( $_POST['form_id'] );
  1036. $sql = 'SELECT `options` FROM `' . WPSC_TABLE_CHECKOUT_FORMS . '` WHERE `id`=' . $id;
  1037. $options = $wpdb->get_var( $sql );
  1038. if ( $options != '' ) {
  1039. $options = maybe_unserialize( $options );
  1040. if ( !is_array( $options ) ) {
  1041. $options = unserialize( $options );
  1042. }
  1043. $output = "<tr class='wpsc_grey'><td></td><td colspan='5'>Please Save your changes before trying to Order your Checkout Forms again.</td></tr>\r\n<tr class='wpsc_grey'><td></td><th>Label</th><th >Value</th><td colspan='3'><a href='' class='wpsc_add_new_checkout_option' title='form_options[" . $id . "]'>+ New Layer</a></td></tr>";
  1044. foreach ( (array)$options as $key => $value ) {
  1045. $output .="<tr class='wpsc_grey'><td></td><td><input type='text' value='" . $key . "' name='wpsc_checkout_option_label[" . $id . "][]' /></td><td colspan='4'><input type='text' value='" . $value . "' name='wpsc_checkout_option_value[" . $id . "][]' />&nbsp;<a class='wpsc_delete_option' href='' <img src='" . WPSC_CORE_IMAGES_URL . "/trash.gif' alt='" . __( 'Delete', 'wpsc' ) . "' title='" . __( 'Delete', 'wpsc' ) . "' /></a></td></tr>";
  1046. }
  1047. } else {
  1048. $output = '';
  1049. }
  1050. exit( $output );
  1051. }
  1052. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'check_form_options') )
  1053. add_action( 'admin_init', 'wpsc_check_form_options' );
  1054. //handles the editing and adding of new checkout fields
  1055. function wpsc_checkout_settings() {
  1056. global $wpdb;
  1057. $wpdb->show_errors = true;
  1058. $filter = isset( $_POST['selected_form_set'] ) ? $_POST['selected_form_set'] : 0;
  1059. if ( $_POST['new_form_set'] != null ) {
  1060. $checkout_sets = get_option( 'wpsc_checkout_form_sets' );
  1061. $checkout_sets[] = $_POST['new_form_set'];
  1062. update_option( 'wpsc_checkout_form_sets', $checkout_sets );
  1063. }
  1064. /*
  1065. // Save checkout options
  1066. */
  1067. $options = array( );
  1068. if ( isset($_POST['wpsc_checkout_option_label']) && is_array( $_POST['wpsc_checkout_option_label'] ) ) {
  1069. foreach ( $_POST['wpsc_checkout_option_label'] as $form_id => $values ) {
  1070. $options = array( );
  1071. foreach ( (array)$values as $key => $form_option ) {
  1072. $form_option = str_ireplace( "'", "", $form_option );
  1073. $form_val = str_ireplace( "'", "", sanitize_title( $_POST['wpsc_checkout_option_value'][$form_id][$key] ) );
  1074. $options[$form_option] = $form_val;
  1075. }
  1076. $options = serialize( $options );
  1077. $wpdb->update(
  1078. WPSC_TABLE_CHECKOUT_FORMS,
  1079. array( 'options' => $options ),
  1080. array( 'id' => $form_id ),
  1081. '%s',
  1082. '%d'
  1083. );
  1084. }
  1085. }
  1086. if ( $_POST['form_name'] != null ) {
  1087. foreach ( $_POST['form_name'] as $form_id => $form_name ) {
  1088. $form_type = $_POST['form_type'][$form_id];
  1089. $form_mandatory = 0;
  1090. if ( isset( $_POST['form_mandatory'][$form_id] ) && ($_POST['form_mandatory'][$form_id] == 1) ) {
  1091. $form_mandatory = 1;
  1092. }
  1093. $form_display_log = 0;
  1094. if ( isset( $_POST['form_display_log'][$form_id] ) && ($_POST['form_display_log'][$form_id] == 1) ) {
  1095. $form_display_log = 1;
  1096. }
  1097. $unique_name = '';
  1098. if ( $_POST['unique_names'][$form_id] != '-1' ) {
  1099. $unique_name = $_POST['unique_names'][$form_id];
  1100. }
  1101. $wpdb->update(
  1102. WPSC_TABLE_CHECKOUT_FORMS,
  1103. array(
  1104. 'name' => $form_name,
  1105. 'type' => $form_type,
  1106. 'mandatory' => $form_mandatory,
  1107. 'display_log' => $form_display_log,
  1108. 'unique_name' => $unique_name,
  1109. ),
  1110. array( 'id' => $form_id ),
  1111. '%s',
  1112. '%d'
  1113. );
  1114. }
  1115. }
  1116. if ( isset( $_POST['new_form_name'] ) ) {
  1117. foreach ( $_POST['new_form_name'] as $form_id => $form_name ) {
  1118. $form_type = $_POST['new_form_type'][$form_id];
  1119. $form_mandatory = 0;
  1120. if ( $_POST['new_form_mandatory'][$form_id] == 1 ) {
  1121. $form_mandatory = 1;
  1122. }
  1123. $form_display_log = 0;
  1124. if ( isset( $_POST['new_form_display_log'][$form_id] ) && $_POST['new_form_display_log'][$form_id] == 1 ) {
  1125. $form_display_log = 1;
  1126. }
  1127. if ( $_POST['new_form_unique_name'][$form_id] != '-1' ) {
  1128. $form_unique_name = $_POST['new_form_unique_name'][$form_id];
  1129. }
  1130. $max_order_sql = "SELECT MAX(`checkout_order`) AS `checkout_order` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `active` = '1';";
  1131. if ( isset( $_POST['new_form_order'][$form_id] ) && $_POST['new_form_order'][$form_id] != '' ) {
  1132. $order_number = $_POST['new_form_order'][$form_id];
  1133. } else {
  1134. $max_order_sql = $wpdb->get_results( $max_order_sql, ARRAY_A );
  1135. $order_number = $max_order_sql[0]['checkout_order'] + 1;
  1136. }
  1137. $wpdb->insert(
  1138. WPSC_TABLE_CHECKOUT_FORMS,
  1139. array(
  1140. 'name' => $form_name,
  1141. 'type' => $form_type,
  1142. 'mandatory' => $form_mandatory,
  1143. 'display_log' => $form_display_log,
  1144. 'default' => '',
  1145. 'active' => '1',
  1146. 'checkout_order' => $order_number,
  1147. 'unique_name' => $form_unique_name,
  1148. 'checkout_set' => $filter,
  1149. ),
  1150. array( '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s' )
  1151. );
  1152. $added++;
  1153. }
  1154. }
  1155. if ( isset( $_POST['wpsc_options'] ) ) {
  1156. foreach ( $_POST['wpsc_options'] as $key => $value ) {
  1157. if ( $value != get_option( $key ) ) {
  1158. update_option( $key, $value );
  1159. $updated++;
  1160. }
  1161. }
  1162. }
  1163. $sendback = wp_get_referer();
  1164. if ( isset( $form_set_key ) ) {
  1165. $sendback = add_query_arg( 'checkout-set', $form_set_key, $sendback );
  1166. } else if ( isset( $_POST['wpsc_form_set'] ) ) {
  1167. $filter = $_POST['wpsc_form_set'];
  1168. $sendback = add_query_arg( 'checkout-set', $filter, $sendback );
  1169. }
  1170. if ( isset( $updated ) ) {
  1171. $sendback = add_query_arg( 'updated', $updated, $sendback );
  1172. }
  1173. if ( isset( $added ) ) {
  1174. $sendback = add_query_arg( 'added', $added, $sendback );
  1175. }
  1176. if ( isset( $_SESSION['wpsc_settings_curr_page'] ) ) {
  1177. $sendback = add_query_arg( 'tab', $_SESSION['wpsc_settings_curr_page'], $sendback );
  1178. }
  1179. $sendback = add_query_arg( 'page', 'wpsc-settings', $sendback );
  1180. wp_redirect( $sendback );
  1181. exit();
  1182. }
  1183. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'checkout_settings') )
  1184. add_action( 'admin_init', 'wpsc_checkout_settings' );
  1185. function wpsc_google_shipping_settings() {
  1186. if ( isset( $_POST['submit'] ) ) {
  1187. foreach ( (array)$_POST['google_shipping'] as $key => $country ) {
  1188. if ( $country == 'on' ) {
  1189. $google_shipping_country[] = $key;
  1190. $updated++;
  1191. }
  1192. }
  1193. update_option( 'google_shipping_country', $google_shipping_country );
  1194. $sendback = wp_get_referer();
  1195. $sendback = remove_query_arg( 'googlecheckoutshipping', $sendback );
  1196. if ( isset( $updated ) ) {
  1197. $sendback = add_query_arg( 'updated', $updated, $sendback );
  1198. }
  1199. wp_redirect( $sendback );
  1200. exit();
  1201. }
  1202. }
  1203. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'google_shipping_settings') ) {
  1204. add_action( 'admin_init', 'wpsc_google_shipping_settings' );
  1205. }
  1206. //for ajax call of settings page tabs
  1207. function wpsc_settings_page_ajax() {
  1208. $html = '';
  1209. $modified_page_title = $_POST['page_title'];
  1210. $page_title = str_replace( "tab-", "", $modified_page_title );
  1211. check_admin_referer( $modified_page_title );
  1212. switch ( $page_title ) {
  1213. case 'checkout' :
  1214. require_once( WPSC_FILE_PATH . '/wpsc-admin/includes/settings-pages/checkout.php' );
  1215. wpsc_options_checkout();
  1216. break;
  1217. case 'gateway' :
  1218. require_once( WPSC_FILE_PATH . '/wpsc-admin/includes/settings-pages/gateway.php' );
  1219. wpsc_options_gateway();
  1220. break;
  1221. case 'shipping' :
  1222. require_once( WPSC_FILE_PATH . '/wpsc-admin/includes/settings-pages/shipping.php' );
  1223. wpsc_options_shipping();
  1224. break;
  1225. case 'admin' :
  1226. require_once( WPSC_FILE_PATH . '/wpsc-admin/includes/settings-pages/admin.php' );
  1227. wpsc_options_admin();
  1228. break;
  1229. case 'presentation' :
  1230. require_once( WPSC_FILE_PATH . '/wpsc-admin/includes/settings-pages/presentation.php' );
  1231. wpsc_options_presentation();
  1232. break;
  1233. case 'taxes' :
  1234. wpec_taxes_settings_page(); //see wpec-taxes view
  1235. break;
  1236. case 'marketing' :
  1237. require_once( WPSC_FILE_PATH . '/wpsc-admin/includes/settings-pages/marketing.php' );
  1238. wpsc_options_marketing();
  1239. break;
  1240. case 'import' :
  1241. require_once( WPSC_FILE_PATH . '/wpsc-admin/includes/settings-pages/import.php' );
  1242. wpsc_options_import();
  1243. break;
  1244. case 'general' :
  1245. default;
  1246. require_once( WPSC_FILE_PATH . '/wpsc-admin/includes/settings-pages/general.php' );
  1247. wpsc_options_general();
  1248. break;
  1249. }
  1250. $_SESSION['wpsc_settings_curr_page'] = $page_title;
  1251. exit( $html );
  1252. }
  1253. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ($_REQUEST['wpsc_admin_action'] == 'settings_page_ajax') )
  1254. add_action( 'admin_init', 'wpsc_settings_page_ajax' );
  1255. function wpsc_update_variations() {
  1256. $product_id = absint( $_POST["product_id"] );
  1257. $product_type_object = get_post_type_object('wpsc-product');
  1258. if (!current_user_can($product_type_object->cap->edit_post, $product_id))
  1259. return;
  1260. //Setup postdata
  1261. $post_data = array( );
  1262. $post_data['edit_var_val'] = isset( $_POST['edit_var_val'] ) ? $_POST["edit_var_val"] : '';
  1263. $post_data['description'] = isset( $_POST['description'] ) ? $_POST["description"] : '';
  1264. $post_data['additional_description'] = isset( $_POST['additional_description'] ) ? $_POST['additional_description'] : '';
  1265. $post_data['name'] = (!empty($_POST['name']))?$_POST['name']:$_POST["post_title"];
  1266. //Add or delete variations
  1267. wpsc_edit_product_variations( $product_id, $post_data );
  1268. if (defined('DOING_AJAX') && DOING_AJAX) {
  1269. wpsc_admin_product_listing( $product_id );
  1270. die();
  1271. }
  1272. }
  1273. if ( isset($_POST["edit_var_val"]) )
  1274. add_action( 'admin_init', 'wpsc_update_variations', 50 );
  1275. add_action('wp_ajax_wpsc_update_variations', 'wpsc_update_variations', 50 );
  1276. function wpsc_delete_variation_set() {
  1277. check_admin_referer( 'delete-variation' );
  1278. if ( is_numeric( $_GET['deleteid'] ) ) {
  1279. $variation_id = absint( $_GET['deleteid'] );
  1280. $variation_set = get_term( $variation_id, 'wpsc-variation', ARRAY_A );
  1281. $variations = get_terms( 'wpsc-variation', array(
  1282. 'hide_empty' => 0,
  1283. 'parent' => $variation_id
  1284. ) );
  1285. foreach ( (array)$variations as $variation ) {
  1286. $return_value = wp_delete_term( $variation->term_id, 'wpsc-variation' );
  1287. }
  1288. if ( !empty( $variation_set ) ) {
  1289. $return_value = wp_delete_term( $variation_set['term_id'], 'wpsc-variation' );
  1290. }
  1291. $deleted = 1;
  1292. }
  1293. $sendback = wp_get_referer();
  1294. if ( isset( $deleted ) ) {
  1295. $sendback = add_query_arg( 'deleted', $deleted, $sendback );
  1296. }
  1297. $sendback = remove_query_arg( array(
  1298. 'deleteid',
  1299. 'variation_id'
  1300. ), $sendback );
  1301. wp_redirect( $sendback );
  1302. exit();
  1303. }
  1304. function wpsc_force_flush_theme_transients() {
  1305. // Flush transients
  1306. wpsc_flush_theme_transients( true );
  1307. // Bounce back
  1308. $sendback = wp_get_referer();
  1309. wp_redirect( $sendback );
  1310. exit();
  1311. }
  1312. if ( isset( $_REQUEST['wpsc_flush_theme_transients'] ) && ( $_REQUEST['wpsc_flush_theme_transients'] == 'true' ) )
  1313. add_action( 'admin_init', 'wpsc_force_flush_theme_transients' );
  1314. function wpsc_backup_theme() {
  1315. $wp_theme_path = get_stylesheet_directory();
  1316. wpsc_recursive_copy( $wp_theme_path, WPSC_THEME_BACKUP_DIR );
  1317. $_SESSION['wpsc_themes_backup'] = true;
  1318. $sendback = wp_get_referer();
  1319. wp_redirect( $sendback );
  1320. exit();
  1321. }
  1322. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ( $_REQUEST['wpsc_admin_action'] == 'backup_themes' ) )
  1323. add_action( 'admin_init', 'wpsc_backup_theme' );
  1324. function wpsc_delete_coupon(){
  1325. global $wpdb;
  1326. check_admin_referer( 'delete-coupon' );
  1327. $coupon_id = (int)$_GET['delete_id'];
  1328. if(isset($coupon_id)) {
  1329. $wpdb->query("DELETE FROM `".WPSC_TABLE_COUPON_CODES."` WHERE `id` = '$coupon_id' LIMIT 1;");
  1330. $deleted = 1;
  1331. }
  1332. $sendback = wp_get_referer();
  1333. if ( isset( $deleted ) )
  1334. $sendback = add_query_arg( 'deleted', $deleted, $sendback );
  1335. $sendback = remove_query_arg( array('deleteid',), $sendback );
  1336. wp_redirect( $sendback );
  1337. exit();
  1338. }
  1339. if ( isset( $_GET['action'] ) && ( 'purchase_log' == $_GET['action'] ) )
  1340. add_action( 'admin_init', 'wpsc_admin_sale_rss' );
  1341. if ( isset( $_GET['purchase_log_csv'] ) && ( 'true' == $_GET['purchase_log_csv'] ) )
  1342. add_action( 'admin_init', 'wpsc_purchase_log_csv' );
  1343. if ( isset( $_REQUEST['ajax'] ) && isset( $_REQUEST['admin'] ) && ($_REQUEST['ajax'] == "true") && ($_REQUEST['admin'] == "true") )
  1344. add_action( 'admin_init', 'wpsc_admin_ajax' );
  1345. // Variation set deleting init code starts here
  1346. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ( 'wpsc-delete-variation-set' == $_REQUEST['wpsc_admin_action'] ) )
  1347. add_action( 'admin_init', 'wpsc_delete_variation_set' );
  1348. //Delete Coupon
  1349. if ( isset( $_REQUEST['wpsc_admin_action'] ) && ( 'wpsc-delete-coupon' == $_REQUEST['wpsc_admin_action'] ) )
  1350. add_action( 'admin_init', 'wpsc_delete_coupon' );
  1351. function flat_price( $price ) {
  1352. if ( isset( $price ) && !empty( $price ) && strchr( $price, '-' ) === false && strchr( $price, '+' ) === false && strchr( $price, '%' ) === false )
  1353. return true;
  1354. }
  1355. function percentile_price( $price ) {
  1356. if ( isset( $price ) && !empty( $price ) && ( strchr( $price, '-' ) || strchr( $price, '+' ) ) && strchr( $price, '%' ) )
  1357. return true;
  1358. }
  1359. function differential_price( $price ) {
  1360. if ( isset( $price ) && !empty( $price ) && ( strchr( $price, '-' ) || strchr( $price, '+' ) ) && strchr( $price, '%' ) === false )
  1361. return true;
  1362. }
  1363. /**
  1364. * If it doesn't exist, let's create a multi-dimensional associative array
  1365. * that will contain all of the term/price associations
  1366. *
  1367. * @param <type> $variation
  1368. */
  1369. function variation_price_field( $variation ) {
  1370. $term_prices = get_option( 'term_prices' );
  1371. if ( is_object( $variation ) )
  1372. $term_id = $variation->term_id;
  1373. if ( empty( $term_prices ) || !is_array( $term_prices ) ) {
  1374. $term_prices = array( );
  1375. if ( isset( $term_id ) ) {
  1376. $term_prices[$term_id] = array( );
  1377. $term_prices[$term_id]["price"] = '';
  1378. $term_prices[$term_id]["checked"] = '';
  1379. }
  1380. add_option( 'term_prices', $term_prices );
  1381. }
  1382. if ( isset( $term_id ) && is_array( $term_prices ) && array_key_exists( $term_id, $term_prices ) )
  1383. $price = esc_attr( $term_prices[$term_id]["price"] );
  1384. else
  1385. $price = '';
  1386. if( !isset( $_GET['action'] ) ) {
  1387. ?>
  1388. <div class="form-field">
  1389. <label for="variation_price"><?php _e( 'Variation Price', 'wpsc' ); ?></label>
  1390. <input type="text" name="variation_price" id="variation_price" style="width:50px;" value="<?php echo $price; ?>"><br />
  1391. <span class="description"><?php _e( 'You can list a default price here for this variation. You can list a regular price (18.99), differential price (+1.99 / -2) or even a percentage-based price (+50% / -25%).', 'wpsc' ); ?></span>
  1392. </div>
  1393. <script type="text/javascript">
  1394. jQuery('#parent option:contains("   ")').remove();
  1395. jQuery('#parent').mousedown(function(){
  1396. jQuery('#parent option:contains("   ")').remove();
  1397. });
  1398. </script>
  1399. <?php
  1400. } else{
  1401. ?>
  1402. <tr class="form-field">
  1403. <th scope="row" valign="top">
  1404. <label for="variation_price"><?php _e( 'Variation Price', 'wpsc' ); ?></label>
  1405. </th>
  1406. <td>
  1407. <input type="text" name="variation_price" id="variation_price" style="width:50px;" value="<?php echo $price; ?>"><br />
  1408. <span class="description"><?php _e( 'You can list a default price here for this variation. You can list a regular price (18.99), differential price (+1.99 / -2) or even a percentage-based price (+50% / -25%).', 'wpsc' ); ?></span>
  1409. </td>
  1410. </tr>
  1411. <?php
  1412. }
  1413. }
  1414. add_action( 'wpsc-variation_edit_form_fields', 'variation_price_field' );
  1415. add_action( 'wpsc-variation_add_form_fields', 'variation_price_field' );
  1416. function variation_price_field_check( $variation ) {
  1417. $term_prices = get_option( 'term_prices' );
  1418. if ( is_array( $term_prices ) && array_key_exists( $variation->term_id, $term_prices ) )
  1419. $checked = ($term_prices[$variation->term_id]["checked"] == 'checked') ? 'checked' : '';
  1420. else
  1421. $checked = ''; ?>
  1422. <tr class="form-field">
  1423. <th scope="row" valign="top"><label for="apply_to_current"><?php _e( 'Apply to current variations?' ) ?></label></th>
  1424. <td>
  1425. <span class="description"><input type="checkbox" name="apply_to_current" id="apply_to_current" style="width:2%;" <?php echo $checked; ?> /><?php _e( 'By checking this box, the price rule you implement above will be applied to all variations that currently exist. If you leave it unchecked, it will only apply to products that use this variation created or edited from now on. Take note, this will apply this rule to <strong>every</strong> product using this variation. If you need to override it for any reason on a specific product, simply go to that product and change the price.', 'wpsc' ); ?></span>
  1426. </td>
  1427. </tr>
  1428. <?php
  1429. }
  1430. add_action( 'wpsc-variation_edit_form_fields', 'variation_price_field_check' );
  1431. /**
  1432. * @todo - Should probably refactor this at some point - very procedural,
  1433. * WAY too many foreach loops for my liking :) But it does the trick
  1434. *
  1435. * @param <type> $term_id
  1436. */
  1437. function save_term_prices( $term_id ) {
  1438. // First - Saves options from input
  1439. if ( isset( $_POST['variation_price'] ) || isset( $_POST["apply_to_current"] ) ) {
  1440. $term_prices = get_option( 'term_prices' );
  1441. $term_prices[$term_id]["price"] = $_POST["variation_price"];
  1442. $term_prices[$term_id]["checked"] = (isset( $_POST["apply_to_current"] )) ? "checked" : "unchecked";
  1443. update_option( 'term_prices', $term_prices );
  1444. }
  1445. // Second - If box was checked, let's then check whether or not it was flat, differential, or percentile, then let's apply the pricing to every product appropriately
  1446. if ( isset( $_POST["apply_to_current"] ) ) {
  1447. //Check for flat, percentile or differential
  1448. $var_price_type = '';
  1449. if ( flat_price( $_POST["variation_price"] ) )
  1450. $var_price_type = 'flat';
  1451. elseif ( differential_price( $_POST["variation_price"] ) )
  1452. $var_price_type = 'differential';
  1453. elseif ( percentile_price( $_POST["variation_price"] ) )
  1454. $var_price_type = 'percentile';
  1455. //Now, find all products with this term_id, update their pricing structure (terms returned include only parents at this point, we'll grab relevent children soon)
  1456. $products_to_mod = get_objects_in_term( $term_id, "wpsc-variation" );
  1457. $product_parents = array( );
  1458. foreach ( (array)$products_to_mod as $get_parent ) {
  1459. $post = get_post( $get_parent );
  1460. if ( !$post->post_parent )
  1461. $product_parents[] = $post->ID;
  1462. }
  1463. //Now that we have all parent IDs with this term, we can get the children (only the ones that are also in $products_to_mod, we don't want to apply pricing to ALL kids)
  1464. foreach ( $product_parents as $parent ) {
  1465. $args = array(
  1466. 'post_parent' => $parent,
  1467. 'post_type' => 'wpsc-product'
  1468. );
  1469. $children = get_children( $args, ARRAY_A );
  1470. foreach ( $children as $childrens ) {
  1471. $parent = $childrens["post_parent"];
  1472. $children_ids[$parent][] = $childrens["ID"];
  1473. $children_ids[$parent] = array_intersect( $children_ids[$parent], $products_to_mod );
  1474. }
  1475. }
  1476. //Got the right kids, let's grab their parent pricing and modify their pricing based on var_price_type
  1477. foreach ( (array)$children_ids as $parents => $kids ) {
  1478. $kids = array_values( $kids );
  1479. $parent_pricing = get_product_meta( $parents, "price", true );
  1480. foreach ( $kids as $kiddos ) {
  1481. $child_pricing = get_product_meta( $kiddos, "price", true );
  1482. if ( $var_price_type == 'flat' ) {
  1483. update_product_meta( $kiddos, "price", floatval( $_POST["variation_price"] ) );
  1484. } elseif ( $var_price_type == 'percentile' ) {
  1485. //Are we decreasing or increasing the price?
  1486. if ( strchr( $_POST["variation_price"], '-' ) )
  1487. $positive = false;
  1488. else
  1489. $positive = true;
  1490. //Now, let's get the parent product price, +/- by the percentage given
  1491. $percentage = (absint( $_POST["variation_price"] ) / 100);
  1492. if ( $positive )
  1493. $price = $parent_pricing + ($parent_pricing * $percentage);
  1494. else
  1495. $price = $parent_pricing - ($parent_pricing * $percentage);
  1496. update_product_meta( $kiddos, "price", $price );
  1497. } elseif ( $var_price_type == 'differential' ) {
  1498. //Are we decreasing or increasing the price?
  1499. if ( strchr( $_POST["variation_price"], '-' ) )
  1500. $positive = false;
  1501. else
  1502. $positive = true;
  1503. //Now, let's get the parent product price, +/- by the differential given
  1504. $differential = (absint( $_POST["variation_price"] ));
  1505. if ( $positive )
  1506. $price = $parent_pricing + $differential;
  1507. else
  1508. $price = $parent_pricing - $differential;
  1509. update_product_meta( $kiddos, "price", $price );
  1510. }
  1511. }
  1512. }
  1513. }
  1514. }
  1515. add_action( 'edited_wpsc-variation', 'save_term_prices' );
  1516. add_action( 'created_wpsc-variation', 'save_term_prices' );
  1517. ?>