Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新开发版代码 #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion WP-Editor.MD.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
}

//引入资源模板
require WP_EDITORMD_PLUGIN_PATH . '/editormd_class.php';
require WP_EDITORMD_PLUGIN_PATH . '/editormd_class.php';

//引入粘贴
require WP_EDITORMD_PLUGIN_PATH . '/imagepaste.php';

//引入设置页面
require WP_EDITORMD_PLUGIN_PATH . '/editormd_options.php';
Expand All @@ -39,6 +42,7 @@
add_action('admin_init', array($editormd, 'editormd_jetpack_markdown_posting_always_on'), 11);
add_action('plugins_loaded', array($editormd, 'editormd_init_languages'));
add_action('plugins_loaded', array($editormd, 'editormd_jetpack_markdown_load_textdomain'));
add_action('admin_notices', array($editormd, 'imagepaste_admin_notices'));
add_filter('quicktags_settings', array($editormd, 'quicktags_settings'), $editorId = 'content');// 删除编辑器的快捷按钮标签
add_filter('pre_option_' . WPCom_Markdown::POST_OPTION, '__return_true');
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($editormd, 'jetpack_markdown_settings_link'));//添加插件设置链接
Expand Down
44 changes: 39 additions & 5 deletions editormd_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,34 @@ public function load_editormd()
document.getElementById("htmlDom").remove();
};
//Emoji表情自定义服务器地址
editormd.emoji = {
editormd.emoji = {
path : 'https:' === document.location.protocol ? "https://staticfile.qnssl.com/emoji-cheat-sheet/1.0.0/" : "http://cdn.staticfile.org/emoji-cheat-sheet/1.0.0/",
ext : ".png"
};
//

});
//TODO 图片粘贴上传
jQuery(document).on('paste', function(event) {
var data = {
action: "imagepaste_action",
dataurl: results.dataURL,
filename: results.filename,
name: results.name
};
new jQuery.ajax({
url: ajaxurl,
type: "post",
data: data,
success: function (request) {
var obj = eval("(" + request + ")");
if (obj.error) {
alert(obj.error)
} else {
var id = iframe.contents().find("body").attr("id");
EditorMD.insertValue('<img src="' + obj.url + '" />'));//HTML
}
}
})
})
})
//]]>
</script>
<?php
Expand All @@ -172,6 +193,8 @@ public function add_admin_js()
wp_deregister_script(array('media-upload'));//禁止加载多媒体脚本(减少对编辑器的干扰);
wp_enqueue_script('editormdjs', WP_EDITORMD_PLUGIN_URL . '/js/editormd.min.js', array('jquery'), WP_EDITORMD_PLUGIN_VERSION, true);//使用WP自带的jQuery库

wp_enqueue_script('jquery_paste_image_reader',WP_EDITORMD_PLUGIN_URL . '/js/jquery.paste_image_reader.js',array('jquery'),WP_EDITORMD_PLUGIN_VERSION,true);

