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; }
/** * Process post content * * @param array $atts Shortcode attributes * @param string $input Data wrapped by the [layotter] shortcode * @return string HTML for frontend view of the current post */ function layotter_frontend_shortcode($atts, $input = '') { // since 1.5.0, shortcode attributes carry a post ID, and JSON is stored in a custom field // before 1.5.0, JSON was stored directly in the post content // get_the_ID() wouldn't be reliable here because this shortcode handler might be triggered in a context where the // $post variable hasn't been correctly initialized, like do_shortcode() or apply_filters('the_content') if (isset($atts['post']) and Layotter::is_enabled_for_post($atts['post'])) { $post_id = intval($atts['post']); $layotter = new Layotter_Post($post_id); } else { $layotter = new Layotter_Post($input); } $html = $layotter->get_frontend_view(); // apply wptexturize manually after post HTML has been parsed because automatic wptexturizing is disabled for // Layotter content (see layotter_disable_wptexturize() below) return wptexturize($html); }