Created
February 6, 2018 09:29
-
-
Save ThemeCatcher/1f75e96b3fc34e16aa9eeca44edf070f to your computer and use it in GitHub Desktop.
Max form submissions for Quform v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Quform Max Submissions | |
* Description: Allows you to specify a maximum number of form submissions for Quform v2 forms. | |
* Version: 1.0.0 | |
* Author: ThemeCatcher | |
* Author URI: https://www.themecatcher.net/ | |
*/ | |
class Quform_Max_Submissions | |
{ | |
const FIELD_NAME = 'max_number_of_submissions'; | |
public function __construct() | |
{ | |
add_action('quform_post_process', array($this, 'disableFormIfLimitReached'), 10, 2); | |
add_shortcode('quform_remaining_entries', array($this, 'quformRemainingEntriesShortcode')); | |
add_shortcode('quform_disabled_form', array($this, 'quformDisabledFormShortcode')); | |
} | |
/** | |
* @param array $result | |
* @param Quform_Form $form | |
* @return array | |
*/ | |
public function disableFormIfLimitReached(array $result, Quform_Form $form) | |
{ | |
$max = $this->getMaxSubmissions($form); | |
if ($max <= 0) { | |
return $result; | |
} | |
/** @var Quform_Repository $repository */ | |
$repository = Quform::getService('repository'); | |
if ($repository->getEntryCount($form->getId()) >= $max) { | |
$repository->deactivateForms(array($form->getId())); | |
} | |
return $result; | |
} | |
/** | |
* Display the number of remaining entries for a form | |
* | |
* Usage: [quform_remaining_entries id="1"] | |
* | |
* @param array $atts Attributes containing the form as id. | |
* @return int|string The number of max entries for the given form. | |
*/ | |
public function quformRemainingEntriesShortcode($atts) | |
{ | |
if ( ! class_exists('Quform')) { | |
return '<p>Quform must be installed and activated to use this shortcode.</p>'; | |
} | |
$atts = shortcode_atts(array( | |
'id' => -1, | |
), $atts); | |
$atts['id'] = (int) $atts['id']; | |
if ($atts['id'] < 1) { | |
return '<p>Please select a valid Quform form.</p>'; | |
} | |
/** @var Quform_Repository $repository */ | |
$repository = Quform::getService('repository'); | |
$config = $repository->getConfig($atts['id']); | |
if ( ! is_array($config)) { | |
return '<p>Please select a valid Quform form.</p>'; | |
} | |
/** @var Quform_Form_Factory $formFactory */ | |
$formFactory = Quform::getService('formFactory'); | |
$form = $formFactory->create($config); | |
$max = $this->getMaxSubmissions($form); | |
if ($max < 0) { | |
return '∞'; | |
} | |
return max($max - $repository->getEntryCount($form->getId()), 0); | |
} | |
/** | |
* Display the shortcode content if the form is not active | |
* | |
* Usage: [quform_disabled_form id="1"]The form is disabled.[/quform_disabled_form] | |
* | |
* @param array $atts Attributes containing the form as id. | |
* @param string|null $content The content will be shown, if the form is disabled. | |
* @return string The message if the form is disabled. | |
*/ | |
public function quformDisabledFormShortcode($atts, $content = null) | |
{ | |
if ( ! class_exists('Quform')) { | |
return '<p>Quform must be installed and activated to use this shortcode.</p>'; | |
} | |
$atts = shortcode_atts(array( | |
'id' => -1, | |
), $atts); | |
$atts['id'] = (int) $atts['id']; | |
if ($atts['id'] < 1) { | |
return '<p>Please select a valid Quform form.</p>'; | |
} | |
/** @var Quform_Repository $repository */ | |
$repository = Quform::getService('repository'); | |
$config = $repository->getConfig($atts['id']); | |
if ( ! is_array($config)) { | |
return '<p>Please select a valid Quform form.</p>'; | |
} | |
/** @var Quform_Form_Factory $formFactory */ | |
$formFactory = Quform::getService('formFactory'); | |
$form = $formFactory->create($config); | |
if ($form->isActive()) { | |
return ''; | |
} | |
return $content == null ? '<p>The form is disabled.</p>' : $content; | |
} | |
/** | |
* @param Quform_Form $form The form | |
* @return int The max number of submissions or -1 if infinite. | |
*/ | |
private function getMaxSubmissions(Quform_Form $form) | |
{ | |
foreach ($form->getRecursiveIterator() as $element) { | |
if ($element instanceof Quform_Element_Hidden && $element->config('label') == self::FIELD_NAME) { | |
return intval($element->getDefaultValue()); | |
} | |
} | |
return -1; | |
} | |
} | |
new Quform_Max_Submissions(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment