settings = new Settings();
}
/**
* Init class actions and filters.
*/
public function init() {
add_action( 'wp_head', array( $this, 'show_header' ) );
}
/**
* Print poster image meta tags.
*/
public function show_header() {
/**
* Easy way to hide poster meta.
*
* @param bool $hide_meta Set true to hide poster meta.
*/
$hide_meta = apply_filters( 'sharing_image_hide_meta', false );
if ( $hide_meta ) {
return;
}
$poster = $this->get_poster_src();
if ( false === $poster ) {
return;
}
list( $image, $width, $height ) = $poster;
printf(
'' . "\n",
esc_url( $image )
);
printf(
'' . "\n",
esc_attr( $width )
);
printf(
'' . "\n",
esc_attr( $height )
);
}
/**
* Public method to get Sharing Image poster.
*
* @param int $object_id Optional. Post ID or Taxonomy term ID.
* @return string Url to poster.
*/
public function get_poster( $object_id = null ) {
$poster = $this->get_poster_src( $object_id );
if ( ! isset( $poster[0] ) ) {
return null;
}
return $poster[0];
}
/**
* Public method to get Sharing Image poster url, width and height.
*
* @param int $object_id Optional. Post ID or Taxonomy term ID.
* @return array|false Poster image, width and height data or false if undefined.
*/
public function get_poster_src( $object_id = null ) {
$poster = $this->get_widget_poster_src( $object_id );
if ( false === $poster ) {
$poster = $this->settings->get_default_poster_src();
}
/**
* Filters default template image data.
*
* @param array|false $image List of image data, or boolean false if no image is available.
* @param int $object_id Post ID or Taxonomy term ID.
*/
return apply_filters( 'sharing_image_poster_src', $poster, $object_id );
}
/**
* Get post or term meta Sharing Image poster data.
*
* @param int $object_id Optional. Post ID or Taxonomy term ID.
* @return array|false Widget poster image, width and height data or false if undefined.
*/
public function get_widget_poster_src( $object_id = null ) {
$meta = array();
if ( is_singular() ) {
$meta = $this->get_singular_poster_meta( $object_id );
}
if ( is_category() || is_tag() || is_tax() ) {
$meta = $this->get_taxonomy_poster_meta( $object_id );
}
$poster = false;
if ( is_array( $meta ) ) {
$meta = wp_array_slice_assoc( $meta, array( 'poster', 'width', 'height' ) );
if ( 3 === count( $meta ) ) {
$poster = array_values( $meta );
}
}
return $poster;
}
/**
* Get singular template poster meta.
*
* @param int $post_id Optional. Post ID.
* @return array|false Singular poster meta. False if undefined.
*/
private function get_singular_poster_meta( $post_id = null ) {
$post = get_post( $post_id );
// Get post meta using Widget meta name const.
$meta = get_post_meta( $post->ID, Widget::WIDGET_META, true );
/**
* Filters singular template poster data.
*
* @param array|false $meta Singular poster meta.
* @param int $post_id Post ID.
*/
return apply_filters( 'sharing_image_singular_poster_meta', $meta, $post_id );
}
/**
* Get taxonomy template poster data.
*
* @param int $term_id Optional. Term ID.
* @return array|false Taxonomy poster meta. False if undefined.
*/
private function get_taxonomy_poster_meta( $term_id = null ) {
if ( ! $this->settings->is_premium_features() ) {
return false;
}
if ( null === $term_id ) {
$term_id = get_queried_object_id();
}
// Get term meta using Widget meta name const.
$meta = get_term_meta( $term_id, Widget::WIDGET_META, true );
/**
* Filters taxonomy template poster data.
*
* @param array $meta Term poster image meta.
* @param int $post_id Term ID.
*/
return apply_filters( 'sharing_image_taxonomy_poster_meta', $meta, $term_id );
}
}