Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calypsoify: Copy the module code to the Calypsoify package #37339

Merged
merged 10 commits into from
May 13, 2024
Merged
21 changes: 21 additions & 0 deletions projects/packages/calypsoify/.phan/baseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* This is an automatically generated baseline for Phan issues.
* When Phan is invoked with --load-baseline=path/to/baseline.php,
* The pre-existing issues listed in this file won't be emitted.
*
* This file can be updated by invoking Phan with --save-baseline=path/to/baseline.php
* (can be combined with --load-baseline)
*/
return [
// # Issue statistics:
// PhanTypeMismatchArgumentProbablyReal : 1 occurrence
// PhanTypeMismatchPropertyDefault : 1 occurrence

// Currently, file_suppressions and directory_suppressions are the only supported suppressions
'file_suppressions' => [
'src/class-jetpack-calypsoify.php' => ['PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchPropertyDefault'],
],
// 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed.
// (directory_suppressions will currently be ignored by subsequent calls to --save-baseline, but may be preserved in future Phan releases)
];
8 changes: 5 additions & 3 deletions projects/packages/calypsoify/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# calypsoify
# Calypsoify

Calypsoify is designed to make sure specific wp-admin pages include navigation that prioritizes the Calypso navigation experience.

## How to install calypsoify
![](https://cldup.com/awmrHOWz7t.png)

## How to install Calypsoify

### Installation From Git Repo

Expand All @@ -20,5 +22,5 @@ Need to report a security vulnerability? Go to [https://automattic.com/security/

## License

calypsoify is licensed under [GNU General Public License v2 (or later)](./LICENSE.txt)
Calypsoify is licensed under [GNU General Public License v2 (or later)](./LICENSE.txt)

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Calypsoify: Copy the code from the Jetpack module into the package.
5 changes: 3 additions & 2 deletions projects/packages/calypsoify/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "jetpack-library",
"license": "GPL-2.0-or-later",
"require": {
"php": ">=7.0"
"php": ">=7.0",
"automattic/jetpack-status": "@dev"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down Expand Up @@ -47,7 +48,7 @@
"mirror-repo": "Automattic/jetpack-calypsoify",
"textdomain": "jetpack-calypsoify",
"version-constants": {
"::PACKAGE_VERSION": "src/class-calypsoify.php"
"::PACKAGE_VERSION": "src/class-jetpack-calypsoify.php"
}
},
"suggest": {
Expand Down
16 changes: 0 additions & 16 deletions projects/packages/calypsoify/src/class-calypsoify.php

This file was deleted.

215 changes: 215 additions & 0 deletions projects/packages/calypsoify/src/class-jetpack-calypsoify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php
/**
* This is Calypso skin of the wp-admin interface that is conditionally triggered via the ?calypsoify=1 param.
*
* @package automattic/jetpack-calypsoify
*/

namespace Automattic\Jetpack\Calypsoify;

use Automattic\Jetpack\Status;

/**
* Class Jetpack_Calypsoify
*/
class Jetpack_Calypsoify {

const PACKAGE_VERSION = '0.1.0-alpha';

/**
* Singleton instance of `Jetpack_Calypsoify`.
*
* @var object
*/
public static $instance = false;

/**
* Is Calypsoify enabled, based on any value of `calypsoify` user meta.
*
* @var bool
*/
public $is_calypsoify_enabled = false;

/**
* Jetpack_Calypsoify constructor.
*/
private function __construct() {
add_action( 'admin_init', array( $this, 'setup' ), 4 );
}

/**
* Singleton.
*
* @return Jetpack_Calypsoify
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Setup function that is loaded on the `wp_loaded` hook via the constructor.
*/
public function setup() {
$this->is_calypsoify_enabled = isset( $_GET['calypsoify'] ) && 1 === (int) $_GET['calypsoify'] && $this->is_page_gutenberg(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

$this->check_meta();

if ( $this->is_calypsoify_enabled ) {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_for_gutenberg' ), 100 );
}
}

/**
* Enqueues scripts, data, and styles for Gutenberg.
*/
public function enqueue_for_gutenberg() {
$site_suffix = ( new Status() )->get_site_suffix();
wp_enqueue_style( 'calypsoify_wpadminmods_css', plugin_dir_url( __FILE__ ) . 'style-gutenberg.min.css', false, self::PACKAGE_VERSION );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we changed the file structure with css files currently living under the css folder we'll need to fix the path here as well.
I guess the style-gutenberg.min.css will be created on build but added to the .gitignore too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks!

wp_style_add_data( 'calypsoify_wpadminmods_css', 'rtl', 'replace' );
wp_style_add_data( 'calypsoify_wpadminmods_css', 'suffix', '.min' );

wp_enqueue_script( 'calypsoify_wpadminmods_js', plugin_dir_url( __FILE__ ) . 'mods-gutenberg.js', array( 'jquery' ), self::PACKAGE_VERSION, false );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we changed the file structure with js files currently living under the js folder we'll need to fix the path here as well.

wp_localize_script(
'calypsoify_wpadminmods_js',
'calypsoifyGutenberg',
array(
'closeUrl' => $this->get_close_gutenberg_url(),
'manageReusableBlocksUrl' => $this->get_calypso_origin() . '/types/wp_block/' . $site_suffix,
'createNewPostUrl' => $this->get_calypso_origin() . '/post/' . $site_suffix,
)
);
}

/**
* Returns the Calypso domain that originated the current request.
*
* @return string
*/
private function get_calypso_origin() {
$origin = ! empty( $_GET['origin'] ) ? wp_unslash( $_GET['origin'] ) : 'https://wordpress.com'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$allowed = array(
'http://calypso.localhost:3000',
'http://127.0.0.1:41050', // Desktop App.
'https://wpcalypso.wordpress.com',
'https://horizon.wordpress.com',
'https://wordpress.com',
);
return in_array( $origin, $allowed, true ) ? $origin : 'https://wordpress.com';
}

/**
* Returns the Calypso URL that displays either the current post type list (if no args
* are supplied) or the classic editor for the current post (if a post ID is supplied).
*
* @param int|null $post_id Post ID.
*
* @return string
*/
public function get_calypso_url( $post_id = null ) {
$screen = get_current_screen();
$post_type = $screen->post_type;
$site_suffix = ( new Status() )->get_site_suffix();

if ( $post_id === null ) {
// E.g. posts or pages have no special suffix. CPTs are in the `types/{cpt}` format.
$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
? "/{$post_type}s/"
: "/types/{$post_type}/";
$post_suffix = '';
} else {
$post_type_suffix = ( 'post' === $post_type || 'page' === $post_type )
? "/{$post_type}/"
: "/edit/{$post_type}/";
$post_suffix = "/{$post_id}";
}

return $this->get_calypso_origin() . $post_type_suffix . $site_suffix . $post_suffix;
}

/**
* Returns the URL to be used on the block editor close button for going back to the
* Calypso post list.
*
* @return string
*/
public function get_close_gutenberg_url() {
return $this->get_calypso_url();
}

/**
* Returns the URL for switching the user's editor to the Calypso (WordPress.com Classic) editor.
*
* @return string
*/
public function get_switch_to_classic_editor_url() {
return add_query_arg(
'set-editor',
'classic',
$this->is_calypsoify_enabled ? $this->get_calypso_url( get_the_ID() ) : false
);
}

/**
* Checks if the calypsoify user meta value is set, and deletes it if it is.
* This is to ensure that Calypsoify is not activated without the URL parameter.
*/
public function check_meta() {
if ( ! empty( get_user_meta( get_current_user_id(), 'calypsoify', true ) ) ) {
delete_user_meta( get_current_user_id(), 'calypsoify' );
}
}

/**
* Return whether a post type should display the Gutenberg/block editor.
*
* @since jetpack-6.7.0
*
* @param string $post_type Post type.
*/
public function is_post_type_gutenberg( $post_type ) {
return use_block_editor_for_post_type( $post_type );
}

/**
* Determines if the page is an instance of the Gutenberg block editor.
*
* @return bool
*/
public function is_page_gutenberg() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
// Disabling WordPress.Security.NonceVerification.Recommended because this function fires within admin_init and this is only changing display.
$page = isset( $_SERVER['REQUEST_URI'] ) ? wp_basename( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '';

if ( str_contains( $page, 'post-new.php' ) && empty( $_GET['post_type'] ) ) {
return true;
}

if ( str_contains( $page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( sanitize_key( $_GET['post_type'] ) ) ) {
return true;
}

if ( str_contains( $page, 'post.php' ) ) {
$post = get_post( isset( $_GET['post'] ) ? intval( $_GET['post'] ) : null );
if ( isset( $post ) && isset( $post->post_type ) && $this->is_post_type_gutenberg( $post->post_type ) ) {
return true;
}
}

if ( str_contains( $page, 'revision.php' ) ) {
$post = get_post( isset( $_GET['revision'] ) ? intval( $_GET['revision'] ) : null );
$parent = get_post( $post->post_parent );
if ( isset( $parent ) && isset( $parent->post_type ) && $this->is_post_type_gutenberg( $parent->post_type ) ) {
return true;
}
}

return false;
// phpcs:enable
}
}

Jetpack_Calypsoify::get_instance();
85 changes: 85 additions & 0 deletions projects/packages/calypsoify/src/css/style-gutenberg.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Hide Fullscreen option from post-editor
*
* The entire menu group is not rendered on smaller screens so we need a media query.
* Hide the third item of the first group when the screen is big enough.
*/
@media (min-width: 782px) {
.interface-more-menu-dropdown__content .components-menu-group:first-child .components-button:nth-child(3) {
display: none;
}
}

/* Hides the Fullscreen option from the Site Editor View modes */
.edit-site-more-menu__content .components-menu-group:first-child > [role=group] > .components-menu-item__button:last-of-type {
display: none;
}

/* REVISIONS */

.revision-php {
background: #f3f6f8;
}

.wp-toolbar .revision-php {
margin-top: -32px;
}

.revision-php #wpadminbar,
.revision-php #adminmenumain,
.revision-php #wp-admin-bar-menu-toggle {
display: none;
}

.revision-php #wpcontent {
margin-left: 50px !important;
}

.revision-php #wpbody {
padding-top: 0;
}

.revision-php #screen-meta-links {
display: none !important;
}

.revision-php #wpfooter {
display: none !important;
}

.revision-tickmarks {
margin-top: 8px;
}

.revisions-controls {
height: 118px;
}
.comparing-two-revisions .revisions-controls {
height: 176px;
}

.revisions-meta {
margin-top: 28px;
}

.diff-meta {
min-height: 46px;
}
.revisions-controls .author-card .avatar {
border-radius: 50%;
height: 38px;
margin-top: 4px;
width: 38px;
}
.revisions-controls .author-card .author-info {
line-height: 20px;
margin-top: 4px;
}

.revision-toggle-compare-mode label {
vertical-align: top;
}

.revisions-tooltip {
transform: translateY(-36px);
}
Loading