base ) {
return;
}
wp_enqueue_script(
'mspc-warning-panel',
plugin_dir_url( dirname( __FILE__ ) ) . '../js/warning-panel.js',
array(),
'1.0.0',
true
);
wp_localize_script(
'mspc-warning-panel',
'mspcWarningPanel',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'action' => self::AJAX_ACTION,
)
);
}
/**
* @param \WP_Post $post
*/
public function render_classic( $post ) {
if ( ! $post instanceof \WP_Post ) {
return;
}
if ( $this->is_block_editor( $post ) ) {
// Block editor path renders via admin_notices to avoid double-render.
return;
}
$this->maybe_render( (int) $post->ID );
}
public function render_block() {
if ( empty( $_GET['post'] ) ) {
return;
}
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen || 'post' !== $screen->base ) {
return;
}
$post_id = isset( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : 0;
if ( ! $post_id ) {
return;
}
$post = get_post( $post_id );
if ( ! $post instanceof \WP_Post ) {
return;
}
if ( ! $this->is_block_editor( $post ) ) {
return;
}
$this->maybe_render( $post_id );
}
private function maybe_render( $post_id ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
$report = Warning_Report::read( $post_id );
if ( ! $report || empty( $report['categories'] ) ) {
return;
}
$nonce = wp_create_nonce( self::NONCE . $post_id );
//$ajax_url = admin_url( 'admin-ajax.php' );
$dismiss_text = esc_html__( 'Dismiss', '42works-multisite-cpt-migrator' );
echo '
';
printf(
'
%s',
(int) $post_id,
esc_attr( $nonce ),
wp_kses_post($dismiss_text)
);
echo '
';
esc_html_e( 'Some content needs to be filled in before publishing.', '42works-multisite-cpt-migrator' );
echo '
';
$this->render_section_attachments( $report['categories']['attachments'] ?? array() );
$this->render_section_references( $report['categories']['references'] ?? array() );
$this->render_section_parent( $report['categories']['post_parent'] ?? array() );
$this->render_section_taxonomies( $report['categories']['taxonomies'] ?? array() );
$this->render_section_drift( $report['categories']['drift'] ?? array() );
$this->render_section_locations( $report['categories']['locations'] ?? array() );
echo '
';
?>
' . esc_html__( 'Images / files', '42works-multisite-cpt-migrator' ) . '';
foreach ( $entries as $e ) {
$label = $this->label_of( $e['field_label'] ?? '' );
$kind = ( 'gallery_item' === ( $e['kind'] ?? '' ) ) ? esc_html__( 'gallery item', '42works-multisite-cpt-migrator' ) : '';
printf(
'- %s%s — %s
',
esc_html( $label ),
$kind ? ' (' . esc_html( $kind ) . ')' : '',
esc_html__( 'failed to copy from source. Re-upload manually.', '42works-multisite-cpt-migrator' )
);
}
echo '
';
}
private function render_section_references( array $entries ) {
if ( empty( $entries ) ) {
return;
}
$by_field = array();
foreach ( $entries as $e ) {
$by_field[ $e['field_label'] ?? '' ][] = $e;
}
echo '' . esc_html__( 'Linked posts', '42works-multisite-cpt-migrator' ) . '
';
foreach ( $by_field as $field_label => $items ) {
$counts = array( 'unverified' => 0, 'ambiguous' => 0, 'dropped' => 0 );
foreach ( $items as $item ) {
$st = $item['status'] ?? '';
if ( isset( $counts[ $st ] ) ) {
$counts[ $st ]++;
}
}
$pieces = array();
if ( $counts['dropped'] ) {
$pieces[] = sprintf(
/* translators: %d: count */
_n( '%d link could not be matched', '%d links could not be matched', $counts['dropped'], '42works-multisite-cpt-migrator' ),
$counts['dropped']
);
}
if ( $counts['ambiguous'] ) {
$pieces[] = sprintf(
/* translators: %d: count */
_n( '%d link had multiple matches', '%d links had multiple matches', $counts['ambiguous'], '42works-multisite-cpt-migrator' ),
$counts['ambiguous']
);
}
if ( $counts['unverified'] ) {
$pieces[] = sprintf(
/* translators: %d: count */
_n( '%d link matched by title (verify)', '%d links matched by title (verify)', $counts['unverified'], '42works-multisite-cpt-migrator' ),
$counts['unverified']
);
}
printf(
'- %s — %s. %s
',
esc_html( $this->label_of( $field_label ) ),
esc_html( implode( '; ', $pieces ) ),
esc_html__( 'Pick the right post from the field.', '42works-multisite-cpt-migrator' )
);
}
echo '
';
}
private function render_section_parent( array $entries ) {
if ( empty( $entries ) ) {
return;
}
echo '' . esc_html__( 'Page parent', '42works-multisite-cpt-migrator' ) . '
';
foreach ( $entries as $e ) {
printf(
'- %s
',
esc_html__( 'Original parent not found on this site. Set parent manually if needed.', '42works-multisite-cpt-migrator' )
);
}
echo '
';
}
private function render_section_taxonomies( array $entries ) {
if ( empty( $entries ) ) {
return;
}
echo '' . esc_html__( 'New taxonomy terms created', '42works-multisite-cpt-migrator' ) . '
';
foreach ( $entries as $e ) {
printf(
'- %s — %s "%s" %s
',
esc_html( $e['taxonomy_label'] ?? '' ),
esc_html__( 'new term', '42works-multisite-cpt-migrator' ),
esc_html( $e['term_name'] ?? '' ),
esc_html__( 'was created. Verify it belongs.', '42works-multisite-cpt-migrator' )
);
}
echo '
';
}
private function render_section_drift( array $entries ) {
if ( empty( $entries ) ) {
return;
}
$labels = array();
foreach ( $entries as $e ) {
$labels[] = '"' . ( $e['field_label'] ?: $e['field_name'] ?? '' ) . '"';
}
echo '' . esc_html__( 'Field-group mismatch', '42works-multisite-cpt-migrator' ) . '
';
printf(
'- %s %s. %s
',
sprintf(
/* translators: %d: count of drifted fields */
esc_html( _n( '%d field exists on the source site but not here:', '%d fields exist on the source site but not here:', count( $entries ), '42works-multisite-cpt-migrator' ) ),
count( $entries )
),
esc_html( implode( ', ', $labels ) ),
esc_html__( 'Their values were still copied — import or sync the ACF field group on this site to see and edit them.', '42works-multisite-cpt-migrator' )
);
echo '
';
}
private function render_section_locations( array $entries ) {
if ( empty( $entries ) ) {
return;
}
echo '' . esc_html__( 'Page-specific field groups', '42works-multisite-cpt-migrator' ) . '
';
foreach ( $entries as $e ) {
$msg = ( 'added' === ( $e['status'] ?? '' ) )
? __( 'this post was added to the group\'s location rules so its fields show here. Values were copied.', '42works-multisite-cpt-migrator' )
: __( 'the group is registered in JSON/PHP, so its location rules could not be updated automatically. Values were copied — add this post to the group\'s location rules to see them.', '42works-multisite-cpt-migrator' );
printf(
'- %s — %s
',
esc_html( $e['group_title'] ?? '' ),
esc_html( $msg )
);
}
echo '
';
}
private function label_of( $label ) {
return '' !== $label ? $label : __( '(unnamed field)', '42works-multisite-cpt-migrator' );
}
/**
* @param string $new_status
* @param string $old_status
* @param \WP_Post $post
*/
public function maybe_clear_on_publish( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status ) {
return;
}
if ( 'publish' === $old_status ) {
return;
}
if ( ! $post instanceof \WP_Post ) {
return;
}
Warning_Report::clear( (int) $post->ID );
}
public function handle_dismiss() {
$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
if ( ! $post_id ) {
wp_send_json_error( array( 'message' => 'no_post' ), 400 );
}
check_ajax_referer( self::NONCE . $post_id );
if ( ! current_user_can( 'edit_post', $post_id ) ) {
wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
}
Warning_Report::clear( $post_id );
wp_send_json_success();
}
/**
* @param \WP_Post $post
* @return bool
*/
private function is_block_editor( $post ) {
if ( ! function_exists( 'use_block_editor_for_post' ) ) {
return false;
}
return (bool) use_block_editor_for_post( $post );
}
}