web-dev-qa-db-fra.com

Construit dans admin ajax hooks?

Je développe un plugin et utilise des hooks et des filtres réguliers. Cependant, j'essaie maintenant de lier ma fonction aux réponses des appels admin ajax de WP.

Par exemple, dans Ajout d'un nouveau plug-in Je voudrais exécuter ma fonction une fois l'installation ajax d'un plug-in terminée et le bouton "Activer" affiché. Comment puis-je activer ma fonction en cas de réponse ajax réussie?

En utilisant "plugin_install_action_links", je peux contrôler les liens d'action via php, mais quel est l'équivalent ajax (s'il en existe un)?

2
EranG

Le noyau WordPress est quelque chose où vous ne pouvez pas créer vos propres crochets. Des crochets dans WordPress sont créés avec ces

apply_filters
apply_filters_ref_array
apply_filters_deprecated
do_action
do_action_ref_array
do_action_deprecated

La seule chose que vous pouvez faire est d’enregistrer votre fonction dans certains hooks principaux de WordPress. Mais où sont ces crochets?

Vous pouvez vous attendre à ces points d'ancrage dans une fonction appelée activate_plugin.

File: wp-admin/includes/plugin.php
512: /**
513:  * Attempts activation of plugin in a "sandbox" and redirects on success.
514:  *
515:  * A plugin that is already activated will not attempt to be activated again.
516:  *
517:  * The way it works is by setting the redirection to the error before trying to
518:  * include the plugin file. If the plugin fails, then the redirection will not
519:  * be overwritten with the success message. Also, the options will not be
520:  * updated and the activation hook will not be called on plugin error.
521:  *
522:  * It should be noted that in no way the below code will actually prevent errors
523:  * within the file. The code should not be used elsewhere to replicate the
524:  * "sandbox", which uses redirection to work.
525:  * {@source 13 1}
526:  *
527:  * If any errors are found or text is outputted, then it will be captured to
528:  * ensure that the success redirection will update the error redirection.
529:  *
530:  * @since 2.5.0
531:  *
532:  * @param string $plugin       Plugin path to main plugin file with plugin data.
533:  * @param string $redirect     Optional. URL to redirect to.
534:  * @param bool   $network_wide Optional. Whether to enable the plugin for all sites in the network
535:  *                             or just the current site. Multisite only. Default false.
536:  * @param bool   $silent       Optional. Whether to prevent calling activation hooks. Default false.
537:  * @return WP_Error|null WP_Error on invalid file or null on success.
538:  */
539: function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {

Notez qu'il existe un paramètre @param bool $silent qui est facultatif et qui vapreventappeler les crochets d'activation. Par défaut, il est défini sur false.

Dans cette fonction pour le moment, vous pouvez trouver des hooks:

File: wp-admin/includes/plugin.php
563:        if ( ! $silent ) {
564:            /**
565:             * Fires before a plugin is activated.
566:             *
567:             * If a plugin is silently activated (such as during an update),
568:             * this hook does not fire.
569:             *
570:             * @since 2.9.0
571:             *
572:             * @param string $plugin       Plugin path to main plugin file with plugin data.
573:             * @param bool   $network_wide Whether to enable the plugin for all sites in the network
574:             *                             or just the current site. Multisite only. Default is false.
575:             */
576:            do_action( 'activate_plugin', $plugin, $network_wide );

Un de plus...

File: wp-admin/includes/plugin.php
563:        if ( ! $silent ) {
...
578:            /**
579:             * Fires as a specific plugin is being activated.
580:             *
581:             * This hook is the "activation" hook used internally by register_activation_hook().
582:             * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
583:             *
584:             * If a plugin is silently activated (such as during an update), this hook does not fire.
585:             *
586:             * @since 2.0.0
587:             *
588:             * @param bool $network_wide Whether to enable the plugin for all sites in the network
589:             *                           or just the current site. Multisite only. Default is false.
590:             */
591:            do_action( "activate_{$plugin}", $network_wide );

Et celui dont vous avez réellement besoin ...

File: wp-admin/includes/plugin.php
605:        if ( ! $silent ) {
606:            /**
607:             * Fires after a plugin has been activated.
608:             *
609:             * If a plugin is silently activated (such as during an update),
610:             * this hook does not fire.
611:             *
612:             * @since 2.9.0
613:             *
614:             * @param string $plugin       Plugin path to main plugin file with plugin data.
615:             * @param bool   $network_wide Whether to enable the plugin for all sites in the network
616:             *                             or just the current site. Multisite only. Default is false.
617:             */
618:            do_action( 'activated_plugin', $plugin, $network_wide );

Je viens de chercher les fonctions du générateur de hook telles que do_action.


Ce que vous faites plugin_install_action_links est unfilertype de hook. Les filtres sont généralement là pour replace certaines choses.

File: wp-admin/includes/class-wp-plugin-install-list-table.php
519:            /**
520:             * Filters the install action links for a plugin.
521:             *
522:             * @since 2.7.0
523:             *
524:             * @param array $action_links An array of plugin action hyperlinks. Defaults are links to Details and Install Now.
525:             * @param array $plugin       The plugin currently being listed.
526:             */
527:            $action_links = apply_filters( 'plugin_install_action_links', $action_links, $plugin );

En utilisant "plugin_install_action_links", je peux contrôler les liens d'action via php, mais quel est l'équivalent ajax (s'il en existe un)?

Il existe un fichier appelé admin-ajax.php. Vous pouvez examiner ce fichier et utiliser des points d'ancrage pour vos tâches spécifiques, mais aucun filtre n'est défini pour le moment dans admin-ajax.php.

2
prosti