Esempio n. 1
0
function wpip_wrapper_render()
{
    $options = get_option('wpip_settings');
    if ($options === FALSE) {
        $options = wpip_set_defaults();
    }
    ?>
	<input type='radio' name='wpip_settings[wpip_wrapper]' <?php 
    checked($options['wpip_wrapper'], 'block');
    ?>
 id="wpip_wrapper_block" value='block'><label for="wpip_wrapper_block">Use block wrapper (div). Example: <code>&lt;div data-post-id="1" class="insert-page">...&lt;/div></code></label><br />
	<input type='radio' name='wpip_settings[wpip_wrapper]' <?php 
    checked($options['wpip_wrapper'], 'inline');
    ?>
 id="wpip_wrapper_inline" value='inline'><label for="wpip_wrapper_inline">Use inline wrapper (span). Example: <code>&lt;span data-post-id="1" class="insert-page">...&lt;/span></code></label><br />
	<small><em>If you want to embed pages inline (for example, you can insert a link to a page in the flow of a normal paragraph), you should use inline tags. Note that the HTML spec does not allow block level elements within inline elements, so the inline wrapper has limited use.</em></small>
	<?php 
}
        function insertPages_handleShortcode_insert($atts, $content = null)
        {
            global $wp_query, $post, $wp_current_filter;
            // Shortcode attributes.
            $attributes = shortcode_atts(array('page' => '0', 'display' => 'all', 'class' => '', 'inline' => false), $atts);
            // Validation checks.
            if ($attributes['page'] === '0') {
                return $content;
            }
            // Trying to embed same page in itself.
            if (!is_null($post) && property_exists($post, 'ID') && ($attributes['page'] == $post->ID || $attributes['page'] == $post->post_name)) {
                return $content;
            }
            // Get options set in WordPress dashboard (Settings > Insert Pages).
            $options = get_option('wpip_settings');
            if ($options === FALSE || !is_array($options) || !array_key_exists('wpip_format', $options) || !array_key_exists('wpip_wrapper', $options) || !array_key_exists('wpip_insert_method', $options)) {
                $options = wpip_set_defaults();
            }
            $attributes['inline'] = $attributes['inline'] !== false && $attributes['inline'] !== 'false' || array_search('inline', $atts) === 0 || array_key_exists('wpip_wrapper', $options) && $options['wpip_wrapper'] === 'inline';
            /**
             * Filter the flag indicating whether to wrap the inserted content in inline tags (span).
             *
             * @param bool $use_inline_wrapper Indicates whether to wrap the content in span tags.
             */
            $attributes['inline'] = apply_filters('insert_pages_use_inline_wrapper', $attributes['inline']);
            $attributes['wrapper_tag'] = $attributes['inline'] ? 'span' : 'div';
            $attributes['should_apply_the_content_filter'] = true;
            /**
             * Filter the flag indicating whether to apply the_content filter to post
             * contents and excerpts that are being inserted.
             *
             * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
             */
            $attributes['should_apply_the_content_filter'] = apply_filters('insert_pages_apply_the_content_filter', $attributes['should_apply_the_content_filter']);
            // Disable the_content filter if using inline tags, since wpautop
            // inserts p tags and we can't have any inside inline elements.
            if ($attributes['inline']) {
                $attributes['should_apply_the_content_filter'] = false;
            }
            $attributes['should_apply_nesting_check'] = true;
            /**
             * Filter the flag indicating whether to apply deep nesting check
             * that can prevent circular loops. Note that some use cases rely
             * on inserting pages that themselves have inserted pages, so this
             * check should be disabled for those individuals.
             *
             * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
             */
            $attributes['should_apply_nesting_check'] = apply_filters('insert_pages_apply_nesting_check', $attributes['should_apply_nesting_check']);
            // Don't allow inserted pages to be added to the_content more than once (prevent infinite loops).
            if ($attributes['should_apply_nesting_check']) {
                $done = false;
                foreach ($wp_current_filter as $filter) {
                    if ('the_content' == $filter) {
                        if ($done) {
                            return $content;
                        } else {
                            $done = true;
                        }
                    }
                }
            }
            // Get the WP_Post object from the provided slug or ID.
            if (!is_numeric($attributes['page'])) {
                // Get list of post types that can be inserted (page, post, custom
                // types), excluding builtin types (nav_menu_item, attachment).
                $insertable_post_types = array_filter(get_post_types(), create_function('$type', 'return ! in_array( $type, array( "nav_menu_item", "attachment" ) );'));
                $inserted_page = get_page_by_path($attributes['page'], OBJECT, $insertable_post_types);
                $attributes['page'] = $inserted_page ? $inserted_page->ID : $attributes['page'];
            } else {
                $inserted_page = get_post(intval($attributes['page']));
            }
            // Use "Normal" insert method (get_post()).
            if ($options['wpip_insert_method'] !== 'legacy') {
                // If we couldn't retrieve the page, fire the filter hook showing a not-found message.
                if ($inserted_page === null) {
                    /**
                     * Filter the html that should be displayed if an inserted page was not found.
                     *
                     * @param string $content html to be displayed. Defaults to an empty string.
                     */
                    $content = apply_filters('insert_pages_not_found_message', $content);
                    // Short-circuit since we didn't find the page.
                    return $content;
                }
                // Start output buffering so we can save the output to a string.
                ob_start();
                // If Beaver Builder plugin is enabled, load any cached styles associated with the inserted page.
                // Note: Temporarily set the global $post->ID to the inserted page ID,
                // since Beaver Builder relies on it to load the appropriate styles.
                if (class_exists('FLBuilder')) {
                    // If we're not in The Loop (i.e., global $post isn't assigned),
                    // temporarily populate it with the post to be inserted so we can
                    // retrieve Beaver Builder styles for that post. Reset $post to null
                    // after we're done.
                    if (is_null($post)) {
                        $old_post_id = null;
                        $post = $inserted_page;
                    } else {
                        $old_post_id = $post->ID;
                        $post->ID = $inserted_page->ID;
                    }
                    FLBuilder::enqueue_layout_styles_scripts($inserted_page->ID);
                    if (is_null($old_post_id)) {
                        $post = null;
                    } else {
                        $post->ID = $old_post_id;
                    }
                }
                // Show either the title, link, content, everything, or everything via a custom template
                // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
                // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
                // are only getting called once. The fix here is to disable processing of filters on the_content in
                // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
                switch ($attributes['display']) {
                    case "title":
                        $title_tag = $attributes['inline'] ? 'span' : 'h1';
                        echo "<{$title_tag} class='insert-page-title'>";
                        echo get_the_title($inserted_page->ID);
                        echo "</{$title_tag}>";
                        break;
                    case "link":
                        ?>
<a href="<?php 
                        echo esc_url(get_permalink($inserted_page->ID));
                        ?>
"><?php 
                        echo get_the_title($inserted_page->ID);
                        ?>
</a><?php 
                        break;
                    case "excerpt":
                        ?>
<h1><a href="<?php 
                        echo esc_url(get_permalink($inserted_page->ID));
                        ?>
"><?php 
                        echo get_the_title($inserted_page->ID);
                        ?>
</a></h1><?php 
                        echo $this->insertPages_trim_excerpt(get_post_field('post_excerpt', $inserted_page->ID), $inserted_page->ID, $attributes['should_apply_the_content_filter']);
                        break;
                    case "excerpt-only":
                        echo $this->insertPages_trim_excerpt(get_post_field('post_excerpt', $inserted_page->ID), $inserted_page->ID, $attributes['should_apply_the_content_filter']);
                        break;
                    case "content":
                        $content = get_post_field('post_content', $inserted_page->ID);
                        if ($attributes['should_apply_the_content_filter']) {
                            $content = apply_filters('the_content', $content);
                        }
                        echo $content;
                        break;
                    case "all":
                        // Title.
                        $title_tag = $attributes['inline'] ? 'span' : 'h1';
                        echo "<{$title_tag} class='insert-page-title'>";
                        echo get_the_title($inserted_page->ID);
                        echo "</{$title_tag}>";
                        // Content.
                        $content = get_post_field('post_content', $inserted_page->ID);
                        if ($attributes['should_apply_the_content_filter']) {
                            $content = apply_filters('the_content', $content);
                        }
                        echo $content;
                        // Meta.
                        // @ref https://core.trac.wordpress.org/browser/tags/4.4/src/wp-includes/post-template.php#L968
                        if ($keys = get_post_custom_keys($inserted_page->ID)) {
                            echo "<ul class='post-meta'>\n";
                            foreach ((array) $keys as $key) {
                                $keyt = trim($key);
                                if (is_protected_meta($keyt, 'post')) {
                                    continue;
                                }
                                $values = array_map('trim', get_post_custom_values($key));
                                $value = implode($values, ', ');
                                /**
                                 * Filter the HTML output of the li element in the post custom fields list.
                                 *
                                 * @since 2.2.0
                                 *
                                 * @param string $html  The HTML output for the li element.
                                 * @param string $key   Meta key.
                                 * @param string $value Meta value.
                                 */
                                echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>{$key}:</span> {$value}</li>\n", $key, $value);
                            }
                            echo "</ul>\n";
                        }
                        break;
                    default:
                        // display is either invalid, or contains a template file to use
                        // Legacy/compatibility code: In order to use custom templates,
                        // we use query_posts() to provide the template with the global
                        // state it requires for the inserted page (in other words, all
                        // template tags will work with respect to the inserted page
                        // instead of the parent page / main loop). Note that this may
                        // cause some compatibility issues with other plugins.
                        // @ref https://codex.wordpress.org/Function_Reference/query_posts
                        if (is_numeric($attributes['page'])) {
                            $args = array('p' => intval($attributes['page']), 'post_type' => get_post_types());
                        } else {
                            $args = array('name' => esc_attr($attributes['page']), 'post_type' => get_post_types());
                        }
                        $inserted_page = query_posts($args);
                        if (have_posts()) {
                            $template = locate_template($attributes['display']);
                            if (strlen($template) > 0) {
                                include $template;
                                // execute the template code
                            } else {
                                // Couldn't find template, so fall back to printing a link to the page.
                                the_post();
                                ?>
<a href="<?php 
                                the_permalink();
                                ?>
"><?php 
                                the_title();
                                ?>
</a><?php 
                            }
                        }
                        wp_reset_query();
                }
                // Save output buffer contents.
                $content = ob_get_clean();
                // Use "Legacy" insert method (query_posts()).
            } else {
                // Construct query_posts arguments.
                if (is_numeric($attributes['page'])) {
                    $args = array('p' => intval($attributes['page']), 'post_type' => get_post_types());
                } else {
                    $args = array('name' => esc_attr($attributes['page']), 'post_type' => get_post_types());
                }
                $posts = query_posts($args);
                if (have_posts()) {
                    // Start output buffering so we can save the output to string
                    ob_start();
                    // If Beaver Builder plugin is enabled, load any cached styles associated with the inserted page.
                    // Note: Temporarily set the global $post->ID to the inserted page ID,
                    // since Beaver Builder relies on it to load the appropriate styles.
                    if (class_exists('FLBuilder')) {
                        // If we're not in The Loop (i.e., global $post isn't assigned),
                        // temporarily populate it with the post to be inserted so we can
                        // retrieve Beaver Builder styles for that post. Reset $post to null
                        // after we're done.
                        if (is_null($post)) {
                            $old_post_id = null;
                            $post = $inserted_page;
                        } else {
                            $old_post_id = $post->ID;
                            $post->ID = $inserted_page->ID;
                        }
                        FLBuilder::enqueue_layout_styles_scripts($inserted_page->ID);
                        if (is_null($old_post_id)) {
                            $post = null;
                        } else {
                            $post->ID = $old_post_id;
                        }
                    }
                    // Show either the title, link, content, everything, or everything via a custom template
                    // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
                    // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
                    // are only getting called once. The fix here is to disable processing of filters on the_content in
                    // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
                    switch ($attributes['display']) {
                        case "title":
                            the_post();
                            $title_tag = $attributes['inline'] ? 'span' : 'h1';
                            echo "<{$title_tag} class='insert-page-title'>";
                            the_title();
                            echo "</{$title_tag}>";
                            break;
                        case "link":
                            the_post();
                            ?>
<a href="<?php 
                            the_permalink();
                            ?>
"><?php 
                            the_title();
                            ?>
</a><?php 
                            break;
                        case "excerpt":
                            the_post();
                            ?>
<h1><a href="<?php 
                            the_permalink();
                            ?>
"><?php 
                            the_title();
                            ?>
</a></h1><?php 
                            if ($attributes['should_apply_the_content_filter']) {
                                the_excerpt();
                            } else {
                                echo get_the_excerpt();
                            }
                            break;
                        case "excerpt-only":
                            the_post();
                            if ($attributes['should_apply_the_content_filter']) {
                                the_excerpt();
                            } else {
                                echo get_the_excerpt();
                            }
                            break;
                        case "content":
                            the_post();
                            if ($attributes['should_apply_the_content_filter']) {
                                the_content();
                            } else {
                                echo get_the_content();
                            }
                            break;
                        case "all":
                            the_post();
                            $title_tag = $attributes['inline'] ? 'span' : 'h1';
                            echo "<{$title_tag} class='insert-page-title'>";
                            the_title();
                            echo "</{$title_tag}>";
                            if ($attributes['should_apply_the_content_filter']) {
                                the_content();
                            } else {
                                echo get_the_content();
                            }
                            the_meta();
                            break;
                        default:
                            // display is either invalid, or contains a template file to use
                            $template = locate_template($attributes['display']);
                            if (strlen($template) > 0) {
                                include $template;
                                // execute the template code
                            } else {
                                // Couldn't find template, so fall back to printing a link to the page.
                                the_post();
                                ?>
<a href="<?php 
                                the_permalink();
                                ?>
"><?php 
                                the_title();
                                ?>
</a><?php 
                            }
                            break;
                    }
                    // Save output buffer contents.
                    $content = ob_get_clean();
                } else {
                    /**
                     * Filter the html that should be displayed if an inserted page was not found.
                     *
                     * @param string $content html to be displayed. Defaults to an empty string.
                     */
                    $content = apply_filters('insert_pages_not_found_message', $content);
                }
                wp_reset_query();
            }
            /**
             * Filter the markup generated for the inserted page.
             *
             * @param string $content The post content of the inserted page.
             * @param object $inserted_page The post object returned from querying the inserted page.
             * @param array $attributes Extra parameters modifying the inserted page.
             *   page: Page ID or slug of page to be inserted.
             *   display: Content to display from inserted page.
             *   class: Extra classes to add to inserted page wrapper element.
             *   inline: Boolean indicating wrapper element should be a span.
             *   should_apply_nesting_check: Whether to disable nested inserted pages.
             *   should_apply_the_content_filter: Whether to apply the_content filter to post contents and excerpts.
             *   wrapper_tag: Tag to use for the wrapper element (e.g., div, span).
             */
            $content = apply_filters('insert_pages_wrap_content', $content, $inserted_page, $attributes);
            return $content;
        }
