gaCode = '';
$this->relatedTaxonomy = 'category';
$this->additionalPostTypes = explode(',','events,ps-event,news,locations,ps-location,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;
}
/**
* Removes the footer from AMP pages ( if it theme loads one )
**/
public function removeWordPressFooter(){
if(is_amp_endpoint()):
remove_all_actions('wp_footer');
endif;
}
/**
* Disable these shortcodes from breaking AMP code
* @return string
**/
public function removeShortcodes($content){
if(is_amp_endpoint()):
remove_shortcode('wpseo_map');
add_shortcode('wpseo_map',function(){return '';});
endif;
return $content;
}
/**
* Use custom layouts for the amp design
* @return string
**/
public function setCustomTemplate($file, $type, $post){
if( 'header-bar' === $type ):
$file = __DIR__ . '/includes/ps-header-bar.php';
endif;
return $file;
}
/**
* Add CSS to overwrite base styles
**/
public function additionalCSS(){
include( __DIR__ . '/includes/custom-styles.php' );
}
/**
* Add site icon as image for the post if no other image is present
* @return array
**/
public 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;
}
/**
* Support other post types for AMP layout
**/
public 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;
}
/**
* Add the AMP analytics tag
**/
public function addToHead(){
if($this->gaCode):
echo '';
endif;
}
/**
* Defines the AMP analytics settings
**/
public function addToFooter(){
if($this->gaCode):
?>
' . __( 'View More Info','postscript') . ' →
'
. $content
. '' . __( 'View More Info','postscript') . ' →
';
endif;
return $content;
}
/**
* If the content is empty ( theme creator is using custom layout most likely ), redirect to the non-AMP page/content
* @return string
**/
public function redirectEmptyContent($content){
if(is_amp_endpoint()&&!strlen($content)):
wp_redirect(get_the_permalink(),'301');
exit;
endif;
return $content;
}
/**
* Find all related posts and add them to the end of the content
* @return string
**/
public 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 .= '' . __( 'Related Articles','postscript') . '
';
$content .= '';
endif;
endif;
return $content;
}
/**
* Returns all posts related to each-other based on taxonomy
* @param array $data
* @return array
**/
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);
}
/**
* Replicates the functionality of array_column() without php version 5.5
* @param string $string
* @param string $string
* @param string $string
* @return array
**/
private 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;
}
/**
* Sorts array by ASC order
* @param array $array
* @param string $key
* @return array
**/
private 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;
}
/**
* Sorts array by DESC order
* @param array $array
* @param string $key
* @return array
**/
private 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;
}
/**
* Gets the featured image, or the first image from any post object
* @param integer $postID
* @return string
**/
private 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'];
}
}
$CustomizeAMP = new CustomizeAMP();
$CustomizeAMP->_initTheme();
endif;