actions – Ajax callback not work

Question

My files is :

synergyservice.php

register_activation_hook( __FILE__, array( 'Synergy', 'plugin_activation' ) );
register_deactivation_hook( __FILE__, array( 'Synergy', 'plugin_deactivation' ) );

if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
    require_once( __DIR__ . '/environments.php' );
    require_once( SYNERGY__PLUGIN_DIR . 'class.synergyservices.php' );
    require_once( wp_normalize_path (SYNERGY__PLUGIN_DIR . 'admin/Helpers/class.helper.php' ) );

    add_action( 'init', array( 'Synergy', 'init' ) );

    require_once(  wp_normalize_path(SYNERGY__PLUGIN_DIR . 'admin/Classes/class.synergy-admin.php') );
    add_action( 'init', array( 'Synergy_admin', 'init' ) );
}

class-synergyservice.php

<?php

class Synergy{

    private static bool $initiated = false;

    public static function init(): void
    {
        if ( ! self::$initiated ) {
            self::init_hooks();
        }
    }

    /**
     * Initializes WordPress hooks
     */
    private static function init_hooks(): void
    {
        self::$initiated = true;

        Synergy_admin::init();

    }


    /**
     * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
     * @static
     */
    public static function plugin_activation(): void
    {
        if ( version_compare( $GLOBALS['wp_version'], SYNERGY__MINIMUM_WP_VERSION, '<' ) ) {

            load_plugin_textdomain( 'synergy', false, SYNERGY__WP_PLUGIN . '/languages/' );

        } elseif ( ! empty( $_SERVER['SCRIPT_NAME'] ) && false !== strpos( $_SERVER['SCRIPT_NAME'], '/wp-admin/plugins.php' ) ) {
            add_option( 'Activated_SynergyService', true );
        }
    }

    /**
     * Removes all connection options
     * @static
     */
    public static function plugin_deactivation( ): void
    {

    }

}

class-contact.php

<?php    
if( !class_exists('Synergy_contact')) {
    class Synergy_contact {

        private static bool $initiated = false;

        public static function init(): void
        {
            if ( ! self::$initiated ) {
                self::init_hooks();
            }
        }

        public static function init_hooks(): void {

            self::$initiated = true;
            
            wp_register_script( 'contacts.js', plugin_dir_url( __FILE__ ) . '/../../js/contacts.js', array('jquery'), SYNERGY__VERSION );

            $title_nonce = wp_create_nonce( 'synergy_nonce_form_contact' );

            wp_localize_script(
                'contacts.js',
                'ajax_obj_form_contact',
                array(
                    'ajax_url' => admin_url( 'admin-ajax.php' ),
                    'nonce'    => $title_nonce,
                )
            );

            wp_enqueue_script( 'contacts.js' );

            add_action('wp_ajax_get_data_from_form', 'get_data_from_form' );
            add_action('wp_ajax_nopriv_get_data_from_form', 'get_data_from_form');

               // render my view to use script


            }

        }
            public function get_data_from_form(){
        echo "a";
        wp_die('ub');

    }

}

My script:

jQuery.ajax({
    type: "POST",
    url: ajax_obj_form_contact.ajax_url,
    // _ajax_nonce: ajax_obj_form_contact.nonce,
    // dataType: "JSON",
    data: {
        action : 'get_data_from_form',
    }
  });

Error 404 bad request. Response 0 from wp-admin.

I see on $GLOBALS[‘wp_filter’] my hook and found it.

I guess problem is instance of class, because I try to NOT use auto-instance at end of file class, and wp-admin cannot found method.
I try run do_action(my_hook) and it’s work.

Is it a bad practice not to instantiate the class at the end of the file?

Anyone can help me?

Best regards,

0
tecnico57 3 months 2022-05-18T09:58:55-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse