Edit File by line
/home/barbar84/www/wp-conte.../plugins/ninja-fo.../includes/Abstract...
File: Model.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Class NF_Abstracts_Model
[3] Fix | Delete
*/
[4] Fix | Delete
class NF_Abstracts_Model
[5] Fix | Delete
{
[6] Fix | Delete
/**
[7] Fix | Delete
* Database Object
[8] Fix | Delete
*
[9] Fix | Delete
* @var string
[10] Fix | Delete
*/
[11] Fix | Delete
protected $_db = '';
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* ID
[15] Fix | Delete
*
[16] Fix | Delete
* The ID is assigned after being saved to the database.
[17] Fix | Delete
*
[18] Fix | Delete
* @var int
[19] Fix | Delete
*/
[20] Fix | Delete
protected $_id = '';
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Temporary ID
[24] Fix | Delete
*
[25] Fix | Delete
* The temporary ID is used to reference unsaved objects
[26] Fix | Delete
* before they are stored in the database.
[27] Fix | Delete
*
[28] Fix | Delete
* @var string
[29] Fix | Delete
*/
[30] Fix | Delete
protected $_tmp_id = '';
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Type
[34] Fix | Delete
*
[35] Fix | Delete
* The type is used to pragmatically identify the type
[36] Fix | Delete
* of an object without inspecting the class.
[37] Fix | Delete
*
[38] Fix | Delete
* @var string
[39] Fix | Delete
*/
[40] Fix | Delete
protected $_type = '';
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Parent ID
[44] Fix | Delete
*
[45] Fix | Delete
* The ID of the parent object.
[46] Fix | Delete
*
[47] Fix | Delete
* @var string
[48] Fix | Delete
*/
[49] Fix | Delete
protected $_parent_id = '';
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* Parent Type
[53] Fix | Delete
*
[54] Fix | Delete
* The type of the parent object.
[55] Fix | Delete
*
[56] Fix | Delete
* @var string
[57] Fix | Delete
*/
[58] Fix | Delete
protected $_parent_type = '';
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Table Name
[62] Fix | Delete
*
[63] Fix | Delete
* The name of the table where the model objects are stored.
[64] Fix | Delete
*
[65] Fix | Delete
* @var string
[66] Fix | Delete
*/
[67] Fix | Delete
protected $_table_name = '';
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Meta Table Name
[71] Fix | Delete
*
[72] Fix | Delete
* The name of the table where the object settings are stored.
[73] Fix | Delete
*
[74] Fix | Delete
* @var string
[75] Fix | Delete
*/
[76] Fix | Delete
protected $_meta_table_name = '';
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* ? Deprecated ?
[80] Fix | Delete
* @var string
[81] Fix | Delete
*/
[82] Fix | Delete
protected $_relationships_table = 'nf3_relationships';
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* Columns
[86] Fix | Delete
*
[87] Fix | Delete
* A list of settings that are stored in the main table as columns.
[88] Fix | Delete
* These settings are NOT stored in the meta table.
[89] Fix | Delete
*
[90] Fix | Delete
* @var array
[91] Fix | Delete
*/
[92] Fix | Delete
protected $_columns = array();
[93] Fix | Delete
[94] Fix | Delete
/**
[95] Fix | Delete
* Settings
[96] Fix | Delete
*
[97] Fix | Delete
* A list of settings that are stored in the meta table.
[98] Fix | Delete
*
[99] Fix | Delete
* @var array
[100] Fix | Delete
*/
[101] Fix | Delete
protected $_settings = array();
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Results
[105] Fix | Delete
*
[106] Fix | Delete
* The last results returned by a query.
[107] Fix | Delete
*
[108] Fix | Delete
* @var array
[109] Fix | Delete
*/
[110] Fix | Delete
protected $_results = array();
[111] Fix | Delete
[112] Fix | Delete
/**
[113] Fix | Delete
* Cache
[114] Fix | Delete
*
[115] Fix | Delete
* A Flag for using or bypassing caching.
[116] Fix | Delete
*
[117] Fix | Delete
* @var bool
[118] Fix | Delete
*/
[119] Fix | Delete
protected $_cache = TRUE;
[120] Fix | Delete
[121] Fix | Delete
/**
[122] Fix | Delete
* A Flag for testing whether or not we've completed stage 1 of our db.
[123] Fix | Delete
*
[124] Fix | Delete
* @var boolean
[125] Fix | Delete
*/
[126] Fix | Delete
private $db_stage_1_complete = TRUE;
[127] Fix | Delete
[128] Fix | Delete
//-----------------------------------------------------
[129] Fix | Delete
// Public Methods
[130] Fix | Delete
//-----------------------------------------------------
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* NF_Abstracts_Model constructor.
[134] Fix | Delete
*
[135] Fix | Delete
* @param $db
[136] Fix | Delete
* @param $id
[137] Fix | Delete
* @param $parent_id
[138] Fix | Delete
*/
[139] Fix | Delete
public function __construct( $db, $id = NULL, $parent_id = '' )
[140] Fix | Delete
{
[141] Fix | Delete
/*
[142] Fix | Delete
* Injected the Database Dependency
[143] Fix | Delete
*/
[144] Fix | Delete
$this->_db = $db;
[145] Fix | Delete
[146] Fix | Delete
/*
[147] Fix | Delete
* Assign Database Tables using the DB prefix
[148] Fix | Delete
*/
[149] Fix | Delete
$this->_table_name = $this->_db->prefix . $this->_table_name;
[150] Fix | Delete
$this->_meta_table_name = $this->_db->prefix . $this->_meta_table_name;
[151] Fix | Delete
$this->_relationships_table = $this->_db->prefix . $this->_relationships_table;
[152] Fix | Delete
[153] Fix | Delete
/*
[154] Fix | Delete
* Set the object ID
[155] Fix | Delete
* Check if the ID is Permanent (int) or Temporary (string)
[156] Fix | Delete
*/
[157] Fix | Delete
if( is_numeric( $id ) ) {
[158] Fix | Delete
$this->_id = absint( $id );
[159] Fix | Delete
} elseif( $id ) {
[160] Fix | Delete
$this->_tmp_id = $id;
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/*
[164] Fix | Delete
* Set the Parent ID for context
[165] Fix | Delete
*/
[166] Fix | Delete
$this->_parent_id = $parent_id;
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
/**
[170] Fix | Delete
* Get the Permanent ID
[171] Fix | Delete
*
[172] Fix | Delete
* @return int
[173] Fix | Delete
*/
[174] Fix | Delete
public function get_id()
[175] Fix | Delete
{
[176] Fix | Delete
return intval( $this->_id );
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
/**
[180] Fix | Delete
* Get the Temporary ID
[181] Fix | Delete
*
[182] Fix | Delete
* @return null|string
[183] Fix | Delete
*/
[184] Fix | Delete
public function get_tmp_id()
[185] Fix | Delete
{
[186] Fix | Delete
return $this->_tmp_id;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Get the Type
[191] Fix | Delete
*
[192] Fix | Delete
* @return string
[193] Fix | Delete
*/
[194] Fix | Delete
public function get_type()
[195] Fix | Delete
{
[196] Fix | Delete
return $this->_type;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
/**
[200] Fix | Delete
* Get a single setting with a default fallback
[201] Fix | Delete
*
[202] Fix | Delete
* @param string $setting
[203] Fix | Delete
* @param bool $default optional
[204] Fix | Delete
* @return string|int|bool
[205] Fix | Delete
*/
[206] Fix | Delete
public function get_setting( $setting, $default = FALSE )
[207] Fix | Delete
{
[208] Fix | Delete
if( isset( $this->_settings[ $setting ] )){
[209] Fix | Delete
$return = $this->_settings[ $setting ];
[210] Fix | Delete
} else {
[211] Fix | Delete
$return = $this->get_settings($setting);
[212] Fix | Delete
if( is_array( $return ) && empty( $return ) ) $return = false;
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
return ( $return !== false ) ? $return : $default;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
/**
[219] Fix | Delete
* Get Settings
[220] Fix | Delete
*
[221] Fix | Delete
* @param string ...$only returns a subset of the object's settings
[222] Fix | Delete
* @return array
[223] Fix | Delete
*/
[224] Fix | Delete
public function get_settings()
[225] Fix | Delete
{
[226] Fix | Delete
// If the ID is not set, then we cannot pull settings from the Database.
[227] Fix | Delete
if( ! $this->_id ) return $this->_settings;
[228] Fix | Delete
[229] Fix | Delete
if( ! $this->_settings && 'field' == $this->_type ) {
[230] Fix | Delete
global $wpdb;
[231] Fix | Delete
$results = $wpdb->get_results(
[232] Fix | Delete
"
[233] Fix | Delete
SELECT Meta.key, Meta.value
[234] Fix | Delete
FROM $this->_table_name as Object
[235] Fix | Delete
JOIN $this->_meta_table_name as Meta
[236] Fix | Delete
ON Object.id = Meta.parent_id
[237] Fix | Delete
WHERE Object.id = '$this->_id'
[238] Fix | Delete
"
[239] Fix | Delete
, ARRAY_A );
[240] Fix | Delete
[241] Fix | Delete
foreach( $results as $result ) {
[242] Fix | Delete
$key = $result[ 'key' ];
[243] Fix | Delete
$this->_settings[ $key ] = $result[ 'value' ];
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
$field = $wpdb->get_row(
[247] Fix | Delete
"
[248] Fix | Delete
SELECT `label`, `key`, `type`
[249] Fix | Delete
FROM $this->_table_name
[250] Fix | Delete
WHERE `id` = '$this->_id'
[251] Fix | Delete
",
[252] Fix | Delete
ARRAY_A
[253] Fix | Delete
);
[254] Fix | Delete
[255] Fix | Delete
if( ! is_wp_error( $field ) ){
[256] Fix | Delete
$this->_settings[ 'label' ] = $field[ 'label' ];
[257] Fix | Delete
$this->_settings[ 'key' ] = $field[ 'key' ];
[258] Fix | Delete
$this->_settings[ 'type' ] = $field[ 'type' ];
[259] Fix | Delete
$this->_settings[ 'field_label' ] = $field[ 'label' ];
[260] Fix | Delete
$this->_settings[ 'field_key' ] = $field[ 'key' ];
[261] Fix | Delete
}
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
if( ! $this->_settings ) {
[265] Fix | Delete
if( WPN_Helper::use_cache() ) {
[266] Fix | Delete
$form_cache = WPN_Helper::get_nf_cache( $this->_parent_id );
[267] Fix | Delete
if ($form_cache) {
[268] Fix | Delete
[269] Fix | Delete
if ('field' == $this->_type) {
[270] Fix | Delete
[271] Fix | Delete
if (isset($form_cache['fields'])) {
[272] Fix | Delete
[273] Fix | Delete
foreach ($form_cache['fields'] as $object) {
[274] Fix | Delete
if ($this->_id != $object['id']) continue;
[275] Fix | Delete
[276] Fix | Delete
$this->update_settings($object['settings']);
[277] Fix | Delete
break;
[278] Fix | Delete
}
[279] Fix | Delete
}
[280] Fix | Delete
}
[281] Fix | Delete
}
[282] Fix | Delete
}
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
// Only query if settings haven't been already queried or cache is FALSE.
[286] Fix | Delete
if( ! $this->_settings || ! $this->_cache ) {
[287] Fix | Delete
[288] Fix | Delete
// Build query syntax from the columns property.
[289] Fix | Delete
$columns = '`' . implode( '`, `', $this->_columns ) . '`';
[290] Fix | Delete
[291] Fix | Delete
// Query column settings
[292] Fix | Delete
$results = $this->_db->get_row(
[293] Fix | Delete
"
[294] Fix | Delete
SELECT $columns
[295] Fix | Delete
FROM `$this->_table_name`
[296] Fix | Delete
WHERE `id` = $this->_id
[297] Fix | Delete
"
[298] Fix | Delete
);
[299] Fix | Delete
[300] Fix | Delete
/*
[301] Fix | Delete
* If the query returns results then
[302] Fix | Delete
* assign settings using the column name as the setting key.
[303] Fix | Delete
*/
[304] Fix | Delete
if( $results ) {
[305] Fix | Delete
foreach ($this->_columns as $column) {
[306] Fix | Delete
$this->_settings[$column] = $results->$column;
[307] Fix | Delete
}
[308] Fix | Delete
}
[309] Fix | Delete
[310] Fix | Delete
$meta_select_fields = "SELECT `key`, `value`";//, `meta_key`,
[311] Fix | Delete
//`meta_value`";
[312] Fix | Delete
[313] Fix | Delete
// Query settings from the meta table.
[314] Fix | Delete
$meta_results = $this->_db->get_results(
[315] Fix | Delete
$meta_select_fields .
[316] Fix | Delete
"
[317] Fix | Delete
FROM `$this->_meta_table_name`
[318] Fix | Delete
WHERE `parent_id` = $this->_id
[319] Fix | Delete
"
[320] Fix | Delete
);
[321] Fix | Delete
[322] Fix | Delete
// Assign settings to the settings property.
[323] Fix | Delete
foreach ($meta_results as $meta) {
[324] Fix | Delete
// If we don't already have a value from the main table...
[325] Fix | Delete
// OR If that value was NULL...
[326] Fix | Delete
if ( ! isset( $this->_settings[ $meta->key ] ) || NULL == $this->_settings[ $meta->key ] ) {
[327] Fix | Delete
// TODO: Update this logic after removal of original meta columns.
[328] Fix | Delete
// Set the value from meta.
[329] Fix | Delete
$this->_settings[ $meta->key ] = $meta->value;
[330] Fix | Delete
}
[331] Fix | Delete
}
[332] Fix | Delete
}
[333] Fix | Delete
[334] Fix | Delete
// Un-serialize queried settings results.
[335] Fix | Delete
foreach( $this->_settings as $key => $value ){
[336] Fix | Delete
$this->_settings[ $key ] = maybe_unserialize( $value );
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
// Check for passed arguments to limit the returned settings.
[340] Fix | Delete
$only = func_get_args();
[341] Fix | Delete
if ( $only && is_array($only)
[342] Fix | Delete
// And if the array is NOT multidimensional
[343] Fix | Delete
&& (count($only) == count($only, COUNT_RECURSIVE))) {
[344] Fix | Delete
[345] Fix | Delete
// If only one setting, return a single value
[346] Fix | Delete
if( 1 == count( $only ) ){
[347] Fix | Delete
[348] Fix | Delete
if( isset( $this->_settings[ $only[0] ] ) ) {
[349] Fix | Delete
return $this->_settings[$only[0]];
[350] Fix | Delete
} else {
[351] Fix | Delete
return NULL;
[352] Fix | Delete
}
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
// Flip the array to match the settings property
[356] Fix | Delete
$only_settings = array_flip( $only );
[357] Fix | Delete
[358] Fix | Delete
// Return only the requested settings
[359] Fix | Delete
return array_intersect_key( $this->_settings, $only_settings );
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
// Return all settings
[363] Fix | Delete
return $this->_settings;
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
/**
[367] Fix | Delete
* Update Setting
[368] Fix | Delete
*
[369] Fix | Delete
* @param $key
[370] Fix | Delete
* @param $value
[371] Fix | Delete
* @return bool|false|int
[372] Fix | Delete
*/
[373] Fix | Delete
public function update_setting( $key, $value )
[374] Fix | Delete
{
[375] Fix | Delete
$this->_settings[ $key ] = $value;
[376] Fix | Delete
[377] Fix | Delete
return $this;
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
/**
[381] Fix | Delete
* Update Settings
[382] Fix | Delete
*
[383] Fix | Delete
* @param $data
[384] Fix | Delete
* @return bool
[385] Fix | Delete
*/
[386] Fix | Delete
public function update_settings( $data )
[387] Fix | Delete
{
[388] Fix | Delete
if( is_array( $data ) ) {
[389] Fix | Delete
foreach ($data as $key => $value) {
[390] Fix | Delete
$this->update_setting($key, $value);
[391] Fix | Delete
}
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
return $this;
[395] Fix | Delete
}
[396] Fix | Delete
[397] Fix | Delete
/**
[398] Fix | Delete
* Delete
[399] Fix | Delete
*
[400] Fix | Delete
* Delete the object, its children, and its relationships.
[401] Fix | Delete
*
[402] Fix | Delete
* @return bool
[403] Fix | Delete
*/
[404] Fix | Delete
public function delete()
[405] Fix | Delete
{
[406] Fix | Delete
if( ! $this->get_id() ) return;
[407] Fix | Delete
[408] Fix | Delete
$results = array();
[409] Fix | Delete
[410] Fix | Delete
// Delete the object from the model's table
[411] Fix | Delete
$results[] = $this->_db->delete(
[412] Fix | Delete
$this->_table_name,
[413] Fix | Delete
array(
[414] Fix | Delete
'id' => $this->_id
[415] Fix | Delete
)
[416] Fix | Delete
);
[417] Fix | Delete
[418] Fix | Delete
// Delete settings from the model's meta table.
[419] Fix | Delete
$results[] = $this->_db->delete(
[420] Fix | Delete
$this->_meta_table_name,
[421] Fix | Delete
array(
[422] Fix | Delete
'parent_id' => $this->_id
[423] Fix | Delete
)
[424] Fix | Delete
);
[425] Fix | Delete
[426] Fix | Delete
// Query for child objects using the relationships table.
[427] Fix | Delete
[428] Fix | Delete
$children = $this->_db->get_results(
[429] Fix | Delete
"
[430] Fix | Delete
SELECT child_id, child_type
[431] Fix | Delete
FROM $this->_relationships_table
[432] Fix | Delete
WHERE parent_id = $this->_id
[433] Fix | Delete
AND parent_type = '$this->_type'
[434] Fix | Delete
"
[435] Fix | Delete
);
[436] Fix | Delete
[437] Fix | Delete
// Delete each child model
[438] Fix | Delete
foreach( $children as $child ) {
[439] Fix | Delete
$model = Ninja_Forms()->form()->get_model( $child->child_id, $child->child_type );
[440] Fix | Delete
$model->delete();
[441] Fix | Delete
}
[442] Fix | Delete
[443] Fix | Delete
// Delete all relationships
[444] Fix | Delete
$this->_db->delete(
[445] Fix | Delete
$this->_relationships_table,
[446] Fix | Delete
array(
[447] Fix | Delete
'parent_id' => $this->_id,
[448] Fix | Delete
'parent_type' => $this->_type
[449] Fix | Delete
)
[450] Fix | Delete
);
[451] Fix | Delete
[452] Fix | Delete
// return False if there are no query errors.
[453] Fix | Delete
return in_array( FALSE, $results );
[454] Fix | Delete
}
[455] Fix | Delete
[456] Fix | Delete
/**
[457] Fix | Delete
* Find
[458] Fix | Delete
*
[459] Fix | Delete
* @param string $parent_id
[460] Fix | Delete
* @param array $where
[461] Fix | Delete
* @return array
[462] Fix | Delete
*/
[463] Fix | Delete
public function find( $parent_id = '', array $where = array() )
[464] Fix | Delete
{
[465] Fix | Delete
// Build the query using the $where argument
[466] Fix | Delete
$query = $this->build_meta_query( $parent_id, $where );
[467] Fix | Delete
[468] Fix | Delete
// Get object IDs from the query
[469] Fix | Delete
$ids = $this->_db->get_col( $query );
[470] Fix | Delete
[471] Fix | Delete
// Get the current class name
[472] Fix | Delete
$class = get_class( $this );
[473] Fix | Delete
[474] Fix | Delete
$results = array();
[475] Fix | Delete
foreach( $ids as $id ){
[476] Fix | Delete
[477] Fix | Delete
// Instantiate a new object for each ID
[478] Fix | Delete
$results[] = $object = new $class( $this->_db, $id, $parent_id );
[479] Fix | Delete
}
[480] Fix | Delete
[481] Fix | Delete
if( ! WPN_Helper::use_cache() ) {
[482] Fix | Delete
$results = $this->get_object_settings($results);
[483] Fix | Delete
}
[484] Fix | Delete
// Return an array of objects
[485] Fix | Delete
return $results;
[486] Fix | Delete
}
[487] Fix | Delete
[488] Fix | Delete
public function get_object_settings($obj_array ) {
[489] Fix | Delete
global $wpdb;
[490] Fix | Delete
$id_array = array();
[491] Fix | Delete
$generic_object_array = array();
[492] Fix | Delete
foreach($obj_array as $obj) {
[493] Fix | Delete
$id_array[] = $obj->get_id();
[494] Fix | Delete
$generic_object_array[$obj->get_id()] = $obj;
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
if( 0 < count($id_array) ) {
[498] Fix | Delete
$obj_query = "SELECT `id`, `" . implode( '`, `', $this->_columns) . "` FROM {$this->_table_name} WHERE id IN (" . implode(',', $id_array) . ")";
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function