'.$message.''; exit; } else { trigger_error($message, $errno); } } /** * Send JSON response. * * @param $data */ function a4bJsonResponse($data) { @header('Content-type: application/json; charset=utf-8'); // echo wp_json_encode($data, JSON_NUMERIC_CHECK); echo json_encode($data, JSON_NUMERIC_CHECK); wp_die(); } /** * Get products by categories ids. * * @param $categoriesIds * @param null $postsIds * @param null $notPostsIds * @param string $postType * @param string $postTaxonomy * * @return array */ function a4bGetPostsByCategories($categoriesIds = array(), $args = array()) { $defaultArgs = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'terms' => $categoriesIds, 'field' => 'term_id', 'operator' => 'IN', ), ), ); $args = array_merge($defaultArgs, $args); $query = new \WP_Query($args); return $query->posts; } /** * Get products by ids. * * @param string $postType * @param null $postsIds * @param null $notPostsIds * @param null $parentsIds * * @return array */ function a4bGetPosts($args) { $defaultArgs = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, ); $args = array_merge($defaultArgs, $args); $query = new \WP_Query($args); return $query->posts; } /** * Exclude posts from array with given IDs. * * @param $posts * @param $excludeIds * * @return array */ function a4bExcludePostsByIds($posts, $excludeIds) { return array_filter( $posts, function ($post) use ($excludeIds) { return !in_array($post->ID, $excludeIds); } ); } /** * Gather values of given field from each object into array. * * @param $objects * @param $field * * @return array */ function a4bObjectsFieldToArray($objects, $field) { $fieldValues = []; // Get value of given field from each object. foreach ($objects as $object) { $fieldValues[] = $object->$field; } return $fieldValues; } /** * Add messages to cookies. * * @param $message * @param $type */ function a4bFlashMessage($message, $type) { $flash = isset($_COOKIE['a4b_flash_messages']) ? json_decode(base64_decode($_COOKIE['a4b_flash_messages']), true) : array(); $flash[] = compact('message', 'type'); $encodedFlashData = base64_encode(json_encode($flash)); $_COOKIE['a4b_flash_messages'] = $encodedFlashData; setcookie('a4b_flash_messages', $encodedFlashData, time() + 10, '/'); } /** * Show errors in a4b_flash_messages cookie and clear it from cookie after. */ function a4bShowNotices() { // Read messages from session $flashMessages = isset($_COOKIE['a4b_flash_messages']) ? json_decode(base64_decode($_COOKIE['a4b_flash_messages']), true) : array(); // Output notices foreach ($flashMessages as $flash) { printf('
%s