-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton-shortcode.php
More file actions
115 lines (98 loc) · 3.41 KB
/
Copy pathbutton-shortcode.php
File metadata and controls
115 lines (98 loc) · 3.41 KB
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
<?php
// phpcs:disable PSR1.Files.SideEffects
/**
* Plugin Name: Button Shortcode by Pivotal
* Plugin URI: https://github.com/pvtl/wp-button-shortcode.git
* Description: Adds a Button Shortcode & popup (with options for the button) to the WYSIWYG
* Author: Pivotal Agency
* Author URI: http://pivotal.agency
* Text Domain: button-shortcode
* Domain Path: /languages
* Version: 0.1.3
*
* @package ButtonShortcode
*/
namespace App\Plugins\Pvtl;
class ButtonShortcode
{
public function __construct()
{
// Call the actions/hooks
add_action('after_setup_theme', array($this, 'afterSetupTheme'));
add_action('init', array($this, 'registerButtonShortcode'));
}
/**
* Add a button to Tinymce, only after theme is setup
*
* @return void
*/
public function afterSetupTheme()
{
add_action('init', function () {
// Only execute script when user has access rights
if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
return;
}
// Only execute script when rich editing is enabled
if (get_user_option('rich_editing') !== 'true') {
return;
}
// Add the JS to the admin screen
add_filter('mce_external_plugins', function ($plugin_array) {
$file = plugin_dir_url(__FILE__) . '/resources/assets/js/shortcode-button.js';
$plugin_array['button-shortcode'] = $file;
return $plugin_array;
});
// Register the button to the editor
add_filter('mce_buttons', function ($buttons) {
array_push($buttons, 'button-shortcode');
return $buttons;
});
});
}
/**
* Handle the shortcode
*
* @return void
*/
public function registerButtonShortcode()
{
add_shortcode('button', function ($input) {
if (!is_admin()) {
$attr = array(
'text' => 'Learn More',
'href' => '#',
'size' => '',
'style' => '',
'target' => '',
'class' => 'button',
);
if (!empty($input['text'])) {
$attr['text'] = $input['text'];
}
if (!empty($input['href'])) {
$attr['href'] = $input['href'];
}
if (!empty($input['size'])) {
$attr['size'] = $input['size'];
}
if (!empty($input['style'])) {
$attr['style'] = $input['style'];
}
if (!empty($input['target'])) {
$attr['target'] = $input['target'];
}
$attr = apply_filters('wpbs_attributes', $attr);
$html = '<a href="' . $attr['href'] . '" ';
$html .= 'class="'.$attr['class'].' ' . $attr['size'] . ' ' . $attr['style'] . '" ';
$html .= (!empty($attr['target'])) ? 'target="'.$attr['target'].'"' : '';
$html .= '>'. $attr['text'] . '</a>';
return $html;
}
});
}
}
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
$pvtlButtonShortcode = new ButtonShortcode();