Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook vp_before_synchronization_{$entityName} not working #152

Open
beabri opened this issue Aug 3, 2018 · 5 comments
Open

Hook vp_before_synchronization_{$entityName} not working #152

beabri opened this issue Aug 3, 2018 · 5 comments

Comments

@beabri
Copy link

beabri commented Aug 3, 2018

Hi all,

I want to make an ALTER TABLE on a Yoast Seo Meta table before Versionpress DB synchronization, so I created a file hook.php inside versionpress folder wp-content\.versionpress\plugins\wordpress_seo, but it seems not to execute code when the action is called.
I'm using vp_before_synchronization_{$entityName}action.

This is the hooks.php code:

error_log('hello world');

<?php

add_action('vp_before_synchronization_yoast_seo_meta', function () {
    error_log('execute before yoast_seo_meta synchro');
});

?>

When I execute therestore_site command, I read Hello world, but anything else.
Am I missing something?

Thanks a lot.

  • VersionPress version: 4.0-beta-16-g2071052a
  • WordPress version: 4.9.8
  • PHP: 5.6.30
  • Git version: 2.12.2
@beabri
Copy link
Author

beabri commented Aug 23, 2018

I'm listing below all the necessary files:

php error log after executing restore-site command:

[23-Aug-2018 08:55:46 UTC] hello world
[23-Aug-2018 08:56:13 UTC] hello world
[23-Aug-2018 08:56:15 UTC] hello world
[23-Aug-2018 08:56:31 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:31 UTC] vp_before_synchronization_user
[23-Aug-2018 08:56:31 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:31 UTC] vp_before_synchronization_term
[23-Aug-2018 08:56:31 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:31 UTC] vp_before_synchronization_termmeta
[23-Aug-2018 08:56:31 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:31 UTC] vp_before_synchronization_term_taxonomy
[23-Aug-2018 08:56:31 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:31 UTC] vp_before_synchronization_usermeta
[23-Aug-2018 08:56:32 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:32 UTC] vp_before_synchronization_post
[23-Aug-2018 08:56:33 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:33 UTC] vp_before_synchronization_postmeta
[23-Aug-2018 08:56:34 UTC] hello world
[23-Aug-2018 08:56:34 UTC] hello world
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_commentmeta
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_user
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_user
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_term
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_termmeta
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_term_taxonomy
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_term_taxonomy
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_usermeta
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_post
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_post
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_post
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_postmeta
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_comment
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_option
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_option
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_yoast_seo_link
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_yoast_seo_meta
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_yoast_seo_meta
[23-Aug-2018 08:56:36 UTC] EXECUTE ACTION
[23-Aug-2018 08:56:36 UTC] vp_before_synchronization_commentmeta
[23-Aug-2018 08:56:37 UTC] hello world
[23-Aug-2018 08:56:39 UTC] hello world
[23-Aug-2018 08:56:40 UTC] hello world
[23-Aug-2018 08:58:12 UTC] hello world

The file with the action -> wp-content/.versionpress/plugins/wordpress-seo/hooks.php

<?php

error_log('hello world');

add_action('vp_before_synchronization_yoast_seo_meta', function () {
    error_log('execute before yoast_seo_meta synchro');
});

?>

