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

Page not created on plugin activation #911

Closed
faisal-alvi opened this issue Jan 5, 2024 · 6 comments · Fixed by #984
Closed

Page not created on plugin activation #911

faisal-alvi opened this issue Jan 5, 2024 · 6 comments · Fixed by #984
Labels
[Package][@wp-playground] Blueprints [Type] Bug An existing feature does not function as intended

Comments

@faisal-alvi
Copy link

faisal-alvi commented Jan 5, 2024

We are using the playground to show the preview of our Publisher Media Kit plugin.

The plugin creates a page on activation, but while using it in the playground, it does not work, and we have to import the Media Kit page from a test import file. The plugin is supposed to create this itself on activation, but it seems that the activation process that happens when the Playground is initialized doesn't trigger that functionality. This seems to be an issue with the playground.

The code when a plugin creates a page on activation is here:

Related PR: 10up/publisher-media-kit#175

@dmsnell
Copy link
Collaborator

dmsnell commented Jan 8, 2024

@faisal-alvi are you using a blueprint to install the plugin? IIRC the plugins are installed as mu-plugins and don't run their activation hooks. There's an activatePlugin Blueprint step that should do that though, so you might check your configuration to make sure it's doing that.

@dkotter
Copy link

dkotter commented Jan 10, 2024

We currently are using the installPlugin step with the activate option set to true:

{
	"step": "installPlugin",
	"pluginZipFile": {
		"resource": "wordpress.org\/plugins",
		"slug": "publisher-media-kit"
	},
	"options": {
		"activate": true
	}
}

I tried changing this to use activatePlugin as well but that also didn't work:

{
	"step": "installPlugin",
	"pluginZipFile": {
		"resource": "wordpress.org\/plugins",
		"slug": "publisher-media-kit"
	},
	"options": {
		"activate": false
	}
},
{
	"step": "activatePlugin",
	"pluginName": "Publisher Media Kit",
	"pluginPath": "/wordpress/wp-content/plugins/publisher-media-kit"
}

@dkotter
Copy link

dkotter commented Jan 10, 2024

And I guess to be more clear, it doesn't appear in either case it's getting installed as an mu-plugin, at least not showing up in the mu-plugins directory or in the Must-Use section on the plugins page

@dmsnell
Copy link
Collaborator

dmsnell commented Jan 10, 2024

@dkotter thanks for double-checking that. in review, I noticed that the test import file is listed in the Blueprint steps after the plugin install and activation. I rearranged those so that the import file appears first, and it appears as though the Media Kit demo content was imported.

{
  "$schema": "https://playground.wordpress.net/blueprint-schema.json",
  "landingPage": "/wp-admin/post.php?post=7&action=edit",
  "preferredVersions": {
    "php": "7.4",
    "wp": "latest"
  },
  "phpExtensionBundles": [
    "kitchen-sink"
  ],
  "steps": [
    {
      "step": "login",
      "username": "admin",
      "password": "password"
    },
    {
      "step": "importFile",
      "file": {
        "resource": "url",
        "url": "https://raw.githubusercontent.com/10up/publisher-media-kit/65a081ba5d9f03270873d96baf7189d20fafcd7c/.wordpress-org/blueprints/demo-data.xml"
      }
    },
    {
      "step": "installPlugin",
      "pluginZipFile": {
        "resource": "wordpress.org/plugins",
        "slug": "publisher-media-kit"
      },
      "options": {
        "activate": true
      }
    }
  ]
}

https://playground.wordpress.net/#{"$schema":"https://playground.wordpress.net/blueprint-schema.json","landingPage":"/wp-admin/post.php?post=7&action=edit","preferredVersions":{"php":"7.4","wp":"latest"},"phpExtensionBundles":["kitchen-sink"],"steps":[{"step":"login","username":"admin","password":"password"},{"step":"importFile","file":{"resource":"url","url":"https://raw.githubusercontent.com/10up/publisher-media-kit/65a081ba5d9f03270873d96baf7189d20fafcd7c/.wordpress-org/blueprints/demo-data.xml"}},{"step":"installPlugin","pluginZipFile":{"resource":"wordpress.org/plugins","slug":"publisher-media-kit"},"options":{"activate":true}}]}

@dkotter
Copy link

dkotter commented Jan 11, 2024

Thanks for the response.

That import File step shouldn't even be needed. I added it because the Media Kit page which the plugin automatically creates on plugin activation was not being created. So instead I exported that page and we import it. So it shouldn't matter if that step runs before or after activation the problem is we have to use that step at all since it doesn't seem that activating the plugin through the blueprint actually triggers the normal activation code.

@adamziel
Copy link
Collaborator

Plugin activation runs without a "current user" which makes this check fail:

	if ( ! current_user_can( 'activate_plugins' ) ) {
		return;
	}

I have a fix incoming. Thank you for reporting!

@adamziel adamziel added [Type] Bug An existing feature does not function as intended [Package][@wp-playground] Blueprints labels Jan 30, 2024
adamziel added a commit that referenced this issue Jan 30, 2024
…hemes

Sets the current user to admin before activating plugins and themes.

 ## Rationale

Issue #911 uncovered a problem where activating a plugin does not create
a page in the following scenario:

```php
function myplugin_activate() {
    if( current_user_can( 'activate_plugins' ) ) {
        create_page();
    }
}
register_activation_hook( __FILE__, 'myplugin_activate' );
```

The root cause is that the activatePlugin and activateTheme steps do
not set the current user to admin before activating the plugin/theme.

 ## Testing instructions

This PR comes with unit tests – confirm the CI checks pass.
adamziel added a commit that referenced this issue Jan 30, 2024
…hemes (#984)

Sets the current user to admin before activating plugins and themes.

## Rationale

Issue #911 uncovered a problem where activating a plugin does not create
a page in the following scenario:

```php
function myplugin_activate() {
    if( current_user_can( 'activate_plugins' ) ) {
        create_page();
    }
}
register_activation_hook( __FILE__, 'myplugin_activate' );
```

The root cause is that the activatePlugin and activateTheme steps do not
set the current user to admin before activating the plugin/theme.

## Testing instructions

This PR comes with unit tests – confirm the CI checks pass.

Closes #911
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package][@wp-playground] Blueprints [Type] Bug An existing feature does not function as intended
Projects
None yet
4 participants