Esempio n. 3
0
        function insertPages_handleShortcode_insert($atts, $content = null)
        {
            global $wp_query, $post, $wp_current_filter;
            extract(shortcode_atts(array('page' => '0', 'display' => 'all', 'class' => '', 'inline' => false), $atts));
            // Get options set in WordPress dashboard (Settings > Insert Pages).
            $options = get_option('wpip_settings');
            if ($options === FALSE) {
                $options = wpip_set_defaults();
            }
            // Validation checks.
            if ($page === '0') {
                return $content;
            }
            // Trying to embed same page in itself.
            if ($page == $post->ID || $page == $post->post_name) {
                return $content;
            }
            $should_apply_nesting_check = true;
            /**
             * Filter the flag indicating whether to apply deep nesting check
             * that can prevent circular loops. Note that some use cases rely
             * on inserting pages that themselves have inserted pages, so this
             * check should be disabled for those individuals.
             *
             * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
             */
            $should_apply_nesting_check = apply_filters('insert_pages_apply_nesting_check', $should_apply_nesting_check);
            // Don't allow inserted pages to be added to the_content more than once (prevent infinite loops).
            if ($should_apply_nesting_check) {
                $done = false;
                foreach ($wp_current_filter as $filter) {
                    if ('the_content' == $filter) {
                        if ($done) {
                            return $content;
                        } else {
                            $done = true;
                        }
                    }
                }
            }
            // Convert slugs to page IDs to standardize query_posts() lookup below.
            if (!is_numeric($page)) {
                $page_object = get_page_by_path($page, OBJECT, get_post_types());
                $page = $page_object ? $page_object->ID : $page;
            }
            if (is_numeric($page)) {
                $args = array('p' => intval($page), 'post_type' => get_post_types());
            } else {
                $args = array('name' => esc_attr($page), 'post_type' => get_post_types());
            }
            query_posts($args);
            $should_apply_the_content_filter = true;
            /**
             * Filter the flag indicating whether to apply the_content filter to post
             * contents and excerpts that are being inserted.
             *
             * @param bool $apply_the_content_filter Indicates whether to apply the_content filter.
             */
            $should_apply_the_content_filter = apply_filters('insert_pages_apply_the_content_filter', $should_apply_the_content_filter);
            $should_use_inline_wrapper = $inline !== false && $inline !== 'false' || array_search('inline', $atts) === 0 || array_key_exists('wpip_wrapper', $options) && $options['wpip_wrapper'] === 'inline';
            /**
             * Filter the flag indicating whether to wrap the inserted content in inline tags (span).
             *
             * @param bool $use_inline_wrapper Indicates whether to wrap the content in span tags.
             */
            $should_use_inline_wrapper = apply_filters('insert_pages_use_inline_wrapper', $should_use_inline_wrapper);
            // Disable the_content filter if using inline tags, since wpautop
            // inserts p tags and we can't have any inside inline elements.
            if ($should_use_inline_wrapper) {
                $should_apply_the_content_filter = false;
            }
            // Start our new Loop (only iterate once).
            if (have_posts()) {
                ob_start();
                // Start output buffering so we can save the output to string
                // If Beaver Builder plugin is enabled, load any cached styles associated with the inserted page.
                // Note: Temporarily set the global $post->ID to the inserted page ID,
                // since Beaver Builder relies on it to load the appropraite styles.
                if (class_exists('FLBuilder')) {
                    $old_post_id = $post->ID;
                    $post->ID = $page;
                    FLBuilder::enqueue_layout_styles_scripts($page);
                    $post->ID = $old_post_id;
                }
                // Show either the title, link, content, everything, or everything via a custom template
                // Note: if the sharing_display filter exists, it means Jetpack is installed and Sharing is enabled;
                // This plugin conflicts with Sharing, because Sharing assumes the_content and the_excerpt filters
                // are only getting called once. The fix here is to disable processing of filters on the_content in
                // the inserted page. @see https://codex.wordpress.org/Function_Reference/the_content#Alternative_Usage
                switch ($display) {
                    case "title":
                        the_post();
                        $title_tag = $should_use_inline_wrapper ? 'span' : 'h1';
                        echo "<{$title_tag} class='insert-page-title'>";
                        the_title();
                        echo "</{$title_tag}>";
                        break;
                    case "link":
                        the_post();
                        ?>
<a href="<?php 
                        the_permalink();
                        ?>
"><?php 
                        the_title();
                        ?>
</a><?php 
                        break;
                    case "excerpt":
                        the_post();
                        ?>
<h1><a href="<?php 
                        the_permalink();
                        ?>
"><?php 
                        the_title();
                        ?>
</a></h1><?php 
                        if ($should_apply_the_content_filter) {
                            the_excerpt();
                        } else {
                            echo get_the_excerpt();
                        }
                        break;
                    case "excerpt-only":
                        the_post();
                        if ($should_apply_the_content_filter) {
                            the_excerpt();
                        } else {
                            echo get_the_excerpt();
                        }
                        break;
                    case "content":
                        the_post();
                        if ($should_apply_the_content_filter) {
                            the_content();
                        } else {
                            echo get_the_content();
                        }
                        break;
                    case "all":
                        the_post();
                        $title_tag = $should_use_inline_wrapper ? 'span' : 'h1';
                        echo "<{$title_tag} class='insert-page-title'>";
                        the_title();
                        echo "</{$title_tag}>";
                        if ($should_apply_the_content_filter) {
                            the_content();
                        } else {
                            echo get_the_content();
                        }
                        the_meta();
                        break;
                    default:
                        // display is either invalid, or contains a template file to use
                        $template = locate_template($display);
                        if (strlen($template) > 0) {
                            include $template;
                            // execute the template code
                        } else {
                            // Couldn't find template, so fall back to printing a link to the page.
                            the_post();
                            ?>
<a href="<?php 
                            the_permalink();
                            ?>
"><?php 
                            the_title();
                            ?>
</a><?php 
                        }
                        break;
                }
                $content = ob_get_contents();
                // Save off output buffer
                ob_end_clean();
                // End output buffering
            } else {
                /**
                 * Filter the html that should be displayed if an inserted page was not found.
                 *
                 * @param string $content html to be displayed. Defaults to an empty string.
                 */
                $content = apply_filters('insert_pages_not_found_message', $content);
            }
            wp_reset_query();
            $wrapper_tag = $should_use_inline_wrapper ? 'span' : 'div';
            $content = "<{$wrapper_tag} data-post-id='{$page}' class='insert-page insert-page-{$page} {$class}'>{$content}</{$wrapper_tag}>";
            return $content;
            //return do_shortcode($content); // careful: watch for infinite loops with nested inserts
        }
