importers = $importers; $this->add_meta_boxes(); add_action( 'edit_form_top', array( $this, 'add_header' ) ); add_action( 'dbx_post_sidebar', array( $this, 'add_footer' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'register_styles_scripts' ) ); add_filter( 'script_loader_tag', array( $this, 'add_alpine_defer_attribute' ), 10, 2 ); add_action( 'save_post', array( $this, 'save_post' ), 10, 3 ); add_action( 'wp_insert_post_data', array( $this, 'wp_insert_post_data' ), 10, 2 ); add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) ); add_filter( 'bulk_post_updated_messages', array( $this, 'bulk_post_updated_messages' ), 10, 2 ); add_filter( 'redirect_post_location', array( $this, 'redirect_post_location' ), 10, 2 ); add_action( 'admin_notices', array( $this, 'admin_notices' ), 10 ); } /** * Output wrapper opening for alpinejs x-data */ public function add_header( $post ) { if ( 'airwpsync-connection' === get_post_type() ) { $config = json_decode( $post->post_content ); $json = $config ? wp_json_encode( $config ) : '{}'; // Json output is already encoded // phpcs:ignore echo ''; echo '
'; echo ''; } } /** * Output wrapper closing for alpinejs x-data */ public function add_footer() { if ( 'airwpsync-connection' === get_post_type() ) { echo '
'; } } /** * Register admin styles and scripts */ public function register_styles_scripts() { $screen = get_current_screen(); if ( is_object( $screen ) && 'airwpsync-connection' === $screen->id ) { wp_enqueue_script( 'air-wp-sync-alpine', plugins_url( 'assets/js/alpinejs@3.10.2.min.js', AIR_WP_SYNC_PLUGIN_FILE ), false, AIR_WP_SYNC_VERSION, false ); wp_enqueue_script( 'air-wp-sync-admin', plugins_url( 'assets/js/admin-page.js', AIR_WP_SYNC_PLUGIN_FILE ), array( 'air-wp-sync-alpine', 'jquery-ui-tooltip' ), AIR_WP_SYNC_VERSION, false ); wp_enqueue_script( 'air-wp-sync-admin-metabox-mapping', plugins_url( 'assets/js/metabox-mapping/main.js', AIR_WP_SYNC_PLUGIN_FILE ), array( 'air-wp-sync-admin' ), AIR_WP_SYNC_VERSION, false ); wp_add_inline_script( 'air-wp-sync-admin', 'var airWpSync = ' . $this->get_json_config(), 'before' ); wp_localize_script( 'air-wp-sync-admin', 'airWpSyncI18n', array( 'deleteActionConfirmation' => __( 'You have a Custom Post Type declared using this connection. Are you sure to delete it?', 'air-wp-sync' ), 'startingUpdate' => __( 'In progress...', 'air-wp-sync' ), 'canceling' => __( 'Canceling...', 'air-wp-sync' ), ) ); } } /** * Add defer attribute to AlpineJS script */ public function add_alpine_defer_attribute( $tag, $handle ) { if ( 'air-wp-sync-alpine' === $handle ) { $tag = str_replace( ' src', ' defer="defer" src', $tag ); } return $tag; } /** * Add metaboxes for importer post type */ public function add_meta_boxes() { new Air_WP_Sync_Metabox_Global_Settings(); new Air_WP_Sync_Metabox_Post_Settings( $this->importers ); new Air_WP_Sync_Metabox_Field_Mapping(); new Air_WP_Sync_Metabox_Sync_Settings( $this->importers ); new Air_WP_Sync_Metabox_Import_Infos( $this->importers ); } /** * After importer is saved */ public function save_post( $post_id, $post, $update ) { if ( ! $update || wp_is_post_revision( $post_id ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || 'airwpsync-connection' !== $post->post_type ) { return; } // FLush rewrite rules delete_option( 'rewrite_rules' ); // Clear schedule wp_clear_scheduled_hook( 'air_wp_sync_importer_' . $post_id ); } /** * Before importer is saved */ public function wp_insert_post_data( $data, $postarr ) { if ( 'airwpsync-connection' !== $postarr['post_type'] ) { return $data; } // Limit active importers $post_id = $postarr['ID'] ?? 0; if ( ! isset( $data['post_status'] ) || $data['post_status'] === 'publish' ) { $importers = get_posts( array( 'post_type' => 'airwpsync-connection', 'post_status' => 'publish', 'posts_per_page' => -1, 'post__not_in' => array( $post_id ), ) ); if ( count( $importers ) > 0 ) { $data['post_status'] = 'draft'; $this->display_max_connection = true; } } // Update slug if ( array_key_exists( 'post_title', $data ) ) { $data['post_name'] = sanitize_title( $data['post_title'] ); } return $data; } /** * Customize connections update messages */ public function post_updated_messages( $messages ) { $back_link_html = sprintf( ' %2$s', esc_url( admin_url( 'edit.php?post_type=airwpsync-connection' ) ), __( 'Back to list', 'air-wp-sync' ) ); $messages['airwpsync-connection'] = array_replace( $messages['post'], array( 0 => '', // Unused. Messages start at index 1. 1 => __( 'Connection updated.', 'air-wp-sync' ) . $back_link_html, 4 => __( 'Connection updated.', 'air-wp-sync' ), 6 => __( 'Connection published.', 'air-wp-sync' ), 7 => __( 'Connection saved.', 'air-wp-sync' ), 10 => __( 'Connection draft updated.', 'air-wp-sync' ), ) ); return $messages; } /** * Customize connections bulk update messages */ public function bulk_post_updated_messages( $messages, $bulk_counts ) { $messages['airwpsync-connection'] = array_replace( $messages['post'], array( /* translators: %s: Number of connections. */ 'updated' => _n( '%s connection updated.', '%s connections updated.', $bulk_counts['updated'], 'air-wp-sync' ), 'locked' => ( 1 === $bulk_counts['locked'] ) ? __( '1 connection not updated, somebody is editing it.', 'air-wp-sync' ) : /* translators: %s: Number of connections. */ _n( '%s connection not updated, somebody is editing it.', '%s connections not updated, somebody is editing them.', $bulk_counts['locked'], 'air-wp-sync' ), /* translators: %s: Number of connections. */ 'deleted' => _n( '%s connection permanently deleted.', '%s connections permanently deleted.', $bulk_counts['deleted'], 'air-wp-sync' ), /* translators: %s: Number of connections. */ 'trashed' => _n( '%s connection moved to the Trash.', '%s connections moved to the Trash.', $bulk_counts['trashed'], 'air-wp-sync' ), /* translators: %s: Number of connections. */ 'untrashed' => _n( '%s connection restored from the Trash.', '%s connections restored from the Trash.', $bulk_counts['untrashed'], 'air-wp-sync' ), ) ); return $messages; } /** * Add display max connection parameter to location url */ public function redirect_post_location( $location, $post_id ) { if ( $this->display_max_connection ) { $location = add_query_arg( 'display_max_connection', 1, $location ); $location = remove_query_arg( 'message', $location ); } return $location; } /** * Display max connection mesage if parameter was added */ public function admin_notices() { // phpcs:ignore if ( ! empty( $_GET['display_max_connection'] ) && '1' === $_GET['display_max_connection'] ) { printf( '

%2$s

', esc_attr( 'notice notice-error' ), wp_kses( __( 'Thank you for using the Free Version of our plugin! You already have an active connection. To be able to create as many active connections as you want, Upgrade to Pro Version.', 'air-wp-sync' ), array( 'a' => array( 'href' => array(), 'target' => array(), ), ) ) ); } } /** * Get Json config */ protected function get_json_config() { $config = array( 'reservedCptSlugs' => $this->get_reserved_cpt_slugs(), 'mappingOptions' => $this->get_mapping_options(), 'featuresByPostType' => $this->get_features_by_post_type(), ); return wp_json_encode( $config ); } /** * Get reserved CPT slugs */ protected function get_reserved_cpt_slugs() { return array_values( get_post_types() ); } /** * Get mapping options */ protected function get_mapping_options() { return apply_filters( 'airwpsync/get_wp_fields', array() ); } /** * Get available features by post type */ protected function get_features_by_post_type() { $features = array(); foreach ( Air_WP_Sync_Helpers::get_post_types() as $post_type ) { $features[ $post_type['value'] ] = apply_filters( 'airwpsync/features_by_post_type', array(), $post_type['value'] ); } // Default features for custom post type $features['custom'] = array( 'post' => array( 'post_name', 'post_date', 'post_title', 'post_excerpt', 'post_content', ), 'meta' => array( '_thumbnail_id', 'custom_field', ), ); return $features; } }