The versionpress function which contains the "do_action command", placed inside wp-content/plugins/versionpress/src/Synchronizer/Synchronizers.php

    /**
     * Synchronizes entities from storage to the database. It generally only works with tracked
     * entities, i.e. the ignored (untracked) rows in the database are left untouched. The rows
     * corresponding to tracked entities are usually in sync with the storage after this method
     * is done. It may happen that the synchronizer cannot synchronize everything in the first
     * pass. Because of this, the synchronize method takes a task for sychronization (usually
     * "everything" for the first pass) and returns a list of tasks that aren't done yet. It's
     * up to the SynchronizationProcess to call the synchronize method again with this tasks
     * when the previous pass is done.
     *
     * If the $entitiesToSynchronize is null, the synchronizer will synchronize all entities.
     * If it's an array, the synchronizer will synchronize only those entities.
     *
     * @param string $task
     * @param array $entitiesToSynchronize List of VPIDs and their possible parents
     *                                     {@see SynchronizationProcess::synchronize()}
     * @return string[]
     */
    public function synchronize($task, $entitiesToSynchronize = null)
    {
        $entityName = $this->entityName;

        $this->isSelectiveSynchronization = is_array($entitiesToSynchronize);
        $this->entitiesToSynchronize = $entitiesToSynchronize;

        $this->maybeInit();
        $remainingTasks = [];

        error_log('EXECUTE ACTION');
        error_log("vp_before_synchronization_{$entityName}");
        
        do_action("vp_before_synchronization_{$entityName}");

        if ($task === Synchronizer::SYNCHRONIZE_EVERYTHING) {
            $this->createTable();
            $this->updateDatabase();

            $fixedMnReferences = $this->fixMnReferences();
            $this->cleanCache();

            $remainingTasks[] = self::FIX_REFERENCES;

            if (!$fixedMnReferences) {
                $remainingTasks[] = self::SYNCHRONIZE_MN_REFERENCES;
            }

            if ($this->shortcodesReplacer->entityCanContainShortcodes($entityName)) {
                $remainingTasks[] = self::REPLACE_SHORTCODES;
            }

            if ($this->entityContainsComputedValues()) {
                $remainingTasks[] = self::COMPUTE_COLUMN_VALUES;
            }
        }

        if ($task === self::FIX_REFERENCES) {
            $this->fixReferences();
        }

        if ($task === self::SYNCHRONIZE_MN_REFERENCES) {
            $this->fixMnReferences();
        }

        if ($task === self::COMPUTE_COLUMN_VALUES) {
            $this->computeColumnValues();
        }

        if ($task === self::REPLACE_SHORTCODES) {
            $this->restoreShortcodesInAllEntities();
        }

        do_action("vp_after_synchronization_{$entityName}");

        return $remainingTasks;
    }

You can see that 'EXECUTE ACTION' and "vp_before_synchronization_{$entityName}" are correctly logged.

@pavelevap
Copy link
Collaborator

Hi @beabri,

sorry for delay :-(
It looks like you find a bug, because related core actions does not seem to work for me too, for example this. Does it work when you try to hook (and log) to standard post entity, for example vp_before_synchronization_post?

yoast_seo_meta and yoast_seo_link are custom entities for Yoast SEO plugin, could you share your addon for VP (schema) for this plugin to help us test it?

@beabri
Copy link
Author

beabri commented Sep 5, 2018

Hi @pavelevap, don't worry.

I tried with vp_before_synchronization_post but it doesn't seem to hook neither log anything.

This is the schema:

yoast_seo_link:
  table: yoast_seo_links
  id: id
  references:
    post_id: post
    target_post_id: post

yoast_seo_meta:
  table: yoast_seo_meta
  id: id
  references:
    object_id: post
  ignored-columns:
    - internal_link_count
    - incoming_link_count

option:
  ignored-entities:
    - 'option_name: wpseo_tracking_last_request'
    - 'option_name: wpseo_sitemap_*_cache_validator'
    - 'option_name: wpseo_sitemap_cache_validator_global'
    - 'option_name: recently_activated'

usermeta:
  ignored-entities:
    - 'meta_key: wp_yoast_notifications'
    - 'meta_key: community-events-location'

Let me you know if you need anything else.

@beabri
Copy link
Author

beabri commented Sep 24, 2018

Sorry, still no news about it?

@pavelevap
Copy link
Collaborator

Finally, I hope that I found a cause.

Restoring site is run by WP CLI, see function restoreSite(). This function is used without WordPress, but in the end is called function finishRestore() (by WP CLI command finish-restore-site) to load WordPress, at least partially (with SHORTINIT constant), that means without plugins (and without VersionPress).

Basic part of VersionPress is loaded manually later, but it misses loading setup-hooks.php file which contains important function vp_load_hooks_files() needed for loading all hooks.php files. So, nothing from hooks.php is probably loaded and so no hooks are triggered when restoring site :-(

I am not sure about the right approach to fix this issue, but you can try to find following line and add vp_load_hooks_files(); to the next line. It worked for me and it looks that hooks are triggered now. Please let me know if it works for you. I will also create official issue in main VersionPress repository.

Thank you for reporting this problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants