| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FilterEverything\Filter; |
| 5 |
|
| 6 |
if ( ! defined('ABSPATH') ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
class Admin |
| 11 |
{ |
| 12 |
public $tabRenderer; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
add_action( 'admin_menu', array($this, 'adminMenu'), 9); |
| 17 |
$this->tabRenderer = Container::instance()->getTabRenderer(); |
| 18 |
$filterSet = Container::instance()->getFilterSetService(); |
| 19 |
|
| 20 |
add_action( 'pre_post_update', [$filterSet, 'preSaveSet'], 10, 2 ); |
| 21 |
add_action( 'save_post', array( $filterSet, 'saveSet' ), 10, 2 ); |
| 22 |
|
| 23 |
add_action( 'init', array( $this, 'initTabs' ), 11 ); |
| 24 |
|
| 25 |
add_action( 'admin_init', array( $this, 'init' ) ); |
| 26 |
|
| 27 |
add_filter( 'wpc_general_filters_settings', [$this, 'generalFilterSettings'] ); |
| 28 |
|
| 29 |
add_action('admin_head', array($this, 'addAdminStyles')); |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
public function init() |
| 34 |
{ |
| 35 |
$filterFields = Container::instance()->getFilterFieldsService(); |
| 36 |
$filterFields->registerHooks(); |
| 37 |
|
| 38 |
// Check permissions before to show these screens |
| 39 |
add_action( 'load-post.php', [ $this, 'checkPermissions' ] ); |
| 40 |
add_action( 'load-edit.php', [ $this, 'checkPermissions' ] ); |
| 41 |
add_action( 'load-post-new.php', [ $this, 'checkPermissions' ] ); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
public function adminMenu() |
| 46 |
{ |
| 47 |
global $submenu; |
| 48 |
$page = 'edit.php?post_type=' . FLRT_FILTERS_SET_POST_TYPE; |
| 49 |
|
| 50 |
add_menu_page( esc_html__('Filters', 'filter-everything'), esc_html__('Filters', 'filter-everything'), 'manage_options', $page, false, 'none', '85'); |
| 51 |
|
| 52 |
add_submenu_page( $page, esc_html__('Filter Sets', 'filter-everything'), esc_html__('Filter Sets', 'filter-everything'), 'manage_options', $page); |
| 53 |
add_submenu_page( $page, esc_html__('Add New', 'filter-everything'), esc_html__('Add New', 'filter-everything'), 'manage_options', 'post-new.php?post_type=' . FLRT_FILTERS_SET_POST_TYPE); |
| 54 |
|
| 55 |
if (!defined('FLRT_FILTERS_PRO')) { |
| 56 |
$settings = flrt_vailable_in_pro_attr_link(); |
| 57 |
|
| 58 |
// Import/Export is deliberately NOT a menu entry (free or PRO): it is a |
| 59 |
// rare task and lives on its Settings tab — the menu and the toolbar |
| 60 |
// that mirrors it stay short. |
| 61 |
add_submenu_page($page, esc_html__('SEO Rules', 'filter-everything'), esc_html__('SEO Rules', 'filter-everything'), 'manage_options', $settings); |
| 62 |
|
| 63 |
if (isset($submenu[$page])) { |
| 64 |
foreach ($submenu[$page] as $key => $details) { |
| 65 |
if ($details[2] === $settings) { |
| 66 |
$submenu[$page][$key][0] .= flrt_pro_promo_label(true); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
do_action('wpc_add_submenu_pages'); |
| 73 |
|
| 74 |
add_submenu_page( $page, esc_html__('Settings', 'filter-everything'), esc_html__('Settings', 'filter-everything'), 'manage_options', 'filters-settings', array($this, 'filterSettingsPage')); |
| 75 |
|
| 76 |
do_action('wpc_after_add_submenu_pages'); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
public function filterSettingsPage() |
| 81 |
{ |
| 82 |
$this->tabRenderer->render(); |
| 83 |
} |
| 84 |
|
| 85 |
public function initTabs() |
| 86 |
{ |
| 87 |
$this->tabRenderer->register(new SettingsTab()); |
| 88 |
$this->tabRenderer->register(new PermalinksTab()); |
| 89 |
|
| 90 |
do_action( 'wpc_setttings_tabs_register', $this->tabRenderer ); |
| 91 |
|
| 92 |
$this->tabRenderer->register(new ExperimentalTab()); |
| 93 |
|
| 94 |
if( ! defined('FLRT_FILTERS_PRO') ) { |
| 95 |
$this->tabRenderer->register( new AboutProTab() ); |
| 96 |
|
| 97 |
}else{ |
| 98 |
$show_license_tab = false; |
| 99 |
|
| 100 |
if ( is_multisite() ) { |
| 101 |
if ( is_main_site() ) { |
| 102 |
$show_license_tab = true; |
| 103 |
} |
| 104 |
} else { |
| 105 |
$show_license_tab = true; |
| 106 |
} |
| 107 |
|
| 108 |
if ( $show_license_tab ) { |
| 109 |
$this->tabRenderer->register(new LicenseTab( FLRT_LICENSE_KEY )); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$this->tabRenderer->init(); |
| 114 |
} |
| 115 |
|
| 116 |
public function addAdminStyles() |
| 117 |
{ |
| 118 |
if(defined('FLRT_FILTERS_PRO') && FLRT_FILTERS_PRO) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
if (!is_admin()) { |
| 123 |
return; |
| 124 |
} |
| 125 |
?> |
| 126 |
<style type="text/css"> |
| 127 |
.wpc-pro-badge { |
| 128 |
background: #3858E9; |
| 129 |
color: #fff; |
| 130 |
padding: 2px 6px; |
| 131 |
border-radius: 14px; |
| 132 |
margin-left: 6px; |
| 133 |
font-size: 10px; |
| 134 |
border: 1px solid #3858E9; |
| 135 |
position: absolute; |
| 136 |
} |
| 137 |
</style> |
| 138 |
<?php |
| 139 |
} |
| 140 |
|
| 141 |
public function checkPermissions() |
| 142 |
{ |
| 143 |
$screen = get_current_screen(); |
| 144 |
$post_types = [ FLRT_FILTERS_SET_POST_TYPE, FLRT_FILTERS_POST_TYPE ]; |
| 145 |
|
| 146 |
if( defined('FLRT_FILTERS_PRO') && FLRT_FILTERS_PRO ){ |
| 147 |
$post_types[] = FLRT_SEO_RULES_POST_TYPE; |
| 148 |
} |
| 149 |
|
| 150 |
if( ! is_null( $screen ) && property_exists( $screen, 'post_type' ) && in_array( $screen->post_type, $post_types, true ) ){ |
| 151 |
if( ! current_user_can( flrt_plugin_user_caps() ) ) { |
| 152 |
wp_die( esc_html__( 'Sorry, you are not allowed to access this page.' ) ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
public function generalFilterSettings( $settings ) |
| 158 |
{ |
| 159 |
$result_terms = []; |
| 160 |
|
| 161 |
// Chips hooks |
| 162 |
$maybe_saved_terms = flrt_get_option('show_terms_in_content', []); |
| 163 |
$theme_dependencies = flrt_get_theme_dependencies(); |
| 164 |
|
| 165 |
$current_terms = $settings['common_settings']['fields']['show_terms_in_content']['options']; |
| 166 |
|
| 167 |
if( flrt_is_woocommerce() ){ |
| 168 |
$woocommerce_terms = array( |
| 169 |
'woocommerce_archive_description' => esc_html__('WooCommerce archive description', 'filter-everything' ), |
| 170 |
'woocommerce_no_products_found' => esc_html__('WooCommerce no products found', 'filter-everything' ), |
| 171 |
'woocommerce_before_shop_loop' => esc_html__('WooCommerce before Shop loop', 'filter-everything' ), |
| 172 |
'woocommerce_before_main_content' => esc_html__('WooCommerce before main content', 'filter-everything' ) |
| 173 |
); |
| 174 |
|
| 175 |
$result_terms = array_merge( $current_terms, $woocommerce_terms ); |
| 176 |
} |
| 177 |
|
| 178 |
if( $maybe_saved_terms && is_array( $maybe_saved_terms )){ |
| 179 |
foreach ($maybe_saved_terms as $hook ){ |
| 180 |
if( ! in_array( $hook, array_keys( $result_terms ) ) ){ |
| 181 |
$result_terms[$hook] = $hook; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if( isset( $theme_dependencies['chips_hook'] ) && ! empty( $theme_dependencies['chips_hook'] )){ |
| 187 |
foreach ($theme_dependencies['chips_hook'] as $hook ){ |
| 188 |
if( ! in_array( $hook, array_keys( $result_terms ) ) ){ |
| 189 |
$result_terms[$hook] = $hook; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
$settings['common_settings']['fields']['show_terms_in_content']['options'] = $result_terms; |
| 195 |
|
| 196 |
return $settings; |
| 197 |
} |
| 198 |
|
| 199 |
} |