htmlburger / carbon-fields

WordPress Custom Fields Library ✨
https://carbonfields.net/
Other
1.38k stars 245 forks source link

Passing a function to `add_options` still gets called on every admin page #670

Open speedwheel opened 5 years ago

speedwheel commented 5 years ago

This is my code:

<?php
use Carbon_Fields\Block;
use Carbon_Fields\Field;

add_action( 'carbon_fields_register_fields', 'register_package_block' );
function register_package_block() {
    Block::make( __( 'Package' ) )
    ->add_fields( array(
        Field::make( 'select', 'package', __( 'Package' ) )
        ->add_options('get_package_options'),
    ) )

    //->set_icon( 'format-image' )
    ->set_preview_mode( false )
    ->set_category( 'page-content', __( 'Page Content' ), 'smiley' )
    ->set_description( __( 'Block containing a list of packages.' ) )
    ->set_render_callback( function ( $block ) {
?>
         // html
<?php
    } );
}

The function get_package_options makes an HTTP request using CURL which takes couple of seconds to finish. The problem is that this function gets called on every admin page that I navigate to and makes each page to load in 5+ seconds, instead of 500 miliseconds (when I remove this select field).

I've read the aricle here: https://docs.carbonfields.net/#/fields/select?id=performance-implications but it seems that it's not behaving like it should.

I want this function to be called only on the edit post where it's being used.

Any tips?

psaikali commented 3 years ago

Came here to open this issue too, can confirm!

Field::make( 'select', 'email_template_for_password_reset_link', __( 'Template : lien de modification de mot de passe', 'freebulle' ) )->add_options( [ '\Freebulle\Emails', 'get_transactional_templates' ] ),

The get_transactional_templates() method does an API request to get data (a list of templates from SendInBlue) and I noticed that the call is made on any admin page.

It should only be triggered on the page where the CF container is displayed.

psaikali commented 3 years ago

Sorry, the call is not made on all admin pages. It's triggered randomly and I can't seem to find a common event for all these calls.