GP Unique ID // Gravity Perks // Reset Shortcode
<?php
/**
* GP Unique ID // Gravity Perks // Reset Shortcode
*
* Provides a shortcode that generates a link that will reset the sequence of the specified Unique ID field.
*
* @version 1.0
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2016 Gravity Wiz
*
* Plugin Name: GP Unique ID - Reset Shortcode
* Plugin URI: http://gravitywiz.com
* Description: Provides a shortcode that generates a link that will reset the sequence of the specified Unique ID field.
* Author: David Smith
* Version: 1.0
* Author URI: http://gravitywiz.com
*/
class GW_Reset_Shortcode {
private static $instance = null;
public static function get_instance() {
if( self::$instance == null ) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
add_action( 'init', array( $this, 'init' ), 11 );
}
public function init() {
// make sure GF and GP Unique ID are running
if( ! class_exists( 'GFForms' ) || ! is_callable( 'gp_unique_id' ) ) {
return;
}
$this->parse_request();
add_shortcode( 'gpui_reset', array( $this, 'shortcode' ) );
}
public function parse_request() {
if( ! rgget( 'gpui' ) || rgget( 'action' ) != 'reset' || isset( $_GET['result'] ) ) {
return;
}
$form_id = rgget( 'form_id' );
$field_id = rgget( 'field_id' );
$number = rgget( 'number' ) ? rgget( 'number' ) : 0;
$result = gp_unique_id()->set_sequential_starting_number( $form_id, $field_id, $number );
wp_redirect( add_query_arg(
array( 'result' => $result || $result === 0 ),
remove_query_arg( array( 'form_id', 'field_id' ) )
) );
exit;
}
public function shortcode( $atts, $content = '' ) {
$atts = shortcode_atts( array(
'form' => false,
'field' => false,
'number' => 0,
'success' => __( 'Reset Successful!' ),
'error' => __( 'Reset Failed.' )
), $atts, 'gpui_reset' );
if( ! $atts['form'] || ! $atts['field'] ) {
$content = 'Error: Missing a "form" or "field" parameter on the shortcode.';
} else if( $this->get_reset_result() !== null ) {
$content = $this->get_reset_result() ? $atts['success'] : $atts['error'];
} else {
$text = ! empty( $content ) ? $content : __( 'Reset Sequence' );
$url = add_query_arg( array( 'gpui' => 1, 'action' => 'reset', 'form_id' => $atts['form'], 'field_id' => $atts['field'], 'number' => $atts['number'] ) );
$content = sprintf( '<a href="%s">%s</a>', $url, $text );
}
return $content;
}
public function get_reset_result() {
$result = null;
if( rgget( 'gpui' ) && rgget( 'action' ) == 'reset' && isset( $_GET['result'] ) ) {
$result = rgget( 'result' );
}
return $result;
}
}
function gw_reset_shortcode() {
return GW_Reset_Shortcode::get_instance();
}
gw_reset_shortcode();