This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget-plugin.php
89 lines (80 loc) · 2.62 KB
/
widget-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
Plugin Name: wp-qpost
Description: You can post a new post from this widget.
Version: 1.0
Author: PSI
Author URI: https://7io.org/
License: GPL3
*/
class wp_qpost_plugin extends WP_Widget {
// constructor
function __construct() {
parent::__construct(false, $name = __('Quick Post Widget', 'wp_widget_plugin') );
add_action('wp_enqueue_scripts', array(__CLASS__, 'wp_enqueue_scripts'));
}
public static function wp_enqueue_scripts() {
wp_register_script('qpost', plugin_dir_url( __FILE__ ).'app.js', array('wp-api'), null, false);
wp_register_style('qpost-css', plugin_dir_url( __FILE__ ).'style.css' );
//wp_localize_script('qpost', 'wpQpostId', "qpost-".wp_qpost_plugin::makeRandStr(10));
wp_enqueue_script('qpost');
wp_enqueue_style('qpost-css');
}
public static function makeRandStr($length) {
$str = array_merge(range('a', 'z'), range('0', '9'), range('A', 'Z'));
$r_str = null;
for ($i = 0; $i < $length; $i++) {
$r_str .= $str[rand(0, count($str) - 1)];
}
return $r_str;
}
// widget form creation
function form($instance) {
}
// widget update
function update($new_instance, $old_instance) {
return $instance;
}
// display widget
function widget($args, $instance) {
if ( !is_user_logged_in() ) {
return;
}
extract( $args );
// these are the widget options
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
//echo $before_title . "Post Now" . $after_title;
$id = wp_qpost_plugin::makeRandStr(10);
?>
<div>
<div>
<textarea class="qpost-textarea" id="qpost-form-<?php echo $id; ?>" autofocus="autofocus"></textarea>
</div>
<div class="qpost-button-box">
<input class="qpost-button" id="qpost-button-<?php echo $id; ?>" type="button" value="post">
</div>
</div>
<script>
(function(){
function main() {
var btn = document.getElementById('qpost-button-<?php echo $id; ?>');
var form = document.getElementById('qpost-form-<?php echo $id; ?>');
btn.addEventListener('click', function(event) {
return wp_qpost(btn,form);
});
form.addEventListener('keydown', function(event) {
var isShortCut = (event.metaKey === true || event.ctrlKey === true) && event.keyCode === 13 /* Enter */;
if(!isShortCut) return false;
return wp_qpost(btn, form);
});
}
document.addEventListener('DOMContentLoaded', main);
})();
</script>
<?php
echo $after_widget;
}
}
add_action('widgets_init', create_function('', 'return register_widget("wp_qpost_plugin");'));
?>