Skip to content

Commit

Permalink
#4326 - Option to hide/show category and it's related post
Browse files Browse the repository at this point in the history
  • Loading branch information
WasimM3 committed Apr 1, 2020
1 parent a430c4f commit 4cec43a
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
8 changes: 8 additions & 0 deletions includes/admin-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<span class="search-wrapper"><input class="redux_field_search" name="" type="text" placeholder="Search the controls" style="display:none"/><span class="redux-amp-search-icon"><i class="dashicons-before dashicons-search"></i></span></span>');
Expand Down
21 changes: 21 additions & 0 deletions includes/features/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions includes/redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
85 changes: 85 additions & 0 deletions templates/features.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') ))){
Expand Down Expand Up @@ -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 ) {
?>
<tr class="form-field">
<?php if(!isset($tag->term_id)){?>
<th scope="row" valign="top"></th>
<td>
<div class="form-field term-parent-wrap">
<label for="show_amp_category">Show/Hide in AMP</label>
<select name="amp_category" id="show_amp_category" class="postform">
<option class="level-0" value="show">Show</option>
<option class="level-0" value="hide">Hide</option>
</select>
<p>Show/Hide in AMP.</p>
</div>
<div id="amp-show-hide-cat" style="display: none;">
<input type="radio" value="hide-cat" name="hide_cat" checked=""> Hide this category, not the post(s) related to this category.
<br />
<input type="radio" value="hide-cat-post" name="hide_cat"> Hide this category and the post(s) related to this category as well.
</div>
<br>
</td>
<?php }else{
$term_data = ampforwp_get_category_meta($tag->term_id);
$visible = '';
$visible_status = '';
if(isset($term_data['visible'])){
$visible = $term_data['visible'][0];
$visible_status = $term_data['visible_status'][0];
}
?>
<th scope="row"><label for="show_amp_category">Show/Hide AMP</label></th>
<td>
<select name="show_amp_category" id="show_amp_category" class="postform">
<option class="level-0" value="show" <?php if($visible=='show'){ echo "selected"; }?>>Show</option>
<option class="level-0" value="hide" <?php if($visible=='hide'){ echo "selected";} ?>>Hide</option>
</select><br />
<span class="description">Show/Hide in AMP.</span>
<div id="amp-show-hide-cat" <?php if($visible=='show'){?>style="display: none;"<?php }?>>
<input type="radio" value="hide-cat" name="hide_cat" <?php if($visible_status=='hide-cat'){?> checked <?php }?>> Hide this category, not the post(s) related to this category.
<br />
<input type="radio" value="hide-cat-post" name="hide_cat" <?php if($visible_status=='hide-cat-post'){?> checked <?php }?>> Hide this category and the post(s) related to this category as well.
</div>
</td>
<?php }?>
</tr>
<?php
}
/**
* Outputs the content of the meta box for AMP on-off on specific pages
*/
Expand Down

0 comments on commit 4cec43a

Please sign in to comment.