string ('' when the attachment is gone). * * @var callable|null */ public $image_url = null; /** * Attachment alt/caption resolver: fn( int $id, string $field ) => string. * * @var callable|null */ public $attachment = null; /** * Static HTML for a shortcode: fn( string $raw ) => string|null. * * @var callable|null */ public $renderer = null; /** * CSS rules this page needs (kept in a Custom HTML block at the end of the page). * * @var string[] */ public $css = array(); /** * What happened, per tag. * * @var array{converted:array,kept:array,rendered:array,dropped:array,notes:string[]} */ public $report = array( 'converted' => array(), 'kept' => array(), 'rendered' => array(), 'dropped' => array(), 'notes' => array(), ); /** * Counter for generated class names. * * @var int */ private $uid = 0; /** * A context with the WordPress resolvers wired in. * * @param int $post_id Post ID. * @return self */ public static function for_post( $post_id ) { $ctx = new self(); $ctx->post_id = (int) $post_id; $ctx->image_url = static function ( $id, $size ) { $url = wp_get_attachment_image_url( $id, $size ); return $url ? $url : ''; }; $ctx->attachment = static function ( $id, $field ) { if ( 'alt' === $field ) { return (string) get_post_meta( $id, '_wp_attachment_image_alt', true ); } if ( 'caption' === $field ) { return (string) wp_get_attachment_caption( $id ); } if ( 'full' === $field ) { $url = wp_get_attachment_url( $id ); return $url ? $url : ''; } return ''; }; $settings = Settings::get(); $ctx->render = ! empty( $settings['render'] ); $ctx->renderer = static function ( $raw ) { return shortcode_exists( self::tag_of( $raw ) ) ? do_shortcode( $raw ) : null; }; /** * Adjust a conversion context (Bakeout Pro switches on its features here). * * @param Context $ctx Context. */ do_action( 'bakeout_context', $ctx ); return $ctx; } /** * The tag name of a shortcode text. * * @param string $raw Shortcode. * @return string */ public static function tag_of( $raw ) { return preg_match( '/^\s*\[([A-Za-z_][\w\-]*)/', (string) $raw, $m ) ? strtolower( $m[1] ) : ''; } /** * Image URL for an attachment. * * @param int $id Attachment ID. * @param string $size Size. * @return string */ public function image( $id, $size ) { return $this->image_url ? (string) call_user_func( $this->image_url, (int) $id, (string) $size ) : ''; } /** * An attachment field: alt, caption or full (URL of the original). * * @param int $id Attachment ID. * @param string $field Field. * @return string */ public function attachment( $id, $field ) { return $this->attachment ? (string) call_user_func( $this->attachment, (int) $id, (string) $field ) : ''; } /** * A fresh class name for page CSS. * * @param string $prefix Prefix. * @return string */ public function uid( $prefix = 'bakeout-' ) { ++$this->uid; return $prefix . ( $this->post_id ? $this->post_id . '-' : '' ) . $this->uid; } /** * Count a result. * * @param string $what converted|kept|rendered|dropped. * @param string $tag Tag. * @return void */ public function count( $what, $tag ) { if ( ! isset( $this->report[ $what ][ $tag ] ) ) { $this->report[ $what ][ $tag ] = 0; } ++$this->report[ $what ][ $tag ]; } /** * Add a note for the person converting. * * @param string $note Note. * @return void */ public function note( $note ) { if ( ! in_array( $note, $this->report['notes'], true ) ) { $this->report['notes'][] = $note; } } }