-
Notifications
You must be signed in to change notification settings - Fork 1
/
mt8-post-share.php
executable file
·111 lines (79 loc) · 2.87 KB
/
mt8-post-share.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
Plugin Name: Simple Post Share
Plugin URI: https://github.com/mt8/mt8-post-share
Description: Show SNS button on the post page.
Author: mt8.biz
Version: 2.0.0
Author URI: http://mt8.biz
Domain Path: /languages
Text Domain: mt8-post-share
*/
$mt8_ps = new Mt8_Post_Share();
$mt8_ps->register_hooks();
class Mt8_Post_Share {
const TEXT_DOMAIN = 'mt8-post-share';
public function __construct() {
}
public function register_hooks() {
add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
add_action( 'admin_print_styles-post.php', array( &$this, 'admin_print_styles_post_php' ) );
add_action( 'enqueue_block_editor_assets', array( $this,'enqueue_block_editor_assets' ) );
}
public function plugins_loaded() {
load_plugin_textdomain( self::TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ).'/languages' );
}
public function admin_menu() {
add_meta_box( 'mt8_post_share', __( 'Share with SNS.', self::TEXT_DOMAIN ), array( &$this, 'mt8_post_share_inside' ), 'post', 'side', 'high' );
}
public function admin_print_styles_post_php() {
if ( ! $this->_can_share() ) {
return;
}
wp_enqueue_style( 'mt8_ps_style', plugins_url().'/'.dirname( plugin_basename( __FILE__ ) ).'/assets/css/style.css' );
}
public function mt8_post_share_inside() {
$this->_output_sns_share();
}
private function _can_share() {
global $post;
return ( $post && 'publish' == $post->post_status );
}
private function _output_sns_share() { ?>
<div class="mt8_ps_wrap">
<?php if ( $this->_can_share() ) : ?>
<?php
global $post;
$this->_out_put_facebook( $post );
$this->_out_put_twitter( $post );
?>
<?php else : ?>
<p><?php _e( 'Button appears when you publish the post.', self::TEXT_DOMAIN ) ?></p>
<?php endif; ?>
</div>
<?php
}
private function _out_put_facebook( $post ) {
?>
<div class="mt8_ps_fb">
<a target="_blank" class="mt8_ps_sns" href="http://www.facebook.com/sharer.php?src=bm&u=<?php echo esc_attr( urlencode( get_the_permalink( $post->ID ) ) ) ?>"><?php _e( 'Facebook', self::TEXT_DOMAIN ) ?></a>
</div>
<?php
}
private function _out_put_twitter( $post ) {
?>
<div class="mt8_ps_tw">
<a target="_blank" class="mt8_ps_sns" href="https://twitter.com/intent/tweet?url=<?php echo esc_attr( urlencode( get_the_permalink( $post->ID ) ) ) ?>"><?php _e( 'Twitter', self::TEXT_DOMAIN ) ?></a>
</div>
<?php
}
public function enqueue_block_editor_assets() {
wp_enqueue_script(
'mt8-post-share-block-editor',
plugin_dir_url(__FILE__) . 'block-editor.js',
array( 'wp-plugins', 'wp-edit-post', 'wp-element', 'wp-components' ),
filemtime( plugin_dir_path(__FILE__) . 'block-editor.js' )
);
}
}