Skip to content

Commit

Permalink
better movie
Browse files Browse the repository at this point in the history
  • Loading branch information
yduke committed Oct 11, 2022
1 parent 7b2b4d3 commit 23f98eb
Show file tree
Hide file tree
Showing 11 changed files with 451 additions and 274 deletions.
48 changes: 43 additions & 5 deletions includes/post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,39 @@ function create_film_review_taxonomies() {
);

register_taxonomy( 'screenplay', array( 'film_review','tvshow_review' ), $arr_writer );

//
//Language
$label_language = array(
'name' => _x( 'Languages', 'taxonomy general name','duke-yin-helper'),
'singular_name' => _x( 'Languages', 'taxonomy singular name','duke-yin-helper'),
'search_items' => __( 'Search Language','duke-yin-helper'),
'all_items' => __( 'All languages' ,'duke-yin-helper'),
'popular_items' => __( 'Popular languages','duke-yin-helper' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit language' ,'duke-yin-helper'),
'update_item' => __( 'Update language' ,'duke-yin-helper'),
'add_new_item' => __( 'Add New Language' ,'duke-yin-helper'),
'new_item_name' => __( 'New Language Name' ,'duke-yin-helper'),
'separate_items_with_commas' => __( 'Separate languages with commas.','duke-yin-helper' ),
'add_or_remove_items' => __( 'Add or remove Language','duke-yin-helper' ),
'choose_from_most_used' => __( 'Choose from popular Language','duke-yin-helper' ),
'menu_name' => __( 'Languages' ,'duke-yin-helper'),
);

$arr_language = array(
'hierarchical' => false,
'labels' => $label_language,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'languages' ),
);

register_taxonomy( 'languages', array( 'film_review','tvshow_review' ), $arr_language );
//
}
add_action( 'init', 'create_film_review_taxonomies', 0 );
Expand All @@ -492,11 +525,6 @@ function film_review_admin_menu() {
0
);







add_submenu_page(
'edit.php?post_type=film_review',
Expand Down Expand Up @@ -565,6 +593,16 @@ function film_review_admin_menu() {
NULL,
13
);

add_submenu_page(
'edit.php?post_type=film_review',
NULL,
__( 'Languages','duke-yin-helper'),
'manage_options',
'edit-tags.php?taxonomy=languages&post_type=film_review',
NULL,
14
);

}
add_action('admin_menu', 'film_review_admin_menu');
Expand Down
4 changes: 3 additions & 1 deletion includes/wp-movies/class.movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function backdrop( $size=false, $force_copy=false ) {
return Movies::tmdb_image($this->backdrop_path, $size, $force_copy);
}

