first commit

This commit is contained in:
2024-11-10 21:08:49 +01:00
commit 0d932ce5ee
14455 changed files with 2567501 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
( function( wp ) {
var el = wp.element.createElement;
var __ = wp.i18n.__;
var ServerSideRender = wp.components.ServerSideRender;
var InspectorControls = wp.editor.InspectorControls;
var SelectControl = wp.components.SelectControl;
wp.blocks.registerBlockType( 'widget-shortcode/block', {
title: __( 'Widget Shortcode', 'widget-shortcode' ),
icon: 'welcome-widgets-menus',
category: 'widgets',
attributes : {
id: {
default : '',
},
},
// Display block preview and UI
edit( props ) {
return el( 'div', {}, [
el( ServerSideRender, {
block: "widget-shortcode/block",
attributes: props.attributes
} ),
el( InspectorControls, {}, [
el( SelectControl, {
value : props.attributes.id,
label : __( 'Widget', 'widget-shortcode' ),
options : widgetShortcodeGutenberg.widgets,
onChange : function( id ) {
props.setAttributes( { id } );
},
} )
] )
] )
},
save() {
// nothing to see here, ServerSideRender handles this
return null;
},
} );
}(
window.wp
) );

View File

@@ -0,0 +1,30 @@
(function($) {
if ( typeof widgetShortcodeTinyMCE === 'undefined' )
return;
if ( widgetShortcodeTinyMCE.widgets.length < 1 )
return;
tinymce.PluginManager.add( 'widgetShortcode', function( editor, url ) {
var items = [];
$.each( widgetShortcodeTinyMCE.widgets, function( i, v ) {
var item = {
'text' : v.label,
'body': {
'type': v.label
},
'onclick' : function(){
editor.insertContent( '[widget id="' + v.value + '"]' );
}
};
items.push( item );
} );
editor.addButton( 'widgetShortcode', {
title: widgetShortcodeTinyMCE.title,
type : 'menubutton',
image : widgetShortcodeTinyMCE.image,
menu : items
});
});
})(jQuery);

Binary file not shown.

After

Width:  |  Height:  |  Size: 786 B

View File

