<?php if ( ! defined( 'ABSPATH' ) ) exit;
* Handles the output of our form, as well as interacting with its settings.
* @subpackage Classes/Form
* @copyright Copyright (c) 2014, WPNINJAS
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @var settings - Form Settings
* @var fields - Form Fields
* @var fields - Fields List
var $field_keys = array();
* @var errors - Form errors
public function __construct( $form_id = '' ) {
if ( ! empty ( $form_id ) ) { // We've been passed a form id.
// Set our current form id.
$this->form_id = $form_id;
$this->settings = nf_get_form_settings( $form_id );
public function create( $defaults = array() ) {
$form_id = nf_insert_object( 'form' );
$date_updated = date( 'Y-m-d', current_time( 'timestamp' ) );
nf_update_object_meta( $form_id, 'date_updated', $date_updated );
foreach( $defaults as $meta_key => $meta_value ) {
nf_update_object_meta( $form_id, $meta_key, $meta_value );
// Add a single event hook that will check to see if this is an orphaned function.
$timestamp = strtotime( '+24 hours', time() );
wp_schedule_single_event( $timestamp, 'nf_maybe_delete_form', $args );
* Insert a field into our form
public function insert_field( $field_id ) {
return nf_add_relationship( $field_id, 'field', $this->form_id, 'form' );
public function update_fields() {
$this->fields = nf_get_fields_by_form_id( $this->form_id );
* Get one of our form settings.
* @return string $setting
public function get_setting( $setting, $bypass_cache = false ) {
return nf_get_object_meta_value( $this->form_id, 'last_sub' );
if ( isset ( $this->settings[ $setting ] ) ) {
return $this->settings[ $setting ];
* Update a form setting (this doesn't update anything in the database)
* Changes are only applied to this object.
public function update_setting( $setting, $value ) {
$this->settings[ $setting ] = $value;
nf_update_object_meta( $this->form_id, $setting, $value );
* Get all of our settings
* @return array $settings
public function get_all_settings() {
* Get all the submissions for this form
public function get_subs( $args = array() ) {
$args['form_id'] = $this->form_id;
return Ninja_Forms()->subs()->get( $args );
* Return a count of the submissions this form has had
public function sub_count( $args = array() ) {
return count( $this->get_subs( $args ) );
public function delete() {
nf_delete_object( $this->form_id );
// Delete any fields on this form.
$wpdb->query($wpdb->prepare( "DELETE FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE form_id = %d", $this->form_id ) );
* Delete the cached form object (transient)
public function dump_cache()
delete_transient( 'nf_form_' . $this->form_id );
* Deprecated wrapper for dump_cache()
public function dumpCache()