1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
<?php
namespace hji\ResponsiveIDX\utils;
/* Source : http://ottopress.com/2012/themeplugin-dependencies/
Simple class to let themes add dependencies on plugins in ways they might find useful
Example usage:
$test = new Theme_Plugin_Dependency( 'simple-facebook-connect', 'http://ottopress.com/wordpress-plugins/simple-facebook-connect/' );
if ( $test->check_active() )
echo 'SFC is installed and activated!';
else if ( $test->check() )
echo 'SFC is installed, but not activated. <a href="'.$test->activate_link().'">Click here to activate the plugin.</a>';
else if ( $install_link = $test->install_link() )
echo 'SFC is not installed. <a href="'.$install_link.'">Click here to install the plugin.</a>';
else
echo 'SFC is not installed and could not be found in the Plugin Directory. Please install this plugin manually.';
*/
class PluginDependency
{
// input information from the theme
// Plugin slug - most of the time part of the folder name
var $slug;
// Plugin file name - most of the time doesn't change
var $fileName;
// installed plugins and uris of them
private $plugins; // holds the list of plugins and their info
private $uris; // holds just the URIs for quick and easy searching
private $file; // full path to the file
// both slug and PluginURI are required for checking things
function __construct($slug, $fileName)
{
$this->slug = $slug;
$this->fileName = $fileName;
if (empty($this->plugins))
{
$this->plugins = get_plugins();
}
if (empty($this->uris))
{
$this->uris = wp_list_pluck($this->plugins, 'PluginURI');
}
}
/**
* @codeCoverageIgnore
* return true if installed, false if not
*/
function check()
{
_deprecated_function(__FUNCTION__, '3.0.6', 'PluginDependency::isInstalled');
return $this->isInstalled();
}
/**
* @codeCoverageIgnore
* return true if installed and activated, false if not
*/
function check_active()
{
_deprecated_function(__FUNCTION__, '3.0.6', 'PluginDependency::isActive');
return $this->isActive();
}
/**
* @codeCoverageIgnore
* gives a link to activate the plugin
*/
function activate_link()
{
_deprecated_function(__FUNCTION__, '3.0.6', 'PluginDependency::getActivateLink');
return $this->getActivateLink();
}
/**
* @codeCoverageIgnore
* return a nonced installation link for the plugin. checks wordpress.org to make sure it's there first.
*/
function install_link()
{
_deprecated_function(__FUNCTION__, '3.0.6', 'PluginDependency::getInstallLink');
return $this->getInstallLink();
}
/**
* return array key of plugin if installed, false if not, private because this isn't needed for themes, generally
*/
private function get_plugin_file()
{
if (!$this->file)
{
foreach ($this->uris as $k => $v)
{
if (stristr($k, $this->slug) && stristr($k, '/' . $this->fileName))
{
$this->file = $k;
break;
}
}
}
return $this->file;
}
/**
* Returns path to the plugin file relative to plugins directory
*
* @return int|string
*/
function getFile()
{
return $this->get_plugin_file();
}
/**
* Determines if plugin is active
*
* @return bool
*/
function isActive()
{
if ($this->getFile())
{
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
return is_plugin_active($this->getFile());
}
return false;
}
/**
* Determines if plugin is installed
*
* @return bool
*/
function isInstalled()
{
return ($this->getFile()) ? true : false;
}
/**
* Returns plugin installation link
*
* @return bool|string
*/
function getInstallLink()
{
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$info = plugins_api('plugin_information', array('slug' => $this->slug));
if (is_wp_error($info))
{
return false;
} // plugin not available from wordpress.org
return wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $this->slug), 'install-plugin_' . $this->slug);
}
/**
* Returns plugin activation link
*
* @return bool|string
*/
function getActivateLink()
{
$plugin_file = $this->getFile();
if ($plugin_file) return wp_nonce_url(self_admin_url('plugins.php?action=activate&plugin=' . $plugin_file), 'activate-plugin_' . $plugin_file);
return false;
}
}