Last active
August 5, 2016 21:55
-
-
Save caercam/0950fb60adfd0cb3dd4f6f95788f3e83 to your computer and use it in GitHub Desktop.
WPMOLY_SEO_Replacements() class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WPMOLY_SEO_Replacements { | |
/** | |
* Static instance. | |
* | |
* @var object | |
*/ | |
private static $instance; | |
/** | |
* List of replacements. | |
* | |
* @var array | |
*/ | |
private $replacements = array(); | |
private function __construct() { | |
$this->replacements = array( | |
'tmdb_id' => __( 'TMDb ID', 'wpmovielibrary' ), | |
'title' => __( 'Title', 'wpmovielibrary' ), | |
'original_title' => __( 'Original Title', 'wpmovielibrary' ), | |
'tagline' => __( 'Tagline', 'wpmovielibrary' ), | |
'overview' => __( 'Overview', 'wpmovielibrary' ), | |
'release_date' => __( 'Release Date', 'wpmovielibrary' ), | |
'local_release_date' => __( 'Original Release Date', 'wpmovielibrary' ), | |
'runtime' => __( 'Runtime', 'wpmovielibrary' ), | |
'production_companies' => __( 'Production', 'wpmovielibrary' ), | |
'production_countries' => __( 'Country', 'wpmovielibrary' ), | |
'spoken_languages' => __( 'Languages', 'wpmovielibrary' ), | |
'genres' => __( 'Genres', 'wpmovielibrary' ), | |
'director' => __( 'Director', 'wpmovielibrary' ), | |
'producer' => __( 'Producer', 'wpmovielibrary' ), | |
'cast' => __( 'Actors', 'wpmovielibrary' ), | |
'photography' => __( 'Director of Photography', 'wpmovielibrary' ), | |
'composer' => __( 'Original Music Composer', 'wpmovielibrary' ), | |
'author' => __( 'Author', 'wpmovielibrary' ), | |
'writer' => __( 'Writer', 'wpmovielibrary' ), | |
'certification' => __( 'Certification', 'wpmovielibrary' ), | |
'budget' => __( 'Budget', 'wpmovielibrary' ), | |
'revenue' => __( 'Revenue', 'wpmovielibrary' ), | |
'imdb_id' => __( 'IMDb ID', 'wpmovielibrary' ), | |
'adult' => __( 'Adult', 'wpmovielibrary' ), | |
'homepage' => __( 'Homepage', 'wpmovielibrary' ), | |
'status' => __( 'Status', 'wpmovielibrary' ), | |
'media' => __( 'Media', 'wpmovielibrary' ), | |
'rating' => __( 'Rating', 'wpmovielibrary' ), | |
'language' => __( 'Language', 'wpmovielibrary' ), | |
'subtitles' => __( 'Subtitles', 'wpmovielibrary' ), | |
'format' => __( 'Format', 'wpmovielibrary' ), | |
); | |
} | |
/** | |
* Singleton. | |
* | |
* @return object | |
*/ | |
public static final function get_instance() { | |
if ( ! self::$instance instanceof self ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
/** | |
* Use a limited number of methods to catch all replacements callbacks. | |
* | |
* Allows all replacements to be handled by a single callback method. To | |
* provide specific treatment just add a get_{$slug}_replacement() method | |
* to the class. | |
* | |
* @param string $method Called method name. | |
* @param array $arguments Called method parameters. | |
* | |
* @return string | |
*/ | |
public function __call( $method, $arguments ) { | |
foreach ( array_keys( $this->replacements ) as $replacement ) { | |
if ( "get_{$replacement}_replacement" === $method ) { | |
return $this->get_replacement( $replacement, $arguments[1] ); | |
} | |
} | |
} | |
/** | |
* Register extra replacements. | |
* | |
* @return void | |
*/ | |
public function register() { | |
add_action( 'wpseo_register_extra_replacements', array( $this, 'register_replacements' ) ); | |
} | |
/** | |
* Define all needed replacements callbacks. | |
* | |
* @return void | |
*/ | |
public function register_replacements() { | |
foreach ( $this->replacements as $slug => $title ) { | |
wpseo_register_var_replacement( "%%movie_{$slug}%%", array( $this, "get_{$slug}_replacement" ), 'advanced', "Get the movie $title" ); | |
} | |
} | |
/** | |
* Retrieve a replacement value. | |
* | |
* @param string $slug Replacement name | |
* @param WP_Post $post Current Post instance | |
* | |
* @return string | |
*/ | |
private function get_replacement( $slug, $post ) { | |
$details = array( 'status', 'media', 'format', 'subtitles', 'language' ); | |
if ( in_array( $slug, $details ) ) { | |
return $this->get_detail_replacement( $slug, $post ); | |
} else { | |
return $this->get_meta_replacement( $slug, $post ); | |
} | |
} | |
/** | |
* Retrieve a meta replacement value. | |
* | |
* @param string $slug Replacement name | |
* @param WP_Post $post Current Post instance | |
* | |
* @return string | |
*/ | |
private function get_meta_replacement( $slug, $post ) { | |
$replacement = wpmoly_get_movie_meta( $post->ID, $slug ); | |
if ( empty( $replacement ) ) { | |
return ''; | |
} | |
return (string) $replacement; | |
} | |
/** | |
* Retrieve a detail replacement value. | |
* | |
* @param string $slug Replacement name | |
* @param WP_Post $post Current Post instance | |
* | |
* @return string | |
*/ | |
private function get_detail_replacement( $slug, $post ) { | |
$replacement = wpmoly_get_movie_meta( $post->ID, $slug ); | |
if ( empty( $replacement ) ) { | |
return ''; | |
} | |
$details = array(); | |
$default = WPMOLY_Settings::get_supported_movie_details(); | |
foreach ( $replacement as $value ) { | |
$value = trim( $value ); | |
if ( isset( $default[ $slug ]['options'][ $value ] ) ) { | |
$details[] = $default[ $slug ]['options'][ $value ]; | |
} | |
} | |
// Cosmetic: return A and B and A, B and C type lists. | |
if ( 2 == count( $details ) ) { | |
return $details[0] . ' ' . __( 'and' ) . ' ' . $details[1]; | |
} else if ( 2 < count( $details ) ) { | |
$d1 = array_pop( $details ); | |
$d2 = array_pop( $details ); | |
$details[] = $d2 . ' ' . __( 'and' ) . ' ' . $d1; | |
} | |
return implode( ', ', $details ); | |
} | |
/** | |
* Custom replacement example. | |
* | |
* Apply some formatting to the replacement value before returning it. | |
* | |
* @param string $slug Replacement name | |
* @param WP_Post $post Current Post instance | |
* | |
* @return string | |
*/ | |
public function get_budget_replacement( $slug = 'budget', $post ) { | |
$replacement = wpmoly_get_movie_meta( $post->ID, 'budget' ); | |
if ( empty( $replacement ) ) { | |
return ''; | |
} | |
return '$' . number_format_i18n( $replacement ); | |
} | |
/** | |
* Prevent cloning. | |
* | |
* @return void | |
*/ | |
private function __clone() {} | |
} | |
// Get instance and register replacements. | |
WPMOLY_SEO_Replacements::get_instance()->register(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment