Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gravity Wiz // Gravity Perks // GP Limit Choices // Shortcodes
<?php
/**
* Gravity Wiz // Gravity Perks // GP Limit Choices // Shortcodes
*
* Provides several shortcodes providing the ability to display the number of times a choice has been selected,
* the total number of times a choice can be selected, and the remaining number of times a choice can be selected.
*
* @version 1.0
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2014 Gravity Wiz
*/
class GPLC_Shortcode {
private static $instance = null;
public static function get_instance() {
if( null == self::$instance )
self::$instance = new self;
return self::$instance;
}
private function __construct() {
if( ! class_exists( 'GFForms' ) || ! class_exists( 'GWLimitChoices' ) )
return;
add_shortcode( 'gplimitchoices', array( $this, 'do_shortcode' ) );
}
public function do_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'action' => false, // supports "limit", "available",
'form_id' => false,
'field_id' => false,
'choice' => false
), $atts ) );
if( ! $action )
$action = 'available';
$return = '';
switch( $action ) {
case 'count':
$return = $this->get_choice_count( $form_id, $field_id, $choice );
break;
case 'available':
$return = $this->get_choice_available( $form_id, $field_id, $choice );
break;
case 'limit':
$return = $this->get_choice_limit( $form_id, $field_id, $choice );
break;
}
return $return;
}
public function get_choice_available( $form_id, $field_id, $value ) {
$count = $this->get_choice_count( $form_id, $field_id, $value );
$limit = $this->get_choice_limit( $form_id, $field_id, $value );
if( $count === false || $limit === false )
return false;
$available = $limit - $count;
return $available >= 0 ? $available : 0;
}
public function get_choice_count( $form_id, $field_id, $value ) {
$choice = $this->get_choice( $form_id, $field_id, $value );
if( $choice === false || ! isset( $choice['limit'] ) )
return false;
$form = GFAPI::get_form( $form_id );
$field = GFFormsModel::get_field( $form, $field_id );
$count = GWLimitChoices::get_choice_count( $value, $field, $form_id );
return $count;
}
public function get_choice_limit( $form_id, $field_id, $value ) {
$choice = $this->get_choice( $form_id, $field_id, $value );
if( $choice === false || ! isset( $choice['limit'] ) )
return false;
return $choice['limit'];
}
public function get_choice( $form_id, $field_id, $value ) {
$form = GFAPI::get_form( $form_id );
$field = GFFormsModel::get_field( $form, $field_id );
foreach( $field['choices'] as $choice ) {
if( $choice['value'] == $value )
return $choice;
}
return false;
}
}
function gplc_shortcode() {
return GPLC_Shortcode::get_instance();
}
gplc_shortcode();
# Usage
## Shortcodes
// count: number of times this choice has been selected
# [gplimitchoices action="count" form_id="436" field_id="1" choice="The Big Option" /]
// limit: the total number of times this choice can be selected (specified via the UI)
# [gplimitchoices action="limit" form_id="436" field_id="1" choice="The Big Option" /]
// available: the remaining number of times this option can be selected
# [gplimitchoices action="available" form_id="436" field_id="1" choice="The Big Option" /]
// default: defaults to "available" if not "action" is provided
# [gplimitchoices form_id="436" field_id="1" choice="The Big Option" /]
## Functions
// count
# gplc_shortcode()->get_choice_count( $form_id, $field_id, $value );
// limit
# gplc_shortcode()->get_choice_limit( $form_id, $field_id, $value );
// available
# gplc_shortcode()->get_choice_available( $form_id, $field_id, $value );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment