Repository URL to install this package:
<?php
/**
* @package Freemius
* @copyright Copyright (c) 2015, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @since 1.1.3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class FS_Admin_Menu_Manager {
#region Properties
/**
* @since 1.2.2
*
* @var string
*/
protected $_module_unique_affix;
/**
* @since 1.2.2
*
* @var number
*/
protected $_module_id;
/**
* @since 1.2.2
*
* @var string
*/
protected $_module_type;
/**
* @since 1.0.6
*
* @var string
*/
private $_menu_slug;
/**
* @since 1.1.3
*
* @var string
*/
private $_parent_slug;
/**
* @since 1.1.3
*
* @var string
*/
private $_parent_type;
/**
* @since 1.1.3
*
* @var string
*/
private $_type;
/**
* @since 1.1.3
*
* @var bool
*/
private $_is_top_level;
/**
* @since 1.1.3
*
* @var bool
*/
private $_is_override_exact;
/**
* @since 1.1.3
*
* @var array<string,bool>
*/
private $_default_submenu_items;
/**
* @since 1.1.3
*
* @var string
*/
private $_first_time_path;
/**
* @since 1.2.2
*
* @var bool
*/
private $_menu_exists;
/**
* @since 2.0.0
*
* @var bool
*/
private $_network_menu_exists;
#endregion Properties
/**
* @var FS_Logger
*/
protected $_logger;
#region Singleton
/**
* @var FS_Admin_Menu_Manager[]
*/
private static $_instances = array();
/**
* @param number $module_id
* @param string $module_type
* @param string $module_unique_affix
*
* @return FS_Admin_Menu_Manager
*/
static function instance( $module_id, $module_type, $module_unique_affix ) {
$key = 'm_' . $module_id;
if ( ! isset( self::$_instances[ $key ] ) ) {
self::$_instances[ $key ] = new FS_Admin_Menu_Manager( $module_id, $module_type, $module_unique_affix );
}
return self::$_instances[ $key ];
}
protected function __construct( $module_id, $module_type, $module_unique_affix ) {
$this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $module_id . '_admin_menu', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
$this->_module_id = $module_id;
$this->_module_type = $module_type;
$this->_module_unique_affix = $module_unique_affix;
}
#endregion Singleton
#region Helpers
private function get_option( &$options, $key, $default = false ) {
return ! empty( $options[ $key ] ) ? $options[ $key ] : $default;
}
private function get_bool_option( &$options, $key, $default = false ) {
return isset( $options[ $key ] ) && is_bool( $options[ $key ] ) ? $options[ $key ] : $default;
}
#endregion Helpers
/**
* @param array $menu
* @param bool $is_addon
*/
function init( $menu, $is_addon = false ) {
$this->_menu_exists = ( isset( $menu['slug'] ) && ! empty( $menu['slug'] ) );
$this->_network_menu_exists = ( ! empty( $menu['network'] ) && true === $menu['network'] );
$this->_menu_slug = ( $this->_menu_exists ? $menu['slug'] : $this->_module_unique_affix );
$this->_default_submenu_items = array();
// @deprecated
$this->_type = 'page';
$this->_is_top_level = true;
$this->_is_override_exact = false;
$this->_parent_slug = false;
// @deprecated
$this->_parent_type = 'page';
if ( isset( $menu ) ) {
if ( ! $is_addon ) {
$this->_default_submenu_items = array(
'contact' => $this->get_bool_option( $menu, 'contact', true ),
'support' => $this->get_bool_option( $menu, 'support', true ),
'affiliation' => $this->get_bool_option( $menu, 'affiliation', true ),
'account' => $this->get_bool_option( $menu, 'account', true ),
'pricing' => $this->get_bool_option( $menu, 'pricing', true ),
'addons' => $this->get_bool_option( $menu, 'addons', true ),
);
// @deprecated
$this->_type = $this->get_option( $menu, 'type', 'page' );
}
$this->_is_override_exact = $this->get_bool_option( $menu, 'override_exact' );
if ( isset( $menu['parent'] ) ) {
$this->_parent_slug = $this->get_option( $menu['parent'], 'slug' );
// @deprecated
$this->_parent_type = $this->get_option( $menu['parent'], 'type', 'page' );
// If parent's slug is different, then it's NOT a top level menu item.
$this->_is_top_level = ( $this->_parent_slug === $this->_menu_slug );
} else {
/**
* If no parent then top level if:
* - Has custom admin menu ('page')
* - CPT menu type ('cpt')
*/
// $this->_is_top_level = in_array( $this->_type, array(
// 'cpt',
// 'page'
// ) );
}
$first_path = $this->get_option( $menu, 'first-path', false );
if ( ! empty( $first_path ) && is_string( $first_path ) ) {
$this->_first_time_path = $first_path;
}
}
}
/**
* Check if top level menu.
*
* @author Vova Feldman (@svovaf)
* @since 1.1.3
*
* @return bool False if submenu item.
*/
function is_top_level() {
return $this->_is_top_level;
}
/**
* Check if the page should be override on exact URL match.
*
* @author Vova Feldman (@svovaf)
* @since 1.1.3
*
* @return bool False if submenu item.
*/
function is_override_exact() {
return $this->_is_override_exact;
}
/**
* Get the path of the page the user should be forwarded to after first activation.
*
* @author Vova Feldman (@svovaf)
* @since 1.1.3
*
* @param bool $is_network Since 2.4.5
*
* @return string
*/
function get_first_time_path( $is_network = false ) {
if ( empty ( $this->_first_time_path ) ) {
return $this->_first_time_path;
}
if ( $is_network ) {
return network_admin_url( $this->_first_time_path );
} else {
return admin_url( $this->_first_time_path );
}
}
/**
* Check if plugin's menu item is part of a custom top level menu.
*
* @author Vova Feldman (@svovaf)
* @since 1.1.3
*
* @return bool
*/
function has_custom_parent() {
return ! $this->_is_top_level && is_string( $this->_parent_slug );
}
/**
* @author Leo Fajardo (@leorw)
* @since 1.2.2
*
* @return bool
*/
function has_menu() {
return $this->_menu_exists;
}
/**
* @author Vova Feldman (@svovaf)
* @since 2.0.0
*
* @return bool
*/
function has_network_menu() {
return $this->_network_menu_exists;
}
/**
* @author Leo Fajardo (@leorw)
*
* @param string $menu_slug
*
* @since 2.1.3
*/
function set_slug_and_network_menu_exists_flag($menu_slug ) {
$this->_menu_slug = $menu_slug;
$this->_network_menu_exists = false;
}
/**
* @author Vova Feldman (@svovaf)
* @since 1.1.3
*
* @param string $id
* @param bool $default
* @param bool $ignore_menu_existence Since 1.2.2.7 If true, check if the submenu item visible even if there's no parent menu.
*
* @return bool
*/
function is_submenu_item_visible( $id, $default = true, $ignore_menu_existence = false ) {
if ( ! $ignore_menu_existence && ! $this->has_menu() ) {
return false;
}
return fs_apply_filter(
$this->_module_unique_affix,
'is_submenu_visible',
$this->get_bool_option( $this->_default_submenu_items, $id, $default ),
$id
);
}
/**
* Calculates admin settings menu slug.
* If plugin's menu slug is a file (e.g. CPT), uses plugin's slug as the menu slug.
*
* @author Vova Feldman (@svovaf)
* @since 1.1.3
*
* @param string $page
*
* @return string
*/
function get_slug( $page = '' ) {
return ( ( false === strpos( $this->_menu_slug, '.php?' ) ) ?
$this->_menu_slug :
$this->_module_unique_affix ) . ( empty( $page ) ? '' : ( '-' . $page ) );
}
Loading ...