filter(function ($item) {
return strpos($item, 'content');
})->map(function ($item) {
$item = explode('content:', $item);
$name = strstr($item[0], ':', true);
$css = strstr(strrchr($item[1], '\\f'), '"', true);
return [$name, $css];
})->values();
}
public static function js()
{
return Collection::make([
'text' => Text::data(),
'existingAdminMenuTitles' => static::notUsedExistingMenuTitles(),
'data' => static::pageData()
]);
}
public static function pageData()
{
if ($_REQUEST['page'] === SubpageCreate::SLUG) {
return Collection::make([
'registered_options' => static::getAllOptionNamesExcludeCurrentPageFieldNames()
]);
}
if (!isset($_REQUEST['action']) && !isset($_REQUEST['optionpage']) || $_REQUEST['page'] === SubpageMaster::SLUG) {
return [];
}
$currentPage = static::listTableData()->filter(function ($value) {
return $_REQUEST['optionpage'] === $value['ID'];
})->values();
$allRows = $_POST ? DB::allRowsFromPluginTable() : static::getAllRowsFromPluginTable();
$allRowsFromPluginTable = Collection::make($allRows)->filter(function ($row) use ($currentPage) {
return $currentPage[0]['ID'] === $row->id;
})->flatten(1)->map(function ($value) {
return unserialize($value->page_value);
})->first();
return Collection::make($allRowsFromPluginTable)->merge([
'id' => $currentPage[0]['ID'],
'nonce' => wp_create_nonce($currentPage[0]['ID']),
'page' => $_REQUEST['page'],
'registered_options' => static::getAllOptionNamesExcludeCurrentPageFieldNames()
]);
}
/**
* Existing admin menu titles.
*
* @global array $GLOBALS['menu']
* @return object
*/
public static function existingAdminMenuTitles()
{
return Collection::make($GLOBALS['menu'])->filter(function ($value) {
return in_array($value[2], static::defaultWordpressMenuPageSlugs());
})->map(function ($value) {
if (strpos($value[0], '')) {
$value[0] = substr($value[0], 0, strpos($value[0], ' $value[0], 'slug' => $value[2]];
})->values();
}
/**
* Existing admin menu title minus the ones who are already used.
*
* @global array $GLOBALS['menu']
* @return object
*/
public static function notUsedExistingMenuTitles()
{
$parentSlug = isset($_REQUEST['existing_menu'])
? [$_REQUEST['existing_menu']['menu_slug']]
: static::getParentSlugFromSubpages()->values()->toArray();
if ($GLOBALS['menu']) {
global $menu;
} else {
$menu = get_option(Plugin::PREFIX_ . 'admin_menu_list');
}
return Collection::make($menu)->filter(function ($value) use ($parentSlug) {
return in_array($value[2], static::defaultWordpressMenuPageSlugs()) && !in_array($value[2], $parentSlug);
})->map(function ($value) {
if (strpos($value[0], '') !== false) {
$value[0] = substr($value[0], 0, strpos($value[0], ' $value[0], 'slug' => $value[2]];
})->values();
}
/**
* Get all default WordPress menu titles and slugs.
*
* @return object
*/
public static function getDefaultWordpressMenus()
{
if (isset($GLOBALS['menu'])) {
global $menu;
} else {
$menu = get_option(Plugin::PREFIX_ . 'admin_menu_list');
}
return Collection::make($menu)->filter(function ($value) {
return in_array($value[2], static::defaultWordpressMenuPageSlugs());
})->map(function ($value, $key) {
if (strpos($value[0], '') !== false) {
$value[0] = substr($value[0], 0, strpos($value[0], ' $value[0],
'slug' => $value[2],
'position' => $key
];
})->values();
}
public static function defaultWordpressMenuPageSlugs()
{
return [
'index.php',
'edit.php',
'upload.php',
'edit.php?post_type=page',
'edit-comments.php',
'themes.php',
'plugins.php',
'users.php',
'tools.php',
'options-general.php'
];
}
public static function getParentSlugFromSubpages()
{
return static::getSubpages()
->pluck('parent_slug')
->unique();
}
public static function getSubpages($id = null)
{
$value = ($id === null)
? static::getAllFromPageValueColumn()
: static::getPageValueFromSpecificId($id);
return Collection::make($value)
->pluck('pages')
->flatten(1);
}
public static function requestGetSubpages()
{
if (!isset($_REQUEST['pages'])) {
return;
}
return Collection::make($_REQUEST['pages']);
}
public static function getFields()
{
return static::getSubpages()
->pluck('fields')
->flatten(1);
}
public static function getFieldsfromId($id)
{
return static::getSubpages($id)
->pluck('fields')
->flatten(1);
}
public static function getOptionNamesFromSettingPagesById($id)
{
return static::getFieldsfromId($id)->filter(function ($field) {
return isset($field['field_name']);
})->pluck('field_name')->flatten(1);
}
public static function getPageNamesFromOptionsPages()
{
return static::getSubpages()->map(function ($page) {
return $page['menu_slug'];
});
}
public static function getAdminPagesId()
{
return Collection::make([
'options-pages_page_' . SubpageCreate::SLUG,
'admin_page_' . SubpageEdit::SLUG,
'toplevel_page_' . SubpageMaster::SLUG
]);
}
/**
* Get all options names from options table.
*
* @return array
*/
public static function getAllOptionNames()
{
if (static::$getAllOptionNames === null) {
static::$getAllOptionNames = Collection::make(DB::allOptionNamesFromOptionsTable())
->pluck('option_name')
->merge(static::getAllCreatedOptionNames());
}
return static::$getAllOptionNames;
}
/**
* Get all options names from options table exclude current subpage field names.
*
* @return array
*/
public static function getAllOptionNamesExcludeCurrentPageFieldNames()
{
if (static::$getAllOptionNamesExcludeCurrentPageFieldNames === null) {
static::$getAllOptionNamesExcludeCurrentPageFieldNames = static::getAllOptionNames()
->diff(static::getAllRegisteredFieldNames())
->all();
}
return static::$getAllOptionNamesExcludeCurrentPageFieldNames;
}
/**
* Get all options names from options table created by this plugin.
* To be clear: field_name === option_name.
*
* @return object
*/
public static function getAllCreatedOptionNames()
{
return static::$getAllFromPageValueColumn
->pluck('pages')->flatten(1)
->pluck('fields')->flatten(1)
->pluck('field_name');
}
/**
* Get all field names which already registered.
*
* @return array
*/
public static function getAllRegisteredFieldNames()
{
if (!isset($_REQUEST['optionpage'])) {
return [];
}
return static::getFieldsfromId($_REQUEST['optionpage'])->pluck('field_name');
}
public static function getAllCreatedOptionNamesAfterEdit()
{
return Data::getAllCreatedOptionNames()
->diff(Data::getFieldNamesCurrentPage())
->merge(Data::getAllRegisteredFieldNames());
}
public static function getAllCreatedOptionNamesAfterSave()
{
return Data::getAllCreatedOptionNames()
->diff(Data::getFieldNamesFromSession())
->merge(Data::getAllRegisteredFieldNames());
}
public static function getFieldsWithFieldNames()
{
return Collection::make($_REQUEST['pages'])
->pluck('fields')->flatten(1)
->pluck('field_name')->filter();
return Collection::make($_REQUEST['pages'])->flatMap(function ($page) {
return isset($page['fields']) ? $page['fields'] : null;
})->pluck('field_name');
}
public static function getFieldNamesCurrentPage()
{
if (!$_REQUEST['menu_id']) {
return;
}
return static::$getAllFromPageValueColumn->where('menu_id', $_REQUEST['menu_id'])
->pluck('pages')->flatten(1)
->pluck('fields')->flatten(1)
->pluck('field_name');
}
public static function getFieldNamesFromSession()
{
if (!$_SESSION['aop_request_on_save']) {
return;
}
return Collection::make($_SESSION['aop_request_on_save']['pages'])
->pluck('fields')->flatten(1)
->pluck('field_name');
}
/**
* All data from column page_value.
*
* @return object
*/
public static function getAllFromPageValueColumn()
{
if (static::$getAllFromPageValueColumn === null) {
static::$getAllFromPageValueColumn = Collection::make(DB::getAllFromColumnPageValue())
->pluck('page_value')
->map(function ($item) {
return unserialize($item);
});
}
return static::$getAllFromPageValueColumn;
}
public static function getAllRowsFromPluginTable()
{
if (static::$getAllRowsFromPluginTable === null) {
static::$getAllRowsFromPluginTable = DB::allRowsFromPluginTable();
}
return static::$getAllRowsFromPluginTable;
}
public function getIdFromTableRow()
{
return Collection::make(DB::allRowsFromPluginTable())->pluck('id');
}
/**
* The menu_id from all created menus.
*
* @return object
*/
public function menuIdFromAllCreatedMenus()
{
return static::getAllFromPageValueColumn()->map(function ($page) {
return [
'menu_id' => $page['menu_id'],
'menu_type' => $page['menu'] ? 'menu' : 'existing_menu'
];
});
}
/**
* The menu_id from all created menus.
*
* @return object
*/
public function menuIdFromAllExistingMenus()
{
return static::getAllFromPageValueColumn()->filter(function ($page) {
return $page['existing_menu'];
})->pluck('menu_id');
}
/**
* Get page_value from specific id.
*
* @return object
*/
private static function getPageValueFromSpecificId($id)
{
return Collection::make(DB::rowFromPluginTable($id))->map(function ($row) {
return unserialize($row->page_value);
});
}
public static function listTableData()
{
return Collection::make(static::getAllRowsFromPluginTable())->map(function ($row) {
$pageValue = unserialize($row->page_value);
if (isset($pageValue['existing_menu'])) {
$menuItems = Collection::make($GLOBALS['menu'])->filter(function ($value) {
return in_array($value[2], static::defaultWordpressMenuPageSlugs());
})->filter(function ($item) use ($pageValue) {
return $item[2] === $pageValue['existing_menu']['menu_slug'];
})->map(function ($item, $key) {
return array_merge($item, ['position' => $key]);
})->flatten(1);
$dashicon = $menuItems[6];
$position = $menuItems->last();
$menuName = '' . $pageValue['existing_menu']['menu_name'];
}
if (isset($pageValue['menu'])) {
$position = $pageValue['menu']['position'];
$menuName = '' . $pageValue['menu']['menu_title'];
}
$totalPages = count($pageValue['pages']);
return [
'ID' => $row->id,
'title' => $menuName,
'position' => (float) $position,
'pages' => (string) $totalPages
];
});
}
/**
* Undocumented function
*
* @return void
*/
public static function deleteRowFromSpecificId()
{
if (!isset($_REQUEST['action'], $_REQUEST['optionpage'], $_REQUEST['nonce'])) {
return;
}
list($action, $id, $nonce) = [$_REQUEST['action'], $_REQUEST['optionpage'], $_REQUEST['nonce']];
if (!wp_verify_nonce($nonce, $id) && $action !== 'delete') {
return;
}
static::getOptionNamesFromSettingPagesById($id)->map(function ($optionName) {
return delete_option($optionName);
});
session_start();
$_SESSION['aop_pages_deleted'] = true;
$_SESSION['aop_pages_deleted_multiple'] = static::getSubpages($id)->count() > 1 ? true : false;
DB::deleteRowById($id);
wp_safe_redirect(SubpageMaster::url());
exit;
}
/**
* Page ID from specific edit page.
*
* @return int
*/
public static function IdEditPage()
{
return static::listTableData()->filter(function ($value) {
return $_REQUEST['optionpage'] === $value['ID'];
})->values()[0]['ID'];
}
}