function layotter_make_search_dump($data, $raw_post) { $post_id = $raw_post['ID']; // don't change anything if not editing a Layotter-enabled post if (!Layotter::is_enabled_for_post($post_id) or !isset($raw_post['layotter_json'])) { return $data; } // copy JSON from POST and strip slashes that were added by Wordpress $json = $raw_post['layotter_json']; $unslashed_json = stripslashes_deep($json); // turn JSON into post content HTML $layotter_post = new Layotter_Post($unslashed_json); $content = $layotter_post->get_frontend_view(); // save JSON to a custom field (oddly enough, Wordpress breaks JSON if it's stripslashed) update_post_meta($post_id, 'layotter_json', $json); // insert spaces to prevent <p>foo</p><p>bar</p> becoming "foobar" instead of "foo bar" // then strip all tags except <img> // then remove excess whitespace $spaced_content = str_replace('<', ' <', $content); $clean_content = strip_tags($spaced_content, '<img>'); $normalized_content = trim($clean_content); if (function_exists('mb_ereg_replace')) { $normalized_content = mb_ereg_replace('/\\s+/', ' ', $normalized_content); } // wrap search dump with a [layotter] shortcode and return modified post data to be saved to the database // add the post ID because otherwise the shortcode handler would have no reliable way to get the post ID through // which the JSON data will be fetched $shortcoded_content = '[layotter post="' . $post_id . '"]' . $normalized_content . '[/layotter]'; $data['post_content'] = $shortcoded_content; return $data; }
function layotter_disable_wpautop($content) { if (Layotter::is_enabled_for_post(get_the_ID())) { remove_filter('the_content', 'wpautop'); } return $content; }
function layotter_admin_head() { if (!Layotter::is_enabled()) { return; } $post_type = get_post_type(); // remove TinyMCE remove_post_type_support($post_type, 'editor'); // insert layotter add_meta_box('layotter_wrapper', 'Layotter', 'layotter_output_interface', $post_type, 'normal', 'high'); }
function layotter_assets_admin_enqueue_scripts() { // load assets only if necessary if (!Layotter::is_enabled()) { return; } // styles wp_enqueue_style('layotter', plugins_url('assets/css/editor.css', __DIR__)); wp_enqueue_style('layotter-font-awesome', plugins_url('assets/css/font-awesome.min.css', __DIR__)); // jQuery plugin used to serialize form data wp_enqueue_script('layotter-serialize', plugins_url('assets/js/vendor/jquery.serialize-object.compiled.js', __DIR__), array('jquery')); // Angular scripts $scripts = array('angular' => 'assets/js/vendor/angular.js', 'angular-animate' => 'assets/js/vendor/angular-animate.js', 'angular-sanitize' => 'assets/js/vendor/angular-sanitize.js', 'angular-ui-sortable' => 'assets/js/vendor/angular-ui-sortable.js', 'layotter' => 'assets/js/app/app.js', 'layotter-controller-editor' => 'assets/js/app/controllers/editor.js', 'layotter-controller-templates' => 'assets/js/app/controllers/templates.js', 'layotter-controller-form' => 'assets/js/app/controllers/form.js', 'layotter-service-state' => 'assets/js/app/services/state.js', 'layotter-service-data' => 'assets/js/app/services/data.js', 'layotter-service-content' => 'assets/js/app/services/content.js', 'layotter-service-templates' => 'assets/js/app/services/templates.js', 'layotter-service-layouts' => 'assets/js/app/services/layouts.js', 'layotter-service-view' => 'assets/js/app/services/view.js', 'layotter-service-forms' => 'assets/js/app/services/forms.js', 'layotter-service-modals' => 'assets/js/app/services/modals.js', 'layotter-service-history' => 'assets/js/app/services/history.js'); foreach ($scripts as $name => $path) { wp_enqueue_script($name, plugins_url($path, __DIR__)); } // fetch allowed row layouts and default layout $allowed_row_layouts = Layotter_Settings::get_allowed_row_layouts(); $default_row_layout = Layotter_Settings::get_default_row_layout(); // fetch default values for post, row and element options $default_post_options = new Layotter_Options('post'); $default_row_options = new Layotter_Options('row'); $default_col_options = new Layotter_Options('col'); $default_element_options = new Layotter_Options('element'); // fetch content structure for the current post $post_id = get_the_ID(); $content_structure = new Layotter_Post($post_id); // fetch post layouts and element templates $saved_layouts = Layotter_Layouts::get_all(); $saved_templates = Layotter_Templates::get_all_for_post($post_id); // fetch available element types $element_objects = Layotter::get_filtered_element_types($post_id); $element_types = array(); foreach ($element_objects as $element_object) { $element_types[] = array('type' => $element_object->get('type'), 'title' => $element_object->get('title'), 'description' => $element_object->get('description'), 'icon' => $element_object->get('icon')); } // fetch general settings $enable_post_layouts = Layotter_Settings::post_layouts_enabled(); $enable_element_templates = Layotter_Settings::element_templates_enabled(); // inject data for use with Javascript wp_localize_script('layotter', 'layotterData', array('postID' => $post_id, 'isACFPro' => Layotter_ACF::is_pro_installed(), 'contentStructure' => $content_structure->to_array(), 'allowedRowLayouts' => $allowed_row_layouts, 'defaultRowLayout' => $default_row_layout, 'savedLayouts' => $saved_layouts, 'savedTemplates' => $saved_templates, 'enablePostLayouts' => $enable_post_layouts, 'enableElementTemplates' => $enable_element_templates, 'elementTypes' => $element_types, 'options' => array('post' => array('enabled' => $default_post_options->is_enabled(), 'defaults' => $default_post_options->get_clean_values()), 'row' => array('enabled' => $default_row_options->is_enabled(), 'defaults' => $default_row_options->get_clean_values()), 'col' => array('enabled' => $default_col_options->is_enabled(), 'defaults' => $default_col_options->get_clean_values()), 'element' => array('enabled' => $default_element_options->is_enabled(), 'defaults' => $default_element_options->get_clean_values())), 'i18n' => array('delete_row' => __('Delete row', 'layotter'), 'delete_element' => __('Delete element', 'layotter'), 'delete_template' => __('Delete template', 'layotter'), 'edit_template' => __('Edit template', 'layotter'), 'cancel' => __('Cancel', 'layotter'), 'discard_changes' => __('Discard changes', 'layotter'), 'discard_changes_confirmation' => __('Do you want to cancel and discard all changes?', 'layotter'), 'delete_row_confirmation' => __('Do you want to delete this row and all its elements?', 'layotter'), 'delete_element_confirmation' => __('Do you want to delete this element?', 'layotter'), 'delete_template_confirmation' => __('Do you want to delete this template? You can not undo this action.', 'layotter'), 'edit_template_confirmation' => __('When editing a template, your changes will be reflected on all pages that are using it. Do you want to edit this template?', 'layotter'), 'save_new_layout_confirmation' => __('Please enter a name for your layout:', 'layotter'), 'save_layout' => __('Save layout', 'layotter'), 'rename_layout_confirmation' => __('Please enter the new name for this layout:', 'layotter'), 'rename_layout' => __('Rename layout', 'layotter'), 'delete_layout_confirmation' => __('Do want to delete this layout? You can not undo this action.', 'layotter'), 'delete_layout' => __('Delete layout', 'layotter'), 'load_layout_confirmation' => __('Do want to load this layout and overwrite the existing content?', 'layotter'), 'load_layout' => __('Load layout', 'layotter'), 'history' => array('undo' => __('Undo:', 'layotter'), 'redo' => __('Redo:', 'layotter'), 'add_element' => __('Add element', 'layotter'), 'edit_element' => __('Edit element', 'layotter'), 'duplicate_element' => __('Duplicate element', 'layotter'), 'delete_element' => __('Delete element', 'layotter'), 'move_element' => __('Move element', 'layotter'), 'save_element_as_template' => __('Save element as template', 'layotter'), 'create_element_from_template' => __('Create element from template', 'layotter'), 'add_row' => __('Add row', 'layotter'), 'change_row_layout' => __('Change row layout', 'layotter'), 'duplicate_row' => __('Duplicate row', 'layotter'), 'delete_row' => __('Delete row', 'layotter'), 'move_row' => __('Move row', 'layotter'), 'edit_post_options' => __('Edit post options', 'layotter'), 'edit_row_options' => __('Edit row options', 'layotter'), 'edit_column_options' => __('Edit column options', 'layotter'), 'edit_element_options' => __('Edit element options', 'layotter'), 'load_post_layout' => __('Load layout', 'layotter'))))); }
/** * Create a new columns * * @param array $structure Column structure */ public function __construct($structure) { $structure = $this->validate_structure($structure); $this->width = $structure['width']; $this->options = new Layotter_Options('col', $structure['options']); foreach ($structure['elements'] as $element) { $element_object = false; // if a template_id is set, try to create a template if (isset($element['template_id'])) { $element_object = Layotter_Templates::create_element($element); } // if the template doesn't exist anymore, create a regular element if (!$element_object) { $element_object = Layotter::create_element($element); } if ($element_object) { $this->elements[] = $element_object; } } }
/** * Create a new element instance from a saved template * * @param int|array $id_or_structure Template ID or element structure with template_id * @return mixed New element instance, or false on failure */ public static function create_element($id_or_structure, $options = array()) { $element = false; if (is_array($id_or_structure)) { $structure = self::validate_structure($id_or_structure); return self::create_element($structure['template_id'], $structure['options']); } else { if (is_int($id_or_structure)) { $id = $id_or_structure; $templates = get_option('layotter_element_templates'); if (is_array($templates) and isset($templates[$id])) { $template = self::validate_structure($templates[$id]); $element = Layotter::create_element($template['type'], $template['values'], $options); } } } if (!$element) { return false; } if (!isset($template['deleted']) or !$template['deleted']) { $element->set_template_id($id); } return $element; }
function layotter_ajax_update_template() { $post_data = layotter_get_angular_post_data(); // type and field values are required if (isset($post_data['template_id']) and is_int($post_data['template_id'])) { $id = $post_data['template_id']; $template = Layotter_Templates::get($id); if ($template) { $values = Layotter_ACF::unwrap_post_values(); $element = Layotter::create_element($template['type'], $values); if ($element) { $element->set_template_id($id); Layotter_Templates::update($id, $element->get_template_data()); echo json_encode($element->to_array()); } } } die; // required by Wordpress after any AJAX call }
/** * Get element types enabled for a specific post * * @param int $post_id Post ID * @return array Element instances */ public static function get_filtered_element_types($post_id) { $elements = array(); foreach (array_keys(self::$registered_elements) as $element_type) { $element = Layotter::create_element($element_type); if ($element and $element->is_enabled_for($post_id)) { $elements[] = $element; } } usort($elements, array(__CLASS__, 'sort_element_types_helper')); return $elements; }
/** * Helper function for register_backend_hooks * * To make sure that Layotter::is_enabled() returns the correct value, the check is delayed until admin_footer. * Without the check, assets would be included on every single page in the backend. */ public static final function register_backend_hooks_helper() { if (Layotter::is_enabled()) { call_user_func(array(get_called_class(), 'backend_assets')); } }
*/ class Layotter_Example_Element extends Layotter_Element { protected function attributes() { $this->title = __('Example element', 'layotter'); $this->description = __('Use this element to play around and get started with Layotter.', 'layotter'); $this->icon = 'star'; $this->field_group = Layotter_ACF::get_example_field_group_name(); } protected function frontend_view($fields) { echo '<div class="layotter-example-element">'; echo $fields['content']; echo '</div>'; } protected function backend_view($fields) { echo '<div class="layotter-example-element">'; if (empty($fields['content'])) { echo '<center>'; _e('This element is empty. Click the edit button at the top right to add some content.', 'layotter'); echo '</center>'; } else { echo $fields['content']; } echo '</div>'; } } Layotter::register_element('layotter_example_element', 'Layotter_Example_Element');