//载入国际化语言资源文件
$lang = get_bloginfo('language');
switch ( $lang ) {
Expand Down Expand Up @@ -257,9 +280,20 @@ public function emoji_enqueue_scripts() {
wp_enqueue_script('emojify_config', WP_EDITORMD_PLUGIN_URL . '/js/emojifyConfig.js', array(), WP_EDITORMD_PLUGIN_VERSION, true);
}

public function imagepaste_admin_notices() {
$errors=array();

$upload=wp_upload_dir();
if ($upload['error']) $errors[]=$upload['error'];
if (count($errors) > 0) {
echo "<div id='zing-warning' style='background-color:pink' class='updated fade'><p><strong>";
foreach ($errors as $message) echo 'Image Paste: '.$message.'<br />';
echo "</strong></p></div>";
}
}

//编辑器快捷按键
function quicktags_settings($qtInit)
public function quicktags_settings($qtInit)
{
$qtInit['buttons'] = ' ';
return $qtInit;
Expand Down
100 changes: 100 additions & 0 deletions imagepaste.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* Created by PhpStorm.
* User: Jaxson
* Date: 2017/5/3
* Time: 下午5:28
*/
add_action('wp_ajax_imagepaste_action', 'imagepaste_action_callback');

function imagepaste_action_callback() {
$result=array('error'=>'');
$upload = wp_upload_dir();
$uploadUrl=$upload['url'];
$uploadDir=$upload['path'];

list($data,$image)=explode(';',$_REQUEST['dataurl']);
list($field,$type)=explode(':',$data);
list($encoding,$content)=explode(',',$image);
if ($type=='image/png') $extension='png';

$name=md5($_REQUEST['dataurl']);
if (!$extension) {
$result['error']="Could not determine image extension type";
} else {
$file=$uploadDir.'/'.$name.'.'.$extension;
$fileUrl=$uploadUrl.'/'.$name.'.'.$extension;
file_put_contents($file,base64_decode($content));
if (defined('W3TC')) {
$result['w3tc']=1;
ob_start();
$w3tc=new ImagepasteW3tc();
//$result['w3tcconfig']=$w3tc->config;
if ($w3tc->engine=='rscf') $w3tc->upload($uploadDir,$uploadUrl,$name.'.'.$extension);
$result['log']=$w3tc->log;
$result['output']=ob_get_clean();
} else $result['w3tc']=0;

$result['url']=$fileUrl;
$result['elementid']=$_REQUEST['elementid'];
}
echo json_encode($result);
die(); // this is required to return a proper result
}

class ImagepasteW3tc {
var $log=array();

function __construct() {
$this->config=array();
$file=W3TC_CACHE_CONFIG_DIR.'/master.php';
if (file_exists($file)) $this->config=include(W3TC_CACHE_CONFIG_DIR.'/master.php');
$this->engine=$this->config['cdn.engine'];
$this->username=$this->config['cdn.rscf.user'];
$this->apikey=$this->config['cdn.rscf.key'];
$this->container=$this->config['cdn.rscf.container'];
}

function upload($uploadDir,$uploadUrl,$file) {
if (!file_exists(W3TC_LIB_DIR.'/CF/cloudfiles.php')) return;

require_once(W3TC_LIB_DIR.'/CF/cloudfiles.php');

$prefix=str_replace(home_url().'/','',$uploadUrl);

// Lets connect to Rackspace
$authentication = new CF_Authentication($this->username, $this->apikey);
$authentication->authenticate();
$connection = null;
try {
$connection = new CF_Connection($authentication);
}
catch(AuthenticationException $e) {
$this->log('Error 1: '.$e->getMessage());
}

$container = null;
try
{
$container = $connection->get_container($this->container);
$container->make_public();
$this->log('Connected to container '.$this->container);
}
catch(Exception $e) {
$this->log('Error 2: '.$e->getMessage());
}

try {
$object = $container->create_object($prefix.'/'.$file);
$object->load_from_filename($uploadDir.'/'.$file);
$this->log('Copied '.$file.' to '.$object->public_uri(), 'info');
} catch(Exception $e) {
$this->log('Error 3: '.$e->getMessage());
}
}

function log($s) {
$this->log[]=is_array($s) ? print_r($s,true) : $s;
}
}
84 changes: 84 additions & 0 deletions js/jquery.paste_image_reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
(function ($) {
var defaults;
$.event.fix = (function (originalFix) {
return function (event) {
event = originalFix.apply(this, arguments);
if (event.type.indexOf("copy") === 0 || event.type.indexOf("paste") === 0) {
event.clipboardData = event.originalEvent.clipboardData
}
return event
}
})($.event.fix);
defaults = {callback: $.noop, matchType: /image.*/};
return $.fn.pasteImageReader = function (options) {
if (typeof options === "function") {
options = {callback: options}
}
options = $.extend({}, defaults, options);
return this.each(function () {
var $this, element;
element = this;
$this = $(this);
return $this.bind("paste", function (event) {
var clipboardData, found;
found = false;
if (jQuery.browser.chrome) {
clipboardData = event.clipboardData;
return Array.prototype.forEach.call(clipboardData.types, function (type, i) {
var file, reader;
if (found) {
return
}
if (type.match(options.matchType) || clipboardData.items[i].type.match(options.matchType)) {
file = clipboardData.items[i].getAsFile();
reader = new FileReader();
reader.onload = function (evt) {
return options.callback.call(element, {
dataURL: evt.target.result,
event: evt,
file: file,
name: file.name
})
};
reader.readAsDataURL(file);
return found = true
}
})
} else {
if (jQuery.browser.mozilla) {
var that = this;
setTimeout(function () {
images = jQuery(that).find("img");
for (i = 0; i < images.length; i++) {
if (jQuery(images[i]).attr("src").substr(0, 5) == "data:") {
jQuery(images[i]).attr("id", "imagepaste_temp_" + i);
var data = {
action: "imagepaste_action",
dataurl: jQuery(images[i]).attr("src"),
elementid: "imagepaste_temp_" + i
};
jQuery("#publishing-action #publish").attr("disabled", true);
new jQuery.ajax({
url: ajaxurl,
type: "post",
data: data,
success: function (request) {
jQuery("#publishing-action #publish").removeAttr("disabled");
var obj = eval("(" + request + ")");
var target = jQuery("html").find("iframe").contents().find("body").find("#" + obj.elementid);
target.attr("data-mce-src", obj.url);
target.attr("src", obj.url);
target.attr("id", null)
}
})
}
}
}, 1)
} else {
console.log("not supported")
}
}
})
})
}
})(jQuery);