Example #1
0
/**
 * Get shortcodes meta box content
 *
 */
function ts_get_shortcodes_meta_box_cotent()
{
    $content = __('Available shortcodes:', 'framework');
    $aHelp = ts_get_shortcodes_list();
    if (is_array($aHelp)) {
        $iCount = count($aHelp);
        $i = 0;
        $col1 = '';
        $col2 = '';
        foreach ($aHelp as $aShortcode) {
            $col = '
				<div class="framework-box">
					<div class="toggle-shortcode" title="' . __('Click to toggle', 'framework') . '"><br></div>
					<h3><span>' . $aShortcode['name'] . '</span></h3>
					<div class="box-description">';
            $usage = $aShortcode['usage'];
            if (!is_array($aShortcode['usage'])) {
                $usage = array();
                $usage[] = $aShortcode['usage'];
            }
            foreach ($usage as $item) {
                $col .= '<div class="shortcode-usage">' . $item . '</div>';
            }
            $description = $aShortcode['description'];
            if (!is_array($aShortcode['description'])) {
                $description = array();
                $description[] = $aShortcode['description'];
            }
            foreach ($description as $item) {
                $col .= '<p>' . $item . '</p>';
            }
            $col .= '
					</div>
				</div>';
            if ($iCount / 2 > $i) {
                $col1 .= $col;
            } else {
                $col2 .= $col;
            }
            $i++;
        }
    }
    $content .= '
		<div id="framework-shortcodes-help">
			<div class="col">
				<div class="colpad1">
					' . $col1 . '
				</div>
			</div>
			<div class="col">
				<div class="colpad2">
					' . $col2 . '
				</div>
			</div>
			<div class="clearfix"></div>
		</div>
		';
    return $content;
}
/**
 * Get page builder code
 * @return string
 */
