Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 9
CRAP
n/a
0 / 0
jetpack_load_custom_post_types
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
jetpack_custom_post_types_loaded
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
register_rest_route_custom_content_types
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
get_custom_content_type_details
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
custom_content_require_admin_privilege_callback
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
classic_theme_helper_cpt_should_be_active
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
jetpack_cpt_settings_api_init
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_cpt_section_callback
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
remove_custom_content_types_module_list
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Module Name: Custom content types
4 * Module Description: Display different types of content on your site with custom content types.
5 * First Introduced: 3.1
6 * Requires Connection: No
7 * Auto Activate: No
8 * Module Tags: Writing
9 * Sort Order: 34
10 * Feature: Writing
11 * Additional Search Queries: cpt, custom post types, portfolio, portfolios, testimonial, testimonials, nova
12 *
13 * @package automattic/jetpack-classic-theme-helper
14 */
15
16use Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Portfolio;
17use Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Testimonial;
18use Automattic\Jetpack\Redirect;
19use Automattic\Jetpack\Status\Host;
20
21if ( ! defined( 'ABSPATH' ) ) {
22    exit( 0 );
23}
24
25if ( ! function_exists( 'jetpack_load_custom_post_types' ) ) {
26    /**
27     * Load Portfolio, Testimonial, and Nova CPT.
28     */
29    function jetpack_load_custom_post_types() {
30        include __DIR__ . '/custom-post-types/class-jetpack-portfolio.php';
31        include __DIR__ . '/custom-post-types/class-jetpack-testimonial.php';
32        include __DIR__ . '/custom-post-types/class-nova-restaurant.php';
33    }
34    add_action( 'init', array( '\Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Portfolio', 'init' ) );
35    register_activation_hook( __FILE__, array( '\Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Portfolio', 'activation_post_type_support' ) );
36    add_action( 'jetpack_activate_module_custom-content-types', array( '\Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Portfolio', 'activation_post_type_support' ) );
37
38    add_action( 'init', array( '\Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Testimonial', 'init' ) );
39    register_activation_hook( __FILE__, array( '\Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Testimonial', 'activation_post_type_support' ) );
40    add_action( 'jetpack_activate_module_custom-content-types', array( '\Automattic\Jetpack\Classic_Theme_Helper\Jetpack_Testimonial', 'activation_post_type_support' ) );
41
42    add_action( 'init', array( '\Automattic\Jetpack\Classic_Theme_Helper\Nova_Restaurant', 'init' ) );
43
44    add_action( 'rest_api_init', 'register_rest_route_custom_content_types' );
45
46}
47
48if ( ! function_exists( 'jetpack_custom_post_types_loaded' ) ) {
49    /**
50     * Pass the active status to the front-end in it's initial state.
51     */
52    function jetpack_custom_post_types_loaded() {
53        // Ensure we're only adding this script on the Jetpack settings page.
54        if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We are not processing any data here.
55            return;
56        }
57        $initial_state = 'var CUSTOM_CONTENT_TYPE__INITIAL_STATE; typeof CUSTOM_CONTENT_TYPE__INITIAL_STATE === "object" || (CUSTOM_CONTENT_TYPE__INITIAL_STATE = ' . wp_json_encode(
58            array(
59                'active'                   => classic_theme_helper_cpt_should_be_active(),
60                'over_ride'                => false,
61                'should_show_testimonials' => Jetpack_Testimonial::site_should_display_testimonials() ? true : false,
62                'should_show_portfolios'   => Jetpack_Portfolio::site_should_display_portfolios() ? true : false,
63            ),
64            JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
65        ) . ');';
66
67            // Create a global variable with the custom content type feature status so that the value is available
68            // earlier than the API method above allows, preventing delayed loading of the settings card.
69            wp_register_script( 'custom-content-types-data', '', array(), '0.1.0', true );
70            wp_enqueue_script( 'custom-content-types-data' );
71            wp_add_inline_script(
72                'custom-content-types-data',
73                $initial_state,
74                'before'
75            );
76    }
77    add_action( 'init', 'jetpack_custom_post_types_loaded' );
78}
79if ( ! function_exists( 'register_rest_route_custom_content_types' ) ) {
80    /**
81     * Register the REST route for the custom content types.
82     */
83    function register_rest_route_custom_content_types() {
84
85        register_rest_route(
86            'jetpack/v4',
87            '/feature/custom-content-types',
88            array(
89                'methods'             => WP_REST_Server::READABLE,
90                'callback'            => 'get_custom_content_type_details',
91                'permission_callback' => 'custom_content_require_admin_privilege_callback',
92            )
93        );
94    }
95}
96
97/**
98 * Get the custom content type details.
99 *
100 * @return WP_REST_Response
101 */
102function get_custom_content_type_details() {
103
104    $active                    = classic_theme_helper_cpt_should_be_active();
105    $over_ride                 = false;
106    $name                      = 'Custom Content Types';
107    $description               = 'Display different types of content on your site with custom content types.';
108    $additional_search_queries = 'cpt, custom post types, portfolio, portfolios, testimonial, testimonials';
109
110    return rest_ensure_response(
111        array(
112            'custom-content-types' => array(
113                'active'                    => $active,
114                'over_ride'                 => $over_ride,
115                'name'                      => $name,
116                'description'               => $description,
117                'additional_search_queries' => $additional_search_queries,
118            ),
119        )
120    );
121}
122
123/**
124 * Check if the current user has the required capability.
125 *
126 * @return bool|WP_Error True if the request is made by ad administrator, WP_Error otherwise.
127 */
128function custom_content_require_admin_privilege_callback() {
129    if ( current_user_can( 'manage_options' ) ) {
130        return true;
131    }
132
133    return new WP_Error(
134        'rest_forbidden',
135        esc_html__( 'You are not allowed to perform this action.', 'jetpack-classic-theme-helper' ),
136        array( 'status' => rest_authorization_required_code() )
137    );
138}
139
140/**
141 * Check if the custom content types should be active.
142 *
143 * @return bool
144 */
145function classic_theme_helper_cpt_should_be_active() {
146    if ( ! Jetpack_Testimonial::site_should_display_testimonials() && ! Jetpack_Portfolio::site_should_display_portfolios() ) {
147        return false;
148    }
149    return true;
150}
151
152if ( ! function_exists( 'jetpack_cpt_settings_api_init' ) ) {
153    /**
154     * Add Settings Section for CPT
155     */
156    function jetpack_cpt_settings_api_init() {
157        if ( ! classic_theme_helper_cpt_should_be_active() ) {
158            return;
159        }
160
161        add_settings_section(
162            'jetpack_cpt_section',
163            '<span id="cpt-options">' . __( 'Your Custom Content Types', 'jetpack-classic-theme-helper' ) . '</span>',
164            'jetpack_cpt_section_callback',
165            'writing'
166        );
167    }
168    if ( ( new Host() )->is_wpcom_simple() ) {
169        add_action( 'admin_init', 'jetpack_cpt_settings_api_init', 15 );
170    } else {
171        add_action( 'admin_init', 'jetpack_cpt_settings_api_init' );
172    }
173}
174
175if ( ! function_exists( 'jetpack_cpt_section_callback' ) ) {
176    /**
177     * Settings Description
178     */
179    function jetpack_cpt_section_callback() {
180        if ( class_exists( 'Redirect' ) ) {
181            ?>
182            <p>
183                <?php esc_html_e( 'Use these settings to display different types of content on your site.', 'jetpack-classic-theme-helper' ); ?>
184                <a target="_blank" rel="noopener noreferrer" href="<?php echo esc_url( Redirect::get_url( 'jetpack-support-custom-content-types' ) ); ?>"><?php esc_html_e( 'Learn More', 'jetpack-classic-theme-helper' ); ?></a>
185            </p>
186            <?php
187        }
188    }
189}
190
191/**
192 * Remove Custom Content Types from the old Module list.
193 * Available at wp-admin/admin.php?page=jetpack_modules
194 *
195 * @param array $items Array of Jetpack modules.
196 * @todo Remove this function once the module file is removed from the Jetpack plugin.
197 * @return array
198 */
199function remove_custom_content_types_module_list( $items ) {
200    if ( isset( $items['custom-content-types'] ) ) {
201        unset( $items['custom-content-types'] );
202    }
203    return $items;
204}
205add_filter( 'jetpack_modules_list_table_items', 'remove_custom_content_types_module_list' );
206
207if ( function_exists( 'jetpack_load_custom_post_types' ) ) {
208
209    jetpack_load_custom_post_types();
210
211}