<commit_before><?php

/**
 * Abstract base classes and utility classes for controllers in the
 * wp-admin area.
 */

namespace EPFL\AdminController;


if (! defined('ABSPATH')) {
    die('Access denied.');
}

/**
 * Manage pop-up errors or notices at the top of admin pages
 *
 * Use WordPress transients to persist the errors, so that they can
 * be shown after the browser navigates to another page. Errors are
 * per-user and per-page (as materialized by the $slug argument
 * to @link publish and subscribe)
 */

class TransientError
{
    /**
     * Persist $error to disk to be fetched and displayed soon by
     * @link subscribe.
     *
     * @param $slug The "channel" for errors, to distinguish among
     *              several error contexts (e.g. per post, or per page).
     *              Note that errors are further isolated per user,
     *              i.e. a logged-in user won't see someone else's errors
     *              even if they are published with the same $slug.
     */
    function publish ($slug, $error) {
        $transient_key = static::_privatify_slug($slug);
        $errors = get_transient($transient_key);
        if (! $errors) {
            $errors = array();
        } elseif (! is_array($errors)) {
            $errors = array($errors);
        }

        $errors[] = $error;
        set_transient($transient_key, $errors,
                      45);  // Seconds before it self-destructs
    }

    /**
     * Add slag to $slug so that our transients are private per user,
     * and don't collide with other Wordpress transients.
     */
    function _privatify_slug ($slug) {
        $me = get_current_user_id();
        return "epfl:TransientError:$me:$slug";
    }

    /**
     * Show any and all errors stashed with @link publish.
     *
     * Will only render errors from the same logged-in user,
     * *and* having the same $slug set at @link publish time.
     *
     * @param $slug A unique string to classify errors across multiple
     *              channels (e.g. admin pages); must match the $slug
     *              parameter passed to @link publish to recover these
     *              errors and them only.
     *
     * @param $render_callback The render function to pass the errors to.
     *                         Defaults to @link render
     */
    function subscribe ($slug, $render_callback = NULL) {
        $thisclass = get_called_class();
        if (! $render_callback) {
            $render_callback = array($thisclass, 'render');
        }
        if (doing_action('admin_notices')) {
            $thisclass::_do_fetch_and_render($slug, $render_callback);
        } else {
            add_action(
                'admin_notices',
                array(get_called_class(), 
                      function() use ($thisclass, $slug, $render_callback) {
                          $thisclass::_do_fetch_and_render(
                              $slug, $render_callback);
                      }));
        }
    }

    private function _do_fetch_and_render ($slug, $render_callback) {
        $transient_key = static::_privatify_slug($slug);
        if ($errors = get_transient($transient_key)) {
            foreach ($errors as $error) {
                call_user_func($render_callback, $error);
            }
            delete_transient($transient_key);
        }
    }

    /**
     * The default renderer for admin notices managed by this class
     *
     * @param $error Either a string, or an array with keys 'level'
     * and 'msg'.
     */
    function render ($error) {
        if (! is_array($error)) {
            $error = array(
                'level' => 'error',
                'msg' => $error
            );
        }
            ?>
    <div class="notice notice-<?php echo $error['level'] ?> is-dismissible">
        <p><?php echo $error['msg']; ?></p>
    </div><?php
    }
}

/**
 * Abstract base class for all controller classes
 */
