暂无描述

class-jetpack-calypsoify.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * This is Calypso skin of the wp-admin interface that is conditionally triggered via the ?calypsoify=1 param.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. use Automattic\Jetpack\Status;
  8. /**
  9. * Class Jetpack_Calypsoify
  10. */
  11. class Jetpack_Calypsoify {
  12. /**
  13. * Singleton instance of `Jetpack_Calypsoify`.
  14. *
  15. * @var object
  16. */
  17. public static $instance = false;
  18. /**
  19. * Is Calypsoify enabled, based on any value of `calypsoify` user meta.
  20. *
  21. * @var bool
  22. */
  23. public $is_calypsoify_enabled = false;
  24. /**
  25. * Jetpack_Calypsoify constructor.
  26. */
  27. private function __construct() {
  28. add_action( 'admin_init', array( $this, 'setup' ), 4 );
  29. }
  30. /**
  31. * Original singleton.
  32. *
  33. * @todo We need to leave this in place until wpcomsh is updated. wpcomsh can be updated once 9.3.0 is stable.
  34. *
  35. * Deprecated 9.3.0
  36. *
  37. * @return Jetpack_Calypsoify
  38. */
  39. public static function getInstance() { //phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  40. _deprecated_function( __METHOD__, 'Jetpack 9.3.0', 'Jetpack_Calypsoify::get_instance' );
  41. return self::get_instance();
  42. }
  43. /**
  44. * Singleton.
  45. *
  46. * @return Jetpack_Calypsoify
  47. */
  48. public static function get_instance() {
  49. if ( ! self::$instance ) {
  50. self::$instance = new self();
  51. }
  52. return self::$instance;
  53. }
  54. /**
  55. * Setup function that is loaded on the `wp_loaded` hook via the constructor.
  56. */
  57. public function setup() {
  58. $this->is_calypsoify_enabled = isset( $_GET['calypsoify'] ) && 1 === (int) $_GET['calypsoify'] && $this->is_page_gutenberg(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  59. $this->check_meta();
  60. if ( $this->is_calypsoify_enabled ) {
  61. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
  62. }
  63. }
  64. /**
  65. * Enqueues scripts, data, and styles for Gutenberg.
  66. */
  67. public function enqueue_for_gutenberg() {
  68. $site_suffix = ( new Status() )->get_site_suffix();
  69. wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, JETPACK__VERSION );
  70. wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
  71. wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );
  72. wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', false, JETPACK__VERSION, false );
  73. wp_localize_script(
  74. 'calypsoify_wpadminmods_js',
  75. 'calypsoifyGutenberg',
  76. array(
  77. 'closeUrl' => $this->get_close_gutenberg_url(),
  78. 'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block/' . $site_suffix,
  79. 'createNewPostUrl' => $this->get_calypso_origin() . '/post/' . $site_suffix,
  80. )
  81. );
  82. }
  83. /**
  84. * Returns the Calypso domain that originated the current request.
  85. *
  86. * @return string
  87. */
  88. private function get_calypso_origin() {
  89. $origin = ! empty( $_GET['origin'] ) ? $_GET['origin'] : 'https://wordpress.com'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  90. $allowed = array(
  91. 'http://calypso.localhost:3000',
  92. 'http://127.0.0.1:41050', // Desktop App.
  93. 'https://wpcalypso.wordpress.com',
  94. 'https://horizon.wordpress.com',
  95. 'https://wordpress.com',
  96. );
  97. return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
  98. }
  99. /**
  100. * Returns the Calypso URL that displays either the current post type list (if no args
  101. * are supplied) or the classic editor for the current post (if a post ID is supplied).
  102. *
  103. * @param int|null $post_id Post ID.
  104. *
  105. * @return string
  106. */
  107. public function get_calypso_url( $post_id = null ) {
  108. $screen = get_current_screen();
  109. $post_type = $screen->post_type;
  110. $site_suffix = ( new Status() )->get_site_suffix();
  111. if ( is_null( $post_id ) ) {
  112. // E.g. posts or pages have no special suffix. CPTs are in the `types/{cpt}` format.
  113. $post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
  114. ? "/${post_type}s/"
  115. : "/types/${post_type}/";
  116. $post_suffix = '';
  117. } else {
  118. $post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
  119. ? "/${post_type}/"
  120. : "/edit/${post_type}/";
  121. $post_suffix = "/${post_id}";
  122. }
  123. return $this->get_calypso_origin() . $post_type_suffix . $site_suffix . $post_suffix;
  124. }
  125. /**
  126. * Returns the URL to be used on the block editor close button for going back to the
  127. * Calypso post list.
  128. *
  129. * @return string
  130. */
  131. public function get_close_gutenberg_url() {
  132. return $this->get_calypso_url();
  133. }
  134. /**
  135. * Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor.
  136. *
  137. * @return string
  138. */
  139. public function get_switch_to_classic_editor_url() {
  140. return add_query_arg(
  141. 'set-editor',
  142. 'classic',
  143. $this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
  144. );
  145. }
  146. /**
  147. * Checks if the calypsoify user meta value is set, and deletes it if it is.
  148. * This is to ensure that Calypsoify is not activated without the URL parameter.
  149. */
  150. public function check_meta() {
  151. if ( ! empty( get_user_meta( get_current_user_id(), 'calypsoify', true ) ) ) {
  152. delete_user_meta( get_current_user_id(), 'calypsoify' );
  153. }
  154. }
  155. /**
  156. * Return whether a post type should display the Gutenberg/block editor.
  157. *
  158. * @since 6.7.0
  159. *
  160. * @param string $post_type Post type.
  161. */
  162. public function is_post_type_gutenberg( $post_type ) {
  163. return use_block_editor_for_post_type( $post_type );
  164. }
  165. /**
  166. * Determines if the page is an instance of the Gutenberg block editor.
  167. *
  168. * @return bool
  169. */
  170. public function is_page_gutenberg() {
  171. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  172. // Disabling WordPress.Security.NonceVerification.Recommended because this function fires within admin_init and this is only changing display.
  173. $page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
  174. if ( false !== strpos( $page, 'post-new.php' ) && empty( $_GET['post_type'] ) ) {
  175. return true;
  176. }
  177. if ( false !== strpos( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) {
  178. return true;
  179. }
  180. if ( false !== strpos( $page, 'post.php' ) ) {
  181. $post = get_post( $_GET['post'] );
  182. if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
  183. return true;
  184. }
  185. }
  186. if ( false !== strpos( $page, 'revision.php' ) ) {
  187. $post = get_post( $_GET['revision'] );
  188. $parent = get_post( $post->post_parent );
  189. if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. // phpcs:enable
  195. }
  196. }
  197. Jetpack_Calypsoify::get_instance();