Add upstream

This commit is contained in:
root
2019-10-24 00:12:05 +02:00
parent 85d41e4216
commit ac980f592c
3504 changed files with 1049983 additions and 29971 deletions

View File

@@ -0,0 +1,78 @@
<?php
/*
* @package lh_fap
*/
class LH_Fap_Admin {
/**
* Construct the function.
*
* @access public
* @return void
*/
public function __construct(){
$this->action_dispatcher();
$this->filter_dispatcher();
}
/**
* Contains all called actions used by the plugin core.
*
* @access private
* @return void
*/
private function action_dispatcher(){
/* PHP Solution */
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
//add_action( 'fap_media_tab_content', array( $this, 'custom_media_tab_content' ) );
/* /PHP */
}
/**
* Contains all called filters used by the plugin core.
*
* @access private
* @return void
*/
private function filter_dispatcher(){
/* PHP Solution */
//add_filter( 'fap_media_tab', array( $this, 'custom_media_tab' ) );
/* /PHP */
/* Backbone-JS Solution */
add_filter( 'media_view_strings', array( $this, 'fap_media_localisation'), 10, 2 );
/* /BBJS */
//add_filter( 'media_upload_tabs', array($this, 'custom_media_upload_tab_name') );
}
public function fap_media_localisation($strings, $post){
$strings['menuTitle'] = __('Free Images', 'fap');
$strings['addToLibrary'] = __('Insert into Library', 'fap');
$strings['pluginUrl'] = LHFAP__PLUGIN_URL;
return $strings;
}
/**
* Enqueue the neccecary scripts in the admin panel.
*
* @access public
* @return void
*/
public function enqueue_scripts(){
global $pagenow, $post_type;
$screen = get_current_screen();
if( $pagenow === "post.php" || $pagenow === "post-new.php" ) {
// Only load the scripts when we ACTUALLY need them.
wp_register_script( 'lh_fap_admin', LHFAP__PLUGIN_URL . 'js/fap.min.js', array("jquery"), NULL, true);
wp_enqueue_script( 'lh_fap_admin' );
wp_register_style( 'lh_fap_style', LHFAP__PLUGIN_URL . 'css/admin.css', NULL, 1, 'all' );
wp_enqueue_style( 'lh_fap_style' );
}
//wp_register_style( 'lh_event_admin', LHEVENT__PLUGIN_URL . 'css/admin.css', NULL, 1, 'all');
//wp_enqueue_style( 'lh_event_admin' );
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* @package lh_fap
*/
class LH_Fap_Plugin {
/**
* Construct the function.
*
* @access public
* @return void
*/
public function __construct(){
$this->action_dispatcher();
$this->filter_dispatcher();
if(is_admin()){
// Initialize the admin stuff
$this->admin = new LH_Fap_Admin();
// Initialize the importer
$this->importer = new LH_Fap_Import();
}
}
/**
* Contains all called actions used by the plugin core.
*
* @access private
* @return void
*/
private function action_dispatcher(){
add_action( 'init', array($this, 'do_stuff_on_init') );
}
/**
* Contains all called filters used by the plugin core.
*
* @access private
* @return void
*/
private function filter_dispatcher(){}
public function do_stuff_on_init(){
// i18n
load_plugin_textdomain('lhf', false, dirname(plugin_basename(LHFAP__PLUGIN_FILE)) . '/lang' );
}
}

View File

@@ -0,0 +1,214 @@
<?php
/*
* @package lh_fap
*/
/**
* The importer class that handles the sideloading of the images from the api endpoint.
*/
class LH_Fap_Import {
private $_apiUrl = "http://www.free-images.cc/wp-json";
/**
* The constructor of this class.
*
* @access public
* @return void
*/
public function __construct(){
$this->action_dispatcher();
$this->filter_dispatcher();
}
/**
* The function that dispatches all used actions by this class.
*
* @access private
* @return void
*/
private function action_dispatcher(){
add_action('wp_ajax_import_image', array( $this, 'import_image' ) );
add_action('wp_ajax_list_images', array( $this, 'list_images' ) );
}
/**
* The function that dispatches all used filters by this class.
*
* @access private
* @return void
*/
private function filter_dispatcher(){
}
/**
* The ajax call, that actually imports the image.
*
* @access public
* @return void
*/
public function import_image(){
$image_id = isset($_GET['image_id']) ? intval($_REQUEST['image_id']) : null;
$post_id = isset($_GET['post_id']) ? intval($_REQUEST['post_id']) : null;
$response = array(
"error" => true,
"msg" => __("An unknown error occured!", "lhf"),
);
// If the current user cannot upload files, die with an error message
if(!current_user_can("upload_files")){
http_response_code(401);
$response['msg'] = __("I'm sorry Dave, I'm afraid I can't do that. (Insufficient permissions!)", "lhf");
$this->json_response($response);
die();
}
if(!$image_id){
http_response_code(401);
$this->json_response($response);
die();
}
if($image_id){
// Check if we already have an image with that FAP ID in the database
$qry = new WP_Query(array(
"post_type" => 'attachment',
'post_status' => 'any',
"meta_query" => array(
array(
"key" => '_fap_id',
"value" => $image_id,
),
),
));
// If that query returns any results, die with the error message, including
if($qry->have_posts()){
http_response_code(400);
$response['msg'] = __("An image with that ID is already in the database!", "lhf");
$response['attachment_id'] = $qry->posts[0]->ID;
$this->json_response($response);
die();
}
// Download the data from the api
$json_data = $this->curl_download($this->_apiUrl . "/image/" . $image_id . "/");
if($json_data['header']['http_code'] !== 200){
http_response_code($json_data['header']['http_code']);
$response['msg'] = __("An image with that ID could not be retrieved!", "lhf");
$this->json_response($response);
die();
}
// At this point we should have working data from our API
$body = json_decode($json_data['body']);
$id = $this->add_to_library($body->data[0], $post_id);
if($id){
$response = array(
"error" => false,
"msg" => __("The image has been successfully added.", "lhf"),
"image_id" => $id,
);
}
}
$this->json_response($response);
die();
}
public function list_images(){
$response = array(
"success" => false,
);
$search_term = isset($_GET['s']) ? ($_REQUEST['s']) : null;
$paged = isset($_GET['paged']) ? intval($_REQUEST['paged']) : null;
$api_url = $this->_apiUrl . "/image/";
$api_url = add_query_arg(array(
"paged" => $paged,
"s" => $search_term,
), $api_url);
$json_data = $this->curl_download($api_url);
if($json_data){
$response = json_decode($json_data['body']);
}
$this->json_response($response);
die();
}
/**
* add_to_library function.
*
* @access private
* @param mixed $data
* @return void
*/
private function add_to_library($data, $post_id){
if(isset($data->url) && !filter_var($data->url, FILTER_VALIDATE_URL) === false){
// The download URL is legal
$tmp = download_url( $data->url );
if( is_wp_error( $tmp ) ){
// download failed, handle error
}
$desc = $data->title;
$file_array = array();
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\?]+\.(jpg|jpeg|gif|png)/i', $data->url, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
$post_data = array(
'post_content' => sprintf(__("by %s via free-images.cc", "lhf"), $data->author),
);
// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $post_id, $desc, $post_data );
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array['tmp_name']);
return $id;
} else {
$res = update_post_meta($id, '_fap_id', $data->id);
}
return $id;
}
return false;
}
/**
* Make sure we have a legal json response.
*
* @access private
* @param mixed $response
* @return void
*/
private function json_response($response){
header('Content-Type: application/json');
echo json_encode($response);
}
/**
* Handle the downloading of the url.
*
* @access private
* @param mixed $url
* @return void
*/
private function curl_download($url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $url);
// Set a referer
curl_setopt($ch, CURLOPT_REFERER, get_bloginfo("url"));
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Get the connection info
$info = curl_getinfo($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return array(
"body" => $output,
"header" => $info,
);
}
}

