← Back
Editing: utils.php
<?php function advq_print_r( $array, $echo=true ) { if ( $echo ) { echo '<pre>'; print_r($array); echo '</pre>'; } else { $html = '<pre>'; $html = nl2br(json_encode($array, JSON_PRETTY_PRINT)); $html .='</pre>'; return $html; } } function advq_woo_activated() { if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; } } function advq_woo_version_check( $version = '3.0' ) { if ( class_exists( 'WooCommerce' ) ) { global $woocommerce; if ( version_compare( $woocommerce->version, $version, ">=" ) ) { return true; } } return false; } function advq_get_suggestions($id = false, $args = array(), $data=array(),$uid=0,$limit=0) { if (count($args) == 0 || $id == false){ return ''; } //we check if there are multiple queries by cheching if the post_type is set //we need them to be an array in case the count quiz type sends multiple query_args if(isset($args['post_type'])){ $args = array($args); } //now that we have the args as an array of args arrays, //we merge the different query results //this will be the main query $loop = new WP_Query(); foreach ($args as $key => $queryargs) { //set limit if($limit>0){ $queryargs['posts_per_page'] = $limit; } $queryargs = apply_filters( 'advq_query_args', $queryargs ); $temp = new WP_Query($queryargs); // start putting the contents in the new object if(!is_array($loop->posts)){ $loop->posts = array(); } $loop->posts = array_merge( $loop->posts, $temp->posts ); $loop->query_vars = array_merge($loop->query_vars,$temp->query_vars); } // we also need to set post count correctly so as to enable the looping $loop->post_count = count( $loop->posts ); //error_log(json_encode($queryargs)); //now we build the layout if (!$loop->have_posts()){ return ''; } $html = '<div class="pa_suggestions">'; $stitle = isset($data['suggestions_title']) ? $data['suggestions_title'] : __('Recommended for you:','advq'); if($stitle != ''){ $html .= '<div class="advq_suggestions_title">'; $html .= $stitle; $html .= '</div>'; } $target = isset($data['suggestions-target']) ? $data['suggestions-target'] : '_self'; $showexcerpt = isset($data['show-excerpt']) ? boolval($data['show-excerpt']) : false; $layout = isset($data['suggestions-layout']) ? $data['suggestions-layout'] : 'image_list'; if($showexcerpt){ add_filter('excerpt_more', 'adqv_remove_read_more',11); } $maxnumbercolumns = isset($data['max-number-columns']) ? $data['max-number-columns'] : 4; $numbersuggestions = isset($data['number_suggestions']) ? $data['number_suggestions'] : 4; $columns = $numbersuggestions < $maxnumbercolumns ? $numbersuggestions : $maxnumbercolumns; if($loop->post_count < $columns){ $columns = $loop->post_count; } //WooCommerce Products Query if(advq_woo_activated() && isset($loop->query_vars['post_type']) && $loop->query_vars['post_type'] == 'product'){ while ( $loop->have_posts() ) : $loop->the_post(); global $product; global $woocommerce; //add track paramaters only if there's a unique visitor id if($uid!=''){ $url = add_query_arg( array( 'aqui' => $uid, 'qid' => $id ), get_permalink() ); } else { $url = get_permalink(); } $price = ''; $currency = get_woocommerce_currency_symbol(); $realprice = get_post_meta( get_the_ID(), '_regular_price', true); $sale = get_post_meta( get_the_ID(), '_sale_price', true); /* if($sale){ $price = '<span class="product-price-tickr"><del>'.$currency.$realprice.'</del> '.$currency.$sale.'</span>'; } elseif($realprice) { $price = '<span class="product-price-tickr">'.$currency.$realprice.'</span>'; } */ $price .= do_shortcode('[add_to_cart style="" id="' . get_the_ID() . '"]'); $rating = ''; if(1==1){ $average = $product->get_average_rating(); $rating = wc_get_rating_html($average); } if($layout=='columns'){ $img = ''; $image_size = 'shop_catalog'; if ( has_post_thumbnail() ) { $props = wc_get_product_attachment_props( get_post_thumbnail_id() ); $img = get_the_post_thumbnail( get_the_ID(), $image_size, array( 'title' => $props['title'], 'alt' => $props['alt'], 'data-no-lazy' => '1', ) ); } elseif ( wc_placeholder_img_src() ) { $img = wc_placeholder_img( $image_size ); } $html .='<div class="pa_'.$columns.'columns"> <div class="pa_product"> <div><a target="'.$target.'" href="'.$url.'">'.$img.'</a></div> <div><a target="'.$target.'" href="'.$url.'">'.get_the_title().'</a></div>'; if($showexcerpt){ $excerpt = get_the_excerpt(); if($excerpt !== ''){ $html .= '<div class="pa_product_excerpt">'.$excerpt.'</div>'; } } $html .= $rating; $html .= $price; $html .='</div></div>'; } if($layout=='list'){ $html .=' <div class="pa_product"> <a target="'.$target.'" href="'.$url.'">'.get_the_title().'</a>'; if($showexcerpt){ $excerpt = get_the_excerpt(); if($excerpt !== ''){ $html .= '<div class="pa_product_excerpt">'.$excerpt.'</div>'; } } $html .='</div>'; } if($layout=='image_list'){ $img = ''; $image_size = 'shop_catalog'; if ( has_post_thumbnail() ) { $props = wc_get_product_attachment_props( get_post_thumbnail_id() ); $img = get_the_post_thumbnail( get_the_ID(), $image_size, array( 'title' => $props['title'], 'alt' => $props['alt'], 'data-no-lazy' => '1', ) ); } elseif ( wc_placeholder_img_src() ) { $img = wc_placeholder_img( $image_size ); } $html .='<div class="pa_image_list"> <div class="pa_product"> <div class="pa_image"> <a target="'.$target.'" href="'.$url.'">'. $img .'</a> </div> <div class="pa_info"> <div class="pa_title"> <a target="'.$target.'" href="'.$url.'">'.get_the_title().'</a> </div>'; if($showexcerpt){ $excerpt = get_the_excerpt(); if($excerpt !== ''){ $html .= '<div class="pa_product_excerpt">'.$excerpt.'</div>'; } } $html .= $rating; $html .= $price; $html .='</div></div></div>'; } endwhile; wp_reset_query(); } //Any other type of Query else { while ( $loop->have_posts() ) : $loop->the_post(); //add track paramaters only if there's a unique visitor id if($uid!=''){ $url = add_query_arg( array( 'aqui' => $uid, 'qid' => $id ), get_permalink() ); } else { $url = get_permalink(); } if($layout=='columns'){ $img = get_the_post_thumbnail( $loop->ID, 'thumbnail', array( 'data-no-lazy' => '1' ) ); $html .='<div class="pa_'.$columns.'columns"> <div class="pa_suggestion"> <div><a target="'.$target.'" href="'.$url.'">'.$img.'</a></div> <div><a target="'.$target.'" href="'.$url.'">'.get_the_title().'</a></div>'; if($showexcerpt){ $excerpt = get_the_excerpt(); if($excerpt !== ''){ $html .= '<div class="pa_excerpt">'.$excerpt.'</div>'; } } $html .='</div></div>'; } if($layout=='list'){ $html .=' <div class="pa_suggestion pa_suggestion_list"> <a target="'.$target.'" href="'.$url.'">'.get_the_title().'</a>'; if($showexcerpt){ $excerpt = get_the_excerpt(); if($excerpt !== ''){ $html .= '<div class="pa_excerpt">'.$excerpt.'</div>'; } } $html .='</div>'; } if($layout=='image_list'){ $img = get_the_post_thumbnail( $loop->ID, 'thumbnail', array( 'data-no-lazy' => '1' ) ); $html .='<div class="pa_suggestion pa_image_list"> <div class="pa_suggestion"> <div class="pa_image"> <a target="'.$target.'" href="'.$url.'">'. $img .'</a> </div> <div class="pa_info"> <div class="pa_title"> <a target="'.$target.'" href="'.$url.'">'.get_the_title().'</a>'; $html .='</div>'; if($showexcerpt){ $excerpt = get_the_excerpt(); if($excerpt !== ''){ $html .= '<div class="pa_excerpt">'.$excerpt.'</div>'; } } $html .='</div></div></div>'; } endwhile; wp_reset_query(); } //close suggestions div $html .= '</div>'; return $html; } function adqv_remove_read_more($more) { global $post; return ''; } function advq_get_clean_query($value,$existing){ $args = array(); $query = json_decode($value,true); $nquery = $existing; $nquery['post_type'] = 'post'; //build proper array foreach ($query as $qkey => $qvalue) { if($qkey == 'advq[post_type]'){ $nquery['post_type'] = $qvalue; continue; } if($qkey == 'advq['. $nquery['post_type'].'-query-type][]'){ continue; } $nk = str_replace('advq['.$nquery['post_type'].'_', '', $qkey); $nk = str_replace('][]', '', $nk); $nk = str_replace(']', '', $nk); if(substr( $nk, 0, 18 ) === "operator_taxonomy_"){ $operator = $qvalue; continue; } if(substr( $nk, 0, 9 ) === "taxonomy_"){ $nk = str_replace('taxonomy_', '', $nk); if(!isset($nquery['tax_query'])){ $nquery['tax_query'] = array(); } array_push($nquery['tax_query'],array( 'taxonomy' => $nk, 'field' => 'term_id', 'terms' => $qvalue, 'operator' => isset ( $operator ) && $operator != '' ? $operator : 'IN', )); $operator = ''; continue; } if(substr( $nk, 0, 21 ) === "operator_custom_meta_"){ $operator = $qvalue; continue; } if(substr( $nk, 0, 12 ) === "custom_meta_"){ $nk = str_replace('custom_meta_', '', $nk); if(!isset($nquery['meta_query'])){ $nquery['meta_query'] = array(); } array_push($nquery['meta_query'],array( 'key' => $nk, 'value' => $qvalue, 'compare' => isset ( $operator ) && $operator != '' ? $operator : '=', )); $operator = ''; continue; } if($nk == "search_search-by"){ $nquery['s'] = $qvalue; continue; } if($nk == "post__in_post__in" && $qvalue !== ''){ $nquery['post__in'] = explode(',',$qvalue); continue; } //default, if it arrives here $nquery[$nk] = $qvalue; } error_log(json_encode($nquery)); return $nquery; } function adqv_build_custom_css($qid,$data){ $options = get_option('advq'); $qltype = isset($data['quiz-layout']) ? $data['quiz-layout'] : 'minimal'; //global $gstyles = $options[$qltype.'-global-typography']; //buttons $bgcolor = advq_get_color($options[$qltype.'-check-answers-bg-color']); $hoverbgcolor = advq_get_color($options[$qltype.'-check-answers-hover-bg-color']); $color = advq_get_color($options[$qltype.'-check-answers-color']); $hovercolor = advq_get_color($options[$qltype.'-check-answers-color-hover']); $btnstyles = $options[$qltype.'-buttons-typography']; $html = ' <!-- Custom CSS for Quiz --> <style type="text/css"> /* Fixes */ #advq_quiz_'.$qid.' ul.advq_question_list { padding:10px 0; margin:0; } #advq_quiz_'.$qid.' { font-family: '.$gstyles['font-family'].'; font-size: '.$gstyles['font-size'].'; line-height: '.$gstyles['line-height'].'; font-weight: '.$gstyles['font-weight'].'; font-style: '.$gstyles['font-style'].'; } #advq_quiz_'.$qid.' .advq_button, #advq_quiz_'.$qid.' .advq_validade_ck, #advq_quiz_'.$qid.' .advq_next_button, #advq_quiz_'.$qid.' .advq_start_quiz_button, #advq_quiz_'.$qid.' .advq-restart, #advq_quiz_'.$qid.' .advq-restart a { color:'.$color.'; background-color:'.$bgcolor.'; font-family: '.$btnstyles['font-family'].'; } #advq_quiz_'.$qid.' .advq_button, #advq_quiz_'.$qid.' .advq_next_button, #advq_quiz_'.$qid.' .advq_start_quiz_button, { font-size: '.$btnstyles['font-size'].'; line-height: '.$btnstyles['line-height'].'; font-weight: '.$btnstyles['font-weight'].'; font-style: '.$btnstyles['font-style'].'; } #advq_quiz_'.$qid.' .advq_button:hover, #advq_quiz_'.$qid.' .advq_validade_ck:hover, #advq_quiz_'.$qid.' .advq_next_button:hover, #advq_quiz_'.$qid.' .advq_start_quiz_button:hover, #advq_quiz_'.$qid.' .advq-restart:hover, #advq_quiz_'.$qid.' .advq-restart a:hover { color:'.$hovercolor.'; background-color:'.$hoverbgcolor.'; } '; //Entry Screen Background //if there's a bg image if(isset($data['entry_screen_background']) && isset($data['entry_screen_background']['url']) && $data['entry_screen_background']['url'] != ''){ $html .= ' #advq_quiz_'.$qid.' .advq_entry_screen_wrapper { background-repeat: no-repeat; background-size: cover; background-position: center top; background-image:url("'.$data['entry_screen_background']['url'].'"); }'; } //Entry Screen if(isset($data['entry_screen']) && $data['entry_screen'] == 1){ $esstyles = $options[$qltype.'-entry-typography']; $espadding = $options[$qltype.'-entry-spacing']; $esmargin = $options[$qltype.'-entry-margin']; $esbg = $options[$qltype.'-entry-bg-color']; $escolor = $options[$qltype.'-entry-color']; $html .=' #advq_quiz_'.$qid.' .advq_start_quiz_content_wrap { color: '.advq_get_color($escolor).'; font-family: '.$esstyles['font-family'].'; font-size: '.$esstyles['font-size'].'; line-height: '.$esstyles['line-height'].'; padding-top: '.$espadding['padding-top'].'; padding-right: '.$espadding['padding-right'].'; padding-bottom: '.$espadding['padding-bottom'].'; padding-left: '.$espadding['padding-left'].'; margin-top: '.$esmargin['margin-top'].'; margin-right: '.$esmargin['margin-right'].'; margin-bottom: '.$esmargin['margin-bottom'].'; margin-left: '.$esmargin['margin-left'].'; background-color: '.advq_get_color($esbg).'; font-weight: '.$esstyles['font-weight'].'; font-style: '.$esstyles['font-style'].'; } #advq_quiz_'.$qid.' .advq_start_quiz_content { } '; } //Max Widths if(isset($data['max_width']) && $data['max_width'] != ''){ $html .= '#advq_quiz_'.$qid.' { max-width:'.$data['max_width'].'; } '; } if(isset($data['min-height-desktop']) && $data['min-height-desktop'] != ''){ $html .= ' #advq_quiz_'.$qid.' .advq_entry_screen_wrapper, #advq_quiz_'.$qid.' .advq_question_container, #advq_quiz_'.$qid.' .advq_results_wrapper { min-height: '.$data['min-height-desktop'].'; } '; } if(isset($data['min-height-mobile']) && $data['min-height-mobile'] != '' && isset($data['min-height-desktop']) && $data['min-height-desktop'] != $data['min-height-mobile']){ $html .= ' @media only screen and (max-width: 480px) { #advq_quiz_'.$qid.' .advq_entry_screen_wrapper, #advq_quiz_'.$qid.' .advq_question_container { min-height: '.$data['min-height-mobile'].'; } } '; } //questions $qstyles = $options[$qltype.'-question-typography']; $qpadding = $options[$qltype.'-question-spacing']; $qtbg = $options[$qltype.'-question-bg-color']; $qtcolor = $options[$qltype.'-question-color']; $html .= ' /* Questions */ #advq_quiz_'.$qid.' .advq_question { color: '.advq_get_color($qtcolor).'; font-family: '.$qstyles['font-family'].'; font-size: '.$qstyles['font-size'].'; line-height: '.$qstyles['line-height'].'; padding-top: '.$qpadding['padding-top'].'; padding-right: '.$qpadding['padding-right'].'; padding-bottom: '.$qpadding['padding-bottom'].'; padding-left: '.$qpadding['padding-left'].'; background-color: '.advq_get_color($qtbg).'; font-weight: '.$qstyles['font-weight'].'; font-style: '.$qstyles['font-style'].'; }'; //Answers Styles //CSS BG $css_normal = advq_get_color($options[$qltype.'-answer-normal-bg-color']); $css_hover = advq_get_color($options[$qltype.'-answer-hover-bg-color']); $css_selected = advq_get_color($options[$qltype.'-answer-selected-bg-color']); $css_correct = advq_get_color($options[$qltype.'-answer-correct-bg-color']); $css_unselected_correct = advq_get_color($options[$qltype.'-answer-unselected-correct-bg-color']); $css_wrong = advq_get_color($options[$qltype.'-answer-wrong-bg-color']); //CSS Text Color $css_normal_text = advq_get_color($options[$qltype.'-answer-normal-color'],'#000'); $css_hover_text = advq_get_color($options[$qltype.'-answer-hover-color'],'#000'); $css_selected_text = advq_get_color($options[$qltype.'-answer-selected-color'],'#000'); $css_correct_text = advq_get_color($options[$qltype.'-answer-correct-color'],'#000'); $css_unselected_correct_text = advq_get_color($options[$qltype.'-answer-unselected-correct-color'],'#000'); $css_wrong_text = advq_get_color($options[$qltype.'-answer-wrong-color'],'#000'); //font styles & padding $astyles = $options[$qltype.'-answer-typography']; $apadding = $options[$qltype.'-answer-spacing']; $html .= ' /* General rule for Answers */ #advq_quiz_'.$qid.' .advq_question_container li { font-family: '.$astyles['font-family'].'; font-size: '.$astyles['font-size'].'; line-height: '.$astyles['line-height'].'; font-weight: '.$astyles['font-weight'].'; font-style: '.$astyles['font-style'].'; padding-top: '.$apadding['padding-top'].'; padding-right: '.$apadding['padding-right'].'; padding-bottom: '.$apadding['padding-bottom'].'; padding-left: '.$apadding['padding-left'].'; } /* Normal State */ #advq_quiz_'.$qid.' .advq_question_container li.quiz_unselected_answer { background-color: '.$css_normal.'; color: '.$css_normal_text.'; } /* Hover State */ #advq_quiz_'.$qid.' .advq_question_container ul.advq_question_list:not(.quiz_question_done) li:hover { background-color: '.$css_hover.'; color: '.$css_hover_text.'; } /* Selected Answer */ #advq_quiz_'.$qid.' .advq_question_container li.quiz_selected_answer { background-color: '.$css_selected.'; color: '.$css_selected_text.'; } /* Correct Answer */ #advq_quiz_'.$qid.' .advq_question_container li.quiz_selected_answer.quiz_correct_answer { background-color: '.$css_correct.'; color: '.$css_correct_text.'; } /* Unselected Correct Answer */ #advq_quiz_'.$qid.' .advq_question_container li.quiz_unselected_answer.quiz_unselected_correct_answer { background-color: '.$css_unselected_correct.'; color: '.$css_unselected_correct_text.'; } /* Wrong Answer */ #advq_quiz_'.$qid.' .advq_question_container li.quiz_selected_answer.quiz_incorrect_answer { background-color: '.$css_wrong.'; color: '.$css_wrong_text.'; } /* Answer Information */ #advq_quiz_'.$qid.' .advq_answer_commentary { background-color: '.$css_normal.'; color: '.$css_normal_text.'; }'; //answers container $acbgcolor = advq_get_color($options[$qltype.'-answers-container-bg-color'],'#f9f9f9'); $accolor = advq_get_color($options[$qltype.'-answers-container-color'],'#000'); $acpadding = $options[$qltype.'-answers-container-spacing']; $acmargin = $options[$qltype.'-answers-container-margin']; $acborder = $options[$qltype.'-answers-container-border']; $html .= ' /* Answers Container */ #advq_quiz_'.$qid.' .advq_results_wrapper { background-color: '.$acbgcolor.'; color: '.$accolor.'; padding-top: '.$acpadding['padding-top'].'; padding-right: '.$acpadding['padding-right'].'; padding-bottom: '.$acpadding['padding-bottom'].'; padding-left: '.$acpadding['padding-left'].'; margin-top: '.$acmargin['margin-top'].'; margin-right: '.$acmargin['margin-right'].'; margin-bottom: '.$acmargin['margin-bottom'].'; margin-left: '.$acmargin['margin-left'].'; border:'.$acborder['border-top'].' '.$acborder['border-style'].' '.$acborder['border-color'].'; } #advq_quiz_'.$qid.' .advq_review_answers { padding-top: '.$acpadding['padding-top'].'; padding-right: '.$acpadding['padding-right'].'; padding-bottom: '.$acpadding['padding-bottom'].'; padding-left: '.$acpadding['padding-left'].'; } '; //end $html .= ' </style>'; return $html; } function advq_checkanswers_button($label){ $html = '<div class="advq_button">'.$label.'</div> <div class="advq_warning"></div>'; return $html; } function advq_restart_button($qid,$data){ $qltype = isset($data['quiz-layout']) ? $data['quiz-layout'] : 'minimal'; $options = get_option('advq'); $restartlabel = isset($data['restart_label']) ? $data['restart_label'] : __('Retake Quiz','advq'); $html =' <div class="advq-restart"> <a href="'.$_SERVER["REQUEST_URI"].'" title="'.$restartlabel.'"><i class="fa fa-refresh" aria-hidden="true"></i> <span>'.$restartlabel.'</span></a> </div>'; return $html; } function advq_get_current_url(){ return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; } /* Function to add extra parameters and correct some others for the query */ function advq_compile_query($query,$data){ $numbersuggestions = isset($data['number_suggestions']) ? $data['number_suggestions'] : 4; $query['posts_per_page'] = $numbersuggestions; //set only published by default: $query['post_status'] = 'publish'; if(isset($query['meta_query']) && count($query['meta_query']>0)){ $meta_relation = isset( $data['meta_relation'] ) ? $data['meta_relation'] : 'AND'; $query['meta_query']['relation'] = $meta_relation; //the same for custom meta foreach ($query['meta_query'] as $qk => $metaq) { if(!is_array($qk)){ continue; } foreach ($metaq as $key => $value) { if(is_array($value)){ if(isset($value['relation'])){ $relation = $value['relation']; unset($query['meta_query'][$qk][$key]); $query['meta_query'][$qk]['relation'] = $relation; } } } } } if(isset($query['tax_query']) && count($query['tax_query'])>0){ $tax_relation = isset( $data['tax_relation'] ) ? $data['tax_relation'] : 'AND'; $query['tax_query']['relation'] = $tax_relation; //checkfor multiple queries (checkboxes) and correct relation parameter for taxonomies foreach ($query['tax_query'] as $qk => $taxq) { if(!is_array($taxq)){ continue; } foreach ($taxq as $key => $value) { if(is_array($value)){ if(isset($value['relation'])){ $relation = $value['relation']; unset($query['tax_query'][$qk][$key]); $query['tax_query'][$qk]['relation'] = $relation; } } } } } //Add field type to meta_query if it exists in the filters $opts = apply_filters('advq_admin_build_quiz',array()); //set the 'type' for the custom meta fields if(isset($query['post_type']) && isset($opts[$query['post_type']]) && isset($query['meta_query']) && isset($opts[$query['post_type']]['custom_meta'])){ foreach ($query['meta_query'] as $mkey => $mq) { if(isset($mq['key']) && isset($opts[$query['post_type']]['custom_meta'][$mq['key']]) ){ $query['meta_query'][$mkey]['type'] = $opts[$query['post_type']]['custom_meta'][$mq['key']]['type']; } } } return $query; } //WooCommerce get order details //to get full order details function advq_get_woo_order($id, $fields = null, $filter = array()) { if (is_wp_error($id)) return $id; // Get the decimal precession $dp = (isset($filter['dp'])) ? intval($filter['dp']) : 2; $order = wc_get_order($id); //getting order Object //$completedat = !$order->get_date_completed() ? $order->get_date_completed()->date('Y-m-d H:i:s') : ''; $order_data = array( 'id' => $order->get_id(), 'order_number' => $order->get_order_number(), 'created_at' => $order->get_date_created()->date('Y-m-d H:i:s'), 'updated_at' => $order->get_date_modified()->date('Y-m-d H:i:s'), //'completed_at' => $completedat, 'status' => $order->get_status(), 'currency' => $order->get_currency(), 'total' => wc_format_decimal($order->get_total(), $dp), 'subtotal' => wc_format_decimal($order->get_subtotal(), $dp), 'total_line_items_quantity' => $order->get_item_count(), 'total_tax' => wc_format_decimal($order->get_total_tax(), $dp), 'total_shipping' => wc_format_decimal($order->get_total_shipping(), $dp), 'cart_tax' => wc_format_decimal($order->get_cart_tax(), $dp), 'shipping_tax' => wc_format_decimal($order->get_shipping_tax(), $dp), 'total_discount' => wc_format_decimal($order->get_total_discount(), $dp), 'shipping_methods' => $order->get_shipping_method(), 'order_key' => $order->get_order_key(), 'customer_ip' => $order->get_customer_ip_address(), 'customer_user_agent' => $order->get_customer_user_agent(), 'customer_id' => $order->get_user_id(), 'view_order_url' => $order->get_view_order_url(), 'line_items' => array(), 'shipping_lines' => array(), 'tax_lines' => array(), 'fee_lines' => array(), 'coupon_lines' => array(), ); //getting all line items foreach ($order->get_items() as $item_id => $item) { $product = $item->get_product(); $product_id = null; $product_sku = null; // Check if the product exists. if (is_object($product)) { $product_id = $product->get_id(); $product_sku = $product->get_sku(); } $order_data['line_items'][] = array( 'id' => $item_id, 'subtotal' => wc_format_decimal($order->get_line_subtotal($item, false, false), $dp), 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], $dp), 'total' => wc_format_decimal($order->get_line_total($item, false, false), $dp), 'total_tax' => wc_format_decimal($item['line_tax'], $dp), 'price' => wc_format_decimal($order->get_item_total($item, false, false), $dp), 'quantity' => wc_stock_amount($item['qty']), 'tax_class' => (!empty($item['tax_class']) ) ? $item['tax_class'] : null, 'name' => $item['name'], 'product_id' => !$item->get_variation_id() && ('product_variation' === $product->post_type ) ? $product->get_parent_id() : $product_id, 'variation_id' => !$item->get_variation_id() && ('product_variation' === $product->post_type ) ? $product_id : 0, 'product_url' => get_permalink($product_id), //'product_thumbnail_url' => wp_get_attachment_image_src( get_post_thumbnail_id($product_id) , 'thumbnail', TRUE )[0], 'sku' => $product_sku, 'meta' => wc_display_item_meta($item) ); } return array('order' => apply_filters('woocommerce_api_order_response', $order_data, $order, $fields)); } function advq_format_stats($statistics = false) { if($statistics){ $visits = 0; $started = 0; $completed = 0; $clickt = array(); $emails = array(); $nusers = array(); $times = array(); $averagetime = 0; $referals = array(); $answers = array(); $purchases = array(); $results = array(); foreach ($statistics as $vkey => $value) { $visits = isset($value['page_views']) ? $visits + intval($value['page_views']) : $visits; //referals if(isset($value['referer']) && $value['referer'] != ''){ array_push($referals, $value['referer']); } //started if(isset($value['started']) && $value['started'] != '' && is_array($value['started'])){ $started = $started + count($value['started']); } //clicked if(isset($value['clicked']) && is_array($value['clicked']) && count($value['clicked']) > 0){ foreach ($value['clicked'] as $clickedurl) { $clickedurl = esc_url( remove_query_arg( array('aqui','qid') , $clickedurl ) ); array_push($clickt, $clickedurl); } } //purchases if(isset($value['purchases']) && is_array($value['purchases']) && count($value['purchases']) > 0){ foreach ($value['purchases'] as $ordid) { array_push($purchases, $ordid); } } //email if(isset($value['email'])){ array_push($emails, $value['email']); } //new user if(isset($value['new_user'])){ array_push($nusers,$value['new_user']); } if(isset($value['sessions'])){ foreach ($value['sessions'] as $skey => $session) { //increment completed $completed++; //calculate time and add it to times array if(isset($session['end_time'])){ array_push($times, intval($session['end_time'])-intval($session['start_time'])); } //answers if(isset($session['selected']) && $session['selected'] != ''){ array_push($answers, $session['selected']); } //result if(isset($session['result']) && $session['result'] != ''){ array_push($results, $session['result']); } } } } if(count($times)>0){ $averagetime = intval(array_sum($times)/count($times)); } else { $averagetime = 0; } $averagetime = $averagetime.'s'; $finalstats = array( 'visits' => $visits, 'started' => $started, 'completed' => $completed, 'clickt' => $clickt, 'emails' => $emails, 'nusers' => $nusers, 'times' => $times, 'averagetime' => $averagetime, 'referals' => $referals, 'answers' => $answers, 'purchases' => $purchases, 'results' => $results, ); return $finalstats; } else { return array( 'visits' => 0, 'started' => 0, 'completed' => 0, ); } } function advq_get_color($color,$default = 'transparent') { if(is_array($color)){ if(isset($color['rgba']) && $color['rgba'] != ''){ return $color['rgba']; } else { if($color['alpha']=='') { return ''; } else if($color['alpha'] != 1){ return advq_hexToRgb($color['color'],$color['alpha']); } else { return $color['color']; } } } else { return $color; } return $default; } function advq_hexToRgb($hex, $alpha = false) { $hex = str_replace('#', '', $hex); $length = strlen($hex); $rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0)); $rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0)); $rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0)); if ( $alpha ) { $rgb['a'] = $alpha; } return implode(array_keys($rgb)) . '(' . implode(', ', $rgb) . ')'; //return $rgb; } function advq_human_readable_query_args($query) { if(!is_array($query)){ $query = json_decode($query,true); } $html = json_encode($query); foreach ($query as $key => $value) { switch ($key) { case 'post_type': $object = get_post_type_object( $value )->labels; $html .= __('Type of Content','advq').': '.$object->name.'<br>'; break; //tax_query: [{"taxonomy":"product_cat","field":"term_id","terms":["41","42","46"],"operator":"IN"},{"taxonomy":"product_cat","field":"term_id","terms":["46"],"operator":"AND"}] case 'tax_query': foreach ($value as $tax) { $taxobj = get_taxonomy( $tax['taxonomy'] ); $label = $taxobj->labels->name; $terms = ''; foreach ($tax['terms'] as $term) { $thisterm = get_term_by($tax['field'], $term, $tax['taxonomy']); $terms .= $thisterm->name.', '; } $terms = rtrim(trim($terms),','); $html .= $label.': '.$terms.' ('.$tax['operator'].')<br>'; } break; //meta_query: [{"key":"_regular_price","value":"18","compare":"<="}] case 'meta_query': $existingfields = apply_filters('advq_admin_build_quiz',array()); $thisobj = isset($existingfields[$query['post_type']]) ? $existingfields[$query['post_type']] : false; foreach ($value as $meta) { $label = $meta['key']; $oper = $meta['compare']; $val = $meta['value']; if($thisobj){ if(isset($thisobj['custom_meta'][$label]['label'])){ $label = $thisobj['custom_meta'][$label]['label']; } } $html .= $label.' '.$oper.' '.$val.'<br>'; } break; default: if(is_array($value)){ $value = json_encode($value); } $html .= $key .': '.$value.'<br>'; break; } } return $html; } //to remove yoast auto canonical url to parent page function advq_remove_yoast_canonical(){ return false; } function advq_change_yoast_ogimg( $img ){ if(isset($_GET['qresult'])) { return false; } return $img; } function advq_change_yoast_title( $title ) { if(isset($_GET['qresult'])) { return false; } return $title; } function advq_change_yoast_description( $description ) { if(isset($_GET['qresult'])) { return false; } return $description; } /* * Add the duplicate link to action list for post_row_actions */ function advq_duplicate_quiz( $actions, $post ) { if ($post->post_type === 'advquiz' && current_user_can('edit_posts')) { $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=advq_duplicate&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="' . __('Duplicate this item','advq').'" rel="permalink">'.__('Duplicate','advq').'</a>'; } return $actions; } add_filter('post_row_actions', 'advq_duplicate_quiz', 10, 2); /* * Function creates a quiz duplicate as a draft and redirects then to the edit post screen */ function advq_duplicate(){ global $wpdb; if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'advq_duplicate_quiz' == $_REQUEST['action'] ) ) ) { wp_die( __( 'No quiz to duplicate. Something went wrong.','advq' ) ); } /* * Nonce verification */ if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) return; /* * get the original post id */ $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) ); /* * and all the original post data then */ $post = get_post( $post_id ); /* * if you don't want current user to be the new post author, * then change next couple of lines to this: $new_post_author = $post->post_author; */ $current_user = wp_get_current_user(); $new_post_author = $current_user->ID; /* * if post data exists, create the post duplicate */ if (isset( $post ) && $post != null) { /* * new post data array */ $args = array( 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_author' => $new_post_author, 'post_content' => $post->post_content, 'post_excerpt' => $post->post_excerpt, 'post_name' => $post->post_name, 'post_parent' => $post->post_parent, 'post_password' => $post->post_password, 'post_status' => 'draft', 'post_title' => $post->post_title . ' ' . __('(Duplicated)','advq'), 'post_type' => $post->post_type, 'to_ping' => $post->to_ping, 'menu_order' => $post->menu_order ); /* * insert the post by wp_insert_post() function */ $new_post_id = wp_insert_post( $args ); /* * get all current post terms ad set them to the new post draft */ $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag"); foreach ($taxonomies as $taxonomy) { $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs')); wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false); } /* * duplicate all post meta just in two SQL queries */ $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"); if (count($post_meta_infos)!=0) { $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) "; foreach ($post_meta_infos as $meta_info) { $meta_key = $meta_info->meta_key; if( $meta_key == '_wp_old_slug' ) continue; if( $meta_key == '_statistics' ) continue; $meta_value = addslashes($meta_info->meta_value); $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'"; } $sql_query.= implode(" UNION ALL ", $sql_query_sel); $wpdb->query($sql_query); } /* * finally, redirect to the edit post screen for the new draft */ wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) ); exit; } else { wp_die( __('Quiz creation failed, could not find original post:','adqv') . $post_id); } } add_action( 'admin_action_advq_duplicate', 'advq_duplicate' ); ?>
Save File
Cancel