function wpip_insert_method_render()
{
    $options = get_option('wpip_settings');
    if ($options === FALSE || !is_array($options) || !array_key_exists('wpip_insert_method', $options)) {
        $options = wpip_set_defaults();
    }
    ?>
	<input type='radio' name='wpip_settings[wpip_insert_method]' <?php 
    checked($options['wpip_insert_method'], 'legacy');
    ?>
 id="wpip_insert_method_legacy" value='legacy'><label for="wpip_insert_method_legacy">Use legacy method (compatible with <a href="https://wordpress.org/plugins/beaver-builder-lite-version/" target="_blank">Beaver Builder</a> and <a href="https://wordpress.org/plugins/siteorigin-panels/" target="_blank">Page Builder by SiteOrigin</a>, but less efficient). </label><br />
	<input type='radio' name='wpip_settings[wpip_insert_method]' <?php 
    checked($options['wpip_insert_method'], 'normal');
    ?>
 id="wpip_insert_method_normal" value='normal'><label for="wpip_insert_method_normal">Use normal method (more compatible with other plugins, and more efficient).</label><br />
	<small><em>The legacy method uses <a href="https://codex.wordpress.org/Function_Reference/query_posts" target="_blank">query_posts()</a>, which the Codex cautions against using. However, to recreate the exact state that many page builder plugins are expecting, the Main Loop has to be replaced with the inserted page while it is being rendered. The normal method, on the other hand, just uses <a href="https://developer.wordpress.org/reference/functions/get_post/" target="_blank">get_post()</a>.</em></small>
	<?php 
}