Skip to content

Commit

Permalink
Add a setting to prevent the View from rendering in the frontend (#17)
Browse files Browse the repository at this point in the history
This prevents it from being included via a shortcode.
  • Loading branch information
mrcasual committed May 13, 2024
1 parent 7651b70 commit d5308d1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function __construct() {
add_filter( 'gravityview_page_links_args', [ $this, 'rewrite_pagination_links' ] );

add_filter( 'gravityview/view/get', [ $this, 'modify_view' ] );
add_filter( 'pre_do_shortcode_tag', [ $this, 'prevent_gravityview_shortcode_output' ], 10, 3 );

new FoundationSettings();
new ViewSettings();
Expand Down Expand Up @@ -374,7 +375,7 @@ public function rewrite_pagination_links( array $params ) {
* @return mixed The updated View.
*/
public function modify_view( $view ) {
if ( ! $view->settings->get( 'dashboard_views_enable' ) ) {
if ( ! $view instanceof View || ! $view->settings->get( 'dashboard_views_enable' ) ) {
return $view;
}

Expand Down Expand Up @@ -417,6 +418,31 @@ public function modify_view( $view ) {
return $view;
}

/**
* Conditionally prevents the [gravityview] shortcode output.
*
* @since TBD
*
* @param string $output The shortcode output.
* @param string $tag The shortcode tag.
* @param array $attr The shortcode attributes array.
*
* @return string The shortcode output.
*/
public function prevent_gravityview_shortcode_output( $output, $tag, $attr ) {
if ( 'gravityview' !== $tag || empty( $attr['id'] ) ) {
return $output;
}

$view_settings = gravityview_get_template_settings( $attr['id'] );

if ( ! empty( $view_settings[ ViewSettings::SETTINGS_PREFIX . '_enable' ] ) && empty( $view_settings[ ViewSettings::SETTINGS_PREFIX . '_show_in_frontend' ] ) ) {
return '';
}

return $output;
}

/**
* Enqueues UI assets.
*
Expand Down
9 changes: 9 additions & 0 deletions src/ViewSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ public function add_settings_to_the_dashboards_view_tab( $settings ) {
'full_width' => true,
'value' => 0,
],
self::SETTINGS_PREFIX . '_show_in_frontend' => [
'label' => esc_html__( 'Show in Frontend', 'gk-gravityview-dashboard-views' ),
'desc' => esc_html__( 'Unchecking this option will prevent the View from rendering in the frontend.', 'gk-gravityview-dashboard-views' ),
'requires' => self::SETTINGS_PREFIX . '_enable',
'type' => 'checkbox',
'class' => 'widefat',
'full_width' => true,
'value' => 1,
],
self::SETTINGS_PREFIX . '_custom_name' => [
'label' => esc_html__( 'Custom View Name', 'gk-gravityview-dashboard-views' ),
'desc' => esc_html__( 'Use this field to specify the View name as it will appear in the Dashboard.', 'gk-gravityview-dashboard-views' ),
Expand Down

0 comments on commit d5308d1

Please sign in to comment.