function ts_get_page_builder()
{
    $aShortcodes = ts_get_shortcodes_list();
    $options = '<option value="">' . __('Choose item', 'framework') . '</option>';
    if (is_array($aShortcodes)) {
        foreach ($aShortcodes as $aShortcode) {
            $aShortcodesNames[sanitize_text_field($aShortcode["shortcode"])] = $aShortcode["name"];
            $options .= '<option value="' . sanitize_text_field($aShortcode["shortcode"]) . '">' . $aShortcode["name"] . '</option>';
        }
    }
    $page_builder_config = ts_get_page_builder_config();
    $post = null;
    if (isset($_POST['post'])) {
        $post = $_POST['post'];
    }
    if (!isset($_POST['template'])) {
        $template = 'default';
    } else {
        $template = $_POST['template'];
    }
    if (!isset($page_builder_config[$template])) {
        $template = 'default';
    }
    $content = '';
    foreach ($page_builder_config[$template] as $builder_key => $builder_label) {
        $page_builder_data = get_post_meta($post, ts_get_page_builder_meta_key($builder_key, 'page_builder_items'), true);
        $content .= '
			<div class="page_builder_container">
				<div class="format-setting-label">
					<label for="header_background" class="label">' . $builder_label . '</label>
				</div>
				<div class="format-setting type-select">
					<div class="format-setting-inner">
						<div class="select-wrapper" style="float: left">
							<span>' . __('Choose item', 'framework') . '</span>
							<select name="pb_select_items_' . $builder_key . '" id="pb_select_items_' . $builder_key . '" class="pb_select_items option-tree-ui-select ">
								' . $options . '
							</select>
						</div>
						<div id="pb_add_item_' . $builder_key . '" class="pb_add_item button button-primary button-large">' . __('Add Item', 'framework') . '</div>
					</div>
				</div>
				<!-- Pattern used for each added item (hidden element -->
				<div id="pb_item_pattern_' . $builder_key . '" class="pb_item_pattern">
					<div class="pb_item_wrapper" style="width: 50%">
						<div class="pb_item" data-item="{$item}" data-item-id="{$id}" data-size="1/2">
							<div class="pb_item_action pb_item_edit" title="' . __('Edit', 'framework') . '"></div>
							<div class="pb_item_action pb_item_size">1/2</div>
							<div class="pb_item_action pb_item_plus"></div>
							<div class="pb_item_action pb_item_minus"></div>
							<div class="pb_item_action pb_item_clone"></div>
							<div class="pb_item_action pb_item_remove" data-msg="' . esc_attr(__('Are you sure?', 'framework')) . '" title="' . __('Delete', 'framework') . '"></div>
							<div class="pb_item_title">{$title}</div>
							<input type="hidden" class="pb_item_data" id="pb_item_data_' . $builder_key . '_{$id}" name="pb_item_data_' . $builder_key . '_{$id}" value=\'\' />
							<input type="hidden" class="pb_item_size_value" id="pb_item_size_' . $builder_key . '_{$id}" name="pb_item_size_' . $builder_key . '_{$id}" value=\'1/2\' />
							<input type="hidden" class="pb_item_type" id="pb_item_type_' . $builder_key . '_{$id}" name="pb_item_type_' . $builder_key . '_{$id}" value=\'{$type}\' />
						</div>
					</div>
				</div> <!-- #pb_item_pattern -->

				<!-- Page builder items -->
				<div class="pb_wrapper">
					<div class="pb_inner" id="pb_inner_' . $builder_key . '" data-builder="' . $builder_key . '">';
        if (is_array($page_builder_data)) {
            $i = 0;
            foreach ($page_builder_data as $item) {
                if (empty($item['shortcode'])) {
                    continue;
                }
                $i++;
                if (version_compare(PHP_VERSION, '5.3.0', '>=') === true) {
                    $data = json_encode($item['data'], JSON_HEX_APOS);
                } else {
                    $data = str_replace(array('\\"', '\''), array('\\u0022', '\\u0027'), json_encode($item['data']));
                }
                $content .= '
						<div class="pb_item_wrapper" style="width: ' . ts_get_pb_size($item['size']) . '%">
							<div class="pb_item" data-item="' . $item['type'] . '" data-item-id="' . $i . '" data-size="' . $item['size'] . '">
								<div class="pb_item_action pb_item_edit" title="' . __('Edit', 'framework') . '"></div>
								<div class="pb_item_action pb_item_size">' . $item['size'] . '</div>
								<div class="pb_item_action pb_item_plus"></div>
								<div class="pb_item_action pb_item_minus"></div>
								<div class="pb_item_action pb_item_clone"></div>
								<div class="pb_item_action pb_item_remove" data-msg="' . esc_attr(__('Are you sure?', 'framework')) . '" title="' . __('Delete', 'framework') . '"></div>
								<div class="pb_item_title">' . $aShortcodesNames[$item['type']] . '</div>
								<input type="hidden" class="pb_item_data" id="pb_item_data_' . $builder_key . '_' . $i . '" name="pb_item_data_' . $builder_key . '_' . $i . '" value=\'' . $data . '\' />
								<input type="hidden" class="pb_item_size_value" id="pb_item_size_' . $builder_key . '_' . $i . '" name="pb_item_size_' . $builder_key . '_' . $i . '" value=\'' . $item['size'] . '\' />
								<input type="hidden" class="pb_item_type" id="pb_item_type_' . $builder_key . '_' . $i . '" name="pb_item_type_' . $builder_key . '_' . $i . '" value=\'' . $item['type'] . '\' />
							</div>
						</div>';
            }
        }
        $content .= '
						<div class="pb_clear" id="pb_clear' . $builder_key . '"></div>
					</div> <!-- .pb_inner -->
				</div> <!-- .pb_wrapper -->
			</div> <!-- .page_builder_container -->';
    }
    $content .= '
		<script>
			jQuery(document).ready(function ($) {

				$(".pb_inner").sortable({
					cursor: "pointer",
					items: "div.pb_item_wrapper",
					placeholder: "placeholder",
					containment: "#page-builder",
					connectWith: ".pb_inner",
					forcePlaceholderSize: true,
					start: function(e, ui ) {
						ui.placeholder.width(ui.helper.outerWidth() - 5);
					},
					update: function( event, ui ) {
						$this = this;
						
						//get page builder container id
						$pb_inner = $($this).closest(".pb_inner").attr("id");
						$pb_inner_id = $pb_inner.replace("pb_inner_content_","");
						
						//get current element old container id
						$item = $($this).find(".pb_item_data").attr("id");
						
						$item_values = $item.split("_");
						$old_item_id = $item_values[5];
						$old_item_pb_inner_id = $item_values[4];
						
						//change item attributes if item was moved to the new container
						if ($pb_inner_id != $old_item_pb_inner_id) {
							
							//find next id for a new element
							$next_item_id = 1;
							$($this).closest(".page_builder_container").find(".pb_inner .pb_item").each(function(index) {
								$item_id = $(this).attr("data-item-id");
								$item_id = parseInt($item_id);
								if ($item_id >= $next_item_id)
								{
									$next_item_id = $item_id + 1;
								}
							});
							
							
							//replace attributes with a new one with id of the new container $pb_inner_id
							$old_item = $old_item_pb_inner_id + "_" + $old_item_id;
							
							$("#pb_item_data_content_" + $old_item).closest(".pb_item").attr("data-item-id",$next_item_id);
							$("#pb_item_data_content_" + $old_item).attr("name","pb_item_data_content_" + $pb_inner_id + "_" + $next_item_id);
							$("#pb_item_data_content_" + $old_item).attr("id","pb_item_data_content_" + $pb_inner_id + "_" + $next_item_id);
							$("#pb_item_size_content_" + $old_item).attr("name","pb_item_size_content_" + $pb_inner_id + "_" + $next_item_id);
							$("#pb_item_size_content_" + $old_item).attr("id","pb_item_size_content_" + $pb_inner_id + "_" + $next_item_id);
							$("#pb_item_type_content_" + $old_item).attr("name","pb_item_type_content_" + $pb_inner_id + "_" + $next_item_id);
							$("#pb_item_type_content_" + $old_item).attr("id","pb_item_type_content_" + $pb_inner_id + "_" + $next_item_id);
							
						}
					}
				}).disableSelection();
			});
		</script>

	';
    $content .= '<input type="hidden" id="popup_url" name="popup_url" value="' . get_template_directory_uri() . '/framework/" />';
    echo $content;
    die;
}
<?php

header('Content-Type: text/javascript');
$path = realpath(dirname(__FILE__));
$position = strrpos($path, 'wp-content');
$wp_path = substr($path, 0, $position);
require_once $wp_path . 'wp-load.php';
$aShortcodes = ts_get_shortcodes_list();
$count = count($aShortcodes);
?>


( function() {
    tinymce.PluginManager.add( 'ShortcodesSelector', function( editor, url ) {
	
		editor.addCommand("shortcodesPopup", function ( a, params )
		{
			tb_show("<?php 
_e("Shortcodes", "framework");
?>
", "<?php 
echo get_template_directory_uri();
?>
/framework/popup.php?shortcode=" + params.shortcode + "&width=" + 630);
		});
	
        // Add a button that opens a window
        editor.addButton( 'ShortcodesSelector', {

            text: 'Shortcodes',
            icon: false,
 /**
  * Construct
  * @param string $sShortcode
  * @return void
  */
 public function __construct($sShortcode)
 {
     $this->_aShortcodes = ts_get_shortcodes_list();
     $this->_sShortcode = $sShortcode;
 }