protected function fixInternalShortcodes() {
		// Because $fix replaces left to right, it might replace a previously inserted value when doing multiple replacements.
		// Solved by creating a placeholder that can't possibly fall into the replacement order gotcha (famous last words)
		$fix = function ( $post_id, $transition_type, $shortcode ) {
			$replace_pairs = [];
			$post = get_post( $post_id );
			foreach ( $this->transitions as $transition ) {
				if ( $transition->type === $transition_type ) {
					$md5 = md5( $transition->oldId . $transition->newId . rand() );
					$to = "<!-- pb_fixme_{$md5} -->";
					$replace_pairs[ $to ] = $transition->newId;
					$post->post_content = \Pressbooks\Utility\shortcode_att_replace(
						$post->post_content,
						$shortcode,
						'id',
						$transition->oldId,
						$to
					);
				}
			}
			if ( ! empty( $replace_pairs ) ) {
				$post->post_content = strtr( $post->post_content, $replace_pairs );
				wp_update_post( $post );
			}
		};

		// Glossary
		foreach ( $this->postsWithGlossaryShortcodesToFix as $post_id ) {
			$fix( $post_id, 'glossary', \Pressbooks\Shortcodes\Glossary\Glossary::SHORTCODE );
		}
		// Attachments
		foreach ( $this->postsWithAttachmentsShortcodesToFix as $post_id ) {
			$fix( $post_id, 'attachment', \Pressbooks\Shortcodes\Attributions\Attachments::SHORTCODE );
		}
		// TODO
		//  H5P not supported in WXR imports
	}
protected function fixInternalShortcodes() {
		// Because $fix replaces left to right, it might replace a previously inserted value when doing multiple replacements.
		// Solved by creating a placeholder that can't possibly fall into the replacement order gotcha (famous last words)
		$fix = function ( $post_id, $transition_type, $shortcode ) {
			$replace_pairs = [];
			$post = get_post( $post_id );
			foreach ( $this->transitions as $transition ) {
				if ( $transition->type === $transition_type ) {
					$md5 = md5( $transition->oldId . $transition->newId . rand() );
					$to = "<!-- pb_fixme_{$md5} -->";
					$replace_pairs[ $to ] = $transition->newId;
					$post->post_content = \Pressbooks\Utility\shortcode_att_replace(
						$post->post_content,
						$shortcode,
						'id',
						$transition->oldId,
						$to
					);
				}
			}
			if ( ! empty( $replace_pairs ) ) {
				$post->post_content = strtr( $post->post_content, $replace_pairs );
				wp_update_post( $post );
			}
		};

		// Glossary
		foreach ( $this->postsWithGlossaryShortcodesToFix as $post_id ) {
			$fix( $post_id, 'glossary', \Pressbooks\Shortcodes\Glossary\Glossary::SHORTCODE );
		}
		// Attachments
		foreach ( $this->postsWithAttachmentsShortcodesToFix as $post_id ) {
			$fix( $post_id, 'attachment', \Pressbooks\Shortcodes\Attributions\Attachments::SHORTCODE );
		}
		// TODO
		//  H5P not supported in WXR imports
	}
protected function fixInternalShortcodes ( ) { // Because $fix replaces left to right, it might replace a previously inserted value when doing multiple replacements. // Solved by creating a placeholder that can't possibly fall into the replacement order gotcha (famous last words) $ fix = function ( $ post_id , $ transition_type , $ shortcode ) { $ replace_pairs = [ ] ; $ post = get_post ( $ post_id ) ; foreach ( $ this -> transitions as $ transition ) { if ( $ transition -> type === $ transition_type ) { $ md5 = md5 ( $ transition -> oldId . $ transition -> newId . rand ( ) ) ; $ to = "<!-- pb_fixme_{$md5} -->" ; $ replace_pairs [ $ to ] = $ transition -> newId ; $ post -> post_content = \ Pressbooks \ Utility \ shortcode_att_replace ( $ post -> post_content , $ shortcode , 'id' , $ transition -> oldId , $ to ) ; } } if ( ! empty ( $ replace_pairs ) ) { $ post -> post_content = strtr ( $ post -> post_content , $ replace_pairs ) ; wp_update_post ( $ post ) ; } } ; // Glossary foreach ( $ this -> postsWithGlossaryShortcodesToFix as $ post_id ) { $ fix ( $ post_id , 'glossary' , \ Pressbooks \ Shortcodes \ Glossary \ Glossary :: SHORTCODE ) ; } // Attachments foreach ( $ this -> postsWithAttachmentsShortcodesToFix as $ post_id ) { $ fix ( $ post_id , 'attachment' , \ Pressbooks \ Shortcodes \ Attributions \ Attachments :: SHORTCODE ) ; } // TODO //  H5P not supported in WXR imports }
Fix shortcodes with references to internal IDs
Fix shortcodes with references to internal IDs