Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/wordpres.../lib
File: orm.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\Lib;
[2] Fix | Delete
[3] Fix | Delete
use wpdb;
[4] Fix | Delete
use Yoast\WP\SEO\Config\Migration_Status;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Yoast ORM class.
[8] Fix | Delete
*
[9] Fix | Delete
* Based on Idiorm
[10] Fix | Delete
*
[11] Fix | Delete
* URL: http://github.com/j4mie/idiorm/
[12] Fix | Delete
*
[13] Fix | Delete
* A single-class super-simple database abstraction layer for PHP.
[14] Fix | Delete
* Provides (nearly) zero-configuration object-relational mapping
[15] Fix | Delete
* and a fluent interface for building basic, commonly-used queries.
[16] Fix | Delete
*
[17] Fix | Delete
* BSD Licensed.
[18] Fix | Delete
*
[19] Fix | Delete
* Copyright (c) 2010, Jamie Matthews
[20] Fix | Delete
* All rights reserved.
[21] Fix | Delete
*
[22] Fix | Delete
* Redistribution and use in source and binary forms, with or without
[23] Fix | Delete
* modification, are permitted provided that the following conditions are met:
[24] Fix | Delete
*
[25] Fix | Delete
* * Redistributions of source code must retain the above copyright notice, this
[26] Fix | Delete
* list of conditions and the following disclaimer.
[27] Fix | Delete
*
[28] Fix | Delete
* * Redistributions in binary form must reproduce the above copyright notice,
[29] Fix | Delete
* this list of conditions and the following disclaimer in the documentation
[30] Fix | Delete
* and/or other materials provided with the distribution.
[31] Fix | Delete
*
[32] Fix | Delete
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
[33] Fix | Delete
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
[34] Fix | Delete
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
[35] Fix | Delete
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
[36] Fix | Delete
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
[37] Fix | Delete
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
[38] Fix | Delete
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
[39] Fix | Delete
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
[40] Fix | Delete
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
[41] Fix | Delete
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[42] Fix | Delete
*
[43] Fix | Delete
* The methods documented below are magic methods that conform to PSR-1.
[44] Fix | Delete
* This documentation exposes these methods to doc generators and IDEs.
[45] Fix | Delete
*
[46] Fix | Delete
* @see http://www.php-fig.org/psr/psr-1/
[47] Fix | Delete
*/
[48] Fix | Delete
class ORM implements \ArrayAccess {
[49] Fix | Delete
[50] Fix | Delete
/*
[51] Fix | Delete
* --- CLASS CONSTANTS ---
[52] Fix | Delete
*/
[53] Fix | Delete
[54] Fix | Delete
const CONDITION_FRAGMENT = 0;
[55] Fix | Delete
[56] Fix | Delete
const CONDITION_VALUES = 1;
[57] Fix | Delete
[58] Fix | Delete
/*
[59] Fix | Delete
* --- INSTANCE PROPERTIES ---
[60] Fix | Delete
*/
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Holds the class name. Wrapped find_one and find_many classes will return an instance or instances of this class.
[64] Fix | Delete
*
[65] Fix | Delete
* @var string
[66] Fix | Delete
*/
[67] Fix | Delete
protected $class_name;
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Holds the name of the table the current ORM instance is associated with.
[71] Fix | Delete
*
[72] Fix | Delete
* @var string
[73] Fix | Delete
*/
[74] Fix | Delete
protected $table_name;
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Holds the alias for the table to be used in SELECT queries.
[78] Fix | Delete
*
[79] Fix | Delete
* @var string
[80] Fix | Delete
*/
[81] Fix | Delete
protected $table_alias = null;
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Values to be bound to the query.
[85] Fix | Delete
*
[86] Fix | Delete
* @var array
[87] Fix | Delete
*/
[88] Fix | Delete
protected $values = [];
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* Columns to select in the result.
[92] Fix | Delete
*
[93] Fix | Delete
* @var array
[94] Fix | Delete
*/
[95] Fix | Delete
protected $result_columns = [ '*' ];
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Are we using the default result column or have these been manually changed?
[99] Fix | Delete
*
[100] Fix | Delete
* @var bool
[101] Fix | Delete
*/
[102] Fix | Delete
protected $using_default_result_columns = true;
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Holds the join sources.
[106] Fix | Delete
*
[107] Fix | Delete
* @var array
[108] Fix | Delete
*/
[109] Fix | Delete
protected $join_sources = [];
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Should the query include a DISTINCT keyword?
[113] Fix | Delete
*
[114] Fix | Delete
* @var bool
[115] Fix | Delete
*/
[116] Fix | Delete
protected $distinct = false;
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Is this a raw query?
[120] Fix | Delete
*
[121] Fix | Delete
* @var bool
[122] Fix | Delete
*/
[123] Fix | Delete
protected $is_raw_query = false;
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* The raw query.
[127] Fix | Delete
*
[128] Fix | Delete
* @var string
[129] Fix | Delete
*/
[130] Fix | Delete
protected $raw_query = '';
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* The raw query parameters.
[134] Fix | Delete
*
[135] Fix | Delete
* @var array
[136] Fix | Delete
*/
[137] Fix | Delete
protected $raw_parameters = [];
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Array of WHERE clauses.
[141] Fix | Delete
*
[142] Fix | Delete
* @var array
[143] Fix | Delete
*/
[144] Fix | Delete
protected $where_conditions = [];
[145] Fix | Delete
[146] Fix | Delete
/**
[147] Fix | Delete
* LIMIT.
[148] Fix | Delete
*
[149] Fix | Delete
* @var int
[150] Fix | Delete
*/
[151] Fix | Delete
protected $limit = null;
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* OFFSET.
[155] Fix | Delete
*
[156] Fix | Delete
* @var int
[157] Fix | Delete
*/
[158] Fix | Delete
protected $offset = null;
[159] Fix | Delete
[160] Fix | Delete
/**
[161] Fix | Delete
* ORDER BY.
[162] Fix | Delete
*
[163] Fix | Delete
* @var array
[164] Fix | Delete
*/
[165] Fix | Delete
protected $order_by = [];
[166] Fix | Delete
[167] Fix | Delete
/**
[168] Fix | Delete
* GROUP BY.
[169] Fix | Delete
*
[170] Fix | Delete
* @var array
[171] Fix | Delete
*/
[172] Fix | Delete
protected $group_by = [];
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* HAVING.
[176] Fix | Delete
*
[177] Fix | Delete
* @var array
[178] Fix | Delete
*/
[179] Fix | Delete
protected $having_conditions = [];
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* The data for a hydrated instance of the class.
[183] Fix | Delete
*
[184] Fix | Delete
* @var array
[185] Fix | Delete
*/
[186] Fix | Delete
protected $data = [];
[187] Fix | Delete
[188] Fix | Delete
/**
[189] Fix | Delete
* Lifetime of the object.
[190] Fix | Delete
*
[191] Fix | Delete
* @var array
[192] Fix | Delete
*/
[193] Fix | Delete
protected $dirty_fields = [];
[194] Fix | Delete
[195] Fix | Delete
/**
[196] Fix | Delete
* Fields that are to be inserted in the DB raw.
[197] Fix | Delete
*
[198] Fix | Delete
* @var array
[199] Fix | Delete
*/
[200] Fix | Delete
protected $expr_fields = [];
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* Is this a new object (has create() been called)?
[204] Fix | Delete
*
[205] Fix | Delete
* @var bool
[206] Fix | Delete
*/
[207] Fix | Delete
protected $is_new = false;
[208] Fix | Delete
[209] Fix | Delete
/**
[210] Fix | Delete
* Name of the column to use as the primary key for
[211] Fix | Delete
* this instance only. Overrides the config settings.
[212] Fix | Delete
*
[213] Fix | Delete
* @var string
[214] Fix | Delete
*/
[215] Fix | Delete
protected $instance_id_column = null;
[216] Fix | Delete
[217] Fix | Delete
/*
[218] Fix | Delete
* --- STATIC METHODS ---
[219] Fix | Delete
*/
[220] Fix | Delete
[221] Fix | Delete
/**
[222] Fix | Delete
* Factory method, return an instance of this class bound to the supplied
[223] Fix | Delete
* table name.
[224] Fix | Delete
*
[225] Fix | Delete
* A repeat of content in parent::for_table, so that created class is ORM.
[226] Fix | Delete
*
[227] Fix | Delete
* @param string $table_name The table to create instance for.
[228] Fix | Delete
*
[229] Fix | Delete
* @return ORM Instance of the ORM.
[230] Fix | Delete
*/
[231] Fix | Delete
public static function for_table( $table_name ) {
[232] Fix | Delete
return new static( $table_name, [] );
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* Executes a raw query as a wrapper for wpdb::query.
[237] Fix | Delete
* Useful for queries that can't be accomplished through Idiorm,
[238] Fix | Delete
* particularly those using engine-specific features.
[239] Fix | Delete
*
[240] Fix | Delete
* @example raw_execute('INSERT OR REPLACE INTO `widget` (`id`, `name`) SELECT `id`, `name` FROM `other_table`')
[241] Fix | Delete
* @example raw_execute('SELECT `name`, AVG(`order`) FROM `customer` GROUP BY `name` HAVING AVG(`order`) > 10')
[242] Fix | Delete
*
[243] Fix | Delete
* @param string $query The raw SQL query.
[244] Fix | Delete
* @param array $parameters Optional bound parameters.
[245] Fix | Delete
*
[246] Fix | Delete
* @return bool Success.
[247] Fix | Delete
*/
[248] Fix | Delete
public static function raw_execute( $query, $parameters = [] ) {
[249] Fix | Delete
return self::execute( $query, $parameters );
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
/**
[253] Fix | Delete
* Internal helper method for executing statements.
[254] Fix | Delete
*
[255] Fix | Delete
* @param string $query The query.
[256] Fix | Delete
* @param array $parameters An array of parameters to be bound in to the query.
[257] Fix | Delete
*
[258] Fix | Delete
* @return bool|int Response of wpdb::query
[259] Fix | Delete
*/
[260] Fix | Delete
protected static function execute( $query, $parameters = [] ) {
[261] Fix | Delete
/**
[262] Fix | Delete
* The global WordPress database variable.
[263] Fix | Delete
*
[264] Fix | Delete
* @var wpdb
[265] Fix | Delete
*/
[266] Fix | Delete
global $wpdb;
[267] Fix | Delete
[268] Fix | Delete
$show_errors = $wpdb->show_errors;
[269] Fix | Delete
[270] Fix | Delete
if ( YoastSEO()->classes->get( Migration_Status::class )->get_error( 'free' ) ) {
[271] Fix | Delete
$wpdb->show_errors = false;
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
$parameters = \array_filter(
[275] Fix | Delete
$parameters,
[276] Fix | Delete
static function( $parameter ) {
[277] Fix | Delete
return $parameter !== null;
[278] Fix | Delete
}
[279] Fix | Delete
);
[280] Fix | Delete
if ( ! empty( $parameters ) ) {
[281] Fix | Delete
$query = $wpdb->prepare( $query, $parameters );
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
$result = $wpdb->query( $query );
[285] Fix | Delete
[286] Fix | Delete
$wpdb->show_errors = $show_errors;
[287] Fix | Delete
[288] Fix | Delete
return $result;
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
/*
[292] Fix | Delete
* --- INSTANCE METHODS ---
[293] Fix | Delete
*/
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* "Private" constructor; shouldn't be called directly.
[297] Fix | Delete
* Use the ORM::for_table factory method instead.
[298] Fix | Delete
*
[299] Fix | Delete
* @param string $table_name Table name.
[300] Fix | Delete
* @param array $data Data to populate table.
[301] Fix | Delete
*/
[302] Fix | Delete
protected function __construct( $table_name, $data = [] ) {
[303] Fix | Delete
$this->table_name = $table_name;
[304] Fix | Delete
$this->data = $data;
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
/**
[308] Fix | Delete
* Sets the name of the class which the wrapped methods should return instances of.
[309] Fix | Delete
*
[310] Fix | Delete
* @param string $class_name The classname to set.
[311] Fix | Delete
*
[312] Fix | Delete
* @return void
[313] Fix | Delete
*/
[314] Fix | Delete
public function set_class_name( $class_name ) {
[315] Fix | Delete
$this->class_name = $class_name;
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* Creates a new, empty instance of the class. Used to add a new row to your database. May optionally be passed an
[320] Fix | Delete
* associative array of data to populate the instance. If so, all fields will be flagged as dirty so all will be
[321] Fix | Delete
* saved to the database when save() is called.
[322] Fix | Delete
*
[323] Fix | Delete
* @param array|null $data Data to populate table.
[324] Fix | Delete
*
[325] Fix | Delete
* @return bool|Model|ORM
[326] Fix | Delete
*/
[327] Fix | Delete
public function create( $data = null ) {
[328] Fix | Delete
$this->is_new = true;
[329] Fix | Delete
if ( ! \is_null( $data ) ) {
[330] Fix | Delete
$this->hydrate( $data )->force_all_dirty();
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
return $this->create_model_instance( $this );
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
/**
[337] Fix | Delete
* Specifies the ID column to use for this instance or array of instances only.
[338] Fix | Delete
* This overrides the id_column and id_column_overrides settings.
[339] Fix | Delete
*
[340] Fix | Delete
* This is mostly useful for libraries built on top of Idiorm, and will not normally be used in manually built
[341] Fix | Delete
* queries. If you don't know why you would want to use this, you should probably just ignore it.
[342] Fix | Delete
*
[343] Fix | Delete
* @param string $id_column The ID column.
[344] Fix | Delete
*
[345] Fix | Delete
* @return ORM
[346] Fix | Delete
*/
[347] Fix | Delete
public function use_id_column( $id_column ) {
[348] Fix | Delete
$this->instance_id_column = $id_column;
[349] Fix | Delete
[350] Fix | Delete
return $this;
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
/**
[354] Fix | Delete
* Creates an ORM instance from the given row (an associative array of data fetched from the database).
[355] Fix | Delete
*
[356] Fix | Delete
* @param array $row A row from the database.
[357] Fix | Delete
*
[358] Fix | Delete
* @return bool|Model
[359] Fix | Delete
*/
[360] Fix | Delete
protected function create_instance_from_row( $row ) {
[361] Fix | Delete
$instance = self::for_table( $this->table_name );
[362] Fix | Delete
$instance->use_id_column( $this->instance_id_column );
[363] Fix | Delete
$instance->hydrate( $row );
[364] Fix | Delete
[365] Fix | Delete
return $this->create_model_instance( $instance );
[366] Fix | Delete
}
[367] Fix | Delete
[368] Fix | Delete
/**
[369] Fix | Delete
* Tells the ORM that you are expecting a single result back from your query, and execute it. Will return a single
[370] Fix | Delete
* instance of the ORM class, or false if no rows were returned. As a shortcut, you may supply an ID as a parameter
[371] Fix | Delete
* to this method. This will perform a primary key lookup on the table.
[372] Fix | Delete
*
[373] Fix | Delete
* @param int|null $id An (optional) ID.
[374] Fix | Delete
*
[375] Fix | Delete
* @return bool|Model
[376] Fix | Delete
*/
[377] Fix | Delete
public function find_one( $id = null ) {
[378] Fix | Delete
if ( ! \is_null( $id ) ) {
[379] Fix | Delete
$this->where_id_is( $id );
[380] Fix | Delete
}
[381] Fix | Delete
$this->limit( 1 );
[382] Fix | Delete
$rows = $this->run();
[383] Fix | Delete
if ( empty( $rows ) ) {
[384] Fix | Delete
return false;
[385] Fix | Delete
}
[386] Fix | Delete
[387] Fix | Delete
return $this->create_instance_from_row( $rows[0] );
[388] Fix | Delete
}
[389] Fix | Delete
[390] Fix | Delete
/**
[391] Fix | Delete
* Tells the ORM that you are expecting multiple results from your query, and execute it. Will return an array of
[392] Fix | Delete
* instances of the ORM class, or an empty array if no rows were returned.
[393] Fix | Delete
*
[394] Fix | Delete
* @return array
[395] Fix | Delete
*/
[396] Fix | Delete
public function find_many() {
[397] Fix | Delete
$rows = $this->run();
[398] Fix | Delete
[399] Fix | Delete
if ( $rows === false ) {
[400] Fix | Delete
return [];
[401] Fix | Delete
}
[402] Fix | Delete
[403] Fix | Delete
return \array_map( [ $this, 'create_instance_from_row' ], $rows );
[404] Fix | Delete
}
[405] Fix | Delete
[406] Fix | Delete
/**
[407] Fix | Delete
* Creates an instance of the model class associated with this wrapper and populate it with the supplied Idiorm
[408] Fix | Delete
* instance.
[409] Fix | Delete
*
[410] Fix | Delete
* @param ORM $orm The ORM used by model.
[411] Fix | Delete
*
[412] Fix | Delete
* @return bool|Model Instance of the model class.
[413] Fix | Delete
*/
[414] Fix | Delete
protected function create_model_instance( $orm ) {
[415] Fix | Delete
if ( $orm === false ) {
[416] Fix | Delete
return false;
[417] Fix | Delete
}
[418] Fix | Delete
[419] Fix | Delete
/**
[420] Fix | Delete
* An instance of Model is being made.
[421] Fix | Delete
*
[422] Fix | Delete
* @var Model $model
[423] Fix | Delete
*/
[424] Fix | Delete
$model = new $this->class_name();
[425] Fix | Delete
$model->set_orm( $orm );
[426] Fix | Delete
[427] Fix | Delete
return $model;
[428] Fix | Delete
}
[429] Fix | Delete
[430] Fix | Delete
/**
[431] Fix | Delete
* Tells the ORM that you are expecting multiple results from your query, and execute it. Will return an array, or
[432] Fix | Delete
* an empty array if no rows were returned.
[433] Fix | Delete
*
[434] Fix | Delete
* @return array The query results.
[435] Fix | Delete
*/
[436] Fix | Delete
public function find_array() {
[437] Fix | Delete
return $this->run();
[438] Fix | Delete
}
[439] Fix | Delete
[440] Fix | Delete
/**
[441] Fix | Delete
* Tells the ORM that you wish to execute a COUNT query.
[442] Fix | Delete
*
[443] Fix | Delete
* @param string $column The table column.
[444] Fix | Delete
*
[445] Fix | Delete
* @return float|int An integer representing the number of rows returned.
[446] Fix | Delete
*/
[447] Fix | Delete
public function count( $column = '*' ) {
[448] Fix | Delete
return $this->call_aggregate_db_function( __FUNCTION__, $column );
[449] Fix | Delete
}
[450] Fix | Delete
[451] Fix | Delete
/**
[452] Fix | Delete
* Tells the ORM that you wish to execute a MAX query.
[453] Fix | Delete
*
[454] Fix | Delete
* @param string $column The table column.
[455] Fix | Delete
*
[456] Fix | Delete
* @return float|int The max value of the chosen column.
[457] Fix | Delete
*/
[458] Fix | Delete
public function max( $column ) {
[459] Fix | Delete
return $this->call_aggregate_db_function( __FUNCTION__, $column );
[460] Fix | Delete
}
[461] Fix | Delete
[462] Fix | Delete
/**
[463] Fix | Delete
* Tells the ORM that you wish to execute a MIN query.
[464] Fix | Delete
*
[465] Fix | Delete
* @param string $column The table column.
[466] Fix | Delete
*
[467] Fix | Delete
* @return float|int The min value of the chosen column.
[468] Fix | Delete
*/
[469] Fix | Delete
public function min( $column ) {
[470] Fix | Delete
return $this->call_aggregate_db_function( __FUNCTION__, $column );
[471] Fix | Delete
}
[472] Fix | Delete
[473] Fix | Delete
/**
[474] Fix | Delete
* Tells the ORM that you wish to execute a AVG query.
[475] Fix | Delete
*
[476] Fix | Delete
* @param string $column The table column.
[477] Fix | Delete
*
[478] Fix | Delete
* @return float|int The average value of the chosen column.
[479] Fix | Delete
*/
[480] Fix | Delete
public function avg( $column ) {
[481] Fix | Delete
return $this->call_aggregate_db_function( __FUNCTION__, $column );
[482] Fix | Delete
}
[483] Fix | Delete
[484] Fix | Delete
/**
[485] Fix | Delete
* Tells the ORM that you wish to execute a SUM query.
[486] Fix | Delete
*
[487] Fix | Delete
* @param string $column The table column.
[488] Fix | Delete
*
[489] Fix | Delete
* @return float|int The sum of the chosen column.
[490] Fix | Delete
*/
[491] Fix | Delete
public function sum( $column ) {
[492] Fix | Delete
return $this->call_aggregate_db_function( __FUNCTION__, $column );
[493] Fix | Delete
}
[494] Fix | Delete
[495] Fix | Delete
/**
[496] Fix | Delete
* Returns the select query as SQL.
[497] Fix | Delete
*
[498] Fix | Delete
* @return string The select query in SQL.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function