View File

@@ -0,0 +1,120 @@
<?php
function fap_mediaview_templates() {
?>
<script type="text/html" id="tmpl-fap-image-upload">
<div class="progress-container">
<ul class="progress">
<li data-name="CSS Skill" data-percent="100%">
<svg viewBox="-10 -10 220 220">
<g fill="none" stroke-width="6" transform="translate(100,100)">
<path d="M 0,-100 A 100,100 0 0,1 86.6,-50" stroke="url(#cl1)"/>
<path d="M 86.6,-50 A 100,100 0 0,1 86.6,50" stroke="url(#cl2)"/>
<path d="M 86.6,50 A 100,100 0 0,1 0,100" stroke="url(#cl3)"/>
<path d="M 0,100 A 100,100 0 0,1 -86.6,50" stroke="url(#cl4)"/>
<path d="M -86.6,50 A 100,100 0 0,1 -86.6,-50" stroke="url(#cl5)"/>
<path d="M -86.6,-50 A 100,100 0 0,1 0,-100" stroke="url(#cl6)"/>
</g>
</svg>
<svg viewBox="-10 -10 220 220">
<path d="M200,100 C200,44.771525 155.228475,0 100,0 C44.771525,0 0,44.771525 0,100 C0,155.228475 44.771525,200 100,200 C155.228475,200 200,155.228475 200,100 Z" stroke-dashoffset="629"></path>
</svg>
<span class="dashicons dashicons-yes"></span>
</li>
</ul>
<!-- Defining Angle Gradient Colors -->
<svg width="0" height="0">
<defs>
<linearGradient id="cl1" gradientUnits="objectBoundingBox" x1="0" y1="0" x2="1" y2="1">
<stop stop-color="#e67724"/>
<stop offset="100%" stop-color="#e7642f"/>
</linearGradient>
<linearGradient id="cl2" gradientUnits="objectBoundingBox" x1="0" y1="0" x2="0" y2="1">
<stop stop-color="#e7642f"/>
<stop offset="100%" stop-color="#e85139"/>
</linearGradient>
<linearGradient id="cl3" gradientUnits="objectBoundingBox" x1="1" y1="0" x2="0" y2="1">
<stop stop-color="#e85139"/>
<stop offset="100%" stop-color="#e85139"/>
</linearGradient>
<linearGradient id="cl4" gradientUnits="objectBoundingBox" x1="1" y1="1" x2="0" y2="0">
<stop stop-color="#e85139"/>
<stop offset="100%" stop-color="#e7652e"/>
</linearGradient>
<linearGradient id="cl5" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="0" y2="0">
<stop stop-color="#e7652e"/>
<stop offset="100%" stop-color="#e67824"/>
</linearGradient>
<linearGradient id="cl6" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="1" y2="0">
<stop stop-color="#e67824"/>
<stop offset="100%" stop-color="#e67724"/>
</linearGradient>
</defs>
</svg>
</div>
</script>
<script type="text/html" id="tmpl-fap-image-details">
<h3>
<?php _e('Attachment Details'); ?>
</h3>
<div class="attachment-info">
<div class="thumbnail thumbnail-{{ data.type }}">
<# if ( 'image' === data.type && data.sizes ) { #>
<img src="{{ data.size.url }}" draggable="false" />
<# } else { #>
<img src="{{ data.icon }}" class="icon" draggable="false" />
<# } #>
</div>
<div class="details">
<div class="filename">{{ data.filename }}</div>
<div class="uploaded">{{ data.dateFormatted }}</div>
<div class="file-size">{{ data.filesizeHumanReadable }}</div>
<# if ( 'image' === data.type && ! data.uploading ) { #>
<# if ( data.width && data.height ) { #>
<div class="dimensions">{{ data.width }} &times; {{ data.height }}</div>
<a href="{{ data.link }}?utm_source=fap-plugin&utm_medium=preview&utm_campaign=media-preview&utm_term=preview" target="_blank">Preview</a>
<# } #>
<# } #>
<# if ( data.fileLength ) { #>
<div class="file-length"><?php _e( 'Length:' ); ?> {{ data.fileLength }}</div>
<# } #>
<div class="compat-meta">
<# if ( data.compat && data.compat.meta ) { #>
{{{ data.compat.meta }}}
<# } #>
</div>
</div>
</div>
<label class="setting" data-setting="url">
<span class="name"><?php _e('URL'); ?></span>
<input type="text" value="{{ data.size.url }}" readonly />
</label>
<# var maybeReadOnly = data.can.save || data.allowLocalEdits ? '' : 'readonly'; #>
<label class="setting" data-setting="title">
<span class="name"><?php _e('Title'); ?></span>
<input type="text" value="{{ data.title }}" {{ maybeReadOnly }} />
</label>
</script>
<script type="text/html" id="tmpl-lh-logo">
<div class="lh-logo">
<a href="http://www.luehrsen-heinrich.de/?utm_source=fap-plugin&utm_medium=banner&utm_campaign=media-banner&utm_term=banner" target="_blank">
<img src="<?=LHFAP__PLUGIN_URL?>img/lh_logo_head.svg" alt="Luehrsen // Heinrich GmbH" title="Luehrsen // Heinrich GmbH">
</a>
</div>
</script>
<?php
}
add_action('print_media_templates', 'fap_mediaview_templates');