public function checkBanners() { $arguments = array("post_type" => "custom_notice", "posts_per_page" => -1, "status" => "publish"); $arguments = apply_filters("can_banner_arguments", $arguments); $options = Settings\getFieldValues(true, "default"); $posts = get_posts($arguments); $current_user = get_current_user_id(); $script = <<<EOT <script> // We might as well just use \$.post (function(\$){ \$(document).on("click", ".notice-dismiss", function(e){ \$.post(ajaxurl, { action: "notice_dismissed", user_id: {$current_user}, notice_id: \$(e.target).parent().data("noticeid") }).done(function(response){ // console.log(response); }); }); })(jQuery); </script> EOT; echo $script; foreach ($posts as $post) { setup_postdata($post); $show_banner = true; $header = "<h2>" . apply_filters("the_title", $post->post_title) . "</h2>"; $header = apply_filters("can_notice_title", $header, $post->ID); $text = apply_filters("the_content", $post->post_content); $text = apply_filters("can_notice_content", $text, $post->ID); $content = $header . $text; $is_dismissible = (int) get_post_meta($post->ID, "can_dismissible", true); $is_dismissible = apply_filters("can_is_dismissible", $is_dismissible, $post->ID); // Allow filtering the dismissible status. Basically allows to always enable dismissing or disable it, or per post. $type = get_post_meta($post->ID, "can_type", true); $user_dismissed = get_user_meta(get_current_user_id(), "dismissed_notices", true); $user_dismissed = empty($user_dismissed) ? array() : $user_dismissed; $user_dismissed = in_array($post->ID, $user_dismissed); $env = get_post_meta($post->ID, "can_environment", true); if ($options["determine-environment"] && $options["allow-environments"]) { if (!is_array($env)) { error_log("Custom Admin Notices: Did you change the environment settings? Invalid value set for environment when rendering banner."); $env = array(); } if (!in_array(getenv("WP_ENV"), $env)) { $show_banner = false; } } elseif (isset($options["allow-environments"])) { $pageurl = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; $env = !empty($env) ? $env : 'not-empty-needle'; // FFS if your URL actually contains "not-empty-needle". if (!(strpos($pageurl, $env) > -1)) { $show_banner = false; } } if (!$is_dismissible) { $user_dismissed = false; // If the notice isn't dismissible, show it for all users even if they dismissed it. } if ($show_banner && !$user_dismissed) { $this->renderBanner($type, $content, $is_dismissible, $post->ID); } } }
function renderSettingsPage() { $settings = Settings\getSettings(); $settingsValues = WP_DEBUG ? Settings\getFieldValues(true) : null; include Plugin\HOME_DIR . DIRECTORY_SEPARATOR . "templates" . DIRECTORY_SEPARATOR . "plugin-settings.php"; }