@@ -0,0 +1,77 @@
<?php
/**
* Gutenberg compatibility for Widget Shortcode plugin
*
* @package Widget Shortcode
* @since 0.3.4
*/
class Widget_Shortcode_Gutenberg {
private static $instance = null;
/**
* Creates or returns an instance of this class.
*
* @return A single instance of this class.
*/
public static function get_instance() {
return null == self::$instance ? self::$instance = new self : self::$instance;
}
private function __construct() {
if ( ! function_exists( 'register_block_type' ) ) {
// Gutenberg is not active.
return;
}
add_action( 'init', array( $this, 'register_block' ) );
}
function register_block() {
wp_register_script(
'widget-shortcode-gutenberg',
WIDGET_SHORTCODE_URL . 'assets/block.js',
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components' ),
filemtime( WIDGET_SHORTCODE_DIR . 'assets/block.js' ),
true
);
$widgets = Widget_Shortcode::get_instance()->get_widgets_list();
array_unshift( $widgets, array( 'value' => '', 'label' => '' ) );
wp_localize_script( 'widget-shortcode-gutenberg', 'widgetShortcodeGutenberg', array(
'widgets' => $widgets,
) );
register_block_type( 'widget-shortcode/block', array(
'editor_script' => 'widget-shortcode-gutenberg',
'render_callback' => array( $this, 'render_callback' ),
'attributes' => array(
'id' => array(
'default' => '',
'type' => 'string',
),
'className' => array(
'default' => '',
'type' => 'string',
),
),
) );
}
/**
* Render the widget preview in Gutenberg window
*
* @return string
*/
function render_callback( $atts, $content ) {
if ( ! isset( $atts['id'] ) || empty( $atts['id'] ) ) {
return '<p>' . __( 'Select the widget you want to show.', 'widget-shortcode' ) . '</p>';
}
return Widget_Shortcode::get_instance()->do_widget( array(
'echo' => false,
'id' => $atts['id'],
'css_class' => $atts['className'],
) );
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* Shortcode generator for Widget Shortcode plugin
*
* @package Widget Shortcode
* @since 0.3.4
*/
class Widget_Shortcode_TinyMCE {
private static $instance = null;
/**
* Creates or returns an instance of this class.
*
* @return A single instance of this class.
*/
public static function get_instance() {
return null == self::$instance ? self::$instance = new self : self::$instance;
}
private function __construct() {
add_filter( 'mce_external_plugins', array( $this, 'mce_external_plugins' ) );
add_filter( 'mce_buttons', array( $this, 'mce_buttons' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'editor_parameters' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'editor_parameters' ) );
}
function mce_external_plugins( $plugins ) {
$plugins['widgetShortcode'] = WIDGET_SHORTCODE_URL . 'assets/tinymce.js';
return $plugins;
}
function mce_buttons( $mce_buttons ) {
array_push( $mce_buttons, 'separator', 'widgetShortcode' );
return $mce_buttons;
}
function editor_parameters() {
wp_localize_script( 'editor', 'widgetShortcodeTinyMCE', array(
'title' => __( 'Widget Shortcode', 'widget-shortcode' ),
'widgets' => Widget_Shortcode::get_instance()->get_widgets_list(),
'image' => WIDGET_SHORTCODE_URL . 'assets/widget-icon.png',
) );
}
}

View File

@@ -0,0 +1,266 @@
<?php
/*
* Widget Shortcode main class
* Provides mains functions for the plugin
*
* @package Widget Shortcode
*/
class Widget_Shortcode {
private static $instance = null;
/**
* Creates or returns an instance of this class.
*
* @return A single instance of this class.
* @since 0.2.4
*/
public static function get_instance() {
return null == self::$instance ? self::$instance = new self : self::$instance;
}
private function __construct() {
add_shortcode( 'widget', array( $this, 'shortcode' ) );
add_action( 'plugins_loaded', array( $this, 'i18n' ), 5 );
add_action( 'widgets_init', array( $this, 'arbitrary_sidebar' ), 20 );
add_action( 'in_widget_form', array( $this, 'in_widget_form' ), 10, 3 );
}
/**
* Load translation files
*
* @since 0.2.4
*/
function i18n() {
load_plugin_textdomain( 'widget-shortcode', false, '/languages' );
}
/**
* output a widget using 'widget' shortcode.
*
* Requires the widget ID.
* You can overwrite widget args: before_widget, before_title, after_title, after_widget
*
* @example [widget id="text-1"]
* @since 0.1
*/
public function shortcode( $atts, $content = null ) {
$atts['echo'] = false;
return $this->do_widget( $atts );
}
/**
* Registers arbitrary widget area
*
* Although you can use the widget shortcode for any widget in any widget area,
* you can use this arbitrary widget area for your widgets, since they don't show up
* in the front-end.
*
* @since 0.1
* @return void
*/
function arbitrary_sidebar() {
register_sidebar( array(
'name' => __( 'Widget Shortcode', 'widget-shortcode' ),
'description' => __( 'This widget area is not displayed on frontend and can be used for [widget] shortcode.', 'widget-shortcode' ),
'id' => 'arbitrary',
'before_widget' => '',
'after_widget' => '',
) );
}
/**
* Shows the shortcode for the widget
*
* @since 0.1
* @return void
*/
function in_widget_form( $widget, $return, $instance ) {
echo '<p>' .
__( 'Shortcode', 'widget-shortcode' ) . ': ' . ( ( $widget->number == '__i__' ) ? __( 'Please save this first.', 'widget-shortcode' ) : '<input type="text" value="' . esc_attr( '[widget id="'. $widget->id .'"]' ) . '" readonly="readonly" class="widefat" onclick="this.select()" />' ) .
'</p>';
}
/**
* Returns an array of all widgets as the key, their position as the value
*
* @since 0.2.2
* @return array
*/
function get_widgets_map() {
$sidebars_widgets = wp_get_sidebars_widgets();
$widgets_map = array();
if ( ! empty( $sidebars_widgets ) )
foreach ( $sidebars_widgets as $position => $widgets )
if ( ! empty( $widgets) )
foreach ( $widgets as $widget )
$widgets_map[ $widget ] = $position;
return $widgets_map;
}
/**
* Get widget options
*
* @since 0.2.4
*/
public function get_widget_options( $widget_id ) {
global $wp_registered_widgets;
if ( isset( $wp_registered_widgets[ $widget_id ] ) ) {
preg_match( '/-(\d+)$/', $widget_id, $number );
$options = get_option( $wp_registered_widgets[ $widget_id ]['callback'][0]->option_name );
$instance = $options[ $number[1] ];
}
return isset( $instance ) ? $instance : array();
}
/**
* Displays a widget
*
* @param mixed args
* @since 0.2
* @return string widget output
*/
function do_widget( $args ) {
global $_wp_sidebars_widgets, $wp_registered_widgets, $wp_registered_sidebars;
extract( shortcode_atts( array(
'id' => '',
'css_class' => '',
'title' => true, /* whether to display the widget title */
'container_tag' => 'div',
'container_class' => 'widget %2$s',
'container_id' => '%1$s',
'title_tag' => 'h2',
'title_class' => 'widgettitle',
'echo' => true
), $args, 'widget' ) );
/*
* @note: for backward compatibility: allow overriding widget args through the shortcode parameters
*/
$widget_args = shortcode_atts( array(
'before_widget' => '<' . $container_tag . ' id="' . $container_id . '" class="' . $container_class . ' ' . $css_class . '">',
'before_title' => '<' . $title_tag . ' class="' . $title_class . '">',
'after_title' => '</' . $title_tag . '>',
'after_widget' => '</' . $container_tag . '>',
), $args );
extract( $widget_args );
if ( empty( $id ) || ! isset( $wp_registered_widgets[ $id ] ) )
return;
// get the widget instance options
preg_match( '/-(\d+)$/', $id, $number );
$options = ( ! empty( $wp_registered_widgets ) && ! empty( $wp_registered_widgets[ $id ] ) ) ? get_option( $wp_registered_widgets[ $id ]['callback'][0]->option_name ) : array();
$instance = isset( $options[ $number[1] ] ) ? $options[ $number[1] ] : array();
$class = get_class( $wp_registered_widgets[ $id ]['callback'][0] );
// maybe the widget is removed or de-registered
if ( ! $class )
return;
/* build the widget args that needs to be filtered through dynamic_sidebar_params */
$params = array(
0 => array(
'name' => '',
'id' => '',
'description' => '',
'before_widget' => $before_widget,
'before_title' => $before_title,
'after_title' => $after_title,
'after_widget' => $after_widget,
'widget_id' => $id,
'widget_name' => $wp_registered_widgets[ $id ]['name']
),
1 => array(
'number' => $number[0]
)
);
// if feasable, use sidebar's parameters
$widgets_map = $this->get_widgets_map();
if ( isset( $widgets_map[ $id ] ) ) {
$params[0]['name'] = $wp_registered_sidebars[ $widgets_map[ $id ] ]['name'];
$params[0]['id'] = $wp_registered_sidebars[ $widgets_map[ $id ] ]['id'];
$params[0]['description'] = $wp_registered_sidebars[ $widgets_map[ $id ] ]['description'];
}
$params = apply_filters( 'dynamic_sidebar_params', $params );
$show_title = ( '0' === $title || 'no' === $title || false === $title ) ? false : true;
if ( ! $show_title ) {
$params[0]['before_title'] = '<!-- widget_shortcode_before_title -->';
$params[0]['after_title'] = '<!-- widget_shortcode_after_title -->';
} elseif ( is_string( $title ) && strlen( $title ) > 0 ) {
$instance['title'] = $title;
}
// Substitute HTML id and class attributes into before_widget
$classname_ = '';
foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) {
if ( is_string( $cn ) )
$classname_ .= '_' . $cn;
elseif ( is_object($cn) )
$classname_ .= '_' . get_class( $cn );
}
$classname_ = ltrim( $classname_, '_' );
$classname_ .= ' widget-shortcode';
/* adds area-{AREA} classname to the widget, indicating the widget's original location */
if ( isset( $widgets_map[ $id ] ) ) {
$classname_ .= ' area-' . $widgets_map[ $id ];
}
$params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $id, $classname_ );
// render the widget
ob_start();
echo '<!-- Widget Shortcode -->';
the_widget( $class, $instance, $params[0] );
echo '<!-- /Widget Shortcode -->';
$content = ob_get_clean();
// supress the title if we wish
if ( ! $show_title ) {
$content = preg_replace( '/<!-- widget_shortcode_before_title -->(.*?)<!-- widget_shortcode_after_title -->/', '', $content );
}
if ( $echo !== true )
return $content;
echo $content;
}
/**
* Returns an array of widgets in the format of [ $id => $label ]
*
* @since 0.3.4
* @return array
*/
function get_widgets_list() {
global $wp_registered_widgets;
$widgets = array();
$all_widgets = $this->get_widgets_map();
if ( ! empty( $all_widgets ) ) {
foreach ( $all_widgets as $id => $position ) {
if ( $position == 'arbitrary' ) {
$title = isset( $wp_registered_widgets[ $id ]['name'] ) ? $wp_registered_widgets[ $id ]['name'] : '';
$options = $this->get_widget_options( $id );
if ( isset( $options['title'] ) && ! empty( $options['title'] ) ) {
$title = join( ': ', array( $title, $options['title'] ) );
}
$widgets[] = array(
'value' => $id,
'label' => $title,
);
}
}
}
return $widgets;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
Plugin Name: Widget Shortcode
Description: Output widgets using a simple shortcode.
Author: Hassan Derakhshandeh
Version: 0.3.5
Text Domain: widget-shortcode
Domain Path: /languages
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
defined( 'ABSPATH' ) || die;
define( 'WIDGET_SHORTCODE_URL', trailingslashit( plugin_dir_url( __FILE__ ) ) );
define( 'WIDGET_SHORTCODE_DIR', trailingslashit( plugin_dir_path( __FILE__ ) ) );
include WIDGET_SHORTCODE_DIR . 'includes/class-widget-shortcode.php';
include WIDGET_SHORTCODE_DIR . 'includes/class-widget-shortcode-tinymce.php';
include WIDGET_SHORTCODE_DIR . 'includes/class-widget-shortcode-gutenberg.php';
Widget_Shortcode::get_instance();
Widget_Shortcode_TinyMCE::get_instance();
Widget_Shortcode_Gutenberg::get_instance();

View File

@@ -0,0 +1,91 @@
=== Widget Shortcode ===
Contributors: shazdeh
Plugin Name: Widget Shortcode
Tags: widget, shortcode, theme, admin
Requires at least: 3.0
Tested up to: 5.4.1
Stable tag: 0.3.5
Adds [widget] shortcode which enables you to output widgets anywhere you like.
== Description ==
The shortcode requires the widget ID, but no need to guess, the plugin generates the code for you. To override the widget title for a widget you can use the "title" parameter:
<code>[widget id="text-1" title="New title"]</code>
You can also hide the widget title entirely if desired:
<code>[widget id="text-1" title="0"]</code>
Additional Parameters:
* <strong>container_tag</strong>: the HTML tag to use for the widget container, default: <code>div</code>
* <strong>container_class</strong>: CSS classname added to the widget container, default: <code>widget %2$s</code> (Note: uses sprintf for variable substitution)
* <strong>container_id</strong>: HTML ID attribute for the widget container, default: <code>%1$s</code> (Note: uses sprintf for variable substitution)
* <strong>title_tag</strong>: HTML tag to use for the widget title wrapper, default: <code>h2</code>
* <strong>title_class</strong>: CSS classname for the widget title wrapper, default: <code>widgettitle</code>
== Installation ==
1. Upload the whole plugin directory to the `/wp-content/plugins/` directory
2. Activate the plugin through the 'Plugins' menu in WordPress
3. That's it. Use the [widget] shortcode anywhere you want
4. Enjoy!
== Screenshots ==
1. The plugin generates the shortcode for you
2. The shortcode generator in TinyMCE editor
3. The new Widget Shortcode block in Gutenberg
== Changelog ==
= 0.3.5 =
* Add support for the Additional CSS Classes field in Gutenberg
= 0.3.4 =
* New Gutenberg block
= 0.3.3 =
* Additional CSS classes to the widget wrapper
= 0.3.2 =
* Fix bug with getting correct widget instance. Thanks @frankieandshadow!
= 0.3.0 =
* Fix possible notice message
= 0.2.9 =
* Fix notice messages regarding unregistered widgets
= 0.2.8 =
* Fix bug where widget titles do not show. Thanks @websitedons!
= 0.2.7 =
* update how the shortcode code is displayed to avoid potential problems; thanks @websitedons!
= 0.2.6 =
* Fix for possible notice message
* Additional parameters to control the widget output
= 0.2.5 =
* Fix notice message if widget is inactive
= 0.2.4 =
* Shortcode generator in TinyMCE
* i18n
* Minor code refactoring
= 0.2.3 =
* Fixed missing widget classes and ID from before_widget
= 0.2.2 =
* Fixed a bug with removing titles from widget
* Compatibility with Widget Title Links and other plugins that change the widget args
= 0.2.1 =
* Added the ability to override the widget title in shortcode
* Support for filtering the default widget args
* Cleared the PHP notice
= 0.2 =
* Added do_widget function
* Added the 'title' option which enables to suppress the widget title