ID;
}
$this->post = get_post( $post_id );
}
/**
* Setter or getter for a post_meta field
*
* @param string $key
* @param null|mixed $value if $value is null, the function acts as a getter
* @param bool $use_lp_key whether or not to get / set the value in the LandingPage meta space
* @param mixed $default default value to return if meta value is empty
*
* @return mixed|TCB_Post
*/
public function meta( $key, $value = null, $use_lp_key = false, $default = null ) {
if ( ! $this->post ) {
return null;
}
if ( null === $value ) {
$fn = $use_lp_key ? 'tve_get_post_meta' : 'get_post_meta';
$value = $fn( $this->post->ID, $key, true );
if ( empty( $value ) && null !== $default ) {
$value = $default;
}
return $value;
}
$fn = $use_lp_key ? 'tve_update_post_meta' : 'update_post_meta';
$fn( $this->post->ID, $key, $value );
return $this;
}
/**
* Deletes post meta field
*
* @param string $key
*/
public function meta_delete( $key ) {
delete_post_meta( $this->post->ID, $key );
return $this;
}
/**
* @param $name
*
* @return mixed|null
*/
public function __get( $name ) {
switch ( $name ) {
case 'tcb_content':
return $this->meta( 'tve_updated_post', null, true );
}
return $this->post ? $this->post->{$name} : null;
}
/**
* Get the HTML for a single WP content element containing everything from the post_content field
*/
public function get_wp_element() {
$html = '';
$wp_content = wpautop( $this->post->post_content );
$wp_content = shortcode_unautop( $wp_content );
if ( trim( $wp_content ) ) {
$html = '
___TVE_SHORTCODE_RAW__' . htmlentities( $wp_content ) . '__TVE_SHORTCODE_RAW___
';
}
return $html;
}
/**
* Migrates the post to TCB2
*
* @param boolean $update_plain_text whether or not to also update the plain text version of the post content
*/
public function migrate( $update_plain_text = true ) {
$wp_content = $this->post->post_content;
$tcb_content = $this->tcb_content;
if ( ! empty( $wp_content ) ) {
$tcb_content .= $this->get_wp_element();
}
$this->meta( 'tve_updated_post', $tcb_content, true )
->meta( 'tcb2_ready', 1 )
->meta( 'tcb_editor_enabled', 1 )
->meta_delete( 'tcb_editor_disabled' );
if ( $update_plain_text ) {
$this->update_plain_text_content( $tcb_content );
}
}
/**
* Generates the text version of a TCB-saved post
*
* @param string $tcb_content
*
* @return TCB_Post
*/
public function update_plain_text_content( $tcb_content = null ) {
if ( ! $this->editor_enabled() ) {
return $this;
}
if ( null === $tcb_content ) {
$tcb_content = $this->tcb_content;
}
$tcb_content = tcb_clean_frontend_content( $tcb_content );
/* Make sure WP shortcode element is decoded before saving it */
$tcb_content = preg_replace_callback( '#___TVE_SHORTCODE_RAW__(.+?)__TVE_SHORTCODE_RAW___#s', [ $this, 'plain_text_decode_content' ], $tcb_content );
$tcb_content = wp_unslash( $tcb_content );
$tcb_content = tve_thrive_shortcodes( $tcb_content );
do_action( 'tcb_plain_content_do_shortcode_before' );
$tcb_content = do_shortcode( $tcb_content );
do_action( 'tcb_plain_content_do_shortcode_after' );
$tcb_content = preg_replace( '/