gaCode = ''; $this->relatedTaxonomy = 'category'; $this->additionalPostTypes = explode(',','events,event-post,news,locations,location-post,wpseo_locations,tribe_events'); $this->disableRelatedPosts = false; if(defined('PS_AMP_GA_ANALYTICS')): $this->gaCode = PS_AMP_GA_ANALYTICS; endif; if(defined('PS_AMP_RELATED_TAXONOMY')): $this->relatedTaxonomy = PS_AMP_RELATED_TAXONOMY; endif; if(defined('PS_AMP_ADDITIONAL_POST_TYPES')): $this->additionalPostTypes = explode(',',PS_AMP_ADDITIONAL_POST_TYPES); endif; if(defined('PS_AMP_DISABLE_RELATED_POSTS')): $this->disableRelatedPosts = PS_AMP_DISABLE_RELATED_POSTS; endif; } /* removeWordPressFooter() ================================================== */ function removeWordPressFooter(){ if(is_amp_endpoint()): remove_all_actions('wp_footer'); endif; } /* removeShortcodes() ================================================== */ function removeShortcodes($content){ if(is_amp_endpoint()): remove_shortcode('wpseo_map'); add_shortcode('wpseo_map',function(){return '';}); endif; return $content; } /* setCustomTemplate() ================================================== */ function setCustomTemplate($file, $type, $post){ if( 'header-bar' === $type ): $file = __DIR__ . '/includes/ps-header-bar.php'; endif; return $file; } /* additionalCSS() ================================================== */ function additionalCSS(){ include( __DIR__ . '/includes/custom-styles.php' ); } /* modifyMetaData() ================================================== */ function modifyMetaData($metadata,$post){ if(function_exists('has_site_icon')&&function_exists ('get_site_icon_url')): if( !isset($metadata['image']) && has_site_icon() ): $metadata['image'] = array( '@type' => 'ImageObject', 'url' => get_site_icon_url(), 'height' => 512, 'width' => 512, ); endif; endif; return $metadata; } /* addPostTypeSupport() ================================================== */ function addPostTypeSupport(){ add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK | EP_PAGES); foreach($this->additionalPostTypes as $postType): if(post_type_exists($postType)): add_post_type_support($postType,AMP_QUERY_VAR); endif; endforeach; } /* addToHead() ================================================== */ function addToHead(){ if($this->gaCode): echo ''; endif; } /* addToFooter() ================================================== */ function addToFooter(){ if($this->gaCode): ?> View More Info →

' . $content . '

View More Info →

'; endif; return $content; } /* redirectEmptyContent() ================================================== */ function redirectEmptyContent($content){ if(is_amp_endpoint()&&!strlen($content)): wp_redirect(get_the_permalink(),'301'); exit; endif; return $content; } /* addRelatedPosts() ================================================== */ function addRelatedPosts($content){ if(is_amp_endpoint()&&!$this->disableRelatedPosts): $this->disableRelatedPosts = true; $relatedContent = $this->relatedPosts(array( "type" => get_post_type(), "post_id" => get_the_id(), "count" => 4, "taxonomy" => $this->relatedTaxonomy, )); if(!empty($relatedContent['data'])): $content .= ''; $content .= ''; endif; endif; return $content; } /* FIND RELATED POSTS ================================================== */ public function relatedPosts( $data = array( ) ){ $opts = array( "post_id" => '', "type" => 'post', "count" => 20, "taxonomy" => 'category', 'post_status' => 'publish', ); $opts = array_merge($opts,$data); if( !$opts['post_id'] ): return 'Missing Required Parameters'; endif; // CREATE OUTPUT DATA $outputData = ''; $output = ''; $transientName = 'relatedPosts_' . $opts['post_id'] . '_' . $opts['type'] . '_' . $opts['count'] . '_' . $opts['taxonomy'] . '_' . $opts['post_status']; if(( $output = get_transient( $transientName )) === false ): $relatedTaxonomy = get_the_terms($opts['post_id'],$opts['taxonomy']); if(count($relatedTaxonomy)>0&&$relatedTaxonomy): $loopCount = 0; foreach($relatedTaxonomy as $key => $value): $args = array( 'post_type' => $opts['type'], 'taxonomy' => $opts['taxonomy'], 'term' => $value->slug, 'post_status' => $opts['post_status'], ); $loop = new WP_Query($args); while ($loop->have_posts()): $loop->the_post(); if($opts['post_id']!=get_the_ID()): $item_key = 'none'; if($loopCount): $item_key = array_search(get_the_ID(),$this->array_key($outputData,'id')); endif; if( is_numeric($item_key) ): $outputData[$item_key]['count']++; else: $outputData[$loopCount]['count'] = 1; $outputData[$loopCount]['id'] = get_the_ID(); $outputData[$loopCount]['content'] = get_post(get_the_ID()); endif; $loopCount++; endif; endwhile; endforeach; endif; $outputData = $this->aasortASC($outputData,'count'); if(!empty($outputData)): $count = 0; foreach($outputData as $data): if($data&&$data['content']&&$count<$opts['count']): $output[$count] = $data['content']; $count++; endif; endforeach; endif; // SET TRANSIENT set_transient( $transientName, $output, 24 * HOUR_IN_SECONDS ); endif; // RETURN wp_reset_query(); return array('data'=>$output); } /* ITEM KEY FROM ARRAY | ex: $item_key = array_search($_GET['id'], $functions->array_key($array, 'uid')); ================================================== */ public function array_key($input = null, $columnKey = null, $indexKey = null){ $params = func_get_args(); $paramsInput = $params[0]; $paramsColumnKey = ($params[1] !== null) ? (string)$params[1] : null; $paramsIndexKey = null; if(isset($params[2])): if(is_float($params[2]) || is_int($params[2])): $paramsIndexKey = (int)$params[2]; else: $paramsIndexKey = (string)$params[2]; endif; endif; $returnArray = array(); foreach($paramsInput as $row): $key = $value = null; $keySet = $valueSet = false; if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) { $keySet = true; $key = (string)$row[$paramsIndexKey]; } if($paramsColumnKey === null): $valueSet = true; $value = $row; elseif(is_array($row) && array_key_exists($paramsColumnKey, $row)): $valueSet = true; $value = $row[$paramsColumnKey]; endif; if($valueSet): if($keySet): $returnArray[$key] = $value; else: $returnArray[] = $value; endif; endif; endforeach; return $returnArray; } /* SORT BY COLUMN | ex: aasortASC($outputData,'count'); ================================================== */ public function aasortASC($array, $key){ if(!is_array($array)){ return $array; } $sorter=array(); $ret=array(); reset($array); foreach ($array as $ii => $va) { $sorter[$ii]=$va[$key]; } ksort($sorter); foreach ($sorter as $ii => $va) { $ret[$ii]=$array[$ii]; } return $ret; } public function aasortDSC($array, $key){ if(!is_array($array)){ return $array; } $sorter=array(); $ret=array(); reset($array); foreach ($array as $ii => $va) { $sorter[$ii]=$va[$key]; } asort($sorter); foreach ($sorter as $ii => $va) { $ret[$ii]=$array[$ii]; } return $ret; } /* GET POST MAIN IMAGE | ex: mainImageFromPost(get_the_id()); ================================================== */ public function mainImageFromPost($postID){ $data = wp_get_attachment_image_src(get_post_thumbnail_id($postID),'full'); if($data[0]): $data['src'] = $data[0]; else: $first_img = '';ob_start();ob_end_clean(); $output = preg_match_all('//i', get_post($postID)->post_content, $matches); $first_img = ( $matches && $matches[1] && $matches[1][0] ) ? $matches[1][0] : '' ; if(!empty($first_img)): $resizePattern = array ('/(.*)-([0-9]*)x([0-9]*).([a-zA-Z]*)/i'); $fullReplace = array ('\1.\4'); $first_img = preg_replace($resizePattern, $fullReplace, $first_img); else: $first_img = ''; endif; $data['src'] = $first_img; endif; return $data['src']; } } $PostScriptCustomAMP = new PostScriptCustomAMP(); endif;