abstract class Controller
{
    /**
     * Arrange for a nonfatal error to be shown in a so-called "admin
     * notice."
     *
     * This notice will be put up on any page for which the @link
     * is_active method returns true.
     */
    static protected function admin_notice ($msg, $level = 'notice') {
        TransientError::publish(
            static::_get_transient_errors_slug(), array(
                'level' => $level,
                'msg' => $msg));
    }<commit_msg>Put "static" everywhere in class TransientError<commit_after><?php

/**
 * Abstract base classes and utility classes for controllers in the
 * wp-admin area.
 */

namespace EPFL\AdminController;


if (! defined('ABSPATH')) {
    die('Access denied.');
}

/**
 * Manage pop-up errors or notices at the top of admin pages
 *
 * Use WordPress transients to persist the errors, so that they can
 * be shown after the browser navigates to another page. Errors are
 * per-user and per-page (as materialized by the $slug argument
 * to @link publish and subscribe)
 *
 * This is a "pure static" class; no instances are ever constructed.
 */

class TransientError
{
    /**
     * Persist $error to disk to be fetched and displayed soon by
     * @link subscribe.
     *
     * @param $slug The "channel" for errors, to distinguish among
     *              several error contexts (e.g. per post, or per page).
     *              Note that errors are further isolated per user,
     *              i.e. a logged-in user won't see someone else's errors
     *              even if they are published with the same $slug.
     */
    static function publish ($slug, $error) {
        $transient_key = static::_privatify_slug($slug);
        $errors = get_transient($transient_key);
        if (! $errors) {
            $errors = array();
        } elseif (! is_array($errors)) {
            $errors = array($errors);
        }

        $errors[] = $error;
        set_transient($transient_key, $errors,
                      45);  // Seconds before it self-destructs
    }

    /**
     * Add slag to $slug so that our transients are private per user,
     * and don't collide with other Wordpress transients.
     */
    static function _privatify_slug ($slug) {
        $me = get_current_user_id();
        return "epfl:TransientError:$me:$slug";
    }

    /**
     * Show any and all errors stashed with @link publish.
     *
     * Will only render errors from the same logged-in user,
     * *and* having the same $slug set at @link publish time.
     *
     * @param $slug A unique string to classify errors across multiple
     *              channels (e.g. admin pages); must match the $slug
     *              parameter passed to @link publish to recover these
     *              errors and them only.
     *
     * @param $render_callback The render function to pass the errors to.
     *                         Defaults to @link render
     */
    static function subscribe ($slug, $render_callback = NULL) {
        $thisclass = get_called_class();
        if (! $render_callback) {
            $render_callback = array($thisclass, 'render');
        }
        if (doing_action('admin_notices')) {
            $thisclass::_do_fetch_and_render($slug, $render_callback);
        } else {
            add_action(
                'admin_notices',
                array(get_called_class(), 
                      function() use ($thisclass, $slug, $render_callback) {
                          $thisclass::_do_fetch_and_render(
                              $slug, $render_callback);
                      }));
        }
    }

    private static function _do_fetch_and_render ($slug, $render_callback) {
        $transient_key = static::_privatify_slug($slug);
        if ($errors = get_transient($transient_key)) {
            foreach ($errors as $error) {
                call_user_func($render_callback, $error);
            }
            delete_transient($transient_key);
        }
    }

    /**
     * The default renderer for admin notices managed by this class
     *
     * @param $error Either a string, or an array with keys 'level'
     * and 'msg'.
     */
    static function render ($error) {
        if (! is_array($error)) {
            $error = array(
                'level' => 'error',
                'msg' => $error
            );
        }
            ?>
    <div class="notice notice-<?php echo $error['level'] ?> is-dismissible">
        <p><?php echo $error['msg']; ?></p>
    </div><?php
    }
}

/**
 * Abstract base class for all controller classes
 */
abstract class Controller
{
    /**
     * Arrange for a nonfatal error to be shown in a so-called "admin
     * notice."
     *
     * This notice will be put up on any page for which the @link
     * is_active method returns true.
     */
    static protected function admin_notice ($msg, $level = 'notice') {
        TransientError::publish(
            static::_get_transient_errors_slug(), array(
                'level' => $level,
                'msg' => $msg));
    }
{'fraction_non_alphanumeric': 0.1073616215614211, 'fraction_numerical': 0.00044548390689386344, 'mean_word_length': 1.939443535188216, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5997188', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2476, 'n_words': 805}