| Server IP : 43.130.17.34 / Your IP : 216.73.217.6 Web Server : nginx/1.28.0 System : Linux VM-4-12-opencloudos 6.6.92-34.1.oc9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 25 21:32:13 CST 2025 x86_64 User : www ( 1000) PHP Version : 8.0.26 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wwwroot/yespkg.com/wp-content/plugins/admin-columns-pro.5.1.3/classes/Search/ |
Upload File : |
<?php
namespace ACP\Search;
use AC\Preferences;
use ACP\Search\Segments\Segment;
final class Segments {
const ERROR_DUPLICATE_NAME = 'Segment with this name already exists.';
const ERROR_NAME_NOT_FOUND = 'No segment found with this name.';
const ERROR_SAVING = 'Could not save segments.';
/**
* @var Preferences
*/
private $preferences;
/**
* @var Segment[]
*/
private $segments = [];
/**
* @var array
*/
private $errors = [];
/**
* @param Preferences $preferences
*/
public function __construct( Preferences $preferences ) {
$this->preferences = $preferences;
$this->set_segments();
}
/**
* @return array
*/
public function get_errors() {
return $this->errors;
}
/**
* @return string
*/
public function get_first_error() {
$errors = $this->get_errors();
return array_shift( $errors );
}
/**
* @return bool
*/
public function has_errors() {
return count( $this->errors ) > 0;
}
/**
* @param string $context
* @param string $message
*/
private function add_error( $context, $message ) {
$this->errors[ $context ] = $message;
}
/**
* @return void
*/
private function set_segments() {
if ( ! $this->preferences->get( 'segments' ) ) {
return;
}
foreach ( $this->preferences->get( 'segments' ) as $segment ) {
$data = unserialize( $segment['data'] );
if ( ! is_array( $data ) ) {
continue;
}
$this->segments[ $segment['name'] ] = new Segment(
$segment['name'],
$data
);
}
}
/**
* @return Segment[]
*/
public function get_segments() {
return $this->segments;
}
/**
* @param $name
*
* @return false|Segment
*/
public function get_segment( $name ) {
return isset( $this->segments[ $name ] )
? $this->segments[ $name ]
: false;
}
/**
* @param Segment $segment
*
* @return $this
*/
public function add_segment( Segment $segment ) {
if ( $this->get_segment( $segment->get_name() ) ) {
$this->add_error( 'Adding segment', self::ERROR_DUPLICATE_NAME );
}
$this->segments[ $segment->get_name() ] = $segment;
return $this;
}
/**
* @param string $name
*
* @return $this
*/
public function remove_segment( $name ) {
if ( ! $this->get_segment( $name ) ) {
$this->add_error( 'Removing segment', self::ERROR_NAME_NOT_FOUND );
}
unset( $this->segments[ $name ] );
return $this;
}
/**
* @param string $name
* @param Segment $segment
*
* @return $this
*/
public function update_segment( $name, Segment $segment ) {
if ( $name !== $segment->get_name() && $this->get_segment( $segment->get_name() ) ) {
$this->add_error( 'Updating segment', self::ERROR_DUPLICATE_NAME );
}
if ( ! $this->remove_segment( $name ) ) {
$this->add_error( 'Updating segment', self::ERROR_NAME_NOT_FOUND );
}
$this->add_segment( $segment );
return $this;
}
/**
* @return bool
*/
public function save() {
if ( $this->has_errors() ) {
return false;
}
$data = [];
// Sort natural by name
uksort( $this->segments, 'strnatcmp' );
foreach ( $this->segments as $segment ) {
$data[] = [
'name' => $segment->get_name(),
'data' => serialize( $segment->get_data() ),
];
}
$result = $this->preferences->set( 'segments', $data );
if ( ! $result ) {
$this->add_error( 'Saving segments', 'Could not save segment' );
return false;
}
return true;
}
}