Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Abstract...
File: ModelFactory.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Abstracts_ModelFactory
[3] Fix | Delete
*/
[4] Fix | Delete
class NF_Abstracts_ModelFactory
[5] Fix | Delete
{
[6] Fix | Delete
/**
[7] Fix | Delete
* Database Object
[8] Fix | Delete
*
[9] Fix | Delete
* @var
[10] Fix | Delete
*/
[11] Fix | Delete
protected $_db;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* The last set object.
[15] Fix | Delete
* Used to create context between two objects in a chain.
[16] Fix | Delete
*
[17] Fix | Delete
* @var object
[18] Fix | Delete
*/
[19] Fix | Delete
protected $_object;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Form
[23] Fix | Delete
*/
[24] Fix | Delete
protected $_form;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Fields
[28] Fix | Delete
*
[29] Fix | Delete
* An array of field model objects.
[30] Fix | Delete
*
[31] Fix | Delete
* @var array
[32] Fix | Delete
*/
[33] Fix | Delete
protected $_fields = array();
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Actions
[37] Fix | Delete
*
[38] Fix | Delete
* An array of action model objects.
[39] Fix | Delete
*
[40] Fix | Delete
* @var array
[41] Fix | Delete
*/
[42] Fix | Delete
protected $_actions = array();
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Objects
[46] Fix | Delete
*
[47] Fix | Delete
* An array of generic model objects.
[48] Fix | Delete
*
[49] Fix | Delete
* @var array
[50] Fix | Delete
*/
[51] Fix | Delete
protected $_objects = array();
[52] Fix | Delete
[53] Fix | Delete
//-----------------------------------------------------
[54] Fix | Delete
// Public Methods
[55] Fix | Delete
//-----------------------------------------------------
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* NF_Abstracts_ModelFactory constructor.
[59] Fix | Delete
* @param $db
[60] Fix | Delete
* @param $id
[61] Fix | Delete
*/
[62] Fix | Delete
public function __construct( $db, $id )
[63] Fix | Delete
{
[64] Fix | Delete
$this->_db = $db;
[65] Fix | Delete
[66] Fix | Delete
$this->_object = $this->_form = new NF_Database_Models_Form( $this->_db, $id );
[67] Fix | Delete
[68] Fix | Delete
if(WPN_Helper::use_cache()) {
[69] Fix | Delete
$form_cache = WPN_Helper::get_nf_cache( $id );
[70] Fix | Delete
[71] Fix | Delete
if( $form_cache && isset ( $form_cache[ 'settings' ] ) ){
[72] Fix | Delete
$this->_object->update_settings( $form_cache[ 'settings' ] );
[73] Fix | Delete
}
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
return $this;
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* PHP MAGIC method for catching undefined methods.
[81] Fix | Delete
* Use this as a passthrough for methods of the form model.
[82] Fix | Delete
*
[83] Fix | Delete
* @param $method (String) The name of the called method.
[84] Fix | Delete
* @param $args (Array) The arguments supplied to the method.
[85] Fix | Delete
*
[86] Fix | Delete
* @return (Mixed) Will match the return type of the supplied method.
[87] Fix | Delete
*
[88] Fix | Delete
* @since UPDATE_VERSION_ON_MERGE
[89] Fix | Delete
*/
[90] Fix | Delete
public function __call( $method, $args ) {
[91] Fix | Delete
// If this is a method of the form model...
[92] Fix | Delete
if ( method_exists( $this->_object, $method ) ) {
[93] Fix | Delete
// Instantiate a reflector method.
[94] Fix | Delete
$mirror = new ReflectionMethod( get_class( $this->_object ), $method );
[95] Fix | Delete
// If the method is not private...
[96] Fix | Delete
if ( ! $mirror->isPrivate() ) {
[97] Fix | Delete
// If args were supplied...
[98] Fix | Delete
if ( ! empty( $args ) ) {
[99] Fix | Delete
// Pass them into the function.
[100] Fix | Delete
return $this->_object->{$method}( $args );
[101] Fix | Delete
}
[102] Fix | Delete
return $this->_object->{$method}();
[103] Fix | Delete
}
[104] Fix | Delete
}
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Returns the parent object set by the constructor for chained methods.
[109] Fix | Delete
*
[110] Fix | Delete
* @return object
[111] Fix | Delete
*/
[112] Fix | Delete
public function get()
[113] Fix | Delete
{
[114] Fix | Delete
$object = $this->_object;
[115] Fix | Delete
$this->_object = $this->_form;
[116] Fix | Delete
return $object;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
/**
[120] Fix | Delete
* Get Forms
[121] Fix | Delete
*
[122] Fix | Delete
* Returns an array of Form Model Objects.
[123] Fix | Delete
*
[124] Fix | Delete
* @param array $where
[125] Fix | Delete
* @return array|bool
[126] Fix | Delete
*/
[127] Fix | Delete
public function get_forms( array $where = array() )
[128] Fix | Delete
{
[129] Fix | Delete
if( 'form' != $this->_object->get_type() ) return FALSE;
[130] Fix | Delete
[131] Fix | Delete
return $this->_object->find( NULL, $where );
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
/**
[135] Fix | Delete
* Export Form
[136] Fix | Delete
*
[137] Fix | Delete
* A wrapper for the Form Model export method.
[138] Fix | Delete
*
[139] Fix | Delete
* @param bool|FALSE $return
[140] Fix | Delete
* @return array
[141] Fix | Delete
*/
[142] Fix | Delete
public function export_form( $return = FALSE )
[143] Fix | Delete
{
[144] Fix | Delete
$form_id = $this->_object->get_id();
[145] Fix | Delete
[146] Fix | Delete
return NF_Database_Models_Form::export( $form_id, $return );
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
/**
[150] Fix | Delete
* Import Form
[151] Fix | Delete
*
[152] Fix | Delete
* A wrapper for the Form Model import method.
[153] Fix | Delete
*
[154] Fix | Delete
* @param $import
[155] Fix | Delete
* @param $decode_utf8
[156] Fix | Delete
* @param $id
[157] Fix | Delete
* @param $is_conversion
[158] Fix | Delete
*/
[159] Fix | Delete
public function import_form( $import, $decode_utf8 = TRUE, $id = FALSE,
[160] Fix | Delete
$is_conversion = FALSE )
[161] Fix | Delete
{
[162] Fix | Delete
[163] Fix | Delete
if( ! is_array( $import ) ){
[164] Fix | Delete
// Check to see if user turned off UTF-8 encoding
[165] Fix | Delete
if( $decode_utf8 ) {
[166] Fix | Delete
$data = WPN_Helper::utf8_decode( json_decode( WPN_Helper::json_cleanup( html_entity_decode( $import ) ), true ) );
[167] Fix | Delete
} else {
[168] Fix | Delete
$data = json_decode( WPN_Helper::json_cleanup( html_entity_decode( $import ) ), true );
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
if( ! is_array( $data ) ) {
[172] Fix | Delete
if( $decode_utf8 ) {
[173] Fix | Delete
$data = WPN_Helper::utf8_decode( json_decode( WPN_Helper::json_cleanup( $import ), true ) );
[174] Fix | Delete
} else {
[175] Fix | Delete
$data = json_decode( WPN_Helper::json_cleanup( $import ), true );
[176] Fix | Delete
}
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
if( ! is_array( $data ) ){
[180] Fix | Delete
$data = WPN_Helper::maybe_unserialize( $import );
[181] Fix | Delete
[182] Fix | Delete
if( ! is_array( $data ) ){
[183] Fix | Delete
return false;
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
$import = $data;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
return NF_Database_Models_Form::import( $import, $id, $is_conversion );
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
/*
[193] Fix | Delete
* FIELDS
[194] Fix | Delete
*/
[195] Fix | Delete
[196] Fix | Delete
/**
[197] Fix | Delete
* Sets the parent object for chained methods as a Field.
[198] Fix | Delete
*
[199] Fix | Delete
* @param string $id
[200] Fix | Delete
* @return $this
[201] Fix | Delete
*/
[202] Fix | Delete
public function field( $id = '' )
[203] Fix | Delete
{
[204] Fix | Delete
$form_id = $this->_object->get_id();
[205] Fix | Delete
[206] Fix | Delete
$this->_object = new NF_Database_Models_Field( $this->_db, $id, $form_id );
[207] Fix | Delete
[208] Fix | Delete
return $this;
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
/**
[212] Fix | Delete
* Returns a field object.
[213] Fix | Delete
*
[214] Fix | Delete
* @param $id
[215] Fix | Delete
* @return NF_Database_Models_Field
[216] Fix | Delete
*/
[217] Fix | Delete
public function get_field( $id )
[218] Fix | Delete
{
[219] Fix | Delete
if( isset( $this->_fields[ $id ] ) ){
[220] Fix | Delete
return $this->_fields[ $id ];
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/* MISSING FORM ID FALLBACK */
[224] Fix | Delete
/*
[225] Fix | Delete
if( ! $form_id ){
[226] Fix | Delete
$form_id = $wpdb->get_var( $wpdb->prepare(
[227] Fix | Delete
"SELECT parent_id from {$wpdb->prefix}nf3_fields WHERE id = %d", $id
[228] Fix | Delete
));
[229] Fix | Delete
$this->_object = $this->_form = new NF_Database_Models_Form( $this->_db, $id );
[230] Fix | Delete
}
[231] Fix | Delete
*/
[232] Fix | Delete
[233] Fix | Delete
if( ! $this->_fields ){
[234] Fix | Delete
$this->get_fields();
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
if( ! isset( $this->_fields[ $id ] ) ){
[238] Fix | Delete
$form_id = $this->_object->get_id();
[239] Fix | Delete
$this->_fields[ $id ] = new NF_Database_Models_Field( $this->_db, $id, $form_id );
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
return $this->_fields[ $id ];
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Returns an array of field objects for the set form (object).
[247] Fix | Delete
*
[248] Fix | Delete
* @param array $where
[249] Fix | Delete
* @param bool|FALSE $fresh
[250] Fix | Delete
* @return array
[251] Fix | Delete
*/
[252] Fix | Delete
public function get_fields( $where = array(), $fresh = FALSE)
[253] Fix | Delete
{
[254] Fix | Delete
[255] Fix | Delete
$field_by_key = array();
[256] Fix | Delete
[257] Fix | Delete
$form_id = $this->_object->get_id();
[258] Fix | Delete
if ( ! $form_id && empty( $where ) ) {
[259] Fix | Delete
$this->_fields = array();
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
if( $where || $fresh || ! $this->_fields ){
[263] Fix | Delete
[264] Fix | Delete
// @TODO: Remove the second half of this IF block and replace it with a required update check.
[265] Fix | Delete
if(0 !== $form_id && (WPN_Helper::use_cache() || 1 == $form_id)) {
[266] Fix | Delete
$form_cache = WPN_Helper::get_nf_cache( $form_id );
[267] Fix | Delete
} else {
[268] Fix | Delete
$form_cache = false;
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
if( ! $form_cache ) {
[272] Fix | Delete
$model_shell = new NF_Database_Models_Field($this->_db, 0);
[273] Fix | Delete
[274] Fix | Delete
$fields = $model_shell->find($form_id, $where);
[275] Fix | Delete
[276] Fix | Delete
foreach ($fields as $field) {
[277] Fix | Delete
$this->_fields[$field->get_id()] = $field;
[278] Fix | Delete
$field_by_key[ $field->get_setting( 'key' ) ] = $field;
[279] Fix | Delete
}
[280] Fix | Delete
} else {
[281] Fix | Delete
foreach( $form_cache[ 'fields' ] as $cached_field ){
[282] Fix | Delete
$field = new NF_Database_Models_Field( $this->_db, $cached_field[ 'id' ], $form_id );
[283] Fix | Delete
$field->update_settings( $cached_field[ 'settings' ] );
[284] Fix | Delete
$this->_fields[$field->get_id()] = $field;
[285] Fix | Delete
$field_by_key[ $field->get_setting( 'key' ) ] = $field;
[286] Fix | Delete
}
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
/*
[290] Fix | Delete
* If a filter is registered to modify field order, then use that filter.
[291] Fix | Delete
* If not, then usort??.
[292] Fix | Delete
*/
[293] Fix | Delete
$order = apply_filters( 'ninja_forms_get_fields_sorted', array(), $this->_fields, $field_by_key, $form_id );
[294] Fix | Delete
[295] Fix | Delete
if ( ! empty( $order ) ) {
[296] Fix | Delete
$this->_fields = $order;
[297] Fix | Delete
}
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
/*
[301] Fix | Delete
* Broke the sub edit screen order when I have this enabled.
[302] Fix | Delete
*/
[303] Fix | Delete
// usort( $this->_fields, "NF_Abstracts_Field::sort_by_order" );
[304] Fix | Delete
[305] Fix | Delete
return $this->_fields;
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
/**
[309] Fix | Delete
* Import Field
[310] Fix | Delete
*
[311] Fix | Delete
* A wrapper for the Form Model import method.
[312] Fix | Delete
*
[313] Fix | Delete
* @param $import
[314] Fix | Delete
*/
[315] Fix | Delete
public function import_field( $settings, $field_id = '', $is_conversion = FALSE )
[316] Fix | Delete
{
[317] Fix | Delete
$settings = maybe_unserialize( $settings );
[318] Fix | Delete
NF_Database_Models_Field::import( $settings, $field_id, $is_conversion );
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
[322] Fix | Delete
/*
[323] Fix | Delete
* ACTIONS
[324] Fix | Delete
*/
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Sets the parent object for chained methods as an Action.
[328] Fix | Delete
*
[329] Fix | Delete
* @param string $id
[330] Fix | Delete
* @return $this
[331] Fix | Delete
*/
[332] Fix | Delete
public function action( $id ='' )
[333] Fix | Delete
{
[334] Fix | Delete
$form_id = $this->_object->get_id();
[335] Fix | Delete
[336] Fix | Delete
$this->_object = new NF_Database_Models_Action( $this->_db, $id, $form_id );
[337] Fix | Delete
[338] Fix | Delete
return $this;
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
/**
[342] Fix | Delete
* Returns an action object.
[343] Fix | Delete
*
[344] Fix | Delete
* @param $id
[345] Fix | Delete
* @return NF_Database_Models_Action
[346] Fix | Delete
*/
[347] Fix | Delete
public function get_action( $id )
[348] Fix | Delete
{
[349] Fix | Delete
$form_id = $this->_object->get_id();
[350] Fix | Delete
[351] Fix | Delete
return $this->_actions[ $id ] = new NF_Database_Models_Action( $this->_db, $id, $form_id );
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
/**
[355] Fix | Delete
* Returns an array of action objects for the set form (object).
[356] Fix | Delete
*
[357] Fix | Delete
* @param array $where
[358] Fix | Delete
* @param bool|FALSE $fresh
[359] Fix | Delete
* @return array
[360] Fix | Delete
*/
[361] Fix | Delete
public function get_actions( $where = array(), $fresh = FALSE)
[362] Fix | Delete
{
[363] Fix | Delete
if( $where || $fresh || ! $this->_actions ){
[364] Fix | Delete
[365] Fix | Delete
$form_id = $this->_object->get_id();
[366] Fix | Delete
[367] Fix | Delete
$model_shell = new NF_Database_Models_Action($this->_db, 0);
[368] Fix | Delete
[369] Fix | Delete
$actions = $model_shell->find($form_id, $where);
[370] Fix | Delete
[371] Fix | Delete
foreach ($actions as $action) {
[372] Fix | Delete
$action->get_setting( 'type' ); // Pre-load the type of action for usort()
[373] Fix | Delete
$this->_actions[$action->get_id()] = $action;
[374] Fix | Delete
}
[375] Fix | Delete
}
[376] Fix | Delete
[377] Fix | Delete
usort( $this->_actions, 'NF_Abstracts_Action::sort_actions' );
[378] Fix | Delete
[379] Fix | Delete
return $this->_actions;
[380] Fix | Delete
}
[381] Fix | Delete
[382] Fix | Delete
/*
[383] Fix | Delete
* OBJECTS
[384] Fix | Delete
*/
[385] Fix | Delete
[386] Fix | Delete
/**
[387] Fix | Delete
* Sets the parent object for chained methods as an Object.
[388] Fix | Delete
*
[389] Fix | Delete
* @param string $id
[390] Fix | Delete
* @return $this
[391] Fix | Delete
*/
[392] Fix | Delete
public function object( $id = '' )
[393] Fix | Delete
{
[394] Fix | Delete
$parent_id = $this->_object->get_id();
[395] Fix | Delete
$parent_type = $this->_object->get_type();
[396] Fix | Delete
[397] Fix | Delete
$this->_object = new NF_Database_Models_Object( $this->_db, $id, $parent_id, $parent_type );
[398] Fix | Delete
[399] Fix | Delete
return $this;
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
/**
[403] Fix | Delete
* Returns an object.
[404] Fix | Delete
*
[405] Fix | Delete
* @param $id
[406] Fix | Delete
* @return NF_Database_Models_Object
[407] Fix | Delete
*/
[408] Fix | Delete
public function get_object( $id )
[409] Fix | Delete
{
[410] Fix | Delete
return $this->_objects[ $id ] = new NF_Database_Models_Object( $this->_db, $id );
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
/**
[414] Fix | Delete
* Returns an array of objects for the set object.
[415] Fix | Delete
*
[416] Fix | Delete
* @param array $where
[417] Fix | Delete
* @param bool|FALSE $fresh
[418] Fix | Delete
* @return array
[419] Fix | Delete
*/
[420] Fix | Delete
public function get_objects( $where = array(), $fresh = FALSE)
[421] Fix | Delete
{
[422] Fix | Delete
if( $where || $fresh || ! $this->_objects ){
[423] Fix | Delete
[424] Fix | Delete
$form_id = $this->_object->get_id();
[425] Fix | Delete
[426] Fix | Delete
$model_shell = new NF_Database_Models_Object( $this->_db, 0 );
[427] Fix | Delete
[428] Fix | Delete
$objects = $model_shell->find( $form_id, $where );
[429] Fix | Delete
[430] Fix | Delete
foreach( $objects as $object ){
[431] Fix | Delete
$this->_objects[ $object->get_id() ] = $object;
[432] Fix | Delete
}
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
return $this->_objects;
[436] Fix | Delete
}
[437] Fix | Delete
[438] Fix | Delete
/*
[439] Fix | Delete
* SUBMISSIONS
[440] Fix | Delete
*/
[441] Fix | Delete
[442] Fix | Delete
/**
[443] Fix | Delete
* Submission
[444] Fix | Delete
*
[445] Fix | Delete
* Returns a single submission by ID,
[446] Fix | Delete
* or an empty submission.
[447] Fix | Delete
*
[448] Fix | Delete
* @param string $id
[449] Fix | Delete
* @return $this
[450] Fix | Delete
*/
[451] Fix | Delete
public function sub( $id = '' )
[452] Fix | Delete
{
[453] Fix | Delete
$form_id = $this->_object->get_id();
[454] Fix | Delete
[455] Fix | Delete
$this->_object = new NF_Database_Models_Submission( $id, $form_id );
[456] Fix | Delete
[457] Fix | Delete
return $this;
[458] Fix | Delete
}
[459] Fix | Delete
[460] Fix | Delete
/**
[461] Fix | Delete
* Get Submission
[462] Fix | Delete
*
[463] Fix | Delete
* Returns a single submission by ID.
[464] Fix | Delete
*
[465] Fix | Delete
* @param $id
[466] Fix | Delete
* @return NF_Database_Models_Submission
[467] Fix | Delete
*/
[468] Fix | Delete
public function get_sub( $id )
[469] Fix | Delete
{
[470] Fix | Delete
$parent_id = $this->_object->get_id();
[471] Fix | Delete
[472] Fix | Delete
return $this->_objects[ $id ] = new NF_Database_Models_Submission( $id, $parent_id );
[473] Fix | Delete
}
[474] Fix | Delete
[475] Fix | Delete
/**
[476] Fix | Delete
* Get Submissions
[477] Fix | Delete
*
[478] Fix | Delete
* Returns an array of Submission Model Objects.
[479] Fix | Delete
*
[480] Fix | Delete
* @param array $where
[481] Fix | Delete
* @param bool|FALSE $fresh
[482] Fix | Delete
* @return array
[483] Fix | Delete
*/
[484] Fix | Delete
public function get_subs( $where = array(), $fresh = FALSE, $sub_ids = array() )
[485] Fix | Delete
{
[486] Fix | Delete
if( $where || $fresh || $sub_ids || ! $this->_objects ){
[487] Fix | Delete
[488] Fix | Delete
$form_id = $this->_object->get_id();
[489] Fix | Delete
[490] Fix | Delete
$model_shell = new NF_Database_Models_Submission( 0 );
[491] Fix | Delete
[492] Fix | Delete
$objects = $model_shell->find( $form_id, $where, $sub_ids );
[493] Fix | Delete
[494] Fix | Delete
foreach( $objects as $object ){
[495] Fix | Delete
$this->_objects[ $object->get_id() ] = $object;
[496] Fix | Delete
}
[497] Fix | Delete
}
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function