| 1 |
<?php |
| 2 |
|
| 3 |
class PostLockdown_AdminNotice { |
| 4 |
/** Query arg used to determine if an admin notice should be displayed. */ |
| 5 |
const QUERY_ARG = 'plstatuschange'; |
| 6 |
|
| 7 |
private $plugin_path; |
| 8 |
|
| 9 |
public function __construct( $plugin_path ) { |
| 10 |
$this->plugin_path = $plugin_path; |
| 11 |
add_action( 'admin_notices', array( $this, '_output_admin_notices' ) ); |
| 12 |
add_filter( 'removable_query_args', array( $this, '_remove_query_arg' ) ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Filter for the 'redirect_post_location' hook. |
| 17 |
* @see PostLockdown::_prevent_status_change() |
| 18 |
* |
| 19 |
* @param string $location |
| 20 |
* @return string |
| 21 |
*/ |
| 22 |
public function _add_query_arg( $location ) { |
| 23 |
return add_query_arg( self::QUERY_ARG, 1, $location ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Filter for the 'removable_query_args' hook. |
| 28 |
* |
| 29 |
* Adds the plugin's query arg to the array of args |
| 30 |
* removed by WordPress using the JavaScript History API. |
| 31 |
* @param array $args Array of query args to be removed. |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function _remove_query_arg( $args ) { |
| 35 |
$args[] = self::QUERY_ARG; |
| 36 |
|
| 37 |
return $args; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Callback for the 'admin_notices' hook. |
| 42 |
* |
| 43 |
* Outputs the plugin's admin notices if there are any. |
| 44 |
*/ |
| 45 |
public function _output_admin_notices() { |
| 46 |
$notices = array(); |
| 47 |
|
| 48 |
if ( ! empty( $_GET[ self::QUERY_ARG ] ) ) { |
| 49 |
$notices[] = array( |
| 50 |
'class' => 'error', |
| 51 |
'message' => __( 'This post is protected by Post Lockdown and must stay published.', 'postlockdown' ), |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! empty( $notices ) ) { |
| 56 |
include_once( $this->plugin_path . 'view/admin-notices.php' ); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|