| 1 |
<?php |
| 2 |
namespace Depicter\Editor; |
| 3 |
|
| 4 |
|
| 5 |
use Depicter\Editor\Migrations\JobsQueue; |
| 6 |
|
| 7 |
|
| 8 |
class Editor |
| 9 |
{ |
| 10 |
protected $document_id; |
| 11 |
|
| 12 |
public function __construct(){ |
| 13 |
} |
| 14 |
|
| 15 |
public function init() |
| 16 |
{ |
| 17 |
add_action( 'admin_action_depicter', [ $this, 'make' ] ); |
| 18 |
add_action( 'depicter/plugin/updated', [ $this, 'check_migration_tasks' ] ); |
| 19 |
} |
| 20 |
|
| 21 |
public function make() |
| 22 |
{ |
| 23 |
if ( empty( $_REQUEST['document'] ) ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
define( 'IS_DEPICTER_EDITOR_PREVIEW', true ); |
| 28 |
|
| 29 |
$this->document_id = absint( $_REQUEST['document'] ); |
| 30 |
|
| 31 |
$this->clearEditPage() |
| 32 |
->enqueueAssets() |
| 33 |
->printEditorPage(); |
| 34 |
|
| 35 |
do_action( 'depicter/editor/open' ); |
| 36 |
|
| 37 |
die(); |
| 38 |
} |
| 39 |
|
| 40 |
protected function clearEditPage() |
| 41 |
{ |
| 42 |
// Send MIME Type header like WP admin-header. |
| 43 |
header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) ); |
| 44 |
|
| 45 |
add_filter( 'show_admin_bar', '__return_false' ); |
| 46 |
|
| 47 |
// Remove all WordPress actions |
| 48 |
remove_all_actions( 'wp_head' ); |
| 49 |
remove_all_actions( 'wp_print_styles' ); |
| 50 |
remove_all_actions( 'wp_print_head_scripts' ); |
| 51 |
remove_all_actions( 'wp_footer' ); |
| 52 |
|
| 53 |
// Handle `wp_head` |
| 54 |
add_action( 'wp_head', 'wp_enqueue_scripts', 1 ); |
| 55 |
add_action( 'wp_head', 'wp_print_styles', 8 ); |
| 56 |
add_action( 'wp_head', 'wp_print_head_scripts', 9 ); |
| 57 |
add_action( 'wp_head', 'wp_site_icon' ); |
| 58 |
|
| 59 |
// Handle `wp_footer` |
| 60 |
add_action( 'wp_footer', 'wp_print_footer_scripts', 20 ); |
| 61 |
|
| 62 |
// Handle `wp_enqueue_scripts` |
| 63 |
remove_all_actions( 'wp_enqueue_scripts' ); |
| 64 |
|
| 65 |
// Also remove all scripts hooked into after_wp_tiny_mce. |
| 66 |
remove_all_actions( 'after_wp_tiny_mce' ); |
| 67 |
|
| 68 |
// Change heartbeat options |
| 69 |
add_filter( 'heartbeat_settings', function( $settings ) { |
| 70 |
$settings['interval'] = 15; |
| 71 |
return $settings; |
| 72 |
}); |
| 73 |
|
| 74 |
add_filter('wp_title', function( $title ){ |
| 75 |
if( $document = \Depicter::document()->repository()->findOne( $this->document_id ) ){ |
| 76 |
if( $documentTitle = $document->getFieldValue('name') ){ |
| 77 |
$title = __( 'Depicter', 'depicter' ) . ' | ' . $documentTitle; |
| 78 |
} |
| 79 |
} |
| 80 |
return $title; |
| 81 |
} ); |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return $this |
| 88 |
*/ |
| 89 |
protected function enqueueAssets(){ |
| 90 |
\Depicter::resolve('depicter.editor.assets')->bootstrap(); |
| 91 |
return $this; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @return $this |
| 96 |
*/ |
| 97 |
private function printEditorPage(){ |
| 98 |
echo \Depicter::view('admin/editor/open/content.php')->toString(); |
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Whether we are in the editor preview mode or not. |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public function isPreview(){ |
| 108 |
return defined( 'IS_DEPICTER_EDITOR_PREVIEW' ) && IS_DEPICTER_EDITOR_PREVIEW; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Retrieves the document edit page |
| 113 |
* |
| 114 |
* @param $id |
| 115 |
* |
| 116 |
* @return mixed|void |
| 117 |
*/ |
| 118 |
public function getEditUrl( $id ) { |
| 119 |
$url = add_query_arg( |
| 120 |
[ |
| 121 |
'document' => $id, |
| 122 |
'action' => 'depicter' |
| 123 |
], |
| 124 |
self_admin_url( 'post.php' ) |
| 125 |
); |
| 126 |
|
| 127 |
return apply_filters( 'depicter/document/urls/edit', $url, $this ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Check migration tasks after plugin upgraded |
| 132 |
*/ |
| 133 |
public function check_migration_tasks() { |
| 134 |
( new JobsQueue() )->migrate(); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
// http://idev/wp/en/wp-admin/post.php?post=105&action=depicter |
| 140 |
// http://idev/wp/en/wp-admin/post.php?post&action=depicter |
| 141 |
|