scribu / wp-posts-to-posts

Efficient many-to-many connections between posts, pages, custom post types, users.
http://wordpress.org/plugins/posts-to-posts/
969 stars 260 forks source link

Tables wp_p2p and wp_p2pmeta not created upon plugin activation #555

Open Flimm opened 6 years ago

Flimm commented 6 years ago

Let's say that I install the "Posts 2 Posts" plugin on a brand new WordPress installation, using the WP CLI, like this:

$ wp plugin install --activate posts-to-posts
Installing Posts 2 Posts (1.6.5)
Downloading install package from https://downloads.wordpress.org/plugin/posts-to-posts.1.6.5.zip...
Using cached file '.wp-cli/cache/plugin/posts-to-posts-1.6.5.zip'...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Activating 'posts-to-posts'...
Plugin 'posts-to-posts' activated.
Success: Installed 1 of 1 plugins.

At this point, the plugin has been installed and activated. However, the tables have not been created yet:

> show tables;
+-----------------------+
| Tables_in_dt          |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+

As you can see, the tables wp_p2p and wp_p2pmeta are missing. If there are any other plugins that depend on this functionality, this could cause errors like this one to appear:

WordPress database error: [Table 'wordpress.wp_p2p' doesn't exist]

Based on reading the source code, it looks like the tables are created when an instance of the class P2P_Tools_Page is created for the first time here:

https://github.com/scribu/wp-posts-to-posts/blob/d2f297dbe257a09438da6ffe22c9a876acb7b49b/posts-to-posts.php#L53

This is inside the function _p2p_load_admin, which is only called if is_admin() is true. Instead, the tables should be created upon plugin registration.

Of course, the workaround is to visit the WordPress admin before using the site. Sure enough, as soon as the WordPress admin dashboard is visited, the tables are created.

Requiring visiting the WordPress admin makes it harder to automate setting up new websites with certain plugins using the WP CLI.

dylanjameswagner commented 6 years ago

@Flimm Were you able to find a solution to this issue?

Flimm commented 6 years ago

I've just been using the workaround mentioned in the issue description.