From 4cec43a18a5419fef87b7d9aa525090371cca05d Mon Sep 17 00:00:00 2001 From: Wasim Akhtar <53761154+WasimM3@users.noreply.github.com> Date: Wed, 1 Apr 2020 18:28:24 +0530 Subject: [PATCH] #4326 - Option to hide/show category and it's related post --- includes/admin-script.js | 8 ++++ includes/features/functions.php | 21 ++++++++ includes/redirect.php | 28 +++++++++++ templates/features.php | 85 +++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) diff --git a/includes/admin-script.js b/includes/admin-script.js index 6c8d6487b..1a3c50309 100644 --- a/includes/admin-script.js +++ b/includes/admin-script.js @@ -113,6 +113,14 @@ jQuery(function($) { captureLength:0 }); } + $("#show_amp_category").on('change', function(){ + var thisval = $(this).val(); + if(thisval=='hide'){ + $("#amp-show-hide-cat").css({'display':'block'}); + }else if(thisval=='show'){ + $("#amp-show-hide-cat").css({'display':'none'}); + } + }); $('.redux-container').each(function() { if (!$(this).hasClass('redux-no-sections')) { $(this).find('.display_header').append(''); diff --git a/includes/features/functions.php b/includes/features/functions.php index c7f61f0df..83381fd57 100644 --- a/includes/features/functions.php +++ b/includes/features/functions.php @@ -160,6 +160,27 @@ 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); + if($type=='' || $type=='data'){ + return $data; + }else if($type=='status'){ + if(isset($amp_category[0]) && $amp_category[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; + } + } +} + // Backward Compatibility function ampforwp_correct_frontpage() { return ampforwp_get_frontpage_id(); diff --git a/includes/redirect.php b/includes/redirect.php index 5486c374e..35f90a62b 100644 --- a/includes/redirect.php +++ b/includes/redirect.php @@ -71,6 +71,34 @@ function ampforwp_redirection() { wp_safe_redirect( $current_url ); exit; } + + if(ampforwp_is_amp_endpoint() ) { + if(is_category()){ + $term_id = get_queried_object()->term_id; + $category_status = ampforwp_get_category_meta($term_id,'status'); + if($category_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); + $go_to_url = str_replace("?amp", '', $go_to_url); + wp_safe_redirect( $go_to_url ); + 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){ + $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); + $go_to_url = str_replace("?amp", '', $go_to_url); + wp_safe_redirect( $go_to_url ); + exit; + } + } + } + //Auto redirect /amp to ?amp when 'Change End Point to ?amp' option is enabled #2480 if ( ampforwp_is_amp_endpoint() && true == ampforwp_get_setting('amp-core-end-point') ){ $current_url = $endpoint = $new_url = ''; diff --git a/templates/features.php b/templates/features.php index 1c0a36884..518d982de 100644 --- a/templates/features.php +++ b/templates/features.php @@ -209,6 +209,23 @@ function ampforwp_amphtml_generator(){ if( is_front_page() && ! ampforwp_get_setting('ampforwp-homepage-on-off-support') ) { return; } + + if(is_category()){ + $term_id = get_queried_object()->term_id; + $category_status = ampforwp_get_category_meta($term_id,'status'); + if($category_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){ + return; + } + } + + // Skip this condition for woocommerce product archive and shop pages. if( function_exists('amp_woocommerce_pro_add_woocommerce_support') && (function_exists('is_product_category') && !is_product_category()) && (function_exists('is_product_tag') && !is_product_tag()) && (function_exists('is_shop') && !is_shop() ) ){ if( is_archive() && ( !ampforwp_get_setting('ampforwp-archive-support') || ( is_category() && !ampforwp_get_setting('ampforwp-archive-support-cat') ) || ( is_tag() && !ampforwp_get_setting('ampforwp-archive-support-tag') ))){ @@ -732,6 +749,74 @@ 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_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); + } +} +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 ) { +?> +
Show/Hide in AMP.
+