Sindbad~EG File Manager
<?php
/*
* Security : blocking direct access
* Source : http://codex.wordpress.org/Theme_Development#Template_Files
*/
defined('ABSPATH') or die("Access Restricted");
function mwp_get_facebook_is_public($id) {
$fb_access_token = mwp_option('facebook-access-token');
if (!is_array($id) && !empty($fb_access_token)) {
$url = sprintf( 'https://graph.facebook.com/%s', $id );
$param = array(
'fields' => 'format,source,length',
'access_token' => $fb_access_token,
);
$response = wp_remote_get( add_query_arg( $param, $url ) , array( 'timeout' => 15 ) );
$error_code = json_decode( $response['body'] , TRUE );
if( ! is_wp_error( $response ) && isset( $response['response']['code'] ) && 400 === $response['response']['code'] && $error_code['error']['code'] == 100 ) {
$statu = 'private';
} else {
$statu = 'public';
}
return $statu;
}
}
/*
* Description : Get Video Image Name Helper
* Author : Sinha
* Source : https://stackoverflow.com/a/43678650/2535061
* Developed by : Mouad achemli
* Note : this function is important do not remove from here
*
*/
function mwp_get_image_name($url) {
if (strpos($url, '?') !== false) {
$t = explode('?',$url);
$image_clean_url = $t[0];
} else {
$image_clean_url = $url;
}
$pathinfo = pathinfo($image_clean_url);
return $pathinfo['filename'].'.'.$pathinfo['extension'];
}
/*
* Source : https://stackoverflow.com/questions/41524931/how-to-set-featured-image-programmatically-from-url
* Fix Error imagecreatefromjpeg : https://github.com/libgd/libgd/issues/206#issuecomment-276694447
*
*/
function mwp_yt_crop_image($sourceImagePath, $width, $height, $path){
// Figure out the size of the source image
$imageSize = getimagesize($sourceImagePath);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];
// If the source image is already smaller than the crop request, return (do nothing)
if ($imageWidth < $width || $imageHeight < $height) return;
// Get the adjustment by dividing the difference by two
$adjustedWidth = ($imageWidth - $width) / 2;
$adjustedHeight = ($imageHeight - $height) / 2;
$src = @imagecreatefromjpeg($sourceImagePath);
if (!$src) {
$src = imagecreatefromstring(file_get_contents($sourceImagePath));
}
// Create the new image
$dest = imagecreatetruecolor($width,$height);
// Copy, using the adjustment to crop the source image
imagecopy($dest, $src, 0, 0, $adjustedWidth, $adjustedHeight, $width, $height);
imagejpeg($dest, $path);
imagedestroy($dest);
imagedestroy($src);
}
/*
* https://stackoverflow.com/a/49700649/2535061
*
*/
function mwp_fb_crop_image($sourceImagePath, $after_width, $path){
//separate the file name and the extention
$source_url_parts = pathinfo($sourceImagePath);
$filename = $source_url_parts['filename'];
$extension = $source_url_parts['extension'];
//define the quality from 1 to 100
$quality = 100;
//detect the width and the height of original image
$imageSize = getimagesize($sourceImagePath);
$width = $imageSize[0];
$height = $imageSize[1];
//resize only when the original image is larger than expected with.
//this helps you to avoid from unwanted resizing.
if ($width > $after_width) {
//get the reduced width
$reduced_width = ($width - $after_width);
//now convert the reduced width to a percentage and round it to 2 decimal places
$reduced_radio = round(($reduced_width / $width) * 100, 2);
//ALL GOOD! let's reduce the same percentage from the height and round it to 2 decimal places
$reduced_height = round(($height / 100) * $reduced_radio, 2);
//reduce the calculated height from the original height
$after_height = $height - $reduced_height;
$src_jpg = @imagecreatefromjpeg($sourceImagePath);
if (!$src_jpg) {
$src_jpg = imagecreatefromstring(file_get_contents($sourceImagePath));
}
$src_png = @imagecreatefrompng($sourceImagePath);
if (!$src_png) {
$src_png = imagecreatefromstring(file_get_contents($sourceImagePath));
}
//Now detect the file extension
//if the file extension is 'jpg', 'jpeg', 'JPG' or 'JPEG'
if ($extension == 'jpg' || $extension == 'jpeg' || $extension == 'JPG' || $extension == 'JPEG') {
//then return the image as a jpeg image for the next step
$img = $src_jpg;
} elseif ($extension == 'png' || $extension == 'PNG') {
//then return the image as a png image for the next step
$img = $src_png;
} else {
//show an error message if the file extension is not available
echo 'image extension is not supporting';
}
//HERE YOU GO :)
//Let's do the resize thing
//imagescale([returned image], [width of the resized image], [height of the resized image], [quality of the resized image]);
$imgResized = imagescale($img, $after_width, $after_height, $quality);
imagejpeg($imgResized, $path);
imagedestroy($imgResized);
imagedestroy($img);
}
}
/*
* Description : Download Given Image URL
* Author : Mouad achemli
* Note : this function is important do not remove from here
*
*/
function mwp_down_images($url) {
$response = wp_remote_get( $url, array( 'timeout' => 15 ) );
if( ! is_wp_error( $response ) && isset( $response['response']['code'] ) && 200 === $response['response']['code'] ) {
return wp_remote_retrieve_body( $response );
}
}
/*
* Description : Video Image Helper
* Author : Remi
* Source : https://www.wpexplorer.com/wordpress-featured-image-url/
* Developed by : Mouad achemli
* Note : this function is important do not remove from here
*
*/
function mwp_download_video_image($post_id, $get_url, $source, $image_name){
$image_check = parse_url($get_url, PHP_URL_HOST);
$whitelist = array (
'scontent.xx.fbcdn.net',
'img.youtube.com',
's1.dmcdn.net',
's2.dmcdn.net',
'i.vimeocdn.com',
);
if (in_array($image_check, $whitelist) ) {
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = mwp_down_images(esc_url($get_url)); // Get image data
$filename = basename( $image_name ); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
// file_put_contents( $file, $image_data );
// https://wordpress.stackexchange.com/a/160919
global $wp_filesystem;
if (empty($wp_filesystem)) {
require_once (ABSPATH . '/wp-admin/includes/file.php');
WP_Filesystem();
}
if ( !$wp_filesystem->put_contents($file, $image_data, FS_CHMOD_FILE) ) {
return __('Failed to create file');
}
if ($source == 'youtube' || $source == 'dailymotion' ) {
mwp_yt_crop_image($file, 640, 360, $file);
} elseif ($source == 'facebook') {
mwp_fb_crop_image($file, 640, $file);
}
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'guid' => $upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
//require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );
} else {
echo "sorry not allowed";
}
}
function mwp_set_video_image() {
global $pagenow;
global $post;
if (is_edit_page('edit') && get_post_type($post->ID) == 'post' && !has_post_thumbnail($post->ID)){
$postid = $post->ID;
// New custom field Youtube & Vimeo & Dailymotion
$key_exists_yt = metadata_exists( 'post', $postid, 'mwp_yt_id' );
$key_exists_vi = metadata_exists( 'post', $postid, 'mwp_vi_id' );
$key_exists_dm = metadata_exists( 'post', $postid, 'mwp_dm_id' );
$key_exists_fb = metadata_exists( 'post', $postid, 'mwp_fb_id' );
// Get New custom field value (Video ID)
$check_y = get_post_meta($postid, 'mwp_yt_id' , true);
$check_v = get_post_meta($postid, 'mwp_vi_id' , true);
$check_d = get_post_meta($postid, 'mwp_dm_id' , true);
$check_f = get_post_meta($postid, 'mwp_fb_id' , true);
if ($key_exists_yt == true && !empty($check_y)) {
$video_id = $check_y;
$video_source = 'youtube';
} elseif ($key_exists_vi == true && !empty($check_v)) {
$video_id = $check_v;
$video_source = 'vimeo';
} elseif ($key_exists_dm == true && !empty($check_d)) {
$video_id = $check_d;
$video_source = 'dailymotion';
} elseif ($key_exists_fb == true && !empty($check_f)) {
$video_id = $check_f;
$video_source = 'facebook';
}
if (
$key_exists_yt == true && !empty($check_y) ||
$key_exists_vi == true && !empty($check_v) ||
$key_exists_dm == true && !empty($check_d) ||
$key_exists_fb == true && !empty($check_f)
) {
if ($video_source == 'youtube') {
$image_url = 'https://img.youtube.com/vi/'.$video_id.'/sddefault.jpg';
$image_name = $post->ID.'-'.mwp_get_image_name($image_url);
mwp_download_video_image($post->ID, $image_url, $video_source, $image_name);
}
if ($video_source == 'dailymotion') {
$image_url = mwp_dm_get_thumb_lg($video_id);
$image_name = $post->ID.'-'.mwp_get_image_name($image_url);
mwp_download_video_image($post->ID, $image_url, $video_source, $image_name);
}
if ($video_source == 'vimeo') {
$image_url = mwp_vi_get_thumb($video_id);
$image_name = $post->ID.'-'.mwp_get_image_name($image_url);
mwp_download_video_image($post->ID, $image_url, $video_source, $image_name);
}
if ($video_source == 'facebook') {
$fb_chk = mwp_get_facebook_is_public($video_id);
$fb_access_token = mwp_option('facebook-access-token');
if ($fb_chk == 'public' && !empty($fb_access_token)) {
$image_url = mwp_fb_get_thumb($video_id);
$image_name = $post->ID.'-'.mwp_get_image_name($image_url);
mwp_download_video_image($post->ID, $image_url, $video_source, $image_name);
} else {
}
}
} else {
return null;
}
} else {
return false;
}
}
add_action('publish_post', 'mwp_set_video_image');
add_action('save_post', 'mwp_set_video_image');
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists