Plugin Directory

Changeset 2579683


Ignore:
Timestamp:
08/07/2021 01:18:18 PM (5 years ago)
Author:
mapsiter
Message:

added options page and logic to be able to set own domain for envs.

Location:
devstage/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • devstage/trunk/devstage.php

    r2458333 r2579683  
    55Description: Simplest admin bar indicator whether you are in development, staging or live environment.
    66Author: Weblings.co
    7 Version: 1.0.6
     7Version: 1.1.0
    88Author URI: https://www.weblings.co
    99*/
    1010
    1111
    12 // Enqueue plugin scripts and styles
     12
     13/**
     14 * Enqueue plugin scripts and styles
     15 */
    1316function devstage_styles_and_scripts() {
    1417    wp_enqueue_style('devstage', plugins_url('assets/css/devstage.css', __FILE__));
     
    2427
    2528
    26 // Mark WP admin bar on ://dev. and ://staging. enviroments
     29/**
     30 * Generated by the WordPress Option Page generator
     31 * at http://jeremyhixon.com/wp-tools/option-page/
     32 */
     33
     34class DevStage {
     35    private $devstage_options;
     36
     37    public function __construct() {
     38        add_action( 'admin_menu', array( $this, 'devstage_add_plugin_page' ) );
     39        add_action( 'admin_init', array( $this, 'devstage_page_init' ) );
     40    }
     41
     42    public function devstage_add_plugin_page() {
     43        add_options_page(
     44            'DevStage', // page_title
     45            'DevStage', // menu_title
     46            'manage_options', // capability
     47            'devstage', // menu_slug
     48            array( $this, 'devstage_create_admin_page' ) // function
     49        );
     50    }
     51
     52    public function devstage_create_admin_page() {
     53        $this->devstage_options = get_option( 'devstage_option_name' ); ?>
     54
     55        <div class="wrap">
     56            <h2>DevStage</h2>
     57            <p>Only need to set the corresponding field if you are in the same environment.
     58      <br><br>
     59      Include the part between the protocol and the directory path: https://<strong><span style="background:white;padding:1px 4px;">www.domain.com</span></strong>/something/index.html
     60      <br><br>
     61      You can leave the field empty if you are using <strong><span style="background:white;padding:1px 4px;">://dev.</span></strong> or <strong><span style="background:white;padding:1px 4px;">://staging.</span></strong> subdomain for your dev or staging environment, respectively. In that case the plugin is already working for you.
     62      </p>
     63            <?php settings_errors(); ?>
     64
     65            <form method="post" action="options.php">
     66                <?php
     67                    settings_fields( 'devstage_option_group' );
     68                    do_settings_sections( 'devstage-admin' );
     69                    submit_button();
     70                ?>
     71            </form>
     72        </div>
     73    <?php }
     74
     75    public function devstage_page_init() {
     76        register_setting(
     77            'devstage_option_group', // option_group
     78            'devstage_option_name', // option_name
     79            array( $this, 'devstage_sanitize' ) // sanitize_callback
     80        );
     81
     82        add_settings_section(
     83            'devstage_setting_section', // id
     84            'Settings', // title
     85            array( $this, 'devstage_section_info' ), // callback
     86            'devstage-admin' // page
     87        );
     88
     89        add_settings_field(
     90            'development_environment_domain_0', // id
     91            'Development environment domain', // title
     92            array( $this, 'development_environment_domain_0_callback' ), // callback
     93            'devstage-admin', // page
     94            'devstage_setting_section' // section
     95        );
     96
     97        add_settings_field(
     98            'staging_environment_domain_1', // id
     99            'Staging environment domain', // title
     100            array( $this, 'staging_environment_domain_1_callback' ), // callback
     101            'devstage-admin', // page
     102            'devstage_setting_section' // section
     103        );
     104    }
     105
     106    public function devstage_sanitize($input) {
     107        $sanitary_values = array();
     108        if ( isset( $input['development_environment_domain_0'] ) ) {
     109            $sanitary_values['development_environment_domain_0'] = sanitize_text_field( $input['development_environment_domain_0'] );
     110        }
     111
     112        if ( isset( $input['staging_environment_domain_1'] ) ) {
     113            $sanitary_values['staging_environment_domain_1'] = sanitize_text_field( $input['staging_environment_domain_1'] );
     114        }
     115
     116        return $sanitary_values;
     117    }
     118
     119    public function devstage_section_info() {
     120       
     121    }
     122
     123    public function development_environment_domain_0_callback() {
     124        printf(
     125            '<input class="regular-text" type="text" placeholder="use this field on dev env only" name="devstage_option_name[development_environment_domain_0]" id="development_environment_domain_0" value="%s">',
     126            isset( $this->devstage_options['development_environment_domain_0'] ) ? esc_attr( $this->devstage_options['development_environment_domain_0']) : ''
     127        );
     128    }
     129
     130    public function staging_environment_domain_1_callback() {
     131        printf(
     132            '<input class="regular-text" type="text" placeholder="use this field on staging env only" name="devstage_option_name[staging_environment_domain_1]" id="staging_environment_domain_1" value="%s">',
     133            isset( $this->devstage_options['staging_environment_domain_1'] ) ? esc_attr( $this->devstage_options['staging_environment_domain_1']) : ''
     134        );
     135    }
     136
     137}
     138if ( is_admin() )
     139    $devstage = new DevStage();
     140
     141/*
     142 * Retrieve this value with:
     143 * $devstage_options = get_option( 'devstage_option_name' ); // Array of All Options
     144 * $development_environment_domain_0 = $devstage_options['development_environment_domain_0']; // development environment domain
     145 * $staging_environment_domain_1 = $devstage_options['staging_environment_domain_1']; // staging environment domain
     146 */
     147
     148
     149
     150/**
     151 * DevStage logic
     152 */
    27153function dev_and_staging($admin_bar){
    28154
    29     $url = '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
     155  $devstage_options = get_option( 'devstage_option_name' ); // Array of All Options
     156  $development_environment_domain_0 = $devstage_options['development_environment_domain_0']; // development environment domain
     157  $staging_environment_domain_1 = $devstage_options['staging_environment_domain_1']; // staging environment domain
     158    $urlAuto = '://' . $_SERVER['HTTP_HOST'];
     159  $urlManual = $_SERVER['HTTP_HOST'];
    30160
    31161    // Dev
    32     if (strpos($url,'://dev') !== false) {
     162    if (strpos($urlAuto,'://dev') !== false || $urlManual == $devstage_options['development_environment_domain_0']) {
    33163        $admin_bar->add_menu( array(
    34164        'id'    => 'dev',
     
    37167        ));
    38168  // STAGING
    39     } elseif (strpos($url,'://staging') !== false) {
     169    } elseif (strpos($urlAuto,'://staging') !== false || $urlManual == $devstage_options['staging_environment_domain_1']) {
    40170        $admin_bar->add_menu( array(
    41171        'id'    => 'staging',
     
    53183}
    54184add_action('admin_bar_menu', 'dev_and_staging');
     185
     186
     187
     188/**
     189 * Add settings link under plugin on plugin screen
     190 */
     191add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'devstage_settings_link' );
     192function devstage_settings_link( $links ) {
     193    // Build and escape the URL.
     194    $url = esc_url( add_query_arg(
     195        'page',
     196        'devstage',
     197        get_admin_url() . 'options-general.php'
     198    ) );
     199    // Create the link.
     200    $settings_link = "<a href='$url'>" . __( 'Settings' ) . '</a>';
     201    // Adds the link to the end of the array.
     202    array_unshift(
     203        $links,
     204        $settings_link
     205    );
     206    return $links;
     207}
  • devstage/trunk/readme.txt

    r2579394 r2579683  
    1212== Description ==
    1313
    14 Simplest admin bar indicator created by [Weblings](https://www.weblings.co/), to show whether you are in Development, Staging or Live environment.
    15 Make sure your development and staging environment's url starts with '://dev' and '://staging' respectively. In every other case the the plugin marks the page as it is a Live environment.
     14Simplest admin bar indicator created by [Weblings](https://www.weblings.co/), to easily recognise whether you are in Development, Staging or Live environment.
    1615
    1716== Installation ==
     
    19181- Download the plugin.
    20192- Upload and activate the plugin through WordPress admin "Plugin" tab.
     203- Check plugin settings under Settings => DevStage
    2121
    2222
Note: See TracChangeset for help on using the changeset viewer.