r with shortcode * @version 2.5 * @param array $atts * @param string $content * @return string */ public function sidebar_shortcode($atts, $content = '') { $a = shortcode_atts([ 'id' => 0, 'conditional' => 0 ], $atts); $id = CAS_App::SIDEBAR_PREFIX . esc_attr($a['id']); if (!isset($this->sidebars[$id]) || $this->sidebars[$id]->post_status != CAS_App::STATUS_ACTIVE) { return $content; } if ($this->metadata()->get('handle')->get_data($a['id']) != CAS_App::ACTION_SHORTCODE) { return $content; } $display = true; if ($a['conditional']) { $sidebars = WPCACore::get_posts(CAS_App::TYPE_SIDEBAR); $display = !empty($sidebars) && isset($sidebars[$id]); } if (is_active_sidebar($id) && apply_filters('cas/shortcode/display', $display, $a['id'])) { ob_start(); do_action('cas/shortcode/before', $a['id']); $this->override_sidebar_styles($id); dynamic_sidebar($id); $content = ob_get_clean(); } return $content; } /** * Get styles from nested sidebars * * @since 3.7 * @param string $i * @return array */ public function get_sidebar_styles($i) { $styles = []; $metadata = $this->metadata()->get('html'); while ($i) { if (isset($this->sidebars[$i])) { $style = apply_filters('cas/sidebar/html', $metadata->get_data($this->sidebars[$i]->ID, true), $this->sidebars[$i]->ID); if ($style) { $styles = array_merge($styles, $style); $styles['widget_id'] = '%1$s'; $styles['sidebar_id'] = CAS_App::SIDEBAR_PREFIX . $this->sidebars[$i]->ID; } } $i = isset($this->replace_map[$i]) ? $this->replace_map[$i] : false; } return $styles; } /** * Render html if present before sidebar * * @deprecated since WP5.6 * @since 3.6 * @param string $i * @param boolean $has_widgets * @return void */ public function render_sidebar_before($i, $has_widgets) { //wp5.6 introduced before_sidebar without notice if (version_compare(get_bloginfo('version'), '5.6', '>=')) { return; } global $wp_registered_sidebars; if ($has_widgets && isset($wp_registered_sidebars[$i]['before_sidebar'])) { echo $wp_registered_sidebars[$i]['before_sidebar']; } } /** * Render html if present after sidebar * * @deprecated since WP5.6 * @since 3.6 * @param string $i * @param boolean $has_widgets * @return void */ public function render_sidebar_after($i, $has_widgets) { //wp5.6 introduced after_sidebar without notice if (version_compare(get_bloginfo('version'), '5.6', '>=')) { return; } global $wp_registered_sidebars; if ($has_widgets && isset($wp_registered_sidebars[$i]['after_sidebar'])) { echo $wp_registered_sidebars[$i]['after_sidebar']; } } /** * Filter out all sidebars if post is password protected * * @since 3.7 * @param array $sidebars * @return array */ public static function filter_password_protection($sidebars) { if (is_singular() && post_password_required()) { return []; } return $sidebars; } /** * Filter out sidebars based on current user * * @since 3.7 * @param array $sidebars * @return array */ public function filter_visibility($sidebars) { if ($sidebars) { $metadata = $this->metadata()->get('visibility'); //temporary filter until WPCACore allows filtering $user_visibility = is_user_logged_in() ? [-1] : []; $user_visibility = apply_filters('cas/user_visibility', $user_visibility); foreach ($sidebars as $id => $sidebar) { $visibility = $metadata->get_data($id, true, false); // Check visibility if ($visibility && !array_intersect($visibility, $user_visibility)) { unset($sidebars[$id]); } } } return $sidebars; } /** * Filter shortcode sidebar based on current user * * @since 3.7.1 * @param boolean $retval * @param int $id * @return boolean */ public function filter_shortcode_visibility($retval, $id) { if ($retval) { $metadata = $this->metadata()->get('visibility'); //temporary filter until WPCACore allows filtering $user_visibility = is_user_logged_in() ? [-1] : []; $user_visibility = apply_filters('cas/user_visibility', $user_visibility); $visibility = $metadata->get_data($id, true, false); // Check visibility if ($visibility && !array_intersect($visibility, $user_visibility)) { $retval = false; } } return $retval; } /** * Runs is_active_sidebar for sidebars * Widget management in Theme Customizer * expects this * * @global WP_Customize_Manager $wp_customize * @since 2.2 * @return void */ public function sidebar_notify_theme_customizer() { global $wp_customize; if (!empty($wp_customize)) { $sidebars = WPCACore::get_posts(CAS_App::TYPE_SIDEBAR); if ($sidebars) { foreach ($sidebars as $sidebar) { is_active_sidebar(CAS_App::SIDEBAR_PREFIX . $sidebar->ID); } } } } /** * Get sidebar edit link * TODO: Consider changing post type _edit_link instead * * @since 3.4 * @param string $link * @param int $post_id * @param string $context * @return string */ public function get_edit_post_link($link, $post_id, $context) { $post = get_post($post_id); if ($post->post_type == CAS_App::TYPE_SIDEBAR) { $sep = '&'; if ($context == 'display') { $sep = '&'; } $link = admin_url('admin.php?page=wpcas-edit' . $sep . 'sidebar_id=' . $post_id); //load page in all languages for wpml, polylang, //ensures post type conditions are not filtered if (defined('ICL_SITEPRESS_VERSION') || defined('POLYLANG_VERSION')) { $link .= $sep . 'lang=all'; } } return $link; } /** * Get sidebar delete link * TODO: Consider changing post type _edit_link instead * * @since 3.4 * @param string $link * @param int $post_id * @param boolean $force_delete * @return string */ public function get_delete_post_link($link, $post_id, $force_delete) { $post = get_post($post_id); if ($post->post_type == CAS_App::TYPE_SIDEBAR) { $action = ($force_delete || !EMPTY_TRASH_DAYS) ? 'delete' : 'trash'; $link = add_query_arg( 'action', $action, admin_url('admin.php?page=wpcas-edit&sidebar_id=' . $post_id) ); $link = wp_nonce_url($link, "$action-post_$post_id"); } return $link; } /** * @param string $layout * @return string */ public function generatepress_adjust_column_width($layout) { if ($layout === 'no-sidebar') { return 'no-sidebar'; } //only adjust layout if custom sidebars are empty (keep theme sidebar behavior intact) $replaced_sidebars = CAS_App::instance()->manager()->get_replacement_map(); $active_left = !isset($replaced_sidebars['sidebar-2']) || is_active_sidebar('sidebar-2'); $active_right = !isset($replaced_sidebars['sidebar-1']) || is_active_sidebar('sidebar-1'); if ($active_left && $active_right) { return $layout; } if ($layout === 'right-sidebar' && !$active_right) { return 'no-sidebar'; } if ($layout === 'left-sidebar' && !$active_left) { return 'no-sidebar'; } if (in_array($layout, ['both-sidebars', 'both-right', 'both-left'])) { if (!$active_left && !$active_right) { return 'no-sidebar'; } if ($active_left) { //todo if layout==both-right, we need to move left-sidebar widgets into right-sidebar return 'left-sidebar'; } if ($active_right) { //todo if layout==both-left, we need to move right-sidebar widgets into left-sidebar return 'right-sidebar'; } } return $layout; } }