Skip to content

Instantly share code, notes, and snippets.

@johnkraczek
Created August 3, 2020 16:12
Show Gist options
  • Save johnkraczek/730b06e6044f97701b372a8f787ef52c to your computer and use it in GitHub Desktop.
Save johnkraczek/730b06e6044f97701b372a8f787ef52c to your computer and use it in GitHub Desktop.
<?php
/**
* Child Theme or plugin
* @since 1.0.0
*/
// include the post not found class.
require get_stylesheet_directory() . '/post_not_found_content.php';
// ... other function code ...
Copyright (c) 2020 John Kraczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
<?php
/**
* Post Not Found Content By FunnelPress
*
* @link https://funnelpress.com/elementor-post-not-found-content/
*
* @since 1.0.0
*
* @license MIT
*
*/
namespace Elementor;
if (!defined('ABSPATH')) {
exit;
}
/**
* Class Post_Not_Found_Content
*/
class Post_Not_Found_Content
{
private static $instance = null;
private static $section_templates = null;
private static $page_templates = null;
private static $widget_templates = null;
public static function get_instance()
{
if (!self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
public function init()
{
if (!defined('ELEMENTOR_PATH')) {
return;
}
add_action('elementor/element/posts/section_query/before_section_end', [$this, 'register_controls'], 10, 2);
add_action('elementor/query/query_results', [$this, 'render_not_found'], 10, 2);
}
public function register_controls($element, $args)
{
$element->add_control(
'hr',
[
'type' => \Elementor\Controls_Manager::DIVIDER,
]
);
$element->add_control(
'enable_post_not_found', [
'label' => __('Show Not Found Content', 'funnelpress'),
'type' => Controls_Manager::SWITCHER,
'default' => '',
'label_on' => __('Yes', 'funnelpress'),
'label_off' => __('No', 'funnelpress'),
'return_value' => 'yes',
]
);
$element->add_control(
'type_of_content',
[
'label' => __('Type Of Content', 'funnelpress'),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => false,
'options' => [
'saved_section' => __('Saved Section', 'funnelpress'),
'simple_text' => __('Simpe Text', 'funnelpress'),
],
'default' => 'saved_section',
'condition' => [
'enable_post_not_found' => 'yes',
],
]
);
// Display Saved Section
$element->add_control(
'post_action_display_saved_section',
[
'type' => Controls_Manager::SELECT2,
'label' => __('Saved Section:', 'funnelpress'),
'options' => $this::get_saved_data('section'),
'default' => 'Select',
'multiple' => false,
'label_block' => true,
'condition' => [
'enable_post_not_found' => 'yes',
'type_of_content' => 'saved_section',
],
]
);
$element->add_control(
'post_action_display_simple_text',
[
'label' => __( 'Text to display', 'funnelpress' ),
'type' => \Elementor\Controls_Manager::WYSIWYG,
'rows' => 10,
'default' => __( 'No Posts Found', 'funnelpress' ),
'placeholder' => __( 'Type your description here', 'funnelpress' ),
'condition' => [
'enable_post_not_found' => 'yes',
'type_of_content' => 'simple_text',
],
]
);
$element->add_control(
'post_action_simple_text_alignment',
[
'label' => __( 'Alignment', 'funnelpress' ),
'type' => \Elementor\Controls_Manager::CHOOSE,
'options' => [
'left' => [
'title' => __( 'Left', 'funnelpress' ),
'icon' => 'fa fa-align-left',
],
'center' => [
'title' => __( 'Center', 'funnelpress' ),
'icon' => 'fa fa-align-center',
],
'right' => [
'title' => __( 'Right', 'funnelpress' ),
'icon' => 'fa fa-align-right',
],
],
'default' => 'center',
'toggle' => true,
'condition' => [
'enable_post_not_found' => 'yes',
'type_of_content' => 'simple_text',
],
]
);
}
private static function get_saved_data($type = 'page')
{
$template_type = $type . '_templates';
$templates_list = array();
if ((null === self::$page_templates && 'page' === $type) || (null === self::$section_templates && 'section' === $type) || (null === self::$widget_templates && 'widget' === $type)) {
$posts = get_posts(
array(
'post_type' => 'elementor_library',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'elementor_library_type',
'field' => 'slug',
'terms' => $type,
),
),
)
);
foreach ($posts as $post) {
$templates_list[] = array(
'id' => $post->ID,
'name' => $post->post_title,
);
}
self::${$template_type}[-1] = __('Select', 'uael');
if (count($templates_list)) {
foreach ($templates_list as $saved_row) {
$content_id = $saved_row['id'];
$content_id = apply_filters('wpml_object_id', $content_id);
self::${$template_type}[$content_id] = $saved_row['name'];
}
} else {
self::${$template_type}['no_template'] = __('It seems that, you have not saved any of those templates yet.', 'uael');
}
}
return self::${$template_type};
}
public function render_not_found($query, $widget)
{
$total = $query->found_posts;
if ($total == 0) {
$settings = $widget->get_settings();
if (!empty($settings['enable_post_not_found']) && $settings['enable_post_not_found'] == 'yes') {
if (!empty($settings['type_of_content'])) {
if ($settings['type_of_content'] == 'saved_section') {
$section = $settings['post_action_display_saved_section'];
$content = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display(apply_filters('wpml_object_id', $section, 'page'));
echo $content;
}
if ($settings['type_of_content'] == 'simple_text') {
$text = $settings['post_action_display_simple_text'];
$align = $settings['post_action_simple_text_alignment'];
echo '<div style="text-align: '.$align.'">
<p>'.$text.'</p>
</div>';
}
}
}
}
}
}
Post_Not_Found_Content::get_instance()->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment