Skip to content

Commit

Permalink
#4326 - Tag and Custom Taxonomy support
Browse files Browse the repository at this point in the history
  • Loading branch information
WasimM3 committed Apr 2, 2020
1 parent 5861948 commit dc924c0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 54 deletions.
6 changes: 3 additions & 3 deletions includes/admin-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
23 changes: 14 additions & 9 deletions includes/features/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
13 changes: 6 additions & 7 deletions includes/redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
90 changes: 55 additions & 35 deletions templates/features.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -749,68 +748,89 @@ 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';
}
}
?>
<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">
<label for="show_amp_taxonomy">Show/Hide in AMP</label>
<select name="amp_taxonomy" id="show_amp_taxonomy" 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.
<div id="amp-show-hide-tax" style="display: none;">
<input type="radio" value="hide-cat" name="hide_tax" checked=""> Hide this <?php echo esc_attr($label);?>, not the post(s) related to this <?php echo esc_attr($label) ?>.
<br />
<input type="radio" value="hide-cat-post" name="hide_cat"> Hide this category and the post(s) related to this category as well.
<input type="radio" value="hide-tax-post" name="hide_tax"> Hide this <?php echo esc_attr($label) ?> and the post(s) related to this <?php echo esc_attr($label) ?> as well.
</div>
<br>
</td>
<?php }else{
$term_data = ampforwp_get_category_meta($tag->term_id);
$term_data = ampforwp_get_taxonomy_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>
<th scope="row"><label for="show_amp_taxonomy">Show/Hide AMP</label></th>
<td>
<select name="show_amp_category" id="show_amp_category" class="postform">
<select name="amp_taxonomy" id="show_amp_taxonomy" 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.
<div id="amp-show-hide-tax" <?php if($visible=='show' || $visible==''){?>style="display: none;"<?php }?>>
<input type="radio" value="hide-cat" name="hide_tax" <?php if($visible_status=='hide-cat' || $visible_status==''){?> checked <?php }?>> Hide this <?php echo esc_attr($label);?>, not the post(s) related to this <?php echo esc_attr($label);?>.
<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.
<input type="radio" value="hide-tax-post" name="hide_tax" <?php if($visible_status=='hide-tax-post'){?> checked <?php }?>> Hide this <?php echo esc_attr($label);?> and the post(s) related to this <?php echo esc_attr($label);?> as well.
</div>
</td>
<?php }?>
Expand Down

0 comments on commit dc924c0

Please sign in to comment.