Skip to content

Commit

Permalink
Merge branch 'dev/6.1.0' into 516-fix/enhance-data-type-and-operator-…
Browse files Browse the repository at this point in the history
…dropdowns
  • Loading branch information
sudar committed Dec 3, 2019
2 parents 931ffdb + feb0e3e commit 75ef073
Show file tree
Hide file tree
Showing 10 changed files with 470 additions and 20 deletions.
1 change: 0 additions & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ enabled:
- function_typehint_space
- hash_to_slash_comment
- include
- long_array_syntax
- lowercase_cast
- lowercase_constants
- lowercase_keywords
Expand Down
83 changes: 83 additions & 0 deletions assets/js/src/delete-term-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Add support for deleting term meta.
*
* @since 6.1.0
*/

/*global ajaxurl*/
jQuery( document ).ready( function () {
var $taxonomyDropdown = jQuery( 'select[name="smbd_term_meta_taxonomy"]' ),
$termDropdown = jQuery( 'select[name="smbd_term_meta_term"]' ),
$metaDropdown = jQuery( 'select[name="smbd_term_meta_key"]' );

/**
* Load Taxonomy terms when taxonomy changes.
*/
function loadTerms() {
var selectedTaxonomy = $taxonomyDropdown.val();

jQuery( '.enhanced-terms-dropdown' ).select2(
{
ajax: {
url: ajaxurl,
dataType: 'json',
delay: 250,
data: function ( params ) {
return {
q: params.term,
taxonomy: selectedTaxonomy,
action: 'bd_load_taxonomy_terms'
};
},
processResults: function ( data ) {
var options = [];

if ( data ) {
jQuery.each(
data, function ( index, dataPair ) {
options.push( { id: dataPair[ 0 ], text: dataPair[ 1 ] } );
}
);
}

return {
results: options
};
},
cache: true
},
minimumInputLength: 2, // the minimum of symbols to input before perform a search.
width: '300px'
}
);
}

/**
* Load Term metas when the term changes.
*/
function loadTermMetas() {
var selectedTermId = $termDropdown.val();

jQuery.ajax( {
type: "GET",
dataType: "json",
url: ajaxurl,
data: {
term_id: selectedTermId,
action: 'bd_load_term_metas'
},
success: function ( data ) {
$metaDropdown.empty();
$metaDropdown.select2( { data: data } );
},
error: function () {
alert( "We are not able to load the term meta" );
}
} );
}

$taxonomyDropdown.change( loadTerms );
loadTerms();

$termDropdown.change( loadTermMetas );
} );
5 changes: 4 additions & 1 deletion assets/js/src/select2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Add select2 functionality..
*/

