is_add_button() ) { add_filter( 'mce_external_plugins', array( $this, 'mce_external_plugins' ), 15 ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_localize_scripts' ), 13 ); add_action( 'media_buttons', array( $this, 'shortcode_button' ) ); } } /** * Ajax handler for shortcode * * @since 2.3.0 */ public function ajax_handler() { add_action( 'wp_ajax_give_shortcode', array( $this, 'shortcode_ajax' ) ); } /** * Register any TinyMCE plugins * * @param array $plugin_array * * @return array|bool * * @since 1.0 */ public function mce_external_plugins( $plugin_array ) { if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { return false; } $plugin_array['give_shortcode'] = GIVE_PLUGIN_URL . 'includes/admin/shortcodes/mce-plugin.js'; return $plugin_array; } /** * Enqueue the admin assets * * @return void * * @since 1.0 */ public function admin_enqueue_assets() { $direction = ( is_rtl() || isset( $_GET['d'] ) && 'rtl' === $_GET['d'] ) ? '.rtl' : ''; wp_enqueue_script( 'give_shortcode', GIVE_PLUGIN_URL . 'build/assets/dist/js/admin-shortcodes.js', array( 'jquery' ), GIVE_VERSION, true ); wp_enqueue_style( 'give-admin-shortcode-button-style', GIVE_PLUGIN_URL . 'build/assets/dist/css/admin-shortcode-button' . $direction . '.css', array(), GIVE_VERSION ); } /** * Localize the admin scripts * * @return void * * @since 1.0 */ public function admin_localize_scripts() { if ( ! empty( self::$shortcodes ) ) { $variables = array(); foreach ( self::$shortcodes as $shortcode => $values ) { if ( ! empty( $values['required'] ) ) { $variables[ $shortcode ] = $values['required']; } } wp_localize_script( 'give_shortcode', 'scShortcodes', $variables ); } } /** * Adds the "Donation Form" button above the TinyMCE Editor on add/edit screens. * * @return string|bool * * @since 1.0 */ public function shortcode_button() { $shortcodes = array(); foreach ( self::$shortcodes as $shortcode => $values ) { /** * Filters the condition for including the current shortcode * * @since 1.0 */ if ( apply_filters( sanitize_title( $shortcode ) . '_condition', true ) ) { $shortcodes[ $shortcode ] = sprintf( '
', $shortcode, $shortcode, $values['label'] ); } } if ( ! empty( $shortcodes ) ) { // check current WP version $img = ( version_compare( get_bloginfo( 'version' ), '3.5', '<' ) ) ? '
'
: '';
reset( $shortcodes );
if ( 1 === count( $shortcodes ) ) {
$shortcode = key( $shortcodes );
printf(
'',
$shortcode,
sprintf(
'%s %s %s',
$img,
__( 'Insert', 'give' ),
self::$shortcodes[ $shortcode ]['label']
)
);
} else {
printf(
'