| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Hooks; |
| 4 |
|
| 5 |
class PostPreview { |
| 6 |
|
| 7 |
protected $post_id; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
if ( isset( $_GET['fl_asst_post_preview'] ) ) { |
| 11 |
if ( isset( $_GET['p'] ) ) { |
| 12 |
$this->post_id = absint( $_GET['p'] ); |
| 13 |
} elseif ( isset( $_GET['page_id'] ) ) { |
| 14 |
$this->post_id = absint( $_GET['page_id'] ); |
| 15 |
} |
| 16 |
|
| 17 |
if ( $this->post_id ) { |
| 18 |
add_action( 'parse_query', [ $this, 'parse_query' ], PHP_INT_MAX ); |
| 19 |
add_filter( 'redirect_canonical', '__return_false', PHP_INT_MAX ); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
if ( isset( $_GET['fl_asst_screenshot'] ) ) { |
| 24 |
add_filter( 'body_class', [ $this, 'body_class' ] ); |
| 25 |
show_admin_bar( false ); |
| 26 |
|
| 27 |
if ( $this->post_id ) { |
| 28 |
add_action( 'wp', [ $this, 'disable_known_theme_parts' ], PHP_INT_MAX ); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
public function parse_query( $query ) { |
| 34 |
if ( $query->is_main_query() ) { |
| 35 |
if ( current_user_can( 'edit_others_posts' ) ) { |
| 36 |
add_filter( 'posts_results', [ $this, 'override_posts_results' ], PHP_INT_MAX ); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public function override_posts_results( $posts ) { |
| 42 |
remove_filter( 'posts_results', [ $this, 'override_posts_results' ], PHP_INT_MAX ); |
| 43 |
|
| 44 |
$preview = get_post( $this->post_id ); |
| 45 |
$preview->post_status = 'publish'; |
| 46 |
|
| 47 |
return [ $preview ]; |
| 48 |
} |
| 49 |
|
| 50 |
public function disable_known_theme_parts() { |
| 51 |
$this->disable_bb_theme(); |
| 52 |
$this->disable_generate_press(); |
| 53 |
$this->disable_genesis(); |
| 54 |
} |
| 55 |
|
| 56 |
public function body_class( $classes ) { |
| 57 |
$classes[] = 'fl-asst-screenshot'; |
| 58 |
return $classes; |
| 59 |
} |
| 60 |
|
| 61 |
protected function disable_bb_theme() { |
| 62 |
add_filter( 'fl_topbar_enabled', '__return_false' ); |
| 63 |
add_filter( 'fl_fixed_header_enabled', '__return_false' ); |
| 64 |
add_filter( 'fl_header_enabled', '__return_false' ); |
| 65 |
add_filter( 'fl_footer_enabled', '__return_false' ); |
| 66 |
} |
| 67 |
|
| 68 |
protected function disable_generate_press() { |
| 69 |
remove_action( 'generate_header', 'generate_construct_header' ); |
| 70 |
remove_action( 'generate_after_header', 'generate_add_navigation_after_header', 5 ); |
| 71 |
remove_action( 'generate_footer', 'generate_construct_footer_widgets', 5 ); |
| 72 |
remove_action( 'generate_footer', 'generate_construct_footer' ); |
| 73 |
} |
| 74 |
|
| 75 |
protected function disable_genesis() { |
| 76 |
remove_action( 'genesis_header', 'genesis_header_markup_open', 5 ); |
| 77 |
remove_action( 'genesis_header', 'genesis_do_header' ); |
| 78 |
remove_action( 'genesis_header', 'genesis_header_markup_close', 15 ); |
| 79 |
remove_action( 'genesis_footer', 'genesis_footer_markup_open', 5 ); |
| 80 |
remove_action( 'genesis_footer', 'genesis_do_footer' ); |
| 81 |
remove_action( 'genesis_footer', 'genesis_footer_markup_close', 15 ); |
| 82 |
} |
| 83 |
} |
| 84 |
|