クラス
Utils
ソース ソース
ファイル: src/Utils.php
class Utils { const UTC_TIME_ZONE = 'UTC'; const DATETIME_FORMAT = 'Y-m-d H:i:s'; /** * Returns UTC `DateTime` string * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return string */ public static function getCurrentUTCDateTimeString() { return self::getCurrentUTCDateTime()->format(self::DATETIME_FORMAT); } /** * Returns UTC `DateTime` * * @author Evan D Shaw <evandanielshaw@gmail.com> * @return DateTime */ public static function getCurrentUTCDateTime() { return (new DateTime('now', new DateTimeZone(self::UTC_TIME_ZONE))); } /** * Returns data for all Welcart items that have a post type of 'published' * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \wpdb $wpdb * @return array */ public static function getAllPublishedWelcartItems() { global $wpdb; $welitems = []; $itemt = usces_get_tablename('usces_item'); $query = $wpdb->prepare( "SELECT posts.ID FROM $wpdb->posts AS posts JOIN $itemt AS itemt ON itemt.post_id = posts.ID WHERE post_status = %s", 'publish' ); $allitems = $wpdb->get_col($query); if (!empty($allitems)) { foreach ($allitems as $alli_post_id) { $welitem = self::getWelItemData($alli_post_id); if (!empty($welitem)) { $welitems[] = $welitem; } } } return $welitems; } /** * Gets and adds extra meta data to a `welitem` array and returns it * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param int $post_id * @return array */ public static function getWelItemData($post_id) { $welitem = wel_get_product($post_id); if (!empty($welitem)) { $mainpictid = ''; if (is_array($welitem['itemPicts']) && !empty($welitem['itemPicts'][0])) { $mainpictid = (int)$welitem['itemPicts'][0]; } foreach ($welitem['_sku'] as $index => $sku) { $pid = $mainpictid; if (!empty($sku['pict_id'])) { $pid = (int)$sku['pict_id']; } $welitem['_sku'][$index]['images'] = [ 'fullSizeLink' => !empty($pid) ? wp_get_attachment_image_src($pid, 'full-size', true) : '', 'mediumSizeLink' => !empty($pid) ? wp_get_attachment_image_src($pid, 'medium', true) : '', 'thumbnailLink' => !empty($pid) ? wp_get_attachment_image_src($pid, 'thumbnail', true) : '', ]; } } return $welitem; } /** * Gets data for an SKU * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \wpdb $wpdb * @param int $skuMetaId * @return array */ public static function getSkuData($skuMetaId) { global $wpdb; $sku_table = usces_get_tablename('usces_skus'); $res = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$sku_table} WHERE meta_id = %d", $skuMetaId ), ARRAY_A ); if (empty($res)) { return []; } $welitem = new \Welcart\ItemData($res['post_id']); if (empty($welitem)) { return []; } $product = $welitem->get_product(); $sku = $welitem->get_sku_by_id($skuMetaId); if (empty($sku)) { return []; } $pictid = ''; if (is_array($product['itemPicts']) && !empty($product['itemPicts'][0])) { $pictid = (int)$product['itemPicts'][0]; } if (!empty($sku['pict_id'])) { $pictid = (int)$sku['pict_id']; } $sku['images'] = [ 'fullSizeLink' => !empty($pictid) ? wp_get_attachment_image_src($pictid, 'full-size', true) : '', 'mediumSizeLink' => !empty($pictid) ? wp_get_attachment_image_src($pictid, 'medium', true) : '', 'thumbnailLink' => !empty($pictid) ? wp_get_attachment_image_src($pictid, 'thumbnail', true) : '', ]; return [ 'item' => $product, 'sku' => $sku, ]; } /** * Checks whether an SKU exists and returns its post ID * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \wpdb $wpdb * @param int $skuMetaId * @return GenericError|int post ID if exists */ public static function getSkuPostId($skuMetaId) { global $wpdb; $sku_table = usces_get_tablename('usces_skus'); $postId = (int)$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$sku_table} WHERE meta_id = %d", $skuMetaId ) ); if ($postId < 1) { return Master::getErrorStore()->getErrorResponse( ErrorStore::SKU_NOT_FOUND, [$skuMetaId], [], [$skuMetaId] ); } return $postId; } /** * Returns `true` if the SKU is a group item in any combo-sets, `false` otherwise * * @author Evan D Shaw <evandanielshaw@gmail.com> * @global \wpdb $wpdb * @param int $skuMetaId * @return bool */ public static function isGroupItem($skuMetaId) { global $wpdb; $csgit = Schema::getComboSetGroupItemsTable(); return (bool)(int)$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$csgit} WHERE welitem_sku_meta_id = %d", (int)$skuMetaId ) ); } /** * Returns full size and thumbnail size SKU image. Defaults to item image if no * SKU image was found. * * @author Evan D Shaw <evandanielshaw@gmail.com> * @param int $skuMetaId * @return string[] */ public static function getSkuImageLinks($skuMetaId) { global $wpdb, $usces; $defaultres = [ 'fullSizeLink' => '', 'thumbnailLink' => '', ]; $data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE meta_id = %d AND meta_key = '_isku_'", (int)$skuMetaId ), ARRAY_A ); if (empty($data)) { return $defaultres; } $skudata = unserialize($data['meta_value']); if (empty($skudata)) { return $defaultres; } $imgid = $usces->get_subpictid($skudata['code']); if (empty($imgid)) { $imgid = $usces->get_mainpictid($usces->getItemCode($data['post_id'])); } $imgid = !empty($imgid) ? $imgid : 0; return [ 'fullSizeLink' => wp_get_attachment_image_src($imgid, 'full-size', true), 'mediumSizeLink' => wp_get_attachment_image_src($imgid, 'medium', true), 'thumbnailLink' => wp_get_attachment_image_src($imgid, 'thumbnail', true), ]; } }
- getAllPublishedWelcartItems — Returns data for all Welcart items that have a post type of 'published'
- getCurrentUTCDateTime — Returns UTC `DateTime`
- getCurrentUTCDateTimeString — Returns UTC `DateTime` string
- getSkuData — Gets data for an SKU
- getSkuImageLinks — Returns full size and thumbnail size SKU image. Defaults to item image if no SKU image was found.
- getSkuPostId — Checks whether an SKU exists and returns its post ID
- getWelItemData — Gets and adds extra meta data to a `welitem` array and returns it
- isGroupItem — Returns `true` if the SKU is a group item in any combo-sets, `false` otherwise