Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
wp-basic-requirements
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wpdesk
wp-basic-requirements
Commits
ed63403c
Commit
ed63403c
authored
5 years ago
by
vasili.guruli
Browse files
Options
Downloads
Patches
Plain Diff
Changes with required plugin data retrieving
parent
4f81bddd
No related branches found
No related tags found
1 merge request
!19
Feature/minimum plugin version check demo1
Pipeline
#9826
failed
5 years ago
Stage: tools
Stage: tests
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Basic_Requirement_Checker.php
+50
-38
50 additions, 38 deletions
src/Basic_Requirement_Checker.php
tests/unit/Test_Basic_Requirement_Checker.php
+11
-2
11 additions, 2 deletions
tests/unit/Test_Basic_Requirement_Checker.php
with
61 additions
and
40 deletions
src/Basic_Requirement_Checker.php
+
50
−
38
View file @
ed63403c
...
...
@@ -17,8 +17,10 @@
const
PLUGIN_INFO_KEY_NAME
=
'name'
;
const
PLUGIN_INFO_VERSION
=
'version'
;
const
PLUGIN_INFO_FAKE_REQUIRED_MINIMUM_VERSION
=
'0.0'
;
const
PLUGIN_INFO_TRANSIENT_NAME
=
'require_plugins_data'
;
const
PLUGIN_INFO_TRANSIENT_EXPIRATION_TIME
=
0
;
const
PLUGIN_INFO_APPEND_PLUGIN_DATA
=
'required_version'
;
const
PLUGIN_INFO_REQUIRED_PLUGINS_TRANSIENT_NAME
=
'require_plugins_data'
;
const
PLUGIN_INFO_IS_WP_PLUGIN_INSTALLED_TRANSIENT_NAME
=
'is_wp_plugin_installed'
;
/** @var string */
protected
$plugin_name
;
...
...
@@ -283,18 +285,28 @@
return
defined
(
'OPENSSL_VERSION_NUMBER'
)
&&
OPENSSL_VERSION_NUMBER
>
(
int
)
$required_version
;
}
/**
* @return array Transient Names
*/
private
function
transient_names
()
{
return
array
(
self
::
PLUGIN_INFO_REQUIRED_PLUGINS_TRANSIENT_NAME
,
self
::
PLUGIN_INFO_IS_WP_PLUGIN_INSTALLED_TRANSIENT_NAME
,
);
}
/**
* @param $notices
*
* @return array
*/
private
function
check_minimum_require_plugins_version_and_append_notices
(
$notices
)
{
if
(
count
(
$
this
->
retrieve_
required_plugins
_data
()
)
>
0
)
{
foreach
(
$
this
->
retrieve_
required_plugins
_data
()
as
$plugin
)
{
if
(
$plugin
[
ucfirst
(
self
::
PLUGIN_INFO_VERSION
)
]
<
$plugin
[
'required_version'
]
)
{
$required_plugins
=
$this
->
retrieve_required_plugins_data
();
if
(
count
(
$required_plugins
)
>
0
)
{
foreach
(
$required_plugins
as
$plugin
)
{
if
(
$plugin
[
ucfirst
(
self
::
PLUGIN_INFO_VERSION
)
]
<
$plugin
[
self
::
PLUGIN_INFO_APPEND_PLUGIN_DATA
]
)
{
$notices
[]
=
$this
->
prepare_notice_message
(
sprintf
(
__
(
'The “%s” plugin requires at least %s version of %s to work correctly. Please update it'
,
$this
->
get_text_domain
()
),
esc_html
(
$this
->
plugin_name
),
$plugin
[
'required_version'
],
$plugin
[
ucfirst
(
self
::
PLUGIN_INFO_KEY_NAME
)
]
)
);
esc_html
(
$this
->
plugin_name
),
$plugin
[
self
::
PLUGIN_INFO_APPEND_PLUGIN_DATA
],
$plugin
[
ucfirst
(
self
::
PLUGIN_INFO_KEY_NAME
)
]
)
);
}
}
}
...
...
@@ -303,29 +315,27 @@
}
/**
* Check the plugins directory and retrieve all plugin file
s with
plugin data.
* Check the plugins directory and retrieve
inst
all
ed
plugin file
or
plugin data.
*
* @return array
* @param $transient_name
* @param $plugin_file
*
* @return array|bool
*/
private
function
retrieve_plugins_data
()
{
if
(
function_exists
(
'get_transient'
)
)
{
$plugins
=
get_transient
(
self
::
PLUGIN_INFO_TRANSIENT_NAME
);
if
(
false
===
$plugins
)
{
private
static
function
retrieve_plugins_data_in_transient
(
$transient_name
,
$plugin_file
=
''
)
{
if
(
!
function_exists
(
'get_plugins'
)
)
{
require_once
ABSPATH
.
'/wp-admin/includes/plugin.php'
;
}
$plugins
=
function_exists
(
'get_plugins'
)
?
get_plugins
()
:
array
();
set_transient
(
self
::
PLUGIN_INFO_TRANSIENT_NAME
,
$plugins
,
self
::
PLUGIN_INFO_TRANSIENT_EXPIRATION_TIME
);
}
$plugins
=
get_transient
(
$transient_name
);
return
$plugins
;
if
(
$plugins
===
false
)
{
$plugins
=
!
empty
(
$plugin_file
)
?
array_key_exists
(
$plugin_file
,
get_plugins
()
)
:
get_plugins
();
set_transient
(
$transient_name
,
$plugins
,
self
::
PLUGIN_INFO_TRANSIENT_EXPIRATION_TIME
);
}
return
array
()
;
return
$plugins
;
}
/**
...
...
@@ -336,14 +346,16 @@
private
function
retrieve_required_plugins_data
()
{
$require_plugins
=
array
();
if
(
count
(
$this
->
retrieve_plugins_data
()
)
>
0
)
{
$plugins
=
$this
->
retrieve_plugins_data
();
$plugins
=
self
::
retrieve_plugins_data_in_transient
(
self
::
PLUGIN_INFO_REQUIRED_PLUGINS_TRANSIENT_NAME
);
if
(
count
(
$plugins
)
>
0
)
{
if
(
!
empty
(
$this
->
plugin_require
)
)
{
foreach
(
$this
->
plugin_require
as
$plugin
)
{
if
(
self
::
is_wp_plugin_active
(
$plugin
[
self
::
PLUGIN_INFO_KEY_NAME
]
)
)
{
$require_plugins
[
$plugin
[
self
::
PLUGIN_INFO_KEY_NAME
]
]
=
$plugins
[
$plugin
[
self
::
PLUGIN_INFO_KEY_NAME
]
];
$require_plugins
[
$plugin
[
self
::
PLUGIN_INFO_KEY_NAME
]
][
'required_version'
]
=
$plugin
[
self
::
PLUGIN_INFO_VERSION
];
$plugin_file_name
=
$plugin
[
self
::
PLUGIN_INFO_KEY_NAME
];
$plugin_version
=
$plugin
[
self
::
PLUGIN_INFO_VERSION
];
if
(
self
::
is_wp_plugin_active
(
$plugin_file_name
)
)
{
$require_plugins
[
$plugin
[
$plugin_file_name
]
]
=
$plugins
[
$plugin_file_name
];
$require_plugins
[
$plugin
[
$plugin_file_name
]
][
self
::
PLUGIN_INFO_APPEND_PLUGIN_DATA
]
=
$plugin_version
;
}
}
}
...
...
@@ -422,7 +434,7 @@
$nice_name
=
$plugin_info
[
self
::
PLUGIN_INFO_KEY_NICE_NAME
];
if
(
!
self
::
is_wp_plugin_active
(
$name
)
)
{
if
(
!
self
::
is_wp_plugin_installed
(
$name
)
)
{
if
(
!
self
::
is_wp_plugin_installed
(
self
::
PLUGIN_INFO_IS_WP_PLUGIN_INSTALLED_TRANSIENT_NAME
,
$name
)
)
{
$install_url
=
$this
->
prepare_plugin_repository_install_url
(
$plugin_info
);
return
$this
->
prepare_notice_message
(
sprintf
(
wp_kses
(
__
(
'The “%s” plugin requires free %s plugin. <a href="%s">Install %s →</a>'
,
...
...
@@ -463,15 +475,17 @@
* Checks if plugin is installed. Needs to be enabled in deferred way.
*
* @param string $plugin_file
* @param string $transient_name
*
* @return bool
*/
public
static
function
is_wp_plugin_installed
(
$plugin_file
)
{
if
(
!
function_exists
(
'get_plugins'
)
)
{
require_once
ABSPATH
.
'/wp-admin/includes/plugin.php'
;
public
static
function
is_wp_plugin_installed
(
$transient_name
,
$plugin_file
)
{
if
(
isset
(
$plugin_file
)
)
{
return
self
::
retrieve_plugins_data_in_transient
(
$transient_name
,
$plugin_file
);
}
return
array_key_exists
(
$plugin_file
,
get_plugins
()
)
;
return
false
;
}
/**
...
...
@@ -584,9 +598,7 @@
echo
$notice
;
}
if
(
function_exists
(
'delete_transient'
)
)
{
delete_transient
(
self
::
PLUGIN_INFO_TRANSIENT_NAME
);
}
}
}
\ No newline at end of file
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/unit/Test_Basic_Requirement_Checker.php
+
11
−
2
View file @
ed63403c
...
...
@@ -15,11 +15,22 @@
const
HOOK_TYPE_ACTION
=
'action'
;
const
ALWAYS_FUNCTION_EXISTS
=
'function_exists'
;
public
function
setUp
()
{
WP_Mock
::
setUp
();
WP_Mock
::
wpFunction
(
'get_bloginfo'
)
->
andReturn
(
self
::
ALWAYS_VALID_WP_VERSION
);
WP_Mock
::
wpFunction
(
'get_transient'
)
->
andReturn
(
self
::
ALWAYS_FUNCTION_EXISTS
);
WP_Mock
::
wpFunction
(
'set_transient'
)
->
andReturn
(
self
::
ALWAYS_FUNCTION_EXISTS
);
WP_Mock
::
wpFunction
(
'delete_transient'
)
->
andReturn
(
self
::
ALWAYS_FUNCTION_EXISTS
);
}
public
function
tearDown
()
{
...
...
@@ -49,8 +60,6 @@
$requirements
->
handle_render_notices_action
();
}
/**
* @param string $php
* @param string $wp
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment