| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FilterEverything\Filter; |
| 5 |
|
| 6 |
if ( ! defined('WPINC') ) { |
| 7 |
wp_die(); |
| 8 |
} |
| 9 |
|
| 10 |
class AdminHooks |
| 11 |
{ |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
add_filter( 'manage_edit-'.FLRT_FILTERS_SET_POST_TYPE.'_columns', array( $this, 'filterSetPostTypeCol' ) ); |
| 16 |
add_action( 'manage_'.FLRT_FILTERS_SET_POST_TYPE.'_posts_custom_column', array( $this, 'filterSetPostTypeColContent'), 10, 2 ); |
| 17 |
|
| 18 |
add_filter( 'manage_edit-'.FLRT_FILTERS_SET_POST_TYPE.'_sortable_columns', array( $this, 'filterSetSortableColumn') ); |
| 19 |
add_action( 'pre_get_posts', array( $this, 'filterSetOrderby' ) ); |
| 20 |
|
| 21 |
add_action('admin_notices', [$this, 'adminNotices'] ); |
| 22 |
add_action('admin_notices', [$this, 'checkForbiddenPrefixes'] ); |
| 23 |
|
| 24 |
add_filter( 'plugin_action_links_' . FLRT_PLUGIN_BASENAME, [$this,'actionsLink'] ); |
| 25 |
|
| 26 |
add_action( 'current_screen', [$this, 'currentScreen' ] ); |
| 27 |
add_action( 'wpc_admin_toolbar_right', [$this, 'aboutPro' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
public function aboutPro() |
| 31 |
{ |
| 32 |
if( ! defined('FLRT_FILTERS_PRO') ){ |
| 33 |
echo '<a class="wpc-tab wpc-get-pro" href="'.admin_url('edit.php?post_type=filter-set&page=filters-settings&tab=aboutpro').'">'.esc_html__( 'About PRO', 'filter-everything' ).'</a>'."\r\n"; |
| 34 |
echo '<a class="wpc-tab wpc-get-pro button button-primary" href="'.esc_url(FLRT_PLUGIN_LINK .'/?get_pro=true').'" target="_blank">'.esc_html__( 'Get PRO', 'filter-everything' ).'</a>'."\r\n"; |
| 35 |
} |
| 36 |
|
| 37 |
$this->addHelpTab(); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
public function addHelpTab() { |
| 42 |
$screen = get_current_screen(); |
| 43 |
|
| 44 |
// Overview tab. |
| 45 |
$screen->add_help_tab( |
| 46 |
array( |
| 47 |
'id' => 'overview', |
| 48 |
'title' => esc_html__( 'Overview', 'filter-everything' ), |
| 49 |
'content' => |
| 50 |
'<p><strong>' . esc_html__( 'Overview', 'filter-everything' ) . '</strong></p>' . |
| 51 |
'<p>' . esc_html__( 'Filter Everything — WordPress & WooCommerce product filter plugin, that allows you to design flexible filtering system.', 'filter-everything' ) . '</p>'. |
| 52 |
'<p>' . esc_html__( 'It is fast, convenient and intuitive, and also contains unique features for SEO (in PRO version).', 'filter-everything' ) . '</p>' |
| 53 |
) |
| 54 |
); |
| 55 |
|
| 56 |
// Help tab. |
| 57 |
$screen->add_help_tab( |
| 58 |
array( |
| 59 |
'id' => 'help', |
| 60 |
'title' => esc_html__( 'Help & Support', 'filter-everything' ), |
| 61 |
'content' => |
| 62 |
'<p><strong>' . esc_html__( 'Help & Support', 'filter-everything' ) . '</strong></p>' . |
| 63 |
'<ul>' . |
| 64 |
'<li>' . wp_kses( sprintf( |
| 65 |
__( '<a href="%s" target="_blank">Documentation</a>. Common information how to work with the plugin can be found in our documentation.', 'filter-everything' ), |
| 66 |
'https://filtereverything.pro/resources/' |
| 67 |
), |
| 68 |
array( |
| 69 |
'a' => array( 'href' => true, 'target' => true ) |
| 70 |
) |
| 71 |
). '</li>' . |
| 72 |
'<li>' . wp_kses( |
| 73 |
sprintf( |
| 74 |
__( '<a href="%s" target="_blank">Support</a>. Create a technical support request if you are unable to resolve the issue yourself.', 'filter-everything' ), |
| 75 |
'https://filtereverything.pro/support/' |
| 76 |
), |
| 77 |
array( |
| 78 |
'a' => array( 'href' => false, 'target' => true ) |
| 79 |
) |
| 80 |
). '</li>' . |
| 81 |
'</ul>' |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
// Sidebar. |
| 86 |
$screen->set_help_sidebar( |
| 87 |
'<p><strong>' . esc_html__( 'Information', 'filter-everything' ) . '</strong></p>' . |
| 88 |
'<p><span class="dashicons dashicons-admin-plugins"></span> ' . sprintf( esc_html__( 'Version %s', 'filter-everything' ), FLRT_PLUGIN_VER ) . '</p>' . |
| 89 |
'<p><span class="dashicons dashicons-wordpress"></span> <a href="https://wordpress.org/plugins/filter-everything/" target="_blank">' . esc_html__( 'View details', 'filter-everything' ) . '</a></p>' . |
| 90 |
'<p><span class="dashicons dashicons-admin-home"></span> <a href="https://filtereverything.pro/" target="_blank" target="_blank">' . esc_html__( 'Visit website', 'filter-everything' ) . '</a></p>' . |
| 91 |
'' |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
public function currentScreen( $screen ) |
| 96 |
{ |
| 97 |
$screenList = [FLRT_FILTERS_SET_POST_TYPE]; |
| 98 |
|
| 99 |
if( defined( 'FLRT_SEO_RULES_POST_TYPE' ) ){ |
| 100 |
$screenList[] = FLRT_SEO_RULES_POST_TYPE; |
| 101 |
} |
| 102 |
|
| 103 |
if( isset( $screen->post_type ) && in_array( $screen->post_type, $screenList ) ){ |
| 104 |
add_action( 'in_admin_header', [ $this, 'inAdminHeader' ] ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public function inAdminHeader() |
| 109 |
{ |
| 110 |
$templateManager = Container::instance()->getTemplateManager(); |
| 111 |
$templateManager->includeAdminView( 'header-navigation' ); |
| 112 |
} |
| 113 |
|
| 114 |
public function actionsLink( $actions ) { |
| 115 |
$actionsLink = array( |
| 116 |
'<a href="' . admin_url( 'post-new.php?post_type=filter-set' ) . '">'.esc_html__('Add Filters', 'filter-everything').'</a>', |
| 117 |
'<a href="' . admin_url( 'edit.php?post_type=filter-set&page=filters-settings' ) . '">'.esc_html__('Settings', 'filter-everything').'</a>' |
| 118 |
); |
| 119 |
|
| 120 |
$actions = array_merge( $actionsLink, $actions ); |
| 121 |
|
| 122 |
if( ! defined( 'FLRT_FILTERS_PRO' ) ){ |
| 123 |
$actions[] = '<a href="' . esc_url(FLRT_PLUGIN_LINK .'/?get_pro=true') . '" class="wpc-go-pro" target="_blank">'.esc_html__('Get PRO', 'filter-everything').'</a>'; |
| 124 |
} |
| 125 |
|
| 126 |
return $actions; |
| 127 |
} |
| 128 |
|
| 129 |
public function checkForbiddenPrefixes() |
| 130 |
{ |
| 131 |
$forbiddenPrefixes = flrt_get_forbidden_prefixes(); |
| 132 |
$savedPrefixes = get_option( 'wpc_filter_permalinks', [] ); |
| 133 |
$warningPrefixes = []; |
| 134 |
|
| 135 |
foreach( $forbiddenPrefixes as $prefix ){ |
| 136 |
if( in_array( $prefix, $savedPrefixes ) ){ |
| 137 |
$warningPrefixes[] = $prefix; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
$permalinkOptionsUrl = admin_url( 'edit.php?post_type=filter-set&page=filters-settings&tab=permalinks' ); |
| 142 |
$permalinksTab = new PermalinksTab(); |
| 143 |
$permalinksTabLabel = strtolower( $permalinksTab->getLabel() ); |
| 144 |
|
| 145 |
if( ! empty( $warningPrefixes ) ){ |
| 146 |
|
| 147 |
$message = wp_kses( |
| 148 |
sprintf( |
| 149 |
_n( |
| 150 |
'Error: <strong>%s</strong> filter prefix is not allowed and must be changed on the Filter %s settings <a href="%s" target="_blank">page</a>. Otherwise this site will not work properly.', |
| 151 |
'Error: following filter prefixes <strong>%s</strong> are not allowed and must be changed on the Filter %s settings <a href="%s" target="_blank">page</a>. Otherwise this site will not work properly.', |
| 152 |
count($warningPrefixes), |
| 153 |
'filter-everything' |
| 154 |
), implode( ', ', $warningPrefixes ), $permalinksTabLabel, $permalinkOptionsUrl |
| 155 |
), |
| 156 |
array( |
| 157 |
'strong' => array(), |
| 158 |
'a' => array('href' => true, 'target' => true) |
| 159 |
) |
| 160 |
); |
| 161 |
|
| 162 |
?> |
| 163 |
<div class="notice wpc-error is-dismissible"> |
| 164 |
<p><?php echo $message; // Already escaped ?></p> |
| 165 |
</div> |
| 166 |
<?php |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
public function adminNotices() |
| 173 |
{ |
| 174 |
$get = Container::instance()->getTheGet(); |
| 175 |
|
| 176 |
if( ! isset( $get['message'] ) ){ |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
$messageNum = sanitize_key($get['message']); // no need to escape |
| 181 |
$errors = FilterFields::getErrorsList(); |
| 182 |
|
| 183 |
?> |
| 184 |
<?php foreach ( $errors as $id => $message ) : ?> |
| 185 |
<?php if( isset( $errors[$messageNum] ) ): // no need to escape ?> |
| 186 |
<div class="wpc-error is-dismissible"> |
| 187 |
<p><?php echo $errors[$messageNum]; ?></p> |
| 188 |
<button type="button" class="notice-dismiss"> |
| 189 |
<span class="screen-reader-text"><?php esc_html_e('Dismiss this notice.'); ?></span> |
| 190 |
</button> |
| 191 |
</div> |
| 192 |
<?php break; endif; ?> |
| 193 |
<?php endforeach; ?> |
| 194 |
<?php |
| 195 |
} |
| 196 |
|
| 197 |
public function filterSetOrderby( $query ) { |
| 198 |
if( ! is_admin() ) |
| 199 |
return; |
| 200 |
|
| 201 |
if( $query->get('post_type') !== FLRT_FILTERS_SET_POST_TYPE ){ |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
$orderby = $query->get( 'orderby' ); |
| 206 |
|
| 207 |
if( $orderby === 'menu_order title' ){ |
| 208 |
$query->set( 'orderby', 'date' ); |
| 209 |
$query->set( 'order', 'DESC' ); |
| 210 |
} |
| 211 |
|
| 212 |
if( 'set_post_type' == $orderby ) { |
| 213 |
$query->set( 'meta_key', 'wpc_filter_set_post_type' ); |
| 214 |
$query->set( 'orderby', 'meta_value' ); |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
public function filterSetSortableColumn( $columns ) |
| 220 |
{ |
| 221 |
$columns['set_post_type'] = 'set_post_type'; |
| 222 |
return $columns; |
| 223 |
} |
| 224 |
|
| 225 |
public function filterSetPostTypeCol( $columns ) |
| 226 |
{ |
| 227 |
$newColumns = []; |
| 228 |
|
| 229 |
foreach ( $columns as $columnId => $columnName ) { |
| 230 |
|
| 231 |
if( $columnId === 'date' ){ |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
$newColumns[$columnId] = $columnName; |
| 236 |
if( $columnId === 'title' ){ |
| 237 |
$newColumns['set_post_type'] = esc_html__( 'Post type', 'filter-everything' ); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
return $newColumns; |
| 242 |
} |
| 243 |
|
| 244 |
public function filterSetPostTypeColContent( $column_name, $post_id ) |
| 245 |
{ |
| 246 |
if( 'set_post_type' == $column_name ){ |
| 247 |
$fss = Container::instance()->getFilterSetService(); |
| 248 |
$theSet = $fss->getSet( $post_id ); |
| 249 |
|
| 250 |
$postTypeSelected = isset( $theSet['post_type']['value'] ) ? $theSet['post_type']['value'] : ''; |
| 251 |
$postTypes = isset( $theSet['post_type']['options'] ) ? $theSet['post_type']['options'] : ''; |
| 252 |
$postType = isset( $postTypes[ $postTypeSelected ] ) ? $postTypes[ $postTypeSelected ] : ''; |
| 253 |
|
| 254 |
echo esc_html( $postType ); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
new AdminHooks(); |