public function json($copy_images=false) {
public function json($copy_images=false, $copy_poster=false) {
$data = array(
'tmdb_id' => $this->tmdb_id,
'title' => $this->title,
Expand All @@ -63,6 +63,8 @@ public function json($copy_images=false) {
);
if($copy_images) {
$data['backdrop_path'] = self::backdrop();
}
if($copy_poster) {
$data['poster_path'] = self::poster();
}
return base64_encode(json_encode($data));
Expand Down
8 changes: 4 additions & 4 deletions includes/wp-movies/class.movies-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public static function init() {
}

public static function menu() {
add_menu_page('Movies Import Tool', 'Movies', 'publish_posts', 'movies', array( 'Movies_Admin', 'import_page' ),'dashicons-editor-video',9 );
add_submenu_page('movies', 'Movies Import Tool', 'Import', 'publish_posts', 'movies', array( 'Movies_Admin', 'import_page' ) );
$hook_suffix = add_submenu_page('movies', 'Movies Plugin Configuration', 'Settings', 'manage_options', 'movies-settings', array( 'Movies_Admin', 'settings_page' ) );
add_menu_page(__('Movies Import Tool','duke-yin-helper'), __('Movies Tool','duke-yin-helper') , 'publish_posts', 'movies', array( 'Movies_Admin', 'import_page' ),'dashicons-editor-video',9 );
add_submenu_page('movies', 'Movies Import Tool',__( 'Import','duke-yin-helper'), 'publish_posts', 'movies', array( 'Movies_Admin', 'import_page' ) );
$hook_suffix = add_submenu_page('movies', 'Movies Plugin Configuration', __('Settings','duke-yin-helper'), 'manage_options', 'movies-settings', array( 'Movies_Admin', 'settings_page' ) );
add_action( 'load-' . $hook_suffix , array( 'Movies_Admin', 'override_display_notices' ) );
add_submenu_page('movies', 'Movies Plugin Tools', 'Tools', 'publish_posts', 'movies-tools', array( 'Movies_Admin', 'tools_page' ) );
add_submenu_page('movies', 'Movies Plugin Tools', __('Tools','duke-yin-helper'), 'publish_posts', 'movies-tools', array( 'Movies_Admin', 'tools_page' ) );
}

public static function override_display_notices() {
Expand Down
78 changes: 74 additions & 4 deletions includes/wp-movies/class.movies.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,50 @@ public static function is_configured() {
}
return true;
}


public static function webpImage($source, $quality = 75, $removeOld = false)
{
$dir = pathinfo($source, PATHINFO_DIRNAME);
$name = pathinfo($source, PATHINFO_FILENAME);
$destination = $dir . DIRECTORY_SEPARATOR . $name . '.webp';
$info = getimagesize($source);
$isAlpha = false;
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($isAlpha = $info['mime'] == 'image/gif') {
$image = imagecreatefromgif($source);
} elseif ($isAlpha = $info['mime'] == 'image/png') {
$image = imagecreatefrompng($source);
} else {
return $source;
}
if ($isAlpha) {
imagepalettetotruecolor($image);
imagealphablending($image, true);
imagesavealpha($image, true);
}
imagewebp($image, $destination, $quality);

if ($removeOld)
unlink($source);

return $destination;
}

public static function tmdb_image( $file_path, &$size='original', $force_copy=false ) {
if($size == 'poster') { $size = Movie::POSTER_WIDTH; }
if($size == 'backdrop') { $size = Movie::BACKDROP_WIDTH; }
$wp_upload_dir = wp_upload_dir();

$file_destination = '/tmdb/' . $size . $file_path;
if( !file_exists($wp_upload_dir['basedir'] . $file_destination) || $force_copy ) {
self::copy_tmdb_image( $file_path, $size );
}
$image_url = $wp_upload_dir['baseurl'] . $file_destination;
$img_path = $wp_upload_dir['basedir'] . '/tmdb/' . $size . $file_path;

// $image_url = $wp_upload_dir['baseurl'] . $file_destination;
$webp_url = self:: webpImage( $img_path, 75, true );
$image_url = $wp_upload_dir['baseurl'] . '/tmdb/' . $size .'/'. basename($webp_url);
return $image_url;
}

Expand Down Expand Up @@ -140,6 +174,27 @@ public static function clear_all_data() {
self::clear_data_for_post( $post->ID );
}
}
public static function clear_zero(){
self::clear_zero_tax('cast');
self::clear_zero_tax('screenplay');
self::clear_zero_tax('languages');
self::clear_zero_tax('directors');
self::clear_zero_tax('film_review_categories');
}

public static function clear_zero_tax( $tax ){
$terms = get_terms( [
'taxonomy' => $tax,
'hide_empty' => false,
'update_term_meta_cache' => false,
'hierarchical' => false,
] );
foreach ( $terms as $term ) {
if ( 0 === $term->count ) {
wp_delete_term( $term->term_id, $tax );
}
}
}

public static function clear_data_for_tmdb_id( $tmdb_id ) {
$posts = self::posts_with_tmdb_id( $tmdb_id );
Expand All @@ -152,6 +207,16 @@ public static function clear_data_for_post( $post_id ) {
$tmdb_id = get_post_meta( $post_id, 'tmdb_id', true );
delete_post_meta( $post_id, 'tmdb_id' );
delete_post_meta( $post_id, '_zmovies_json' );
delete_post_meta( $post_id, '_r_rdate' );
delete_post_meta( $post_id, '_r_f_original_title' );
delete_post_meta( $post_id, '_headline' );
delete_post_meta( $post_id, '_r_f_year' );
delete_post_meta( $post_id, '_r_f_imdb_id' );
delete_post_meta( $post_id, '_r_f_runtime' );
delete_post_meta( $post_id, '_r_f_overview' );
delete_post_meta( $post_id, '_r_f_poster' );


/* Delete imported media if it's not attached to other posts.
TODO: Remove attachment metadata from this post without
deleting image file (unless image file isn't needed anymore). */
Expand Down Expand Up @@ -192,6 +257,11 @@ public static function posts_with_movie_data() {
'label' => 'TMDB API Key',
'description' => 'Your TMDb API key'
),
'zmovies_language' => array(
'default' => 'en-US',
'label' => 'Language to download',
'description' => 'e.g. `zh-CN` `en-US`.'
),
'zmovies_poster_width' => array(
'default' => 'w500',
'label' => 'Default poster width',
Expand All @@ -203,9 +273,9 @@ public static function posts_with_movie_data() {
'description' => 'e.g. `original` `w1280` `w780` `w300`. Must be supported by TMDb.'
),
'zmovies_post_type' => array(
'default' => '',
'default' => 'film_review',
'label' => 'Post type (optional)',
'description' => 'set to custom post type if needed (e.g. `tribe_events` for use with Events Calendar Pro plugin)'
'description' => 'set to custom post type if needed'
),
'zmovies_attach_media' => array(
'default' => 'y',
Expand Down
71 changes: 0 additions & 71 deletions includes/wp-movies/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,75 +124,4 @@ function posts_without_meta( $meta_key = '', $post_type = 'post', $fields = 'all
return $result;
}

/**
* Generate Webp image format
*
* Uses either Imagick or imagewebp to generate webp image
*
* @param string $file Path to image being converted.
* @param int $compression_quality Quality ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file).
*
* @return false|string Returns path to generated webp image, otherwise returns false.
*/
function dk_generate_webp_image($file, $compression_quality = 80)
{
// check if file exists
if (!file_exists($file)) {
return false;
}

// If output file already exists return path
$output_file = str_replace(array('.jpg', '.jpeg', '.png', '.gif'), '', $file) . '.webp';
if (file_exists($output_file)) {
return $output_file;
}

$file_type = str_replace('image/', '', strtolower(mime_content_type($file)));
if (function_exists('imagewebp')) {
switch ($file_type) {
case 'jpeg':
case 'jpg':
$image = imagecreatefromjpeg($file);
break;

case 'png':
$image = imagecreatefrompng($file);
imagepalettetotruecolor($image);
imagealphablending($image, true);
imagesavealpha($image, true);
break;

case 'gif':
$image = imagecreatefromgif($file);
break;

default:
return false;
}

// Save the image
$result = imagewebp($image, $output_file, $compression_quality);
if (false === $result) {
return false;
}

// Free up memory
imagedestroy($image);

return $output_file;
} elseif (class_exists('Imagick')) {
$image = new Imagick();
$image->readImage($file);

if ($file_type === 'png' || $file_type === 'jpg') {
$image->setImageFormat('webp');
$image->setImageCompressionQuality($compression_quality);
$image->setOption('webp:lossless', 'true');
}

$image->writeImage($output_file);
return $output_file;
}
return false;
}
?>
4 changes: 2 additions & 2 deletions includes/wp-movies/templates/admin/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
<div class="wrap">
<h2><?php echo _e( 'Movie Import Tool', 'duke-yin-helper' ) ?></h2>
<?php if( !Movies::is_configured() ) { ?>
<p>You cannot use the import tool until the plugin has been configured. Please go to Settings in the Movies menu to configure the plugin.</p>
<p><?php _e('You cannot use the import tool until the plugin has been configured. Please go to Settings in the Movies Tool menu to configure the plugin.','duke-yin-helper') ?></p>
<?php } else if(empty($posts)) { ?>
<p>There were no events found without movie data attached to them.</p>
<p><?php _e('There were no posts found without movie data attached to them.','duke-yin-helper') ?></p>
<?php } else { ?>
<br />
<style>
Expand Down
36 changes: 32 additions & 4 deletions includes/wp-movies/templates/admin/import.save.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ function is_featured_image( $poster_or_backdrop, $movie ) {

// Copy images and store JSON
$movie = Movies::TMDb($tmdb_id);
$json = $movie->json($copy_images=true);

if(has_post_thumbnail($post_id)){ //if has thumbnail, then don't import images from tmdb
$json = $movie->json($copy_images=false,$copy_poster=true);
}else{
$json = $movie->json($copy_images=true,$copy_poster=true);

}

add_or_update_post_meta( $post_id, '_zmovies_json', $json );
$date = strtotime($movie->date);
update_post_meta( $post_id, '_r_rdate', $date );
Expand All @@ -70,8 +77,8 @@ function is_featured_image( $poster_or_backdrop, $movie ) {
$imdbid = $movie-> imdb_id;
update_post_meta( $post_id, '_r_f_imdb_id', $imdbid );

$language = $movie-> languages[0];
update_post_meta( $post_id, '_r_f_language', $language );
// $language = $movie-> languages[0];
// update_post_meta( $post_id, '_r_f_language', $language );

$runtime = $movie-> runtime;
update_post_meta($post_id,'_r_f_runtime',$runtime);
Expand Down Expand Up @@ -141,6 +148,27 @@ function is_featured_image( $poster_or_backdrop, $movie ) {
}
}

//Languages
$languages = $movie-> languages;
foreach($languages as $language){
$term_id = term_exists( $language, 'languages' );
if($term_id){
wp_set_object_terms( $post_id, $language, 'languages', true );
}else{
wp_insert_term(
$language,
'languages',
array(
'description' => '',
'slug' => $language,
'parent' => '',
));
$term_id = term_exists( $language, 'languages' );
wp_set_object_terms( $post_id, $language, 'languages', true );
}
}

//Genres
$genres = $movie->genres;
foreach($genres as $genre){
$term_id = term_exists( $genre, 'film_review_categories' );
Expand Down Expand Up @@ -171,7 +199,7 @@ function is_featured_image( $poster_or_backdrop, $movie ) {
$attach_ids[] = $attach_id;
}
if($movie->poster_path) {
$attach_id = attach_media_to_post( $post_id, $movie->poster_path, is_featured_image('poster', $movie), $movie->title );
$attach_id = attach_media_to_post( $post_id, $movie->poster_path, false, $movie->title );
$attach_ids[] = $attach_id;
update_post_meta( $post_id, '_r_f_poster', $movie->poster_path );
}
Expand Down
6 changes: 5 additions & 1 deletion includes/wp-movies/templates/admin/tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
} else if( $_POST['operation'] == 'clear_all_movie_data' ) {
Movies::clear_all_data();
$operation_completed = true;
} else if( $_POST['operation'] == 'restore_defaults' ) {
} else if( $_POST['operation'] == 'clear_zero' ) {
Movies::clear_zero();
$operation_completed = true;
}else if( $_POST['operation'] == 'restore_defaults' ) {
Movies::restore_defaults();
$operation_completed = true;
}
Expand All @@ -53,6 +56,7 @@
<option selected></option>
<option value="clear_for_post_id"><?php _e('Clear movie data for a specific post ID','duke-yin-helper') ?></option>
<option value="clear_for_tmdb_id"><?php _e('Clear movie data for a specific TMDb ID','duke-yin-helper') ?></option>
<option value="clear_zero"><?php _e('Clear cast crew languages and categories with zero posts.','duke-yin-helper') ?></option>
<option value="clear_all_movie_data"><?php _e('Clear all movie data for all posts','duke-yin-helper') ?></option>
<option value="restore_defaults"><?php _e('Restore plugin settings to default (except for TMDB API key)','duke-yin-helper') ?></option>
</select></td>
Expand Down
Loading

0 comments on commit 23f98eb

Please sign in to comment.