-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsbmain.php
143 lines (108 loc) · 3.09 KB
/
msbmain.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/*
Plugin Name: Simple TinyMCE shortcode button
Plugin URI: https://github.com/gripgrip/m-simple-button
Description: A simple WordPress plugin that adds a button to TinyMCE to generate a shortcode.
Version: 1.0.0
Author: Mircea Sandu
Author URI: https://mircian.com
License: GPL2
*/
/**
* Class msb_main
*/
class msb_main {
/**
* @var string Version of the plugin
*/
public $version = '1.0.0';
/**
* msb_main constructor.
*/
public function __construct() {
// define plugin constants
$this->define_constants();
// filter the buttons in the TinyMCE editor - see https://codex.wordpress.org/Plugin_API/Filter_Reference/mce_buttons,_mce_buttons_2,_mce_buttons_3,_mce_buttons_4 for other buttons
add_filter( 'mce_buttons', array( $this, 'add_embed_button' ), 140 );
// filter the external js plugins loaded in the editor
add_filter( 'mce_external_plugins', array( $this, 'add_embed_button_js' ) );
// add the actual shortcode being output
add_shortcode('msb_video', array($this, 'video_shortcode'));
}
/**
* Define plugin constants
*/
private function define_constants() {
define( 'MSB_PLUGIN_FILE', __FILE__ );
define( 'MSB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'MSB_VERSION', $this->version );
}
/**
* @param $buttons array of buttons displayed in TinyMCE
*
* @return array
*/
public function add_embed_button( $buttons ) {
array_push( $buttons, '|', 'msb_button' );
return $buttons;
}
/**
* @param $plugin_array array of plugins loaded by TinyMCE
*
* @return array
*/
function add_embed_button_js( $plugin_array ) {
$plugin_array['msb_button'] = plugin_dir_url( MSB_PLUGIN_FILE ) . 'assets/js/embed_button.js';
return $plugin_array;
}
/**
* @param $atts array of shortcode attributes
*
* @return false|string
*/
public function video_shortcode($atts) {
$a = shortcode_atts(array(
'video' => false,
'autoplay' => false,
'width' => false,
'height' => false
), $atts, 'ttu_video');
$embed = false;
if ( $a['video'] ) {
// add autoplay using a filter
if ( $a['autoplay'] ) {
add_filter('oembed_result',array($this, 'oembed_result') , 15, 3);
}
// build the shortcode based on options
$args = array();
if ( $a['width'] ) {
$args['width'] = $a['width'];
}
if ( $a['height'] ) {
$args['height'] = $a['height'];
}
$embed = wp_oembed_get( esc_url($a['video']), $args );
// remove the filter to prevent changing other oembeds
if ( $a['autoplay'] ) {
remove_filter( 'oembed_result', array( $this, 'oembed_result' ), 15 );
}
}
return $embed;
}
/**
* @param $html string with the html of the embed
* @param $url string the url used to generate the embed
* @param $args array options sent as arguments to the wp_oembed_get function
*
* @return mixed
*/
function oembed_result($html, $url, $args) {
// $args includes custom arguments
// modify $html as you need
if ( isset($args['autoplay']) && $args['autoplay'] ) {
$html = str_replace('feature=oembed', 'feature=oembed&autoplay=1', $html);
}
return $html;
}
}
new msb_main();