/tags/3.7/wpsc-includes/misc.functions.php

https://github.com/evadne/wp-e-commerce · PHP · 217 lines · 147 code · 35 blank · 35 comment · 48 complexity · b8a86ae333e879f1d8568ecb61de868e MD5 · raw file

  1. <?php
  2. /**
  3. * WP eCommerce misc functions
  4. *
  5. * These are the WPSC miscellaneous functions
  6. *
  7. * @package wp-e-commerce
  8. * @since 3.7
  9. */
  10. /**
  11. * WPSC add new user function, validates and adds a new user, for the
  12. *
  13. * @since 3.7
  14. *
  15. * @param string $user_login The user's username.
  16. * @param string $password The user's password.
  17. * @param string $user_email The user's email (optional).
  18. * @return int The new user's ID.
  19. */
  20. function wpsc_add_new_user($user_login,$user_pass, $user_email) {
  21. require_once(ABSPATH . WPINC . '/registration.php');
  22. $errors = new WP_Error();
  23. $user_login = sanitize_user( $user_login );
  24. $user_email = apply_filters( 'user_registration_email', $user_email );
  25. // Check the username
  26. if ( $user_login == '' ) {
  27. $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.'));
  28. } elseif ( !validate_username( $user_login ) ) {
  29. $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid. Please enter a valid username.'));
  30. $user_login = '';
  31. } elseif ( username_exists( $user_login ) ) {
  32. $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
  33. }
  34. // Check the e-mail address
  35. if ($user_email == '') {
  36. $errors->add('empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.'));
  37. } elseif ( !is_email( $user_email ) ) {
  38. $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.'));
  39. $user_email = '';
  40. } elseif ( email_exists( $user_email ) ) {
  41. $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
  42. }
  43. if ( $errors->get_error_code() ) {
  44. return $errors;
  45. }
  46. $user_id = wp_create_user( $user_login, $user_pass, $user_email );
  47. if ( !$user_id ) {
  48. $errors->add('registerfail', sprintf(__('<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !'), get_option('admin_email')));
  49. return $errors;
  50. }
  51. $credentials = array('user_login' => $user_login, 'user_password' => $user_pass, 'remember' => true);
  52. $user = wp_signon($credentials);
  53. return $user;
  54. //wp_new_user_notification($user_id, $user_pass);
  55. }
  56. /**
  57. * WPSC product has variations function
  58. * @since 3.7
  59. * @param int product id
  60. * @return bool true or false
  61. */
  62. function wpsc_product_has_variations($product_id) {
  63. global $wpdb;
  64. if($product_id > 0) {
  65. $variation_count = $wpdb->get_var("SELECT COUNT(`id`) FROM `".WPSC_TABLE_VARIATION_ASSOC."` WHERE `type` IN('product') AND `associated_id` IN('{$product_id}')");
  66. if($variation_count > 0) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. /**
  73. * WPSC canonical URL function
  74. * Needs a recent version
  75. * @since 3.7
  76. * @param int product id
  77. * @return bool true or false
  78. */
  79. function wpsc_change_canonical_url($url) {
  80. global $wpdb, $wpsc_query;
  81. if($wpsc_query->is_single == true) {
  82. $product_id = $wpdb->get_var("SELECT `product_id` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN ( 'url_name' ) AND `meta_value` IN ( '".$wpsc_query->query_vars['product_url_name']."' ) ORDER BY `product_id` DESC LIMIT 1");
  83. $url = wpsc_product_url($product_id);
  84. } else {
  85. if($wpsc_query->query_vars['category_id'] > 0) {
  86. $url = wpsc_category_url($wpsc_query->query_vars['category_id']);
  87. if(get_option('permalink_structure') && ($wpsc_query->query_vars['page'] > 1)) {
  88. $url .= $url."page/{$wpsc_query->query_vars['page']}/";
  89. }
  90. }
  91. }
  92. //echo "<pre>".print_r($wpsc_query,true)."</pre>";
  93. return $url;
  94. }
  95. add_filter('aioseop_canonical_url', 'wpsc_change_canonical_url');
  96. function add_product_meta($product_id, $key, $value, $unique = false, $custom = false) {
  97. global $wpdb, $post_meta_cache, $blog_id;
  98. $product_id = (int)$product_id;
  99. if($product_id > 0) {
  100. if(($unique == true) && $wpdb->get_var("SELECT meta_key FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE meta_key = '$key' AND product_id = '$product_id'")) {
  101. return false;
  102. }
  103. if(!is_string($value)) {
  104. $value = maybe_serialize($value);
  105. } else {
  106. $value = $wpdb->escape($value);
  107. }
  108. if(!$wpdb->get_var("SELECT meta_key FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE meta_key = '$key' AND product_id = '$product_id'")) {
  109. $custom = (int)$custom;
  110. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCTMETA."` (product_id,meta_key,meta_value, custom) VALUES ('$product_id','$key','$value', '$custom')");
  111. } else {
  112. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCTMETA."` SET meta_value = '$value' WHERE meta_key = '$key' AND product_id = '$product_id'");
  113. }
  114. return true;
  115. }
  116. return false;
  117. }
  118. function delete_product_meta($product_id, $key, $value = '') {
  119. global $wpdb, $post_meta_cache, $blog_id;
  120. $product_id = (int)$product_id;
  121. if($product_id > 0) {
  122. if ( empty($value) ) {
  123. $meta_id = $wpdb->get_var("SELECT id FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id = '$product_id' AND meta_key = '$key'");
  124. if(is_numeric($meta_id) && ($meta_id > 0)) {
  125. $wpdb->query("DELETE FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id = '$product_id' AND meta_key = '$key'");
  126. }
  127. } else {
  128. $meta_id = $wpdb->get_var("SELECT id FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id = '$product_id' AND meta_key = '$key' AND meta_value = '$value'");
  129. if(is_numeric($meta_id) && ($meta_id > 0)) {
  130. $wpdb->query("DELETE FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE product_id = '$product_id' AND meta_key = '$key' AND meta_value = '$value'");
  131. }
  132. }
  133. }
  134. return true;
  135. }
  136. function get_product_meta($product_id, $key, $single = false) {
  137. global $wpdb, $post_meta_cache, $blog_id;
  138. $product_id = (int)$product_id;
  139. if($product_id > 0) {
  140. $meta_id = $wpdb->get_var("SELECT `id` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN('$key') AND `product_id` = '$product_id' LIMIT 1");
  141. if(is_numeric($meta_id) && ($meta_id > 0)) {
  142. if($single != false) {
  143. $meta_values = maybe_unserialize($wpdb->get_var("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN('$key') AND `product_id` = '$product_id' LIMIT 1"));
  144. } else {
  145. $meta_values = $wpdb->get_col("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN('$key') AND `product_id` = '$product_id'");
  146. $meta_values = array_map('maybe_unserialize', $meta_values);
  147. }
  148. }
  149. } else {
  150. $meta_values = false;
  151. }
  152. if (is_array($meta_values) && (count($meta_values) == 1)) {
  153. return array_pop($meta_values);
  154. } else {
  155. return $meta_values;
  156. }
  157. }
  158. function update_product_meta($product_id, $key, $value, $prev_value = '') {
  159. global $wpdb, $blog_id;
  160. $product_id = (int)$product_id;
  161. if($product_id > 0) {
  162. if(!is_string($value)) {
  163. $value = $wpdb->escape(maybe_serialize($value));
  164. } else {
  165. $value = $wpdb->escape($value);
  166. }
  167. if(!empty($prev_value)) {
  168. $prev_value = $wpdb->escape(maybe_serialize($prev_value));
  169. }
  170. if($wpdb->get_var("SELECT meta_key FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN('$key') AND product_id = '$product_id'")) {
  171. if (empty($prev_value)) {
  172. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCTMETA."` SET `meta_value` = '$value' WHERE `meta_key` IN('$key') AND product_id = '$product_id'");
  173. } else {
  174. $wpdb->query("UPDATE `".WPSC_TABLE_PRODUCTMETA."` SET `meta_value` = '$value' WHERE `meta_key` IN('$key') AND product_id = '$product_id' AND meta_value = '$prev_value'");
  175. }
  176. } else {
  177. $wpdb->query("INSERT INTO `".WPSC_TABLE_PRODUCTMETA."` (product_id,meta_key,meta_value) VALUES ('$product_id','$key','$value')");
  178. }
  179. return true;
  180. }
  181. }
  182. ?>