Text::data(), 'existingAdminMenuTitles' => static::notUsedExistingMenuTitles(), 'data' => static::pageData() ]); } /** * @return Collection|array */ public static function pageData() { if ($_GET['page'] === SubpageCreate::SLUG) { return Collection::make([ 'registered_options' => static::getAllOptionNamesExcludeCurrentPageFieldNames() ]); } if (!isset($_GET['action']) && !isset($_GET['optionpage']) || $_GET['page'] === SubpageMaster::SLUG) { return []; } $currentPage = static::listTableData()->filter(function ($value) { return $_GET['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' => $_GET['page'], 'registered_options' => static::getAllOptionNamesExcludeCurrentPageFieldNames() ]); } /** * Existing admin menu titles. * * @return object * @global array $GLOBALS ['menu'] */ 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. * * @return object * @global array $GLOBALS ['menu'] */ public static function notUsedExistingMenuTitles() { $parentSlug = static::getParentSlugFromSubpages()->values()->toArray(); if (isset($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' ]; } /** * @return Collection */ public static function getParentSlugFromSubpages() { return static::getSubpages() ->pluck('parent_slug') ->unique(); } /** * @param null $id * * @return Collection */ public static function getSubpages($id = null) { $value = ($id === null) ? static::getAllFromPageValueColumn() : static::getPageValueFromSpecificId($id); // dump(static::getAllFromPageValueColumn()); // dump($value); return Collection::make($value) ->pluck('pages') ->flatten(1); } /** * @return Collection|void */ public static function requestGetSubpages() { if (!isset($_POST['pages'])) { return; } return Collection::make($_POST['pages']); } /** * @return Collection */ public static function getFields() { return static::getSubpages() ->pluck('fields') ->flatten(1); } /** * @param $id * * @return Collection */ public static function getFieldsfromId($id) { return static::getSubpages($id) ->pluck('fields') ->flatten(1); } /** * @param $id * * @return Collection */ public static function getOptionNamesFromSettingPagesById($id) { return static::getFieldsfromId($id)->filter(function ($field) { return isset($field['field_name']); })->pluck('field_name')->flatten(1); } /** * @return Collection */ public static function getPageNamesFromOptionsPages() { return static::getSubpages()->map(function ($page) { return $page['menu_slug']; }); } /** * @return Collection */ public static function adminPagesIds() { 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 = Collection::make(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() { if (is_null(static::$getAllFromPageValueColumn)) { return; } 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($_GET['optionpage'])) { return []; } return static::getFieldsfromId($_GET['optionpage'])->pluck('field_name'); } /** * @return mixed */ public static function getAllCreatedOptionNamesAfterEdit() { return Data::getAllCreatedOptionNames() ->diff(Data::getFieldNamesCurrentPage()) ->merge(Data::getAllRegisteredFieldNames()); } /** * @return mixed */ public static function getAllCreatedOptionNamesAfterSave() { return Data::getAllCreatedOptionNames() ->diff(Data::getFieldNamesFromSession()) ->merge(Data::getAllRegisteredFieldNames()); } /** * @return Collection */ public static function getFieldsWithFieldNames() { return Collection::make($_POST['pages']) ->pluck('fields')->flatten(1) ->pluck('field_name')->filter(); } public static function getFieldNamesCurrentPage() { if (!$_POST['menu_id']) { return; } return static::$getAllFromPageValueColumn->where('menu_id', $_POST['menu_id']) ->pluck('pages')->flatten(1) ->pluck('fields')->flatten(1) ->pluck('field_name'); } public static function getFieldNamesFromSession() { if (!Session::get('request_on_save')) { return; } return Collection::make(Session::get('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) { $item = unserialize($item); if (!isset($item['menu'])) { return $item; } $pages = Collection::make($item['pages'])->filter(function ($item) { return User::hasCapability($item['capability']); }); if ($pages->isEmpty()) { return $item; } $firstPage = $pages->first(); if ($item['menu']['menu_slug'] === $firstPage['menu_slug']) { return $item; } $item['menu']['menu_slug'] = $firstPage['menu_slug']; $item['menu']['menu_slug_suffix'] = $firstPage['subpage_slug_suffix']; $item['pages'] = $pages->map(function ($page) use ($item) { $page['parent_slug'] = $item['menu']['menu_slug']; return $page; }); return $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(static::getAllRowsFromPluginTable())->pluck('id'); // } public function getIdfromOptionPage($slug) { return Collection::make(static::getAllRowsFromPluginTable()) ->where('menu_slug', $slug) ->pluck('id') ->first(); } /** * 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); }); } /** * @return Collection */ 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']; $menuIcon = $dashicon; $menuName = $pageValue['existing_menu']['menu_name']; } else { $position = $pageValue['menu']['position']; // $menuName = '' . $pageValue['menu']['menu_title']; $menuIcon = $pageValue['menu']['icon_url']; $menuName = $pageValue['menu']['menu_title']; } $totalPages = count($pageValue['pages']); return [ 'ID' => $row->id, 'title' => $menuName, 'icon' => $menuIcon, 'position' => (float)$position, 'pages' => (string)$totalPages ]; }); } /** * Delete an options page and its option values. * * Only from the overview or the edit page, for users with manage_options and with a valid nonce. * * @return void */ public static function deleteRowFromSpecificId() { if (!isset($_GET['action'], $_GET['optionpage'], $_GET['nonce']) || $_GET['action'] !== 'delete') { return; } if (!(SubpageEdit::isCurrentPage() || SubpageMaster::isCurrentPage()) || !current_user_can('manage_options')) { return; } $id = absint($_GET['optionpage']); if (!$id || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['nonce'])), (string) $id)) { wp_die(esc_html__('The link you followed has expired.', 'admin-options-pages'), 403); } static::getOptionNamesFromSettingPagesById($id)->map(function ($optionName) { return delete_option($optionName); }); Session::add([ 'pages_deleted' => true, '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() { if (!isset($_GET['optionpage'])) { return; } return static::listTableData()->filter(function ($value) { return $_GET['optionpage'] === $value['ID']; })->values()[0]['ID']; } }