*/ class Affreviews_Admin { /** * Legacy blocks that can be previewed on the settings page. */ const PREVIEW_BLOCKS = array( 'affreviews/table', 'affreviews/table-large', 'affreviews/grid', 'affreviews/single', 'affreviews/single-large', ); /** * Review fields edited in the block editor's "Review details" panel, * mapped to their REST sanitize callback. Keys and stored shapes are the * ones the CMB2 metabox has always saved. */ const REVIEW_TEXT_META = array( 'affreviews_rating' => 'sanitize_text_field', 'affreviews_bonus' => 'wp_kses_post', 'affreviews_afflink' => 'sanitize_review_url', 'affreviews_review_link' => 'sanitize_review_url', 'affreviews_logo_background_color' => 'sanitize_meta_color', 'affreviews_description' => 'wp_kses_post', 'affreviews_tag_text' => 'sanitize_text_field', 'affreviews_tag_background_color' => 'sanitize_meta_color', 'affreviews_terms' => 'wp_kses_post', // Made unique by Affreviews_Links::dedupe_saved_slug() after the save. 'affreviews_link_slug' => 'sanitize_title', ); const REVIEW_LIST_META = array( 'affreviews_positives', 'affreviews_negatives', ); const REVIEW_INFO_LIST_META = 'affreviews_group_info_list'; /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function enqueue_styles( $hook ) { $screen = get_current_screen(); // Load this stylesheet only to Reviews edit screen if ( ( 'post.php' === $hook && 'affreviews_reviews' === $screen->post_type ) || 'toplevel_page_affreviews-settings' === $hook ) { // Versioned by file time so a rebuild isn't hidden by the browser cache. wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'assets/settings/build/admin.css', array(), filemtime( __DIR__ . '/assets/settings/build/admin.css' ), 'all' ); } if ( 'toplevel_page_affreviews-settings' === $hook ) { wp_enqueue_style( 'wp-components' ); wp_enqueue_style( $this->plugin_name . '-public', AFFREVIEWS_URL . 'public/assets/build/public.css', array(), $this->version, 'all' ); // Styles of the legacy blocks shown in the Styles preview. foreach ( self::PREVIEW_BLOCKS as $block_name ) { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); if ( $block_type ) { array_map( 'wp_enqueue_style', $block_type->style_handles ); } } affreviews_register_css_vars( $this->plugin_name . '-public' ); } } /** * Register the JavaScript for the Settings area. * * @since 1.0.0 */ public function enqueue_scripts( $hook ) { if ( 'toplevel_page_affreviews-settings' !== $hook ) { return; } $script_dependencies = array( 'dependencies' => null, 'version' => $this->version, ); if ( file_exists( __DIR__ . '/assets/settings/build/admin.asset.php' ) ) { $script_dependencies = require __DIR__ . '/assets/settings/build/admin.asset.php'; } wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'assets/settings/build/admin.js', $script_dependencies['dependencies'], $script_dependencies['version'], true ); wp_set_script_translations( $this->plugin_name, 'affiliate-reviews' ); $latest_review = get_posts( array( 'post_type' => AFFREVIEWS_CPT, 'post_status' => 'publish', 'posts_per_page' => 1, 'fields' => 'ids', ) ); // JSON keeps the numbers typed (wp_localize_script() would cast them to strings). $data = array( 'version' => $this->version, 'reviewsCount' => (int) wp_count_posts( AFFREVIEWS_CPT )->publish, 'latestReviewId' => $latest_review ? (int) $latest_review[0] : 0, 'homeUrl' => home_url( '/' ), 'plainLinks' => ! get_option( 'permalink_structure' ), 'blocks' => $this->get_guide_blocks(), 'urls' => array( 'newPage' => admin_url( 'post-new.php?post_type=page' ), 'newReview' => admin_url( 'post-new.php?post_type=' . AFFREVIEWS_CPT ), 'reviews' => admin_url( 'edit.php?post_type=' . AFFREVIEWS_CPT ), 'categories' => admin_url( 'edit-tags.php?taxonomy=' . AFFREVIEWS_TAX . '&post_type=' . AFFREVIEWS_CPT ), 'permalinks' => admin_url( 'options-permalink.php' ), ), ); wp_add_inline_script( $this->plugin_name, 'var affreviewsSettings = ' . wp_json_encode( $data ) . ';', 'before' ); } /** * The plugin's registered blocks, for the block list on the Guide tab. * * Read from the registry so the list follows block.json. * * @return array[] */ private function get_guide_blocks() { $blocks = array(); foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $name => $block_type ) { if ( 0 !== strpos( $name, 'affreviews/' ) ) { continue; } $blocks[] = array( 'name' => $name, 'title' => $block_type->title, 'description' => $block_type->description, 'deprecated' => 'affreviews-deprecated' === $block_type->category, ); } return $blocks; } /** * Register the REST route that counts where the legacy blocks are used. * * @return void */ public function register_guide_routes() { register_rest_route( 'affreviews/v1', '/legacy-usage', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_legacy_usage' ), 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, 'args' => array( 'refresh' => array( 'type' => 'boolean', 'default' => false, ), ), ) ); } /** * Posts that contain each legacy block: a count and the latest few. * * Cached for an hour, since it scans post_content. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_legacy_usage( $request ) { $usage = $request['refresh'] ? false : get_transient( 'affreviews_legacy_usage' ); if ( false === $usage ) { global $wpdb; $usage = array(); $shown = 10; foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $name => $block_type ) { if ( 'affreviews-deprecated' !== $block_type->category ) { continue; } // The trailing space keeps "single" from matching "single-large". $like = '%' . $wpdb->esc_like( '