-
Notifications
You must be signed in to change notification settings - Fork 804
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
Custom Content Types: Ensure settings page functionality works without module requirement #41349
base: trunk
Are you sure you want to change the base?
Changes from all commits
68bf544
0c019f5
6933413
7c59989
f22e02f
612ca18
1e0787f
0d0ed20
563ea62
cffc5e2
d1c97ab
1cabb8f
6353dd0
dc1e77b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: changed | ||
|
||
Custom Content Types: Ensure feature works on Jetpack settings page without using module functionality. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: changed | ||
|
||
Custom Content Types: Ensure feature works on Jetpack settings page without using module functionality. |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -13,6 +13,7 @@ | |||
* @package automattic/jetpack-classic-theme-helper | ||||
*/ | ||||
|
||||
use Automattic\Jetpack\Connection\Manager as Connection_Manager; | ||||
use Automattic\Jetpack\Redirect; | ||||
use Automattic\Jetpack\Status\Host; | ||||
|
||||
|
@@ -34,18 +35,74 @@ function jetpack_load_custom_post_types() { | |||
add_action( 'jetpack_activate_module_custom-content-types', array( '\Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Testimonial', 'activation_post_type_support' ) ); | ||||
|
||||
add_action( 'init', array( '\Automattic\Jetpack\Classic_Theme_Helper\Nova_Restaurant', 'init' ) ); | ||||
|
||||
$site_id = Connection_Manager::get_site_id(); | ||||
if ( is_wp_error( $site_id ) ) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding the site id check here prevents issues with multiple APIs, or rather failing tests. See the link in this Slack thread for more on those failing tests: p1738057987804199/1738056861.398469-slack-CDLH4C1UZ I'd like to hear more though from anyone who has more knowledge of the interconnectedness of the REST APIs as well as the api-v2 tests as to how these may have been overlapping, if possible. |
||||
return; | ||||
} | ||||
add_action( 'rest_api_init', 'register_rest_route_custom_content_types' ); | ||||
|
||||
} | ||||
|
||||
if ( ! function_exists( 'jetpack_custom_post_types_loaded' ) ) { | ||||
/** | ||||
* Make module configurable. | ||||
* Pass the active status to the front-end in it's initial state. | ||||
*/ | ||||
function jetpack_custom_post_types_loaded() { | ||||
if ( class_exists( 'Jetpack' ) ) { | ||||
Jetpack::enable_module_configurable( __FILE__ ); | ||||
} | ||||
$initial_state = 'var CUSTOM_CONTENT_TYPE__INITIAL_STATE; typeof CUSTOM_CONTENT_TYPE__INITIAL_STATE === "object" || (CUSTOM_CONTENT_TYPE__INITIAL_STATE = JSON.parse(decodeURIComponent("' . rawurlencode( | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable follows the same style as a couple of other initial states, such as
|
||||
wp_json_encode( | ||||
array( | ||||
'active' => true, | ||||
'over_ride' => false, | ||||
) | ||||
) | ||||
) . '")));'; | ||||
|
||||
// Create a global variable with the custom content type feature status so that the value is available | ||||
// earlier than the API method above allows, preventing delayed loading of the settings card. | ||||
wp_register_script( 'custom-content-types-data', '', array(), '0.1.0', true ); | ||||
wp_enqueue_script( 'custom-content-types-data' ); | ||||
wp_add_inline_script( | ||||
'custom-content-types-data', | ||||
$initial_state, | ||||
'before' | ||||
); | ||||
} | ||||
add_action( 'init', 'jetpack_custom_post_types_loaded' ); | ||||
} | ||||
if ( ! function_exists( 'register_rest_route_custom_content_types' ) ) { | ||||
/** | ||||
* Register the REST route for the custom content types. | ||||
*/ | ||||
function register_rest_route_custom_content_types() { | ||||
|
||||
register_rest_route( | ||||
'jetpack/v4', | ||||
'/feature/custom-content-types', | ||||
array( | ||||
'methods' => WP_REST_Server::READABLE, | ||||
'callback' => function () { | ||||
$active = true; | ||||
$over_ride = false; | ||||
$name = 'Custom Content Types'; | ||||
$description = 'Display different types of content on your site with custom content types.'; | ||||
$additional_search_queries = 'cpt, custom post types, portfolio, portfolios, testimonial, testimonials'; | ||||
return rest_ensure_response( | ||||
array( | ||||
'custom-content-types' => array( | ||||
'active' => $active, | ||||
'over_ride' => $over_ride, | ||||
'name' => $name, | ||||
'description' => $description, | ||||
'additional_search_queries' => $additional_search_queries, | ||||
), | ||||
) | ||||
); | ||||
}, | ||||
'permission_callback' => '__return_true', | ||||
) | ||||
); | ||||
} | ||||
add_action( 'jetpack_modules_loaded', 'jetpack_custom_post_types_loaded' ); | ||||
} | ||||
|
||||
if ( ! function_exists( 'jetpack_cpt_settings_api_init' ) ) { | ||||
|
@@ -83,6 +140,24 @@ function jetpack_cpt_section_callback() { | |||
} | ||||
} | ||||
|
||||
if ( ! function_exists( 'remove_custom_content_types_module_list' ) ) { | ||||
/** | ||||
* Remove Custom Content Types from the old Module list. | ||||
* Available at wp-admin/admin.php?page=jetpack_modules | ||||
* | ||||
* @param array $items Array of Jetpack modules. | ||||
* @todo Remove this function once the module file is removed from the Jetpack plugin. | ||||
* @return array | ||||
*/ | ||||
function remove_custom_content_types_module_list( $items ) { | ||||
if ( isset( $items['custom-content-types'] ) ) { | ||||
unset( $items['custom-content-types'] ); | ||||
} | ||||
return $items; | ||||
} | ||||
add_filter( 'jetpack_modules_list_table_items', 'remove_custom_content_types_module_list' ); | ||||
} | ||||
|
||||
if ( function_exists( 'jetpack_load_custom_post_types' ) ) { | ||||
|
||||
jetpack_load_custom_post_types(); | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import restApi from '@automattic/jetpack-api'; | ||
import { | ||
CUSTOM_FEATURE_ACTIVE_FETCH_FAIL, | ||
CUSTOM_FEATURE_ACTIVE_FETCH_SUCCESS, | ||
CUSTOM_FEATURE_ACTIVE_FETCH, | ||
} from 'state/action-types'; | ||
|
||
/** | ||
* Fetch the status of the custom content types feature. | ||
* | ||
* @param {string} featureType - The custom content type to check. | ||
* @return {Function} The action. | ||
*/ | ||
export const getActiveFeatureDetails = featureType => { | ||
return dispatch => { | ||
dispatch( { | ||
type: CUSTOM_FEATURE_ACTIVE_FETCH, | ||
} ); | ||
return restApi | ||
.getFeatureTypeStatus( featureType ) | ||
.then( data => { | ||
dispatch( { | ||
type: CUSTOM_FEATURE_ACTIVE_FETCH_SUCCESS, | ||
feature_data: data, | ||
} ); | ||
return data; | ||
} ) | ||
.catch( error => { | ||
dispatch( { | ||
type: CUSTOM_FEATURE_ACTIVE_FETCH_FAIL, | ||
error: error, | ||
} ); | ||
} ); | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './reducer'; | ||
export * from './actions'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { assign } from 'lodash'; | ||
import { combineReducers } from 'redux'; | ||
import { | ||
CUSTOM_FEATURE_ACTIVE_FETCH_FAIL, | ||
CUSTOM_FEATURE_ACTIVE_FETCH_SUCCESS, | ||
CUSTOM_FEATURE_ACTIVE_FETCH, | ||
} from 'state/action-types'; | ||
|
||
export const items = ( state = { fetchingCustomContentTypeStatus: false }, action ) => { | ||
switch ( action.type ) { | ||
case CUSTOM_FEATURE_ACTIVE_FETCH: | ||
return assign( {}, state, { fetchingCustomContentTypeStatus: true } ); | ||
case CUSTOM_FEATURE_ACTIVE_FETCH_SUCCESS: | ||
return { | ||
...state, | ||
featureData: { | ||
...state.featureCheck, | ||
...action.feature_data, | ||
}, | ||
}; | ||
case CUSTOM_FEATURE_ACTIVE_FETCH_FAIL: | ||
return { ...state, fetchingCustomContentTypeStatus: false, error: action.error }; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export const reducer = combineReducers( { | ||
items, | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention behind having this as part of the Jetpack HTTP API is because it ideally will be able to be used by multiple removed 'features' depending on their location.