Plugin Directory

Changeset 2852999


Ignore:
Timestamp:
01/23/2023 03:58:19 PM (3 years ago)
Author:
romchyk16
Message:

add api key for search
add options page for save api key

Location:
acf-yandex-maps-field
Files:
2 added
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • acf-yandex-maps-field/trunk/acf-yandex-map-fields.php

    r2832537 r2852999  
    3939        add_action( 'acf/include_field_types', [$this, 'include_field'] );
    4040        add_action( 'acf/register_fields', [$this, 'include_field'] );
     41        // settings page
     42        add_action( 'admin_init', [$this, 'ymf_settings_init'] );
     43        add_action( 'admin_menu', [$this, 'options_page'] );
     44
    4145    }
    4246
     
    5660    }
    5761
     62    function ymf_settings_init() {
     63
     64        // Register a new setting for "ymf" page.
     65        register_setting( 'acf-yandex-map', 'ymf_options' );
     66   
     67        // Register a new section in the "ymf" page.
     68        add_settings_section(
     69            'ymf_section_developers',
     70            '', [$this, 'section_developers_callback'],
     71            'acf-yandex-map'
     72        );
     73        // Register a new field in the "ymf_section_developers" section, inside the "ymf" page.
     74        add_settings_field(
     75            'ymf_field_api_key', // As of WP 4.6 this value is used only internally.
     76                                 // Use $args' label_for to populate the id inside the callback.
     77            __( 'Yandex api key', 'acf-yandex-map' ),
     78            [$this, 'field_api_key'],
     79            'acf-yandex-map',
     80            'ymf_section_developers',
     81            [
     82                'label_for' => 'ymf_field_api_key',
     83                'class' => 'ymf_row',
     84                'ymf_custom_data' => 'custom',
     85            ]
     86        );
     87    }
     88   
     89    function section_developers_callback( $args ) {
     90
     91    }
     92   
     93    function field_api_key( $args ) {
     94
     95        // Get the value of the setting we've registered with register_setting()
     96        $options = get_option( 'ymf_options' );
     97        ?>
     98        <input  type="text"
     99                id="<?php echo esc_attr( $args['label_for'] ); ?>"
     100                data-custom="<?php echo esc_attr( $args['ymf_custom_data'] ); ?>"
     101                name="ymf_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
     102                value="<?php echo isset( $options[ $args['label_for'] ] ) ? $options[ $args['label_for'] ] : null; ?>">
     103
     104        <?php
     105    }
     106   
     107    function options_page() {
     108       
     109        add_submenu_page(
     110            'options-general.php',
     111            'ACF Yandex Map Settings',
     112            'ACF Yandex Map Settings',
     113            'manage_options',
     114            'acf-yandex-map',
     115            [$this, 'options_page_html']
     116        );
     117    }
     118   
     119    function options_page_html() {
     120
     121        // check user capabilities
     122        if ( ! current_user_can( 'manage_options' ) ) {
     123            return;
     124        }
     125   
     126        // add error/update messages
     127   
     128        // check if the user have submitted the settings
     129        // WordPress will add the "settings-updated" $_GET parameter to the url
     130        // if ( isset( $_GET['settings-updated'] ) ) {
     131        //  // add settings saved message with the class of "updated"
     132        //  add_settings_error( 'ymf_messages', 'ymf_message', __( 'Settings Saved', 'acf-yandex-map' ), 'updated' );
     133        // }
     134   
     135        // show error/update messages
     136        settings_errors( 'ymf_messages' );
     137        ?>
     138        <div class="wrap">
     139            <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
     140            <form action="options.php" method="post">
     141                <?php
     142                // output security fields for the registered setting "ymf"
     143                settings_fields( 'acf-yandex-map' );
     144                // output setting sections and their fields
     145                // (sections are registered for "ymf", each field is registered to a specific section)
     146                do_settings_sections( 'acf-yandex-map' );
     147                // output save settings button
     148                submit_button( 'Save Settings' );
     149                ?>
     150            </form>
     151        </div>
     152        <?php
     153    }
     154
    58155}
    59156
  • acf-yandex-maps-field/trunk/fields/class-acf-yandex-map-v4.php

    r2832537 r2852999  
    5353            $url = $this->settings['url'];
    5454            $version = $this->settings['version'];
    55             wp_register_script( 'yandex-map-api', '//api-maps.yandex.ru/2.1/?lang=' . get_bloginfo( 'language' ), ['jquery'], $version );
     55            $options = get_option('ymf_options');
     56            $api_key = $options['ymf_field_api_key'] ?? '';
     57            wp_register_script( 'yandex-map-api', '//api-maps.yandex.com/2.1/?lang=' . get_bloginfo( 'language' ) .'&amp;apikey=' . $api_key, ['jquery'], $version );
    5658            wp_register_script( 'yandex-map-frontend', "{$url}js/acf-yandex-map-frontend.js", ['yandex-map-api'], 'acf-yandex-map' );
    5759            wp_enqueue_script( 'yandex-map-frontend' );
     
    287289        $url = $this->settings['url'];
    288290        $version = $this->settings['version'];
    289 
    290         wp_register_script( 'yandex-map-api', '//api-maps.yandex.ru/2.1/?lang=' . get_bloginfo( 'language' ), ['jquery'], $version );
     291        $options = get_option('ymf_options');
     292        $api_key = $options['ymf_field_api_key'] ?? '';
     293        wp_register_script( 'yandex-map-api', '//api-maps.yandex.com/2.1/?lang=' . get_bloginfo( 'language' ) .'&amp;apikey=' . $api_key, ['jquery'], $version );
    291294        wp_register_script( 'acf-yandex', "{$url}js/acf-yandex-map-admin.js", ['yandex-map-api'], $version, true );
    292295
  • acf-yandex-maps-field/trunk/fields/class-acf-yandex-map-v5.php

    r2832537 r2852999  
    5050            $url = $this->settings['url'];
    5151            $version = $this->settings['version'];
    52             wp_register_script( 'yandex-map-api', '//api-maps.yandex.ru/2.1/?lang=' . get_bloginfo( 'language' ), ['jquery'], $version );
     52            $options = get_option('ymf_options');
     53            $api_key = $options['ymf_field_api_key'] ?? '';
     54            wp_register_script( 'yandex-map-api', '//api-maps.yandex.com/2.1/?lang=' . get_bloginfo( 'language' ) .'&amp;apikey=' . $api_key, ['jquery'], $version );
    5355            wp_register_script( 'yandex-map-frontend', "{$url}js/acf-yandex-map-frontend.js", ['yandex-map-api'], 'acf-yandex-map' );
    5456            wp_enqueue_script( 'yandex-map-frontend' );
     
    197199        $url = $this->settings['url'];
    198200        $version = $this->settings['version'];
    199 
    200         wp_register_script( 'yandex-map-api', '//api-maps.yandex.ru/2.1/?lang=' . get_bloginfo( 'language' ), ['jquery'], $version );
     201        $options = get_option('ymf_options');
     202        $api_key = $options['ymf_field_api_key'] ?? '';
     203        wp_register_script( 'yandex-map-api', '//api-maps.yandex.com/2.1/?lang=' . get_bloginfo( 'language' ) .'&amp;apikey=' . $api_key, ['jquery'], $version );
    201204        wp_register_script( 'acf-yandex', "{$url}js/acf-yandex-map-admin.js", ['yandex-map-api'], $version, true );
    202205
  • acf-yandex-maps-field/trunk/readme.txt

    r2833168 r2852999  
    3636== Screenshots ==
    3737
    38 1. Select field type
    39 2. Field configuration
    40 3. Example of the new field in action
    41 4. Output on the frontend
     381. Add Api key
     392. Select field type
     403. Field configuration
     414. Example of the new field in action
     425. Output on the frontend
    4243
    4344== Changelog ==
Note: See TracChangeset for help on using the changeset viewer.