static fn ( $attributes ) => $renderer->render( $kind, $attributes ) ) ); } add_shortcode( 'waitlistize', array( $renderer, 'shortcode' ) ); } /** Bind only newly authored bare shortcodes on an explicit, permitted content save. * Existing unbound occurrences and explicit zero/other bindings are never retargeted. */ public function save( array $data, array $postarr ): array { if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { return $data; } if ( ! empty( $postarr['ID'] ) && ! current_user_can( 'edit_post', (int) $postarr['ID'] ) ) { return $data; } if ( 'revision' === ( $data['post_type'] ?? '' ) || ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) { return $data; } $account_id = ( new Services() )->account_id(); if ( ! $account_id ) { return $data; } $old = ! empty( $postarr['ID'] ) ? (string) get_post_field( 'post_content', (int) $postarr['ID'], 'raw' ) : ''; $pattern = '/' . get_shortcode_regex( array( 'waitlistize' ) ) . '/s'; preg_match_all( $pattern, $old, $old_shortcodes, PREG_SET_ORDER ); foreach ( $old_shortcodes as $old_shortcode ) { $old_attributes = shortcode_parse_atts( $old_shortcode[3] ); if ( '[' !== $old_shortcode[1] && ']' !== $old_shortcode[6] && ! isset( $old_attributes['connection_account_id'] ) ) { // An old unknown binding must stay unknown even if its text is edited. // Use the admin generator for explicitly bound additions on such pages. return $data; } } $content = wp_unslash( $data['post_content'] ?? '' ); $content = preg_replace_callback( $pattern, static function ( $shortcode_match ) use ( $account_id ) { $parsed = shortcode_parse_atts( $shortcode_match[3] ); if ( '[' === $shortcode_match[1] || ']' === $shortcode_match[6] || ! is_array( $parsed ) || isset( $parsed['connection_account_id'] ) || is_wp_error( Attributes::shortcode( $parsed ) ) ) { return $shortcode_match[0]; } return preg_replace( '/\A\[waitlistize\b/', '[waitlistize connection_account_id="' . $account_id . '"', $shortcode_match[0], 1 ); }, $content ); $data['post_content'] = wp_slash( $content ); return $data; } }