Plugin Directory

Changeset 3335487


Ignore:
Timestamp:
07/28/2025 04:30:22 PM (8 months ago)
Author:
apos37
Message:

1.3.2

  • Update: Added support for additional roles to have capability of managing broken links
Location:
broken-link-notifier
Files:
54 added
4 edited

Legend:

Unmodified
Added
Removed
  • broken-link-notifier/trunk/broken-link-notifier.php

    r3331551 r3335487  
    44 * Plugin URI:          https://pluginrx.com/plugin/broken-link-notifier/
    55 * Description:         Get notified when someone loads a page with a broken link
    6  * Version:             1.3.1.1
     6 * Version:             1.3.2
    77 * Requires at least:   5.9
    88 * Tested up to:        6.8
     
    8383
    8484/**
     85 * Initialize the plugin loader
     86 */
     87$blnotifier_loader_instance = new BLNOTIFIER_LOADER();
     88
     89
     90/**
     91 * Register activation and deactivation hooks for role/capability management.
     92 * These hooks must be called in the main plugin file and target methods of your plugin instance.
     93 */
     94register_activation_hook( __FILE__, [ $blnotifier_loader_instance, 'plugin_activate' ] );
     95register_deactivation_hook( __FILE__, [ $blnotifier_loader_instance, 'plugin_deactivate' ] );
     96
     97
     98/**
    8599 * Delete the cache table on plugin deactivation or uninstall.
    86100 */
  • broken-link-notifier/trunk/includes/loader.php

    r3305248 r3335487  
    1111
    1212/**
    13  * Initialize the class
    14  */
    15 
    16 new BLNOTIFIER_LOADER();
    17 
    18 
    19 /**
    2013 * Main plugin class.
    2114 */
    2215class BLNOTIFIER_LOADER {
     16
     17    /**
     18     * Define your custom capability
     19     */
     20    public $capability = 'manage_broken_links';
     21
     22
     23    /**
     24     * Define your custom role slug
     25     */
     26    public $role_slug = 'blnotifier_link_manager';
     27
    2328
    2429    /**
     
    3237        }
    3338        $this->load_dependencies();
     39
     40        // Add custom capability to the admin
     41        add_action( 'admin_init', [ $this, 'add_custom_capability_to_admin' ] );
    3442       
    3543    } // End __construct()
     
    141149    } // End load_dependencies()
    142150
     151
     152    /**
     153     * Plugin activation hook callback
     154     */
     155    public function plugin_activate() {
     156        $admin_role = get_role( 'administrator' );
     157        if ( $admin_role ) {
     158            $admin_role->add_cap( $this->capability );
     159        }
     160
     161        add_role(
     162            $this->role_slug,
     163            __( 'Broken Link Manager', 'broken-link-notifier' ),
     164            [
     165                'read' => true,
     166                $this->capability  => true,
     167            ]
     168        );
     169    } // End plugin_activate()
     170
     171
     172    /**
     173     * Plugin deactivation hook callback
     174     */
     175    public function plugin_deactivate() {
     176        $admin_role = get_role( 'administrator' );
     177        if ( $admin_role ) {
     178            $admin_role->remove_cap( $this->capability );
     179        }
     180
     181        remove_role( $this->role_slug );
     182    } // End plugin_deactivate()
     183
     184
     185    /**
     186     * Ensures the custom capability exists for administrators on admin_init
     187     */
     188    public function add_custom_capability_to_admin() {
     189        $role = get_role( 'administrator' );
     190        if ( $role && !$role->has_cap( $this->capability ) ) {
     191            $role->add_cap( $this->capability );
     192        }
     193    } // End add_custom_capability_to_admin()
     194
    143195}
  • broken-link-notifier/trunk/includes/menu.php

    r3292111 r3335487  
    3939     */
    4040    public $menu_items;
     41
     42
     43    /**
     44     * The capability required to access the menu
     45     *
     46     * @var string
     47     */
     48    public $capability = 'manage_broken_links';
    4149
    4250
     
    7078        add_action( 'admin_menu', [ $this, 'admin_menu' ] );
    7179
     80        // Update role after sett
     81        add_action( 'admin_init', [ $this, 'update_roles' ] );
     82
    7283        // Fix the Manage link to show active
    7384        add_filter( 'parent_file', [ $this, 'submenus' ] );
     
    8899     */
    89100    public function admin_menu() {
    90         // Capability
    91         $capability = sanitize_key( apply_filters( 'blnotifier_capability', 'manage_options' ) );
     101        // Get the current user's roles
     102        $current_user = wp_get_current_user();
     103        $user_roles = (array) $current_user->roles;
     104
     105        $capability = 'do_not_allow';
     106        $show_menu = false;
     107
     108        if ( in_array( 'administrator', $user_roles ) ) {
     109            $capability = 'manage_options';
     110            $show_menu = true;
     111
     112        } else {
     113            $allowed_roles = get_option( 'blnotifier_editable_roles', [] );
     114            if ( is_array( $allowed_roles ) && !empty( $allowed_roles ) ) {
     115                foreach ( $allowed_roles as $role_slug => $value ) {
     116                    $role_slug = sanitize_key( $role_slug );
     117
     118                    if ( in_array( $role_slug, $user_roles ) ) {
     119                        $capability = $this->capability;
     120                        $show_menu = true;
     121                        break;
     122                    }
     123                }
     124            }
     125        }
     126
     127        if ( !$show_menu ) {
     128            return;
     129        }
    92130
    93131        // Count broken links
     
    112150        }
    113151    } // End admin_menu()
     152
     153
     154    /**
     155     * Update user roles
     156     *
     157     * @return void
     158     */
     159    public function update_roles() {
     160        if ( isset( $_GET[ 'settings-updated' ] ) && $_GET[ 'settings-updated' ] == 'true' ) {
     161
     162            $allowed_roles = get_option( 'blnotifier_editable_roles', [] );
     163            if ( is_array( $allowed_roles ) && !empty( $allowed_roles ) ) {
     164                $saniotized_allowed_roles = [];
     165                foreach ( $allowed_roles as $role_slug => $value ) {
     166                    $saniotized_allowed_roles[] = sanitize_key( $role_slug );
     167                }
     168                $allowed_roles = $saniotized_allowed_roles;
     169            }
     170
     171            $editable_roles = $this->get_editable_roles_choices();
     172            if ( !empty( $editable_roles ) && is_array( $editable_roles ) ) {
     173               
     174                foreach ( $editable_roles as $editable_role_slug => $label ) {
     175                    $role = get_role( $editable_role_slug );
     176                    if ( !$role ) {
     177                        continue;
     178                    }
     179
     180                    if ( in_array( $editable_role_slug, $allowed_roles ) && ! $role->has_cap( $this->capability ) ) {
     181                        $role->add_cap( $this->capability );
     182                    } elseif ( $role->has_cap( $this->capability ) ) {
     183                        $role->remove_cap( $this->capability );
     184                    }
     185                }
     186            }
     187        }
     188    } // End update_roles()
    114189
    115190
     
    449524                'min'      => 0,
    450525                'comments' => 'Use 0 to disable caching. If you are experienced performance issues, you can set the value to 28800 (8 hours), 43200 (12 hours), 86400 (24 hours) or whatever you feel is best. Broken and warning links will never be cached. Deactivating or uninstalling the plugin will clear the cache completely.'
     526            ]
     527        );
     528
     529        // User Roles for Editing Links
     530        $roles_option_name = 'blnotifier_editable_roles';
     531        register_setting( $this->page_slug, $roles_option_name, [ $this, 'sanitize_checkboxes' ] );
     532        add_settings_field(
     533            $roles_option_name,
     534            'Allow These Additional Roles to Manage Broken Links',
     535            [ $this, 'field_checkboxes' ],
     536            $this->page_slug,
     537            'general',
     538            [
     539                'class'    => $roles_option_name,
     540                'name'     => $roles_option_name,
     541                'options'  => $this->get_editable_roles_choices(),
    451542            ]
    452543        );
     
    794885
    795886    /**
     887     * Get editable roles choices
     888     *
     889     * @return array
     890     */
     891    public function get_editable_roles_choices() {
     892        global $wp_roles;
     893        $results = [];
     894        $roles = $wp_roles->get_names();
     895        foreach ( $roles as $role_slug => $role_name ) {
     896            if ( $role_slug == 'administrator' ) {
     897                continue;
     898            }
     899            $results[ $role_slug ] = $role_name;
     900        }
     901        return $results;
     902    } // End get_editable_roles_choices()
     903
     904
     905    /**
    796906     * Custom callback function to print multiple emails field
    797907     *
  • broken-link-notifier/trunk/readme.txt

    r3331551 r3335487  
    55Tested up to: 6.8
    66Requires PHP: 7.4
    7 Stable tag: 1.3.1.1
     7Stable tag: 1.3.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.txt
     
    128128
    129129== Changelog ==
     130= 1.3.2 =
     131* Update: Added support for additional roles to have capability of managing broken links
     132
    130133= 1.3.1.1 =
    131134* Fix: preg_replace() deprecation error (props @venutius)
Note: See TracChangeset for help on using the changeset viewer.