/*global ajaxurl*/
jQuery( document ).ready( function () {
/**
Expand Down Expand Up @@ -30,6 +31,8 @@ jQuery( document ).ready( function () {

/**
* Enable AJAX for Taxonomy Select2.
*
* TODO: See if this is used anywhere. Else remove it.
*/
jQuery( '.select2-taxonomy-ajax' ).select2( {
ajax: {
Expand Down Expand Up @@ -58,7 +61,7 @@ jQuery( document ).ready( function () {
},
cache: true
},
minimumInputLength: 2, // the minimum of symbols to input before perform a search
minimumInputLength: 2, // the minimum of symbols to input before perform a search.
width: '300px'
} );
} );
1 change: 0 additions & 1 deletion bulk-delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
* Version 6.1.0 of the Bulk Delete plugin dropped support for PHP < 5.2.
* If you are still struck with PHP < 5.6 and can't update, then use v6.0.2 of the plugin.
* But note that some add-ons may not work.
*
* @since 6.0.0 Dropped support for PHP 5.2
* @since 6.1.0 Dropped support for < PHP 5.6
*/
Expand Down
16 changes: 9 additions & 7 deletions include/Core/Base/Mixin/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,15 @@ protected function render_taxonomy_dropdown() {
<?php endforeach; ?>
</optgroup>

<optgroup label="<?php esc_attr_e( 'Custom Taxonomies', 'bulk-delete' ); ?>">
<?php foreach ( $custom_taxonomies as $taxonomy ) : ?>
<option value="<?php echo esc_attr( $taxonomy->name ); ?>">
<?php echo esc_html( $taxonomy->label . ' (' . $taxonomy->name . ')' ); ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php if ( ! empty( $custom_taxonomies ) ): ?>
<optgroup label="<?php esc_attr_e( 'Custom Taxonomies', 'bulk-delete' ); ?>">
<?php foreach ( $custom_taxonomies as $taxonomy ) : ?>
<option value="<?php echo esc_attr( $taxonomy->name ); ?>">
<?php echo esc_html( $taxonomy->label . ' (' . $taxonomy->name . ')' ); ?>
</option>
<?php endforeach; ?>
</optgroup>
<?php endif; ?>
</select>
<?php
}
Expand Down
2 changes: 2 additions & 0 deletions include/Core/BulkDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use BulkWP\BulkDelete\Core\Metas\DeleteMetasPage;
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteCommentMetaModule;
use BulkWP\BulkDelete\Core\Metas\Modules\DeletePostMetaModule;
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteTermMetaModule;
use BulkWP\BulkDelete\Core\Metas\Modules\DeleteUserMetaModule;
use BulkWP\BulkDelete\Core\Pages\DeletePagesPage;
use BulkWP\BulkDelete\Core\Pages\Modules\DeletePagesByStatusModule;
Expand Down Expand Up @@ -507,6 +508,7 @@ private function get_delete_metas_admin_page() {

$metas_page->add_module( new DeletePostMetaModule() );
$metas_page->add_module( new DeleteUserMetaModule() );
$metas_page->add_module( new DeleteTermMetaModule() );
$metas_page->add_module( new DeleteCommentMetaModule() );

/**
Expand Down
46 changes: 43 additions & 3 deletions include/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function load() {

add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_get_request_nonce' ), 10, 2 );

add_action( 'wp_ajax_bd_load_taxonomy_term', array( $this, 'load_taxonomy_term' ) );
add_action( 'wp_ajax_bd_load_taxonomy_terms', array( $this, 'load_taxonomy_terms' ) );
add_action( 'wp_ajax_bd_load_term_metas', array( $this, 'load_term_metas' ) );

add_filter( 'bd_help_tooltip', 'bd_generate_help_tooltip', 10, 2 );
add_filter( 'plugin_action_links', array( $this, 'filter_plugin_action_links' ), 10, 2 );
Expand Down Expand Up @@ -131,11 +132,11 @@ public function verify_get_request_nonce( $result, $action ) {
}

/**
* Ajax call back function for getting taxonomies to load select2 options.
* Ajax call back function to load taxonomy terms by taxonomy.
*
* @since 6.0.0
*/
public function load_taxonomy_term() {
public function load_taxonomy_terms() {
$response = array();

$taxonomy = sanitize_text_field( $_GET['taxonomy'] );
Expand All @@ -161,6 +162,45 @@ public function load_taxonomy_term() {
die;
}

/**
* Ajax call back function to load term meta by term.
*
* @since 6.1.0
*/
public function load_term_metas() {
$response = [
[
'id' => 0,
'text' => __( 'No term meta found', 'bulk-delete' ),
],
];

$term_id = absint( $_GET['term_id'] );

$term_metas = get_term_meta( $term_id );

if ( is_array( $term_metas ) && ! empty( $term_metas ) ) {
$keys = array_keys( $term_metas );

$response = [
[
'id' => 0,
'text' => __( 'Select Term Meta Key', 'bulk-delete' ),
],
];

foreach ( $keys as $key ) {
$response[] = [
'id' => $key,
'text' => $key,
];
}
}

echo wp_json_encode( $response );
die;
}

/**
* Adds the settings link in the Plugin page.
*
Expand Down
146 changes: 146 additions & 0 deletions include/Core/Metas/Modules/DeleteTermMetaModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
namespace BulkWP\BulkDelete\Core\Metas\Modules;

use BulkWP\BulkDelete\Core\Metas\MetasModule;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
* Delete Term Meta.
*
* @since 6.1.0
*/
class DeleteTermMetaModule extends MetasModule {
/**
* Initialize the Module.
*/
protected function initialize() {
$this->field_slug = 'term_meta';
$this->meta_box_slug = 'bd-term-meta';
$this->action = 'delete_term_meta';
$this->cron_hook = 'do-bulk-delete-term-meta';
$this->messages = array(
'box_label' => __( 'Bulk Delete Term Meta', 'bulk-delete' ),
'scheduled' => __( 'Meta fields from the selected term with the selected criteria are scheduled for deletion.', 'bulk-delete' ),
'confirm_deletion' => __( 'Are you sure you want to delete all the term meta fields that match the selected criteria?', 'bulk-delete' ),
/* translators: 1 Number of term meta deleted */
'deleted_one' => __( 'Deleted %d term meta', 'bulk-delete' ),
/* translators: 1 Number of term meta deleted */
'deleted_multiple' => __( 'Deleted %d term metas', 'bulk-delete' ),
);
}

public function render() {
?>
<!-- Term Meta box start-->
<fieldset class="options">
<h4><?php _e( 'Select the taxonomy from which you want to delete term meta fields', 'bulk-delete' ); ?></h4>
<table class="optiontable">
<tr>
<td>
<?php $this->render_taxonomy_dropdown(); ?>
</td>
</tr>
</table>

<h4><?php _e( 'Select the taxonomy term from which you want to delete term meta fields', 'bulk-delete' ); ?></h4>
<table class="optiontable">
<tr>
<td>
<select class="enhanced-terms-dropdown" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_term">
<option value="0"><?php _e( 'Select Term', 'bulk-delete' ); ?></option>
</select>
</td>
</tr>
</table>

<h4><?php _e( 'Select the term meta that you want to delete', 'bulk-delete' ); ?></h4>
<table class="optiontable">
<tr>
<td>
<select class="enhanced-term-meta-dropdown" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_key">
<option value="0"><?php _e( 'Select Term Meta Key', 'bulk-delete' ); ?></option>
</select>

<?php $this->render_string_operators_dropdown( 'string', array( '=', '!=' ) ); ?>

<input type="text" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_value" placeholder="<?php echo esc_attr_e( 'Term Meta Value', 'bulk-delete' ); ?>">
</td>
</tr>
</table>

<?php
/**
* Add more fields to the delete term meta field form.
* This hook can be used to add more fields to the delete term meta field form.
*
* @since 6.1.0
*/
do_action( 'bd_delete_term_meta_form' );
?>

</fieldset>

<?php $this->render_submit_button(); ?>

<!-- Term Meta box end-->
<?php
}

/**
* Convert user input to bulkwp standard.
*
* @param array $request Request array.
* @param array $options User options.
*
* @return array User options.
*/
protected function convert_user_input_to_options( $request, $options ) {
$options['term_id'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_term', '' ) );

$options['term_meta_key'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_key', '' ) );
$options['term_meta_value'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_value', '' ) );

$options['term_meta_operator'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator', '' ) );

return $options;
}

public function do_delete( $options ) {
$count = 0;

if ( '=' === $options['term_meta_operator'] ) {
$is_deleted = delete_term_meta( $options['term_id'], $options['term_meta_key'], $options['term_meta_value'] );
if ( $is_deleted ) {
$count++;
}
} elseif ( '!=' === $options['term_meta_operator'] ) {
$term_values = get_term_meta( $options['term_id'], $options['term_meta_key'] );
foreach ( $term_values as $term_value ) {
if ( $options['term_meta_value'] !== $term_value ) {
$is_deleted = delete_term_meta( $options['term_id'], $options['term_meta_key'], $term_value );
if ( $is_deleted ) {
$count++;
}
}
}
}

return $count;
}

/**
* Append any module specific options to JS array.
*
* This function will be overridden by the child classes.
*
* @param array $js_array JavaScript Array.
*
* @return array Modified JavaScript Array
*/
public function append_to_js_array( $js_array ) {
$js_array['validators'][ $this->action ] = 'noValidation';

return $js_array;
}
}
Loading

0 comments on commit 75ef073

Please sign in to comment.