Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,459 @@
<?php
namespace AIOSEO\TableOfContents\Models;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The base Model class.
*
* @since 1.0.0
*/
#[\AllowDynamicProperties]
class Model implements \JsonSerializable {
/**
* Fields that should be null when saving to the database.
*
* @since 1.0.0
*
* @var array
*/
protected $nullFields = [];
/**
* Fields that should be encoded/decoded on save/get.
*
* @since 1.0.0
*
* @var array
*/
protected $jsonFields = [];
/**
* Fields that should be boolean values.
*
* @since 1.0.0
*
* @var array
*/
protected $booleanFields = [];
/**
* Fields that should be integer values.
*
* @since 1.0.0
*
* @var array
*/
protected $integerFields = [];
/**
* Fields that should be float values.
*
* @since 1.0.0
*
* @var array
*/
protected $floatValues = [];
/**
* Fields that should be hidden when serialized.
*
* @since 1.0.0
*
* @var array
*/
protected $hidden = [];
/**
* The table used in the SQL query.
*
* @since 1.0.0
*
* @var string
*/
protected $table = '';
/**
* The primary key retrieved from the table.
*
* @since 1.0.0
*
* @var string
*/
protected $pk = 'id';
/**
* An array of columns from the DB that we can use.
*
* @since 1.0.0
*
* @var array
*/
private static $columns;
/**
* Class constructor.
*
* @since 1.0.0
*
* @param mixed $var This can be the primary key of the resource, or it could be an array of data to manufacture a resource without a database query.
*/
public function __construct( $var = null ) {
$fields = [];
$skip = [ 'id', 'created', 'updated' ];
foreach ( $this->getColumns() as $column => $value ) {
if ( ! in_array( $column, $skip, true ) ) {
$fields[ $column ] = $value;
}
}
$this->applyKeys( $fields );
// Process straight through if we were given a valid array.
if ( is_array( $var ) || is_object( $var ) ) {
// Apply keys to object.
$this->applyKeys( $var );
if ( $this->exists() ) {
return true;
}
return false;
}
return $this->loadData( $var );
}
/**
* Load the data from the database!
*
* @since 1.0.0
*
* @param mixed $var The primary key to load up the model from the DB.
* @return Model Returns the current object.
*/
protected function loadData( $var = null ) {
if ( null === $var ) {
return false;
}
$query = aioseoTableOfContents()->core->db
->start( $this->table )
->where( $this->pk, $var )
->limit( 1 )
->output( 'ARRAY_A' );
$result = $query->run();
if ( ! $result || $result->nullSet() ) {
return $this;
}
$this->applyKeys( $result->result()[0] );
return $this;
}
/**
* Take the keys from the result array and add them to the Model.
*
* @since 1.0.0
*
* @param array $array The array of keys and values to add to the Model.
* @return void
*/
protected function applyKeys( $array ) {
if ( ! is_object( $array ) && ! is_array( $array ) ) {
throw new \Exception( '$array must either be an object or an array.' );
}
foreach ( (array) $array as $key => $value ) {
$key = trim( $key );
$this->$key = $value;
if ( null === $value && in_array( $key, $this->nullFields, true ) ) {
continue;
}
if ( in_array( $key, $this->jsonFields, true ) ) {
$this->$key = json_decode( (string) $value );
continue;
}
if ( in_array( $key, $this->booleanFields, true ) ) {
$this->$key = (bool) $value;
continue;
}
if ( in_array( $key, $this->integerFields, true ) ) {
$this->$key = (int) $value;
}
}
}
/**
* Let's filter out any properties we cannot save to the database.
*
* @since 1.0.0
*
* @param string $key The table column.
* @return array The array of valid columns for the database query.
*/
protected function filter( $key ) {
$fields = [];
$skip = [ 'created', 'updated' ];
$table = aioseoTableOfContents()->core->db->prefix . $this->table;
$results = aioseoTableOfContents()->core->db->execute( 'SHOW COLUMNS FROM `' . $table . '`', true );
$columns = $results->result();
foreach ( $columns as $col ) {
if ( ! in_array( $col->Field, $skip, true ) && array_key_exists( $col->Field, $key ) ) {
$fields[ $col->Field ] = $key[ $col->Field ];
}
}
return $fields;
}
/**
* Transforms the data to be null if it exists in the nullFields variables.
*
* @since 1.0.0
*
* @param array $data The data array to transform.
* @return array The transformed data.
*/
protected function transform( $data, $set = false ) {
foreach ( $this->nullFields as $field ) {
if ( isset( $data[ $field ] ) && empty( $data[ $field ] ) ) {
$data[ $field ] = null;
}
}
foreach ( $this->booleanFields as $field ) {
if ( isset( $data[ $field ] ) && '' === $data[ $field ] ) {
unset( $data[ $field ] );
} elseif ( isset( $data[ $field ] ) ) {
$data[ $field ] = (bool) $data[ $field ] ? 1 : 0;
}
}
if ( $set ) {
return $data;
}
foreach ( $this->integerFields as $field ) {
if ( isset( $data[ $field ] ) ) {
$data[ $field ] = (int) $data[ $field ];
}
}
foreach ( $this->jsonFields as $field ) {
if ( isset( $data[ $field ] ) && ! aioseoTableOfContents()->helpers->isJsonString( $data[ $field ] ) ) {
if ( is_array( $data[ $field ] ) && aioseoTableOfContents()->helpers->isArrayNumeric( $data[ $field ] ) ) {
$data[ $field ] = array_values( $data[ $field ] );
}
$data[ $field ] = wp_json_encode( $data[ $field ] );
}
}
return $data;
}
/**
* Sets a piece of data to the requested resource.
*
* @since 1.0.0
*
* @return void
*/
public function set() {
$args = func_get_args();
$count = func_num_args();
if ( ! is_array( $args[0] ) && $count < 2 ) {
throw new \Exception( 'The set method must contain at least 2 arguments: key and value. Or an array of data. Only one argument was passed and it was not an array.' );
}
$key = $args[0];
$value = ! empty( $args[1] ) ? $args[1] : null;
if ( false === $key ) {
return false;
}
if ( ! is_array( $key ) ) {
$key = [ $key => $value ];
}
$key = $this->transform( $key, true );
foreach ( $key as $k => $v ) {
if ( ! empty( $k ) ) {
$this->$k = $v;
}
}
}
/**
* Delete the row in the DB.
*
* @since 1.0.0
*
* @return null
*/
public function delete() {
aioseoTableOfContents()->core->db
->delete( $this->table )
->where( $this->pk, $this->id )
->run();
return null;
}
/**
* Saves data to the requested resource.
*
* @since 1.0.0
*
* @return void
*/
public function save() {
$fields = $this->transform( $this->filter( (array) get_object_vars( $this ) ) );
$id = null;
$date = gmdate( 'Y-m-d H:i:s' );
if ( ! empty( $fields ) ) {
$pk = $this->pk;
if ( isset( $this->$pk ) && '' !== $this->$pk ) {
// PK specified.
$pkv = $this->$pk;
$query = aioseoTableOfContents()->core->db
->start( $this->table )
->where( [ $pk => $pkv ] )
->run();
if ( ! $query->nullSet() ) {
// Row exists in database.
$fields['updated'] = $date;
aioseoTableOfContents()->core->db
->update( $this->table )
->set( $fields )
->where( [ $pk => $pkv ] )
->run();
$id = $this->$pk;
} else {
// Row does not exist.
$fields[ $pk ] = $pkv;
$fields['created'] = $date;
$fields['updated'] = $date;
$id = aioseoTableOfContents()->core->db
->insert( $this->table )
->set( $fields )
->run()
->insertId();
if ( $id ) {
$this->$pk = $id;
}
}
} else {
$fields['created'] = $date;
$fields['updated'] = $date;
$id = aioseoTableOfContents()->core->db
->insert( $this->table )
->set( $fields )
->run()
->insertId();
if ( $id ) {
$this->$pk = $id;
}
}
}
$this->reset( $id );
}
/**
* Check if the model exists in the database.
*
* @since 1.0.0
*
* @return bool Whether the model exists.
*/
public function exists() {
return ( ! empty( $this->{$this->pk} ) ) ? true : false;
}
/**
* Resets a resource by forcing internal updates to be applied.
*
* @since 1.0.0
*
* @param string $id The resource ID.
* @return void
*/
public function reset( $id = null ) {
$id = ! empty( $id ) ? $id : $this->{$this->pk};
$this->__construct( $id );
}
/**
* Helper function to remove data we don't want serialized.
*
* @since 1.0.0
*
* @return array An array of data that we are OK with serializing.
*/
#[\ReturnTypeWillChange]
// The attribute above omits a deprecation notice from PHP 8.1 that is thrown because the return type of jsonSerialize() isn't "mixed".
// Once PHP 7.x is our minimum supported version, this can be removed in favour of overriding the return type in the method signature like this -
// public function jsonSerialize() : array
public function jsonSerialize() {
$array = [];
foreach ( $this->getColumns() as $column => $value ) {
if ( in_array( $column, $this->hidden, true ) ) {
continue;
}
$array[ $column ] = isset( $this->$column ) ? $this->$column : null;
}
return $array;
}
/**
* Retrieves the columns for the model.
*
* @since 1.0.0
*
* @return array An array of columns.
*/
public function getColumns() {
if ( empty( self::$columns[ get_called_class() ] ) ) {
self::$columns[ get_called_class() ] = [];
// Let's set the columns that are available by default.
$table = aioseoTableOfContents()->core->db->prefix . $this->table;
$results = aioseoTableOfContents()->core->db->execute( 'SHOW COLUMNS FROM `' . $table . '`', true );
foreach ( $results->result() as $col ) {
self::$columns[ get_called_class() ][ $col->Field ] = $col->Default;
}
if ( ! empty( $this->appends ) ) {
foreach ( $this->appends as $append ) {
self::$columns[ get_called_class() ][ $append ] = null;
}
}
}
return self::$columns[ get_called_class() ];
}
}
@@ -0,0 +1,344 @@
<?php
namespace AIOSEO\TableOfContents\Models;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The Notification DB model class.
*
* @since 1.0.0
*/
class Notification extends Model {
/**
* The name of the table in the database, without the prefix.
*
* @since 1.0.0
*
* @var string
*/
protected $table = 'aioseo_table_of_contents_notifications';
/**
* An array of fields to set to null if already empty when saving to the database.
*
* @since 1.0.0
*
* @var array
*/
protected $nullFields = [
'start',
'end',
'notification_id',
'notification_name',
'button1_label',
'button1_action',
'button2_label',
'button2_action'
];
/**
* Fields that should be json encoded on save and decoded on get.
*
* @since 1.0.0
*
* @var array
*/
protected $jsonFields = [ 'level' ];
/**
* Fields that should be boolean values.
*
* @since 1.0.0
*
* @var array
*/
protected $booleanFields = [ 'dismissed' ];
/**
* Fields that should be hidden when serialized.
*
* @var array
*/
protected $hidden = [ 'id' ];
/**
* An array of fields attached to this resource.
*
* @since 1.0.0
*
* @var array
*/
protected $columns = [
'id',
'slug',
'notification_id',
'notification_name',
'title',
'content',
'type',
'level',
'start',
'end',
'button1_label',
'button1_action',
'button2_label',
'button2_action',
'dismissed',
'new',
'created',
'updated'
];
/**
* Returns all notifications.
*
* @since 1.0.0
*
* @param bool $reset Whether or not to reset the new notifications.
* @return array List of notifications.
*/
public static function getNotifications( $reset = true ) {
return [
'active' => self::getAllActiveNotifications(),
'new' => self::getNewNotifications( $reset ),
'dismissed' => self::getAllDismissedNotifications()
];
}
/**
* Returns the active notifications.
*
* @since 1.0.0
*
* @return array List of active notifications.
*/
public static function getAllActiveNotifications() {
$staticNotifications = self::getStaticNotifications();
$notifications = array_values( json_decode( wp_json_encode( self::getActiveNotifications() ), true ) );
return ! empty( $staticNotifications ) ? array_merge( $staticNotifications, $notifications ) : $notifications;
}
/**
* Returns all static notifications.
*
* @since 1.2.0
*
* @return array List of static notifications.
*/
public static function getStaticNotifications() {
$notifications = [ 'review' ];
$staticNotifications = [];
foreach ( $notifications as $notification ) {
switch ( $notification ) {
case 'review':
// If dismissed, don't show again.
$originalDismissed = get_user_meta( get_current_user_id(), '_aioseo_table_of_contents_plugin_review_dismissed', true );
if ( '4' !== $originalDismissed ) {
break;
}
$dismissed = get_user_meta( get_current_user_id(), '_aioseo_table_of_contents_notification_plugin_review_dismissed', true );
if ( '3' === $dismissed ) {
break;
}
if ( ! empty( $dismissed ) && $dismissed > time() ) {
break;
}
$activated = aioseoTableOfContents()->internalOptions->internal->firstActivated( time() );
if ( $activated > strtotime( '-20 days' ) ) {
break;
}
$staticNotifications[] = [
'slug' => 'notification-' . $notification,
'component' => 'notifications-' . $notification . '2',
];
break;
default:
break;
}
}
return $staticNotifications;
}
/**
* Retrieve active notifications.
*
* @since 1.0.0
*
* @return array List of active notifications or empty.
*/
public static function getActiveNotifications() {
return self::filterNotifications(
aioseoTableOfContents()->core->db
->start( 'aioseo_table_of_contents_notifications' )
->where( 'dismissed', 0 )
->whereRaw( "(start <= '" . gmdate( 'Y-m-d H:i:s' ) . "' OR start IS NULL)" )
->whereRaw( "(end >= '" . gmdate( 'Y-m-d H:i:s' ) . "' OR end IS NULL)" )
->orderBy( 'start DESC' )
->orderBy( 'created DESC' )
->run()
->models( 'AIOSEO\\TableOfContents\\Models\\Notification' )
);
}
/**
* Returns all new notifications. After retrieving them, this will reset them.
* This means that calling this method twice will result in no results
* the second time. The only exception is to pass false as a reset variable to prevent it.
*
* @since 1.0.0
*
* @param boolean $reset Whether or not to reset the new notifications.
* @return array List of new notifications if any exist.
*/
public static function getNewNotifications( $reset = true ) {
$notifications = self::filterNotifications(
aioseoTableOfContents()->core->db
->start( 'aioseo_table_of_contents_notifications' )
->where( 'dismissed', 0 )
->where( 'new', 1 )
->whereRaw( "(start <= '" . gmdate( 'Y-m-d H:i:s' ) . "' OR start IS NULL)" )
->whereRaw( "(end >= '" . gmdate( 'Y-m-d H:i:s' ) . "' OR end IS NULL)" )
->orderBy( 'start DESC' )
->orderBy( 'created DESC' )
->run()
->models( 'AIOSEO\\TableOfContents\\Models\\Notification' )
);
if ( $reset ) {
self::resetNewNotifications();
}
return $notifications;
}
/**
* Resets all new notifications.
*
* @since 1.0.0
*
* @return void
*/
public static function resetNewNotifications() {
aioseoTableOfContents()->core->db
->update( 'aioseo_table_of_contents_notifications' )
->where( 'new', 1 )
->set( 'new', 0 )
->run();
}
/**
* Returns a list of dismissed notifications.
*
* @since 1.0.0
*
* @return array List of dismissed notifications.
*/
public static function getAllDismissedNotifications() {
return array_values( json_decode( wp_json_encode( self::getDismissedNotifications() ), true ) );
}
/**
* Retrieve dismissed notifications.
*
* @since 1.0.0
*
* @return array List of dismissed notifications or empty.
*/
public static function getDismissedNotifications() {
return self::filterNotifications(
aioseoTableOfContents()->core->db
->start( 'aioseo_table_of_contents_notifications' )
->where( 'dismissed', 1 )
->orderBy( 'updated DESC' )
->run()
->models( 'AIOSEO\\TableOfContents\\Models\\Notification' )
);
}
/**
* Returns a notification by its name.
*
* @since 1.0.0
*
* @param string $name The notification name.
* @return Notification The notification.
*/
public static function getNotificationByName( $name ) {
return aioseoTableOfContents()->core->db
->start( 'aioseo_table_of_contents_notifications' )
->where( 'notification_name', $name )
->run()
->model( 'AIOSEO\\TableOfContents\\Models\\Notification' );
}
/**
* Stores a new notification in the DB.
*
* @since 1.0.0
*
* @param array $fields The fields.
* @return Notification $notification The notification.
*/
public static function addNotification( $fields ) {
// Set the dismissed status to false.
$fields['dismissed'] = 0;
$notification = new self();
$notification->set( $fields );
$notification->save();
return $notification;
}
/**
* Deletes a notification by its name.
*
* @since 1.0.0
*
* @param string $name The notification name.
* @return void
*/
public static function deleteNotificationByName( $name ) {
aioseoTableOfContents()->core->db
->delete( 'aioseo_table_of_contents_notifications' )
->where( 'notification_name', $name )
->run();
}
/**
* Filters the notifications based on the targeted plan levels.
*
* @since 1.0.0
*
* @param array $notifications The notifications
* @return array $remainingNotifications The remaining notifications.
*/
public static function filterNotifications( $notifications ) {
$remainingNotifications = [];
foreach ( $notifications as $notification ) {
$levels = $notification->level;
if ( ! is_array( $levels ) ) {
$levels = empty( $notification->level ) ? [ 'all' ] : [ $notification->level ];
}
foreach ( $levels as $level ) {
if ( ! aioseoTableOfContents()->notifications->validateType( $level ) ) {
continue 2;
}
}
$remainingNotifications[] = $notification;
}
return $remainingNotifications;
}
}