Example #1
0
function enlightenment_page_builder_form($post)
{
    if (!isset($_GET['post'])) {
        _e('Please save this post as Draft to use the Page Builder.', 'enlightenment');
        return;
    }
    wp_nonce_field('enlightenment_page_builder_form', 'enlightenment_page_builder_form_nonce');
    echo enlightenment_open_tag('p');
    enlightenment_checkbox(array('name' => 'enlightenment_default_template_hooks', 'checked' => '' == get_post_meta($post->ID, '_enlightenment_page_builder', true), 'label' => sprintf(__('Use default template hooks for %1$s', 'enlightenment'), $post->post_type)));
    echo enlightenment_close_tag('p');
    $template = enlightenment_get_template(get_post_type());
    $template['hooks'] = array_keys(enlightenment_template_hooks());
    do_action('enlightenment_before_page_builder');
    echo enlightenment_open_tag('div', 'template-hooks');
    foreach ($template['hooks'] as $hook) {
        $atts = enlightenment_get_template_hook($hook);
        $available_functions = $atts['functions'];
        $template_hooks = get_post_meta($post->ID, '_enlightenment_page_builder', true);
        if ('' == $template_hooks) {
            $template_hooks = enlightenment_theme_option('template_hooks');
        }
        if (isset($template_hooks[enlightenment_current_template()][$hook])) {
            $hooked_functions = $template_hooks[enlightenment_current_template()][$hook];
        } else {
            $hooked_functions = array();
            global $wp_filter;
            if (isset($wp_filter[$hook]) && isset($wp_filter[$hook][10])) {
                foreach ($wp_filter[$hook][10] as $function) {
                    $hooked_functions[] = $function['function'];
                }
            }
        }
        $available_functions = array_diff($available_functions, $hooked_functions);
        if (!empty($available_functions) || !empty($hooked_functions)) {
            echo '<h2>' . esc_attr($atts['name']) . '</h2>';
            enlightenment_template_hook_actions(array('hook' => $hook, 'name' => 'enlightenment_page_builder[' . $hook . ']', 'class' => 'template-hooks'));
        }
    }
    echo enlightenment_close_tag();
    do_action('enlightenment_after_page_builder');
}
function enlightenment_template_hook_actions($args, $echo = true)
{
    global $wp_filter, $pagenow;
    $hook = enlightenment_get_template_hook($args['hook']);
    $available_functions = apply_filters($args['hook'] . '_available_functions', $hook['functions']);
    $template = enlightenment_get_template(enlightenment_current_template());
    if (current_theme_supports('enlightenment-page-builder') && 'post_type' == $template['type'] && ('post.php' == $pagenow || 'post-new.php' == $pagenow) && isset($_GET['action']) && 'edit' == $_GET['action']) {
        global $post;
        $template_hooks = array();
        $template_hooks[enlightenment_current_template()] = get_post_meta($post->ID, '_enlightenment_page_builder', true);
        if ('' == $template_hooks) {
            $template_hooks = enlightenment_theme_option('template_hooks');
        }
    } else {
        $template_hooks = enlightenment_theme_option('template_hooks');
    }
    if (isset($template_hooks[enlightenment_current_template()][$args['hook']])) {
        $hooked_functions = $template_hooks[enlightenment_current_template()][$args['hook']];
    } else {
        $hooked_functions = array();
        if (isset($wp_filter[$args['hook']]) && isset($wp_filter[$args['hook']][10])) {
            foreach ($wp_filter[$args['hook']][10] as $function) {
                $hooked_functions[] = $function['function'];
            }
        }
    }
    $available_functions = array_diff($available_functions, $hooked_functions);
    $output = '';
    $output .= '<div class="widget-liquid-left">';
    $output .= '<div id="widgets-left">';
    $output .= '<div id="available-widgets" class="widgets-holder-wrap available-functions-' . esc_attr($args['hook']) . '">';
    $output .= '<div class="sidebar-name">';
    $output .= '<h3>Available Functions <span id="removing-widget">Deactivate <span></span></span></h3>';
    $output .= '</div>';
    $output .= '<div class="widget-holder">';
    $output .= '<div id="widget-list" class="widget-list">';
    foreach ($available_functions as $function) {
        $output .= enlightenment_open_tag('div', 'widget', '', 'data-function="' . $function . '"');
        // ui-draggable' );
        $output .= '<div class="widget-top">';
        $output .= '<div class="widget-title">';
        $output .= '<h4>' . enlightenment_template_function_name($function) . '</h4>';
        $output .= enlightenment_close_tag('div');
        $output .= enlightenment_close_tag('div');
        $output .= enlightenment_close_tag('div');
    }
    $output .= '</div>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= '<div class="widget-liquid-right">';
    $output .= '<div id="widgets-right">';
    $output .= '<div class="widgets-holder-wrap">';
    $output .= '<div id="sidebar-' . esc_attr($args['hook']) . '" class="widgets-sortables hooked-functions-' . esc_attr($args['hook']) . '">';
    $output .= '<div class="sidebar-name">';
    $output .= '<h3>Hooked Functions <span class="spinner"></span></h3>';
    $output .= '</div>';
    foreach ($hooked_functions as $function) {
        $output .= enlightenment_open_tag('div', 'widget', '', 'data-function="' . $function . '"');
        $output .= '<div class="widget-top">';
        $output .= '<div class="widget-title">';
        $output .= '<h4>' . enlightenment_template_function_name($function) . '</h4>';
        $output .= enlightenment_close_tag('div');
        $output .= enlightenment_close_tag('div');
        $output .= enlightenment_close_tag('div');
    }
    $output .= '</div>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= '</div>';
    $output .= enlightenment_hidden_input(array('name' => $args['name'], 'value' => join(',', $hooked_functions)), false);
    ob_start();
    ?>
	<script>
		jQuery(document).ready(function($) {
			var submitted = false;
			$('form').submit(function() {
				submitted = true;
			});
			$('.available-functions-<?php 
    echo esc_attr($args['hook']);
    ?>
 .widget-list').sortable({
				connectWith: '.hooked-functions-<?php 
    echo esc_attr($args['hook']);
    ?>
',
				items: '.widget',
				placeholder: "widget-placeholder",
				revert: 'invalid',
			});
			$('.hooked-functions-<?php 
    echo esc_attr($args['hook']);
    ?>
').sortable({
				connectWith: '.available-functions-<?php 
    echo esc_attr($args['hook']);
    ?>
 .widget-list',
				items: '.widget',
				placeholder: "widget-placeholder",
				revert: 'invalid',
				activate: function(event, ui) {
					$(this).parent().addClass('widget-hover');
				},
				deactivate: function(event, ui) {
					$(this).parent().removeClass('widget-hover');
				},
				change: function(event, ui) {
					window.onbeforeunload = function(e) {
						if( ! submitted ) {
							var message = '<?php 
    _e('The changes you made will be lost if you navigate away from this page.', 'enlightenment');
    ?>
';
							e = e || window.event;
							// For IE and Firefox
							if(e) {
								e.returnValue = message;
							}
	
							// For Safari
							return message;
						}
					};
				},
				stop: function(event, ui) {
					if( $(ui.item).parent().hasClass('widgets-sortables') ) {
						var functions = [];
						$('.widget', $(ui.item).parent()).each(function() {
							functions.push($(this).data('function'));
						});
						$(ui.item).closest('.widget-liquid-right').next('input').val(functions.join());
					}
				},
				receive: function(event, ui) {
					var functions = [];
					$('.widget', $(ui.item).parent()).each(function() {
						functions.push($(this).data('function'));
					});
					$(ui.item).closest('.widget-liquid-right').next('input').val(functions.join());
				},
				remove: function(event, ui) {
					var functions = [];
					$('.widget', $(ui.item).closest('.widget-liquid-left').next('.widget-liquid-right')).each(function() {
						functions.push($(this).data('function'));
					});
					$(ui.item).closest('.widget-liquid-left').next('.widget-liquid-right').next('input').val(functions.join());
				},
			});
		});
	</script>
	<?php 
    $output .= ob_get_clean();
    $output .= '<br style="clear:both">';
    if (!$echo) {
        return $output;
    }
    echo $output;
}