Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created June 22, 2016 18:13
Show Gist options
  • Select an option

  • Save kellenmace/55fe09bf913c958dec0d1d9ffd8575f0 to your computer and use it in GitHub Desktop.

Select an option

Save kellenmace/55fe09bf913c958dec0d1d9ffd8575f0 to your computer and use it in GitHub Desktop.

Revisions

  1. kellenmace created this gist Jun 22, 2016.
    93 changes: 93 additions & 0 deletions delete-transients-by-prefix.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    <?php

    /**
    * Searches the database for transients stored there that match a specific prefix.
    *
    * @author Brad Parbs, Kellen Mace
    * @param string $prefix prefix to search for.
    * @return array nested array response for wpdb->get_results.
    */
    function wds_campbell_search_database_for_transients_by_prefix( $prefix ) {

    global $wpdb;

    // Add our prefix after concating our prefix with the _transient prefix
    $prefix = $wpdb->esc_like( '_transient_' . $prefix . '_' );

    // Build up our SQL query
    $sql = "SELECT `option_name` FROM $wpdb->options WHERE `option_name` LIKE '%s'";

    // Execute our query
    $transients = $wpdb->get_results( $wpdb->prepare( $sql, $prefix . '%' ), ARRAY_A );

    // If if looks good, pass it back
    if ( $transients && ! is_wp_error( $transients ) ) {
    return $transients;
    }

    // Otherise return false
    return false;
    }

    /**
    * Expects a passed in multidimensional array of transient keys
    * array(
    * array( 'option_name' => '_transient_blah_blah' ),
    * array( 'option_name' => 'transient_another_one' ),
    * Can also pass in an array of transient names
    *
    * @author Brad Parbs, Kellen Mace
    * @param array|string $transients Nested array of transienets, keyed by option_name,
    * or array of names of transients.j
    * @return array Count of total vs deleted.
    */
    function wds_campbell_delete_transients_from_keys( $transients ) {

    if ( ! isset( $transients ) ) {
    return false;
    }

    // If we get a string key passed in, might as well use it correctly
    if ( is_string( $transients ) ) {
    $transients = array( array( 'option_name' => $transients ) );
    }

    // If its not an array, we can't do anything
    if ( ! is_array( $transients ) ) {
    return false;
    }

    $results = array();

    // Loop through our transients
    foreach ( $transients as $transient ) {

    if ( is_array( $transient ) ) {

    // If we have an array, grab the first element
    $transient = current( $transient );
    }

    // Remove that sucker
    $results[ $transient ] = delete_transient( str_replace( '_transient_', '', $transient ) );
    }

    // Return an array of total number, and number deleted
    return array(
    'total' => count( $results ),
    'deleted' => array_sum( $results ),
    );

    }

    /**
    * Delete brands select menu transients
    *
    * @author Kellen Mace
    */
    function wds_delete_brands_region_transients() {
    wds_campbell_delete_transients_from_keys( wds_campbell_search_database_for_transients_by_prefix( 'wds_campbell_brand_regions' ) );
    }
    add_action( 'create_region', 'wds_delete_brands_region_transients' );
    add_action( 'edit_region', 'wds_delete_brands_region_transients' );
    add_action( 'delete_region', 'wds_delete_brands_region_transients' );