PageRenderTime 89ms CodeModel.GetById 26ms RepoModel.GetById 9ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/wpsc-includes/misc.functions.php

https://github.com/AaronFernandes/aquestionof
PHP | 762 lines | 515 code | 96 blank | 151 comment | 142 complexity | 8e42ce2b634ac85659c29fc214e9bf2f MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  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 find purchlog status name looksthrough the wpsc_purchlog_statuses variable to find the name of the given status
  12. *
  13. * @since 3.8
  14. * $param int $id the id for the region
  15. * @param string $return_value either 'name' or 'code' depending on what you want returned
  16. */
  17. function wpsc_find_purchlog_status_name( $purchlog_status ) {
  18. global $wpsc_purchlog_statuses;
  19. foreach ( $wpsc_purchlog_statuses as $status ) {
  20. if ( $status['order'] == $purchlog_status ) {
  21. $status_name = $status['label'];
  22. }
  23. }
  24. return $status_name;
  25. }
  26. /**
  27. * WPSC get state by id function, gets either state code or state name depending on param
  28. *
  29. * @since 3.7
  30. * $param int $id the id for the region
  31. * @param string $return_value either 'name' or 'code' depending on what you want returned
  32. */
  33. function wpsc_get_state_by_id( $id, $return_value ) {
  34. global $wpdb;
  35. $sql = "SELECT `" . $return_value . "` FROM `" . WPSC_TABLE_REGION_TAX . "` WHERE `id`=" . $id;
  36. $value = $wpdb->get_var( $sql );
  37. return $value;
  38. }
  39. function wpsc_country_has_state($country_code){
  40. global $wpdb;
  41. $country_data = $wpdb->get_row("SELECT * FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `isocode`= '".$country_code."' LIMIT 1",ARRAY_A);
  42. return $country_data;
  43. }
  44. /**
  45. * WPSC add new user function, validates and adds a new user, for the
  46. *
  47. * @since 3.7
  48. *
  49. * @param string $user_login The user's username.
  50. * @param string $password The user's password.
  51. * @param string $user_email The user's email (optional).
  52. * @return int The new user's ID.
  53. */
  54. function wpsc_add_new_user( $user_login, $user_pass, $user_email ) {
  55. require_once(ABSPATH . WPINC . '/registration.php');
  56. $errors = new WP_Error();
  57. $user_login = sanitize_user( $user_login );
  58. $user_email = apply_filters( 'user_registration_email', $user_email );
  59. // Check the username
  60. if ( $user_login == '' ) {
  61. $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.', 'wpsc' ) );
  62. } elseif ( !validate_username( $user_login ) ) {
  63. $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid. Please enter a valid username.', 'wpsc' ) );
  64. $user_login = '';
  65. } elseif ( username_exists( $user_login ) ) {
  66. $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.', 'wpsc' ) );
  67. }
  68. // Check the e-mail address
  69. if ( $user_email == '' ) {
  70. $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.', 'wpsc' ) );
  71. } elseif ( !is_email( $user_email ) ) {
  72. $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.', 'wpsc' ) );
  73. $user_email = '';
  74. } elseif ( email_exists( $user_email ) ) {
  75. $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.', 'wpsc' ) );
  76. }
  77. if ( $errors->get_error_code() ) {
  78. return $errors;
  79. }
  80. $user_id = wp_create_user( $user_login, $user_pass, $user_email );
  81. if ( !$user_id ) {
  82. $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !', 'wpsc' ), get_option( 'admin_email' ) ) );
  83. return $errors;
  84. }
  85. $credentials = array( 'user_login' => $user_login, 'user_password' => $user_pass, 'remember' => true );
  86. $user = wp_signon( $credentials );
  87. return $user;
  88. //wp_new_user_notification($user_id, $user_pass);
  89. }
  90. /**
  91. * WPSC product has variations function
  92. * @since 3.7
  93. * @param int product id
  94. * @return bool true or false
  95. */
  96. function wpsc_product_has_variations( $product_id ) {
  97. global $wpdb;
  98. if ( $product_id > 0 ) {
  99. $variation_count = $wpdb->get_var( "SELECT COUNT(`id`) FROM `" . WPSC_TABLE_VARIATION_ASSOC . "` WHERE `type` IN('product') AND `associated_id` IN('{$product_id}')" );
  100. if ( $variation_count > 0 ) {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. function wpsc_post_title_seo( $title ) {
  107. global $wpdb, $page_id, $wp_query;
  108. $new_title = wpsc_obtain_the_title();
  109. if ( $new_title != '' ) {
  110. $title = $new_title;
  111. }
  112. return stripslashes( $title );
  113. }
  114. //add_filter( 'single_post_title', 'wpsc_post_title_seo' );
  115. /**
  116. * WPSC canonical URL function
  117. * Needs a recent version
  118. * @since 3.7
  119. * @param int product id
  120. * @return bool true or false
  121. */
  122. function wpsc_change_canonical_url( $url = '' ) {
  123. global $wpdb, $wp_query, $wpsc_page_titles;
  124. if ( $wp_query->is_single == true && 'wpsc-product' == $wp_query->query_vars['post_type']) {
  125. $categories = wp_get_object_terms( $wp_query->post->ID , 'wpsc_product_category' );
  126. if(count($categories) > 1){
  127. foreach($categories as $category){
  128. if(isset($wp_query->query_vars['wpsc_product_category']) && $category->slug == $wp_query->query_vars['wpsc_product_category'])continue;
  129. if(isset($wp_query->query_vars['wpsc_product_category']) && $category->slug != $wp_query->query_vars['wpsc_product_category']){
  130. return home_url($wpsc_page_titles['products'].'/'.$category->slug.'/'.$wp_query->query_vars['wpsc-product']);
  131. }
  132. }
  133. }
  134. }
  135. return $url;
  136. }
  137. add_filter( 'aioseop_canonical_url', 'wpsc_change_canonical_url' );
  138. function wpsc_insert_canonical_url() {
  139. $wpsc_url = wpsc_change_canonical_url( null );
  140. echo "<link rel='canonical' href='$wpsc_url' />\n";
  141. }
  142. function wpsc_canonical_url() {
  143. $wpsc_url = wpsc_change_canonical_url( null );
  144. if ( $wpsc_url != null ) {
  145. remove_action( 'wp_head', 'rel_canonical' );
  146. add_action( 'wp_head', 'wpsc_insert_canonical_url' );
  147. }
  148. }
  149. add_action( 'template_redirect', 'wpsc_canonical_url' );
  150. // check for all in one SEO pack and the is_static_front_page function
  151. if ( is_callable( array( "All_in_One_SEO_Pack", 'is_static_front_page' ) ) ) {
  152. function wpsc_change_aioseop_home_title( $title ) {
  153. global $aiosp, $aioseop_options;
  154. if ( (get_class( $aiosp ) == 'All_in_One_SEO_Pack') && $aiosp->is_static_front_page() ) {
  155. $aiosp_home_title = $aiosp->internationalize( $aioseop_options['aiosp_home_title'] );
  156. $new_title = wpsc_obtain_the_title();
  157. if ( $new_title != '' ) {
  158. $title = str_replace( $aiosp_home_title, $new_title, $title );
  159. }
  160. }
  161. return $title;
  162. }
  163. add_filter( 'aioseop_home_page_title', 'wpsc_change_aioseop_home_title' );
  164. }
  165. function wpsc_set_aioseop_description( $data ) {
  166. $replacement_data = wpsc_obtain_the_description();
  167. if ( $replacement_data != '' ) {
  168. $data = $replacement_data;
  169. }
  170. return $data;
  171. }
  172. add_filter( 'aioseop_description', 'wpsc_set_aioseop_description' );
  173. function wpsc_set_aioseop_keywords( $data ) {
  174. global $wpdb, $wp_query, $wpsc_title_data, $aioseop_options;
  175. if ( isset( $wp_query->query_vars['product_url_name'] ) ) {
  176. $product_name = $wp_query->query_vars['product_url_name'];
  177. $product_id = $wpdb->get_var( "SELECT `product_id` FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `meta_key` IN ( 'url_name' ) AND `meta_value` IN ( '{$wp_query->query_vars['product_url_name']}' ) ORDER BY `id` DESC LIMIT 1" );
  178. $replacement_data = '';
  179. $replacement_data_array = array( );
  180. if ( $aioseop_options['aiosp_use_categories'] ) {
  181. $category_list = $wpdb->get_col( "SELECT `categories`.`name` FROM `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` AS `assoc` , `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` AS `categories` WHERE `assoc`.`product_id` IN ('{$product_id}') AND `assoc`.`category_id` = `categories`.`id` AND `categories`.`active` IN('1')" );
  182. $replacement_data_array += $category_list;
  183. }
  184. $replacement_data_array += wp_get_object_terms( $product_id, 'product_tag', array( 'fields' => 'names' ) );
  185. $replacement_data .= implode( ",", $replacement_data_array );
  186. if ( $replacement_data != '' ) {
  187. $data = strtolower( $replacement_data );
  188. }
  189. }
  190. return $data;
  191. }
  192. add_filter( 'aioseop_keywords', 'wpsc_set_aioseop_keywords' );
  193. /**
  194. * wpsc_populate_also_bought_list function, runs on checking out, populates the also bought list.
  195. */
  196. function wpsc_populate_also_bought_list() {
  197. global $wpdb, $wpsc_cart, $wpsc_coupons;
  198. $new_also_bought_data = array( );
  199. foreach ( $wpsc_cart->cart_items as $outer_cart_item ) {
  200. $new_also_bought_data[$outer_cart_item->product_id] = array( );
  201. foreach ( $wpsc_cart->cart_items as $inner_cart_item ) {
  202. if ( $outer_cart_item->product_id != $inner_cart_item->product_id ) {
  203. $new_also_bought_data[$outer_cart_item->product_id][$inner_cart_item->product_id] = $inner_cart_item->quantity;
  204. } else {
  205. continue;
  206. }
  207. }
  208. }
  209. $insert_statement_parts = array( );
  210. foreach ( $new_also_bought_data as $new_also_bought_id => $new_also_bought_row ) {
  211. $new_other_ids = array_keys( $new_also_bought_row );
  212. $also_bought_data = $wpdb->get_results( "SELECT `id`, `associated_product`, `quantity` FROM `" . WPSC_TABLE_ALSO_BOUGHT . "` WHERE `selected_product` IN('$new_also_bought_id') AND `associated_product` IN('" . implode( "','", $new_other_ids ) . "')", ARRAY_A );
  213. $altered_new_also_bought_row = $new_also_bought_row;
  214. foreach ( (array)$also_bought_data as $also_bought_row ) {
  215. $quantity = $new_also_bought_row[$also_bought_row['associated_product']] + $also_bought_row['quantity'];
  216. unset( $altered_new_also_bought_row[$also_bought_row['associated_product']] );
  217. $wpdb->query( "UPDATE `" . WPSC_TABLE_ALSO_BOUGHT . "` SET `quantity` = {$quantity} WHERE `id` = '{$also_bought_row['id']}' LIMIT 1;" );
  218. }
  219. if ( count( $altered_new_also_bought_row ) > 0 ) {
  220. foreach ( $altered_new_also_bought_row as $associated_product => $quantity ) {
  221. $insert_statement_parts[] = "(" . absint( $new_also_bought_id ) . "," . absint( $associated_product ) . "," . absint( $quantity ) . ")";
  222. }
  223. }
  224. }
  225. if ( count( $insert_statement_parts ) > 0 ) {
  226. $insert_statement = "INSERT INTO `" . WPSC_TABLE_ALSO_BOUGHT . "` (`selected_product`, `associated_product`, `quantity`) VALUES " . implode( ",\n ", $insert_statement_parts );
  227. $wpdb->query( $insert_statement );
  228. }
  229. }
  230. function wpsc_get_country_form_id_by_type($type){
  231. global $wpdb;
  232. $sql = 'SELECT `id` FROM `'.WPSC_TABLE_CHECKOUT_FORMS.'` WHERE `type`="'.$type.'" LIMIT 1';
  233. $id = $wpdb->get_var($sql);
  234. return $id;
  235. }
  236. function wpsc_get_country( $country_code ) {
  237. global $wpdb;
  238. $country = $wpdb->get_var( "SELECT `country` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `isocode` IN ('" . $country_code . "') LIMIT 1" );
  239. return $country;
  240. }
  241. function wpsc_get_region( $region_code ) {
  242. global $wpdb;
  243. $region = $wpdb->get_var( "SELECT `name` FROM `" . WPSC_TABLE_REGION_TAX . "` WHERE `id` IN('$region_code')" );
  244. return $region;
  245. }
  246. function nzshpcrt_display_preview_image() {
  247. global $wpdb;
  248. if ( (isset( $_GET['wpsc_request_image'] ) && ($_GET['wpsc_request_image'] == 'true'))
  249. || (isset( $_GET['productid'] ) && is_numeric( $_GET['productid'] ))
  250. || (isset( $_GET['image_id'] ) && is_numeric( $_GET['image_id'] ))
  251. || (isset( $_GET['image_name'] ))
  252. ) {
  253. if ( function_exists( "getimagesize" ) ) {
  254. if ( $_GET['image_name'] ) {
  255. $image = basename( $_GET['image_name'] );
  256. $imagepath = WPSC_USER_UPLOADS_DIR . $image;
  257. } else if ( $_GET['category_id'] ) {
  258. $category_id = absint( $_GET['category_id'] );
  259. $image = $wpdb->get_var( "SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `id` = '{$category_id}' LIMIT 1" );
  260. if ( $image != '' ) {
  261. $imagepath = WPSC_CATEGORY_DIR . $image;
  262. }
  263. }
  264. if ( !is_file( $imagepath ) ) {
  265. $imagepath = WPSC_FILE_PATH . "/images/no-image-uploaded.gif";
  266. }
  267. $image_size = @getimagesize( $imagepath );
  268. if ( is_numeric( $_GET['height'] ) && is_numeric( $_GET['width'] ) ) {
  269. $height = (int)$_GET['height'];
  270. $width = (int)$_GET['width'];
  271. } else {
  272. $width = $image_size[0];
  273. $height = $image_size[1];
  274. }
  275. if ( !(($height > 0) && ($height <= 1024) && ($width > 0) && ($width <= 1024)) ) {
  276. $width = $image_size[0];
  277. $height = $image_size[1];
  278. }
  279. if ( $product_id > 0 ) {
  280. $cache_filename = basename( "product_{$product_id}_{$height}x{$width}" );
  281. } else if ( $category_id > 0 ) {
  282. $cache_filename = basename( "category_{$category_id}_{$height}x{$width}" );
  283. } else {
  284. $cache_filename = basename( "product_img_{$image_id}_{$height}x{$width}" );
  285. }
  286. $imagetype = @getimagesize( $imagepath );
  287. $use_cache = false;
  288. switch ( $imagetype[2] ) {
  289. case IMAGETYPE_JPEG:
  290. $extension = ".jpg";
  291. break;
  292. case IMAGETYPE_GIF:
  293. $extension = ".gif";
  294. break;
  295. case IMAGETYPE_PNG:
  296. $extension = ".png";
  297. break;
  298. }
  299. if ( file_exists( WPSC_CACHE_DIR . $cache_filename . $extension ) ) {
  300. $original_modification_time = filemtime( $imagepath );
  301. $cache_modification_time = filemtime( WPSC_CACHE_DIR . $cache_filename . $extension );
  302. if ( $original_modification_time < $cache_modification_time ) {
  303. $use_cache = true;
  304. }
  305. }
  306. if ( $use_cache === true ) {
  307. $cache_url = WPSC_CACHE_URL;
  308. if ( is_ssl ( ) ) {
  309. $cache_url = str_replace( "http://", "https://", $cache_url );
  310. }
  311. header( "Location: " . $cache_url . $cache_filename . $extension );
  312. exit( '' );
  313. } else {
  314. switch ( $imagetype[2] ) {
  315. case IMAGETYPE_JPEG:
  316. $src_img = imagecreatefromjpeg( $imagepath );
  317. $pass_imgtype = true;
  318. break;
  319. case IMAGETYPE_GIF:
  320. $src_img = imagecreatefromgif( $imagepath );
  321. $pass_imgtype = true;
  322. break;
  323. case IMAGETYPE_PNG:
  324. $src_img = imagecreatefrompng( $imagepath );
  325. $pass_imgtype = true;
  326. break;
  327. default:
  328. $pass_imgtype = false;
  329. break;
  330. }
  331. if ( $pass_imgtype === true ) {
  332. $source_w = imagesx( $src_img );
  333. $source_h = imagesy( $src_img );
  334. //Temp dimensions to crop image properly
  335. $temp_w = $width;
  336. $temp_h = $height;
  337. // select our scaling method
  338. $scaling_method = 'cropping';
  339. // set both offsets to zero
  340. $offset_x = $offset_y = 0;
  341. // Here are the scaling methods, non-cropping causes black lines in tall images, but doesnt crop images.
  342. switch ( $scaling_method ) {
  343. case 'cropping':
  344. // if the image is wider than it is high and at least as wide as the target width.
  345. if ( ($source_h <= $source_w ) ) {
  346. if ( $height < $width ) {
  347. $temp_h = ($width / $source_w) * $source_h;
  348. } else {
  349. $temp_w = ($height / $source_h) * $source_w;
  350. }
  351. } else {
  352. $temp_h = ($width / $source_w) * $source_h;
  353. }
  354. break;
  355. case 'non-cropping':
  356. default:
  357. if ( $height < $width ) {
  358. $temp_h = ($width / $source_w) * $source_h;
  359. } else {
  360. $temp_w = ($height / $source_h) * $source_w;
  361. }
  362. break;
  363. }
  364. // Create temp resized image
  365. $temp_img = ImageCreateTrueColor( $temp_w, $temp_h );
  366. $bgcolor = ImageColorAllocate( $temp_img, 255, 255, 255 );
  367. ImageFilledRectangle( $temp_img, 0, 0, $temp_w, $temp_h, $bgcolor );
  368. ImageAlphaBlending( $temp_img, TRUE );
  369. ImageCopyResampled( $temp_img, $src_img, 0, 0, 0, 0, $temp_w, $temp_h, $source_w, $source_h );
  370. $dst_img = ImageCreateTrueColor( $width, $height );
  371. $bgcolor = ImageColorAllocate( $dst_img, 255, 255, 255 );
  372. ImageFilledRectangle( $dst_img, 0, 0, $width, $height, $bgcolor );
  373. ImageAlphaBlending( $dst_img, TRUE );
  374. // X & Y Offset to crop image properly
  375. if ( $temp_w < $width ) {
  376. $w1 = ($width / 2) - ($temp_w / 2);
  377. } else if ( $temp_w == $width ) {
  378. $w1 = 0;
  379. } else {
  380. $w1 = ($width / 2) - ($temp_w / 2);
  381. }
  382. if ( $temp_h < $height ) {
  383. $h1 = ($height / 2) - ($temp_h / 2);
  384. } else if ( $temp_h == $height ) {
  385. $h1 = 0;
  386. } else {
  387. $h1 = ($height / 2) - ($temp_h / 2);
  388. }
  389. switch ( $scaling_method ) {
  390. case 'cropping':
  391. ImageCopy( $dst_img, $temp_img, $w1, $h1, 0, 0, $temp_w, $temp_h );
  392. break;
  393. case 'non-cropping':
  394. default:
  395. ImageCopy( $dst_img, $temp_img, 0, 0, 0, 0, $temp_w, $temp_h );
  396. break;
  397. }
  398. $image_quality = wpsc_image_quality();
  399. ImageAlphaBlending( $dst_img, false );
  400. switch ( $imagetype[2] ) {
  401. case IMAGETYPE_JPEG:
  402. header( "Content-type: image/jpeg" );
  403. imagejpeg( $dst_img );
  404. imagejpeg( $dst_img, WPSC_CACHE_DIR . $cache_filename . '.jpg', $image_quality );
  405. @ chmod( WPSC_CACHE_DIR . $cache_filename . ".jpg", 0775 );
  406. break;
  407. case IMAGETYPE_GIF:
  408. header( "Content-type: image/gif" );
  409. ImagePNG( $dst_img );
  410. ImagePNG( $dst_img, WPSC_CACHE_DIR . $cache_filename . ".gif" );
  411. @ chmod( WPSC_CACHE_DIR . $cache_filename . ".gif", 0775 );
  412. break;
  413. case IMAGETYPE_PNG:
  414. header( "Content-type: image/png" );
  415. ImagePNG( $dst_img );
  416. ImagePNG( $dst_img, WPSC_CACHE_DIR . $cache_filename . ".png" );
  417. @ chmod( WPSC_CACHE_DIR . $cache_filename . ".png", 0775 );
  418. break;
  419. default:
  420. $pass_imgtype = false;
  421. break;
  422. }
  423. exit();
  424. }
  425. }
  426. }
  427. }
  428. }
  429. add_action( 'init', 'nzshpcrt_display_preview_image' );
  430. function wpsc_list_dir( $dirname ) {
  431. /*
  432. lists the provided directory, was nzshpcrt_listdir
  433. */
  434. $dir = @opendir( $dirname );
  435. $num = 0;
  436. while ( ($file = @readdir( $dir )) !== false ) {
  437. //filter out the dots and any backup files, dont be tempted to correct the "spelling mistake", its to filter out a previous spelling mistake.
  438. if ( ($file != "..") && ($file != ".") && !stristr( $file, "~" ) && !stristr( $file, "Chekcout" ) && !stristr( $file, "error_log" ) && !( strpos( $file, "." ) === 0 ) ) {
  439. $dirlist[$num] = $file;
  440. $num++;
  441. }
  442. }
  443. if ( $dirlist == null ) {
  444. $dirlist[0] = "paypal.php";
  445. $dirlist[1] = "testmode.php";
  446. }
  447. return $dirlist;
  448. }
  449. /**
  450. * wpsc_recursive_copy function, copied from here and renamed: http://nz.php.net/copy
  451. * Why doesn't PHP have one of these built in?
  452. */
  453. function wpsc_recursive_copy( $src, $dst ) {
  454. $dir = opendir( $src );
  455. @mkdir( $dst );
  456. while ( false !== ( $file = readdir( $dir )) ) {
  457. if ( ( $file != '.' ) && ( $file != '..' ) ) {
  458. if ( is_dir( $src . '/' . $file ) ) {
  459. wpsc_recursive_copy( $src . '/' . $file, $dst . '/' . $file );
  460. } else {
  461. @ copy( $src . '/' . $file, $dst . '/' . $file );
  462. }
  463. }
  464. }
  465. closedir( $dir );
  466. }
  467. /**
  468. * wpsc_replace_reply_address function,
  469. * Replace the email address for the purchase receipts
  470. */
  471. function wpsc_replace_reply_address( $input ) {
  472. $output = get_option( 'return_email' );
  473. if ( $output == '' ) {
  474. $output = $input;
  475. }
  476. return $output;
  477. }
  478. /**
  479. * wpsc_replace_reply_address function,
  480. * Replace the email address for the purchase receipts
  481. */
  482. function wpsc_replace_reply_name( $input ) {
  483. $output = get_option( 'return_name' );
  484. if ( $output == '' ) {
  485. $output = $input;
  486. }
  487. return $output;
  488. }
  489. /**
  490. * wpsc_object_to_array, recusively converts an object to an array, for usage with SOAP code
  491. * Copied from here, then modified:
  492. * http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html
  493. */
  494. function wpsc_object_to_array( $object ) {
  495. if ( !is_object( $object ) && !is_array( $object ) ) {
  496. return $object;
  497. } else if ( is_object( $object ) ) {
  498. $object = get_object_vars( $object );
  499. }
  500. return array_map( 'wpsc_object_to_array', $object );
  501. }
  502. function wpsc_readfile_chunked( $filename, $retbytes = true ) {
  503. $chunksize = 1 * (1024 * 1024); // how many bytes per chunk
  504. $buffer = '';
  505. $cnt = 0;
  506. $handle = fopen( $filename, 'rb' );
  507. if ( $handle === false ) {
  508. return false;
  509. }
  510. while ( !feof( $handle ) ) {
  511. $buffer = fread( $handle, $chunksize );
  512. echo $buffer;
  513. ob_flush();
  514. flush();
  515. if ( $retbytes ) {
  516. $cnt += strlen( $buffer );
  517. }
  518. }
  519. $status = fclose( $handle );
  520. if ( $retbytes && $status ) {
  521. return $cnt; // return num. bytes delivered like readfile() does.
  522. }
  523. return $status;
  524. }
  525. /**
  526. * wpsc_clear_stock_claims, clears the stock claims, runs using wp-cron
  527. */
  528. function wpsc_clear_stock_claims() {
  529. global $wpdb;
  530. //time
  531. //$old_claimed_stock_timestamp = mktime( date( 'H' ), date( 'i' ), date( 's' ), date( 'm' ), date( 'd' ) - 7, date( 'Y' ) );
  532. $old_claimed_stock_timestamp = time() - (60 * 60); // KONSCRIPT
  533. $old_claimed_stock_datetime = date( "Y-m-d H:i:s", $old_claimed_stock_timestamp );
  534. /// Delete the old claims on stock (only those that weren't sold)
  535. $wpdb->query( "DELETE FROM `" . WPSC_TABLE_CLAIMED_STOCK . "` WHERE `last_activity` < '{$old_claimed_stock_datetime}' AND `cart_submitted` = '0'" );
  536. }
  537. add_action( 'wpsc_daily_cron_tasks', 'wpsc_clear_stock_claims' );
  538. /**
  539. * Retrieve the list of tags for a product.
  540. *
  541. * Compatibility layer for themes and plugins. Also an easy layer of abstraction
  542. * away from the complexity of the taxonomy layer. Copied from the Wordpress posts code
  543. *
  544. * @since 3.8.0
  545. *
  546. * @uses wp_get_object_terms() Retrieves the tags. Args details can be found here.
  547. *
  548. * @param int $product_id Optional. The Post ID.
  549. * @param array $args Optional. Overwrite the defaults.
  550. * @return array
  551. */
  552. function wp_get_product_tags( $product_id = 0, $args = array( ) ) {
  553. $product_id = (int)$product_id;
  554. $defaults = array( 'fields' => 'ids' );
  555. $args = wp_parse_args( $args, $defaults );
  556. $cats = wp_get_object_terms( $product_id, 'product_tag' );
  557. return $cats;
  558. }
  559. /**
  560. * Retrieve the list of categories for a product.
  561. *
  562. * Compatibility layer for themes and plugins. Also an easy layer of abstraction
  563. * away from the complexity of the taxonomy layer. Copied from the Wordpress posts code
  564. *
  565. * @since 3.8.0
  566. *
  567. * @uses wp_get_object_terms() Retrieves the categories. Args details can be found here.
  568. *
  569. * @param int $post_id Optional. The Post ID.
  570. * @param array $args Optional. Overwrite the defaults.
  571. * @return array
  572. */
  573. function wp_get_product_categories( $product_id = 0, $args = array( ) ) {
  574. $product_id = (int)$product_id;
  575. $defaults = array( 'fields' => 'ids' );
  576. $args = wp_parse_args( $args, $defaults );
  577. $cats = wp_get_object_terms( $product_id, 'wpsc_product_category' );
  578. return $cats;
  579. }
  580. /**
  581. * Set categories for a product.
  582. *
  583. * If the post categories parameter is not set, then the default category is
  584. * going used. Copied from the Wordpress posts code
  585. *
  586. * @since 3.8.0
  587. *
  588. * @param int $post_ID Post ID.
  589. * @param array $post_categories Optional. List of categories.
  590. * @return bool|mixed
  591. */
  592. function wp_set_product_categories( $product_id, $post_categories = array( ) ) {
  593. $product_id = (int)$product_id;
  594. // If $post_categories isn't already an array, make it one:
  595. if ( !is_array( $post_categories ) || 0 == count( $post_categories ) || empty( $post_categories ) ) {
  596. return;
  597. } else if ( 1 == count( $post_categories ) && '' == $post_categories[0] ) {
  598. return true;
  599. }
  600. $post_categories = array_map( 'intval', $post_categories );
  601. $post_categories = array_unique( $post_categories );
  602. return wp_set_object_terms( $product_id, $post_categories, 'wpsc_product_category' );
  603. }
  604. //*/
  605. /**
  606. * Retrieve product categories. Copied from the corresponding wordpress function
  607. *
  608. * @since 3.8.0
  609. *
  610. * @param int $id Mandatory, the product ID
  611. * @return array
  612. */
  613. function get_the_product_category( $id ) {
  614. $id = (int)$id;
  615. $categories = get_object_term_cache( $id, 'wpsc_product_category' );
  616. if ( false === $categories ) {
  617. $categories = wp_get_object_terms( $id, 'wpsc_product_category' );
  618. wp_cache_add( $id, $categories, 'product_category_relationships' );
  619. }
  620. if ( !empty( $categories ) )
  621. usort( $categories, '_usort_terms_by_name' );
  622. else
  623. $categories = array( );
  624. foreach ( (array)array_keys( $categories ) as $key ) {
  625. _make_cat_compat( $categories[$key] );
  626. }
  627. return $categories;
  628. }
  629. /**
  630. * Check the memory_limit and calculate a recommended memory size
  631. * inspired by nextGenGallery Code
  632. *
  633. * @return string message about recommended image size
  634. */
  635. function wpsc_check_memory_limit() {
  636. if ( (function_exists( 'memory_get_usage' )) && (ini_get( 'memory_limit' )) ) {
  637. // get memory limit
  638. $memory_limit = ini_get( 'memory_limit' );
  639. if ( $memory_limit != '' )
  640. $memory_limit = substr( $memory_limit, 0, -1 ) * 1024 * 1024;
  641. // calculate the free memory
  642. $freeMemory = $memory_limit - memory_get_usage();
  643. // build the test sizes
  644. $sizes = array( );
  645. $sizes[] = array( 'width' => 800, 'height' => 600 );
  646. $sizes[] = array( 'width' => 1024, 'height' => 768 );
  647. $sizes[] = array( 'width' => 1280, 'height' => 960 ); // 1MP
  648. $sizes[] = array( 'width' => 1600, 'height' => 1200 ); // 2MP
  649. $sizes[] = array( 'width' => 2016, 'height' => 1512 ); // 3MP
  650. $sizes[] = array( 'width' => 2272, 'height' => 1704 ); // 4MP
  651. $sizes[] = array( 'width' => 2560, 'height' => 1920 ); // 5MP
  652. // test the classic sizes
  653. foreach ( $sizes as $size ) {
  654. // very, very rough estimation
  655. if ( $freeMemory < round( $size['width'] * $size['height'] * 5.09 ) ) {
  656. $result = sprintf( __( 'Please refrain from uploading images larger than <strong>%d x %d</strong> pixels', 'wpsc' ), $size['width'], $size['height'] );
  657. return $result;
  658. }
  659. }
  660. }
  661. return;
  662. }
  663. ?>