From dc924c0e76a7e2f906a0c3fb8562a0fc46f0f0ce Mon Sep 17 00:00:00 2001 From: Wasim Akhtar <53761154+WasimM3@users.noreply.github.com> Date: Thu, 2 Apr 2020 19:22:20 +0530 Subject: [PATCH] #4326 - Tag and Custom Taxonomy support --- includes/admin-script.js | 6 +-- includes/features/functions.php | 23 +++++---- includes/redirect.php | 13 +++-- templates/features.php | 90 ++++++++++++++++++++------------- 4 files changed, 78 insertions(+), 54 deletions(-) diff --git a/includes/admin-script.js b/includes/admin-script.js index 1a3c50309..f4a0b9aa5 100644 --- a/includes/admin-script.js +++ b/includes/admin-script.js @@ -113,12 +113,12 @@ jQuery(function($) { captureLength:0 }); } - $("#show_amp_category").on('change', function(){ + $("#show_amp_taxonomy").on('change', function(){ var thisval = $(this).val(); if(thisval=='hide'){ - $("#amp-show-hide-cat").css({'display':'block'}); + $("#amp-show-hide-tax").css({'display':'block'}); }else if(thisval=='show'){ - $("#amp-show-hide-cat").css({'display':'none'}); + $("#amp-show-hide-tax").css({'display':'none'}); } }); $('.redux-container').each(function() { diff --git a/includes/features/functions.php b/includes/features/functions.php index 83381fd57..755d3d415 100644 --- a/includes/features/functions.php +++ b/includes/features/functions.php @@ -160,24 +160,29 @@ function ampforwp_get_the_ID($post_id=''){ return $post_id; } -function ampforwp_get_category_meta($term_id,$type=''){ - $amp_category = get_term_meta( $term_id,'amp_category'); - $amp_hide_cat = get_term_meta( $term_id,'amp_hide_cat'); - $data = array('visible'=>$amp_category,'visible_status'=>$amp_hide_cat); +function ampforwp_get_taxonomy_meta($term_id,$type=''){ if($type=='' || $type=='data'){ + $amp_taxonomy = get_term_meta( $term_id,'amp_taxonomy'); + $amp_hide_tax = get_term_meta( $term_id,'amp_hide_tax'); + $data = array('visible'=>$amp_taxonomy,'visible_status'=>$amp_hide_tax); return $data; }else if($type=='status'){ - if(isset($amp_category[0]) && $amp_category[0]=='hide'){ + $amp_taxonomy = get_term_meta( $term_id,'amp_taxonomy'); + if(isset($amp_taxonomy[0]) && $amp_taxonomy[0]=='hide'){ return false; }else{ return true; } }else if($type=='post_status'){ - if(isset($amp_category[0]) && $amp_category[0]=='hide' && $amp_hide_cat[0]=='hide-cat-post'){ - return false; - }else{ - return true; + $term = wp_get_post_terms(ampforwp_get_the_ID(),array('category','post_tag')); + foreach ($term as $key => $value) { + $amp_taxonomy = get_term_meta( $value->term_id,'amp_taxonomy'); + $amp_hide_tax = get_term_meta( $value->term_id,'amp_hide_tax'); + if(isset($amp_taxonomy[0]) && $amp_taxonomy[0]=='hide' && $amp_hide_tax[0]=='hide-tax-post'){ + return false; + } } + return true; } } diff --git a/includes/redirect.php b/includes/redirect.php index 35f90a62b..1c13e72ce 100644 --- a/includes/redirect.php +++ b/includes/redirect.php @@ -72,11 +72,12 @@ function ampforwp_redirection() { exit; } + // HIDE/SHOW TAG AND CATEGORY #4326 if(ampforwp_is_amp_endpoint() ) { - if(is_category()){ + if(is_tag() || is_category() || is_tax()){ $term_id = get_queried_object()->term_id; - $category_status = ampforwp_get_category_meta($term_id,'status'); - if($category_status==false){ + $tax_status = ampforwp_get_taxonomy_meta($term_id,'status'); + if($tax_status==false){ $go_to_url = home_url(add_query_arg($_GET,$wp->request)); $go_to_url = str_replace("/amp", '', $go_to_url); $go_to_url = str_replace("?amp=1", '', $go_to_url); @@ -85,10 +86,8 @@ function ampforwp_redirection() { exit; } }else if(is_single()){ - $term = get_the_category(); - $term_id = $term[0]->cat_ID; - $category_status = ampforwp_get_category_meta($term_id,'post_status'); - if($category_status==false){ + $tax_status = ampforwp_get_taxonomy_meta('','post_status'); + if($tax_status==false){ $go_to_url = home_url(add_query_arg($_GET,$wp->request)); $go_to_url = str_replace("/amp", '', $go_to_url); $go_to_url = str_replace("?amp=1", '', $go_to_url); diff --git a/templates/features.php b/templates/features.php index 943f9628b..2c1b13133 100644 --- a/templates/features.php +++ b/templates/features.php @@ -210,17 +210,16 @@ function ampforwp_amphtml_generator(){ return; } - if(is_category()){ + // HIDE/SHOW TAG AND CATEGORY #4326 + if(is_tag() || is_category() || is_tax()){ $term_id = get_queried_object()->term_id; - $category_status = ampforwp_get_category_meta($term_id,'status'); - if($category_status==false){ + $tax_status = ampforwp_get_taxonomy_meta($term_id,'status'); + if($tax_status==false){ return; } }else if(is_single()){ - $term = get_the_category(); - $term_id = $term[0]->cat_ID; - $category_status = ampforwp_get_category_meta($term_id,'post_status'); - if($category_status==false){ + $tax_status = ampforwp_get_taxonomy_meta('','post_status'); + if($tax_status==false){ return; } } @@ -749,50 +748,71 @@ function ampforwp_title_custom_meta() { add_action( 'add_meta_boxes', 'ampforwp_title_custom_meta' ); -add_action('edited_category', 'ampforwp_update_category_meta',10,2); -add_action('create_category', 'ampforwp_save_category_meta', 10); -function ampforwp_save_category_meta($term_id){ - - if(isset($_POST['show_amp_category'])){ - $cat_status = sanitize_text_field($_POST['show_amp_category']); - $hide_cat = sanitize_text_field($_POST['hide_cat']); - add_term_meta($term_id, 'amp_category', $cat_status ); - add_term_meta( $term_id,'amp_hide_cat', $hide_cat); +function ampforwp_save_taxonomy_meta($term_id){ + if(isset($_POST['amp_taxonomy'])){ + $cat_status = sanitize_text_field($_POST['amp_taxonomy']); + $hide_tax = sanitize_text_field($_POST['hide_tax']); + add_term_meta($term_id, 'amp_taxonomy', $cat_status ); + add_term_meta( $term_id,'amp_hide_tax', $hide_tax); + } +} +function ampforwp_update_taxonomy_meta($term_id, $term_id1){ + if(isset($_POST['amp_taxonomy'])){ + $cat_status = sanitize_text_field($_POST['amp_taxonomy']); + $hide_tax = sanitize_text_field($_POST['hide_tax']); + update_term_meta( $term_id,'amp_taxonomy', $cat_status); + update_term_meta( $term_id,'amp_hide_tax', $hide_tax); } } -function ampforwp_update_category_meta($term_id, $term_id1){ - if(isset($_POST['show_amp_category'])){ - $cat_status = sanitize_text_field($_POST['show_amp_category']); - $hide_cat = sanitize_text_field($_POST['hide_cat']); - update_term_meta( $term_id,'amp_category', $cat_status); - update_term_meta( $term_id,'amp_hide_cat', $hide_cat); + +if ( isset( $_REQUEST['taxonomy'] )) { + $taxonomy = $_REQUEST['taxonomy']; + add_action('edited_'.$taxonomy, 'ampforwp_update_taxonomy_meta',10,2); + add_action('create_'.$taxonomy, 'ampforwp_save_taxonomy_meta', 10); + add_action('edited_'.$taxonomy, 'ampforwp_update_taxonomy_meta',10,2); + add_action('create_'.$taxonomy, 'ampforwp_save_taxonomy_meta', 10); + if($taxonomy!='category' && isset($_REQUEST['tag_ID'])){ + add_action ( 'edit_tag_form_fields', 'ampforwp_extra_category_fields'); + }else{ + add_action ( 'edit_'.$taxonomy.'_form_fields', 'ampforwp_extra_category_fields'); } + add_action ( $taxonomy.'_add_form_fields', 'ampforwp_extra_category_fields'); } -add_action ( 'edit_category_form_fields', 'ampforwp_extra_category_fields'); -add_action ( 'category_add_form_fields', 'ampforwp_extra_category_fields'); function ampforwp_extra_category_fields( $tag ) { + $label = 'category'; + if(is_object($tag)){ + if($tag->taxonomy=="post_tag"){ + $label = 'tag'; + }else if($tag->taxonomy!='category'){ + $label = $tag->taxonomy; + } + }else{ + if($tag=='post_tag'){ + $label = 'tag'; + } + } ?>