class-tgm-plugin-activation.php 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853
  1. <?php
  2. /**
  3. * Plugin installation and activation for WordPress themes.
  4. *
  5. * Please note that this is a drop-in library for a theme or plugin.
  6. * The authors of this library (Thomas, Gary and Juliette) are NOT responsible
  7. * for the support of your plugin or theme. Please contact the plugin
  8. * or theme author for support.
  9. *
  10. * @package TGM-Plugin-Activation
  11. * @version 2.6.1
  12. * @link http://tgmpluginactivation.com/
  13. * @author Thomas Griffin, Gary Jones, Juliette Reinders Folmer
  14. * @copyright Copyright (c) 2011, Thomas Griffin
  15. * @license GPL-2.0+
  16. */
  17. /*
  18. Copyright 2011 Thomas Griffin (thomasgriffinmedia.com)
  19. This program is free software; you can redistribute it and/or modify
  20. it under the terms of the GNU General Public License, version 2, as
  21. published by the Free Software Foundation.
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. GNU General Public License for more details.
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. if ( ! class_exists( 'TGM_Plugin_Activation' ) ) {
  31. /**
  32. * Automatic plugin installation and activation library.
  33. *
  34. * Creates a way to automatically install and activate plugins from within themes.
  35. * The plugins can be either bundled, downloaded from the WordPress
  36. * Plugin Repository or downloaded from another external source.
  37. *
  38. * @since 1.0.0
  39. *
  40. * @package TGM-Plugin-Activation
  41. * @author Thomas Griffin
  42. * @author Gary Jones
  43. */
  44. class TGM_Plugin_Activation {
  45. /**
  46. * TGMPA version number.
  47. *
  48. * @since 2.5.0
  49. *
  50. * @const string Version number.
  51. */
  52. const TGMPA_VERSION = '2.6.1';
  53. /**
  54. * Regular expression to test if a URL is a WP plugin repo URL.
  55. *
  56. * @const string Regex.
  57. *
  58. * @since 2.5.0
  59. */
  60. const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|';
  61. /**
  62. * Arbitrary regular expression to test if a string starts with a URL.
  63. *
  64. * @const string Regex.
  65. *
  66. * @since 2.5.0
  67. */
  68. const IS_URL_REGEX = '|^http[s]?://|';
  69. /**
  70. * Holds a copy of itself, so it can be referenced by the class name.
  71. *
  72. * @since 1.0.0
  73. *
  74. * @var TGM_Plugin_Activation
  75. */
  76. public static $instance;
  77. /**
  78. * Holds arrays of plugin details.
  79. *
  80. * @since 1.0.0
  81. * @since 2.5.0 the array has the plugin slug as an associative key.
  82. *
  83. * @var array
  84. */
  85. public $plugins = array();
  86. /**
  87. * Holds arrays of plugin names to use to sort the plugins array.
  88. *
  89. * @since 2.5.0
  90. *
  91. * @var array
  92. */
  93. protected $sort_order = array();
  94. /**
  95. * Whether any plugins have the 'force_activation' setting set to true.
  96. *
  97. * @since 2.5.0
  98. *
  99. * @var bool
  100. */
  101. protected $has_forced_activation = false;
  102. /**
  103. * Whether any plugins have the 'force_deactivation' setting set to true.
  104. *
  105. * @since 2.5.0
  106. *
  107. * @var bool
  108. */
  109. protected $has_forced_deactivation = false;
  110. /**
  111. * Name of the unique ID to hash notices.
  112. *
  113. * @since 2.4.0
  114. *
  115. * @var string
  116. */
  117. public $id = 'tgmpa';
  118. /**
  119. * Name of the query-string argument for the admin page.
  120. *
  121. * @since 1.0.0
  122. *
  123. * @var string
  124. */
  125. protected $menu = 'tgmpa-install-plugins';
  126. /**
  127. * Parent menu file slug.
  128. *
  129. * @since 2.5.0
  130. *
  131. * @var string
  132. */
  133. public $parent_slug = 'themes.php';
  134. /**
  135. * Capability needed to view the plugin installation menu item.
  136. *
  137. * @since 2.5.0
  138. *
  139. * @var string
  140. */
  141. public $capability = 'edit_theme_options';
  142. /**
  143. * Default absolute path to folder containing bundled plugin zip files.
  144. *
  145. * @since 2.0.0
  146. *
  147. * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string.
  148. */
  149. public $default_path = '';
  150. /**
  151. * Flag to show admin notices or not.
  152. *
  153. * @since 2.1.0
  154. *
  155. * @var boolean
  156. */
  157. public $has_notices = true;
  158. /**
  159. * Flag to determine if the user can dismiss the notice nag.
  160. *
  161. * @since 2.4.0
  162. *
  163. * @var boolean
  164. */
  165. public $dismissable = true;
  166. /**
  167. * Message to be output above nag notice if dismissable is false.
  168. *
  169. * @since 2.4.0
  170. *
  171. * @var string
  172. */
  173. public $dismiss_msg = '';
  174. /**
  175. * Flag to set automatic activation of plugins. Off by default.
  176. *
  177. * @since 2.2.0
  178. *
  179. * @var boolean
  180. */
  181. public $is_automatic = false;
  182. /**
  183. * Optional message to display before the plugins table.
  184. *
  185. * @since 2.2.0
  186. *
  187. * @var string Message filtered by wp_kses_post(). Default is empty string.
  188. */
  189. public $message = '';
  190. /**
  191. * Holds configurable array of strings.
  192. *
  193. * Default values are added in the constructor.
  194. *
  195. * @since 2.0.0
  196. *
  197. * @var array
  198. */
  199. public $strings = array();
  200. /**
  201. * Holds the version of WordPress.
  202. *
  203. * @since 2.4.0
  204. *
  205. * @var int
  206. */
  207. public $wp_version;
  208. /**
  209. * Holds the hook name for the admin page.
  210. *
  211. * @since 2.5.0
  212. *
  213. * @var string
  214. */
  215. public $page_hook;
  216. /**
  217. * Adds a reference of this object to $instance, populates default strings,
  218. * does the tgmpa_init action hook, and hooks in the interactions to init.
  219. *
  220. * {@internal This method should be `protected`, but as too many TGMPA implementations
  221. * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues.
  222. * Reverted back to public for the time being.}}
  223. *
  224. * @since 1.0.0
  225. *
  226. * @see TGM_Plugin_Activation::init()
  227. */
  228. public function __construct() {
  229. // Set the current WordPress version.
  230. $this->wp_version = $GLOBALS['wp_version'];
  231. // Announce that the class is ready, and pass the object (for advanced use).
  232. do_action_ref_array( 'tgmpa_init', array( $this ) );
  233. /*
  234. * Load our text domain and allow for overloading the fall-back file.
  235. *
  236. * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA
  237. * generator on the website.}}
  238. */
  239. add_action( 'init', array( $this, 'load_textdomain' ), 5 );
  240. add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 );
  241. // When the rest of WP has loaded, kick-start the rest of the class.
  242. add_action( 'init', array( $this, 'init' ) );
  243. }
  244. /**
  245. * Magic method to (not) set protected properties from outside of this class.
  246. *
  247. * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6 where the `menu` property
  248. * is being assigned rather than tested in a conditional, effectively rendering it useless.
  249. * This 'hack' prevents this from happening.}}
  250. *
  251. * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593
  252. *
  253. * @since 2.5.2
  254. *
  255. * @param string $name Name of an inaccessible property.
  256. * @param mixed $value Value to assign to the property.
  257. * @return void Silently fail to set the property when this is tried from outside of this class context.
  258. * (Inside this class context, the __set() method if not used as there is direct access.)
  259. */
  260. public function __set( $name, $value ) {
  261. return;
  262. }
  263. /**
  264. * Magic method to get the value of a protected property outside of this class context.
  265. *
  266. * @since 2.5.2
  267. *
  268. * @param string $name Name of an inaccessible property.
  269. * @return mixed The property value.
  270. */
  271. public function __get( $name ) {
  272. return $this->{$name};
  273. }
  274. /**
  275. * Initialise the interactions between this class and WordPress.
  276. *
  277. * Hooks in three new methods for the class: admin_menu, notices and styles.
  278. *
  279. * @since 2.0.0
  280. *
  281. * @see TGM_Plugin_Activation::admin_menu()
  282. * @see TGM_Plugin_Activation::notices()
  283. * @see TGM_Plugin_Activation::styles()
  284. */
  285. public function init() {
  286. /**
  287. * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter
  288. * you can overrule that behaviour.
  289. *
  290. * @since 2.5.0
  291. *
  292. * @param bool $load Whether or not TGMPA should load.
  293. * Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`.
  294. */
  295. if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) {
  296. return;
  297. }
  298. // Load class strings.
  299. $this->strings = array(
  300. 'page_title' => __( 'Install Required Plugins', 'tgmpa' ),
  301. 'menu_title' => __( 'Install Plugins', 'tgmpa' ),
  302. /* translators: %s: plugin name. */
  303. 'installing' => __( 'Installing Plugin: %s', 'tgmpa' ),
  304. /* translators: %s: plugin name. */
  305. 'updating' => __( 'Updating Plugin: %s', 'tgmpa' ),
  306. 'oops' => __( 'Something went wrong with the plugin API.', 'tgmpa' ),
  307. 'notice_can_install_required' => _n_noop(
  308. /* translators: 1: plugin name(s). */
  309. 'This theme requires the following plugin: %1$s.',
  310. 'This theme requires the following plugins: %1$s.',
  311. 'tgmpa'
  312. ),
  313. 'notice_can_install_recommended' => _n_noop(
  314. /* translators: 1: plugin name(s). */
  315. 'This theme recommends the following plugin: %1$s.',
  316. 'This theme recommends the following plugins: %1$s.',
  317. 'tgmpa'
  318. ),
  319. 'notice_ask_to_update' => _n_noop(
  320. /* translators: 1: plugin name(s). */
  321. 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.',
  322. 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.',
  323. 'tgmpa'
  324. ),
  325. 'notice_ask_to_update_maybe' => _n_noop(
  326. /* translators: 1: plugin name(s). */
  327. 'There is an update available for: %1$s.',
  328. 'There are updates available for the following plugins: %1$s.',
  329. 'tgmpa'
  330. ),
  331. 'notice_can_activate_required' => _n_noop(
  332. /* translators: 1: plugin name(s). */
  333. 'The following required plugin is currently inactive: %1$s.',
  334. 'The following required plugins are currently inactive: %1$s.',
  335. 'tgmpa'
  336. ),
  337. 'notice_can_activate_recommended' => _n_noop(
  338. /* translators: 1: plugin name(s). */
  339. 'The following recommended plugin is currently inactive: %1$s.',
  340. 'The following recommended plugins are currently inactive: %1$s.',
  341. 'tgmpa'
  342. ),
  343. 'install_link' => _n_noop(
  344. 'Begin installing plugin',
  345. 'Begin installing plugins',
  346. 'tgmpa'
  347. ),
  348. 'update_link' => _n_noop(
  349. 'Begin updating plugin',
  350. 'Begin updating plugins',
  351. 'tgmpa'
  352. ),
  353. 'activate_link' => _n_noop(
  354. 'Begin activating plugin',
  355. 'Begin activating plugins',
  356. 'tgmpa'
  357. ),
  358. 'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ),
  359. 'dashboard' => __( 'Return to the Dashboard', 'tgmpa' ),
  360. 'plugin_activated' => __( 'Plugin activated successfully.', 'tgmpa' ),
  361. 'activated_successfully' => __( 'The following plugin was activated successfully:', 'tgmpa' ),
  362. /* translators: 1: plugin name. */
  363. 'plugin_already_active' => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ),
  364. /* translators: 1: plugin name. */
  365. 'plugin_needs_higher_version' => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ),
  366. /* translators: 1: dashboard link. */
  367. 'complete' => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ),
  368. 'dismiss' => __( 'Dismiss this notice', 'tgmpa' ),
  369. 'notice_cannot_install_activate' => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ),
  370. 'contact_admin' => __( 'Please contact the administrator of this site for help.', 'tgmpa' ),
  371. );
  372. do_action( 'tgmpa_register' );
  373. /* After this point, the plugins should be registered and the configuration set. */
  374. // Proceed only if we have plugins to handle.
  375. if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
  376. return;
  377. }
  378. // Set up the menu and notices if we still have outstanding actions.
  379. if ( true !== $this->is_tgmpa_complete() ) {
  380. // Sort the plugins.
  381. array_multisort( $this->sort_order, SORT_ASC, $this->plugins );
  382. add_action( 'admin_menu', array( $this, 'admin_menu' ) );
  383. add_action( 'admin_head', array( $this, 'dismiss' ) );
  384. // Prevent the normal links from showing underneath a single install/update page.
  385. add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) );
  386. add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) );
  387. if ( $this->has_notices ) {
  388. add_action( 'admin_notices', array( $this, 'notices' ) );
  389. add_action( 'admin_init', array( $this, 'admin_init' ), 1 );
  390. add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) );
  391. }
  392. }
  393. // If needed, filter plugin action links.
  394. add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 );
  395. // Make sure things get reset on switch theme.
  396. add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) );
  397. if ( $this->has_notices ) {
  398. add_action( 'switch_theme', array( $this, 'update_dismiss' ) );
  399. }
  400. // Setup the force activation hook.
  401. if ( true === $this->has_forced_activation ) {
  402. add_action( 'admin_init', array( $this, 'force_activation' ) );
  403. }
  404. // Setup the force deactivation hook.
  405. if ( true === $this->has_forced_deactivation ) {
  406. add_action( 'switch_theme', array( $this, 'force_deactivation' ) );
  407. }
  408. }
  409. /**
  410. * Load translations.
  411. *
  412. * @since 2.6.0
  413. *
  414. * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to
  415. * get round the different ways of handling the path and deprecated notices being thrown
  416. * and such. For plugins, the actual file name will be corrected by a filter.}}
  417. *
  418. * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
  419. * generator on the website.}}
  420. */
  421. public function load_textdomain() {
  422. if ( is_textdomain_loaded( 'tgmpa' ) ) {
  423. return;
  424. }
  425. if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) {
  426. // Plugin, we'll need to adjust the file name.
  427. add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 );
  428. load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
  429. remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 );
  430. } else {
  431. load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' );
  432. }
  433. }
  434. /**
  435. * Correct the .mo file name for (must-use) plugins.
  436. *
  437. * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`.
  438. *
  439. * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
  440. * generator on the website.}}
  441. *
  442. * @since 2.6.0
  443. *
  444. * @param string $mofile Full path to the target mofile.
  445. * @param string $domain The domain for which a language file is being loaded.
  446. * @return string $mofile
  447. */
  448. public function correct_plugin_mofile( $mofile, $domain ) {
  449. // Exit early if not our domain (just in case).
  450. if ( 'tgmpa' !== $domain ) {
  451. return $mofile;
  452. }
  453. return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile );
  454. }
  455. /**
  456. * Potentially overload the fall-back translation file for the current language.
  457. *
  458. * WP, by default since WP 3.7, will load a local translation first and if none
  459. * can be found, will try and find a translation in the /wp-content/languages/ directory.
  460. * As this library is theme/plugin agnostic, translation files for TGMPA can exist both
  461. * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory.
  462. *
  463. * This method makes sure both directories are checked.
  464. *
  465. * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
  466. * generator on the website.}}
  467. *
  468. * @since 2.6.0
  469. *
  470. * @param string $mofile Full path to the target mofile.
  471. * @param string $domain The domain for which a language file is being loaded.
  472. * @return string $mofile
  473. */
  474. public function overload_textdomain_mofile( $mofile, $domain ) {
  475. // Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable.
  476. if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) {
  477. return $mofile;
  478. }
  479. // Current fallback file is not valid, let's try the alternative option.
  480. if ( false !== strpos( $mofile, '/themes/' ) ) {
  481. return str_replace( '/themes/', '/plugins/', $mofile );
  482. } elseif ( false !== strpos( $mofile, '/plugins/' ) ) {
  483. return str_replace( '/plugins/', '/themes/', $mofile );
  484. } else {
  485. return $mofile;
  486. }
  487. }
  488. /**
  489. * Hook in plugin action link filters for the WP native plugins page.
  490. *
  491. * - Prevent activation of plugins which don't meet the minimum version requirements.
  492. * - Prevent deactivation of force-activated plugins.
  493. * - Add update notice if update available.
  494. *
  495. * @since 2.5.0
  496. */
  497. public function add_plugin_action_link_filters() {
  498. foreach ( $this->plugins as $slug => $plugin ) {
  499. if ( false === $this->can_plugin_activate( $slug ) ) {
  500. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 );
  501. }
  502. if ( true === $plugin['force_activation'] ) {
  503. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 );
  504. }
  505. if ( false !== $this->does_plugin_require_update( $slug ) ) {
  506. add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 );
  507. }
  508. }
  509. }
  510. /**
  511. * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the
  512. * minimum version requirements.
  513. *
  514. * @since 2.5.0
  515. *
  516. * @param array $actions Action links.
  517. * @return array
  518. */
  519. public function filter_plugin_action_links_activate( $actions ) {
  520. unset( $actions['activate'] );
  521. return $actions;
  522. }
  523. /**
  524. * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate.
  525. *
  526. * @since 2.5.0
  527. *
  528. * @param array $actions Action links.
  529. * @return array
  530. */
  531. public function filter_plugin_action_links_deactivate( $actions ) {
  532. unset( $actions['deactivate'] );
  533. return $actions;
  534. }
  535. /**
  536. * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the
  537. * minimum version requirements.
  538. *
  539. * @since 2.5.0
  540. *
  541. * @param array $actions Action links.
  542. * @return array
  543. */
  544. public function filter_plugin_action_links_update( $actions ) {
  545. $actions['update'] = sprintf(
  546. '<a href="%1$s" title="%2$s" class="edit">%3$s</a>',
  547. esc_url( $this->get_tgmpa_status_url( 'update' ) ),
  548. esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ),
  549. esc_html__( 'Update Required', 'tgmpa' )
  550. );
  551. return $actions;
  552. }
  553. /**
  554. * Handles calls to show plugin information via links in the notices.
  555. *
  556. * We get the links in the admin notices to point to the TGMPA page, rather
  557. * than the typical plugin-install.php file, so we can prepare everything
  558. * beforehand.
  559. *
  560. * WP does not make it easy to show the plugin information in the thickbox -
  561. * here we have to require a file that includes a function that does the
  562. * main work of displaying it, enqueue some styles, set up some globals and
  563. * finally call that function before exiting.
  564. *
  565. * Down right easy once you know how...
  566. *
  567. * Returns early if not the TGMPA page.
  568. *
  569. * @since 2.1.0
  570. *
  571. * @global string $tab Used as iframe div class names, helps with styling
  572. * @global string $body_id Used as the iframe body ID, helps with styling
  573. *
  574. * @return null Returns early if not the TGMPA page.
  575. */
  576. public function admin_init() {
  577. if ( ! $this->is_tgmpa_page() ) {
  578. return;
  579. }
  580. if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) {
  581. // Needed for install_plugin_information().
  582. require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
  583. wp_enqueue_style( 'plugin-install' );
  584. global $tab, $body_id;
  585. $body_id = 'plugin-information';
  586. // @codingStandardsIgnoreStart
  587. $tab = 'plugin-information';
  588. // @codingStandardsIgnoreEnd
  589. install_plugin_information();
  590. exit;
  591. }
  592. }
  593. /**
  594. * Enqueue thickbox scripts/styles for plugin info.
  595. *
  596. * Thickbox is not automatically included on all admin pages, so we must
  597. * manually enqueue it for those pages.
  598. *
  599. * Thickbox is only loaded if the user has not dismissed the admin
  600. * notice or if there are any plugins left to install and activate.
  601. *
  602. * @since 2.1.0
  603. */
  604. public function thickbox() {
  605. if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) {
  606. add_thickbox();
  607. }
  608. }
  609. /**
  610. * Adds submenu page if there are plugin actions to take.
  611. *
  612. * This method adds the submenu page letting users know that a required
  613. * plugin needs to be installed.
  614. *
  615. * This page disappears once the plugin has been installed and activated.
  616. *
  617. * @since 1.0.0
  618. *
  619. * @see TGM_Plugin_Activation::init()
  620. * @see TGM_Plugin_Activation::install_plugins_page()
  621. *
  622. * @return null Return early if user lacks capability to install a plugin.
  623. */
  624. public function admin_menu() {
  625. // Make sure privileges are correct to see the page.
  626. if ( ! current_user_can( 'install_plugins' ) ) {
  627. return;
  628. }
  629. $args = apply_filters(
  630. 'tgmpa_admin_menu_args',
  631. array(
  632. 'parent_slug' => $this->parent_slug, // Parent Menu slug.
  633. 'page_title' => $this->strings['page_title'], // Page title.
  634. 'menu_title' => $this->strings['menu_title'], // Menu title.
  635. 'capability' => $this->capability, // Capability.
  636. 'menu_slug' => $this->menu, // Menu slug.
  637. 'function' => array( $this, 'install_plugins_page' ), // Callback.
  638. )
  639. );
  640. $this->add_admin_menu( $args );
  641. }
  642. /**
  643. * Add the menu item.
  644. *
  645. * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA
  646. * generator on the website.}}
  647. *
  648. * @since 2.5.0
  649. *
  650. * @param array $args Menu item configuration.
  651. */
  652. protected function add_admin_menu( array $args ) {
  653. if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) {
  654. _deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) );
  655. }
  656. if ( 'themes.php' === $this->parent_slug ) {
  657. $this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
  658. } else {
  659. $this->page_hook = call_user_func( 'add_submenu_page', $args['parent_slug'], $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
  660. }
  661. }
  662. /**
  663. * Echoes plugin installation form.
  664. *
  665. * This method is the callback for the admin_menu method function.
  666. * This displays the admin page and form area where the user can select to install and activate the plugin.
  667. * Aborts early if we're processing a plugin installation action.
  668. *
  669. * @since 1.0.0
  670. *
  671. * @return null Aborts early if we're processing a plugin installation action.
  672. */
  673. public function install_plugins_page() {
  674. // Store new instance of plugin table in object.
  675. $plugin_table = new TGMPA_List_Table;
  676. // Return early if processing a plugin installation action.
  677. if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) {
  678. return;
  679. }
  680. // Force refresh of available plugin information so we'll know about manual updates/deletes.
  681. wp_clean_plugins_cache( false );
  682. ?>
  683. <div class="tgmpa wrap">
  684. <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
  685. <?php $plugin_table->prepare_items(); ?>
  686. <?php
  687. if ( ! empty( $this->message ) && is_string( $this->message ) ) {
  688. echo wp_kses_post( $this->message );
  689. }
  690. ?>
  691. <?php $plugin_table->views(); ?>
  692. <form id="tgmpa-plugins" action="" method="post">
  693. <input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" />
  694. <input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" />
  695. <?php $plugin_table->display(); ?>
  696. </form>
  697. </div>
  698. <?php
  699. }
  700. /**
  701. * Installs, updates or activates a plugin depending on the action link clicked by the user.
  702. *
  703. * Checks the $_GET variable to see which actions have been
  704. * passed and responds with the appropriate method.
  705. *
  706. * Uses WP_Filesystem to process and handle the plugin installation
  707. * method.
  708. *
  709. * @since 1.0.0
  710. *
  711. * @uses WP_Filesystem
  712. * @uses WP_Error
  713. * @uses WP_Upgrader
  714. * @uses Plugin_Upgrader
  715. * @uses Plugin_Installer_Skin
  716. * @uses Plugin_Upgrader_Skin
  717. *
  718. * @return boolean True on success, false on failure.
  719. */
  720. protected function do_plugin_install() {
  721. if ( empty( $_GET['plugin'] ) ) {
  722. return false;
  723. }
  724. // All plugin information will be stored in an array for processing.
  725. $slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) );
  726. if ( ! isset( $this->plugins[ $slug ] ) ) {
  727. return false;
  728. }
  729. // Was an install or upgrade action link clicked?
  730. if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) {
  731. $install_type = 'install';
  732. if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) {
  733. $install_type = 'update';
  734. }
  735. check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' );
  736. // Pass necessary information via URL if WP_Filesystem is needed.
  737. $url = wp_nonce_url(
  738. add_query_arg(
  739. array(
  740. 'plugin' => urlencode( $slug ),
  741. 'tgmpa-' . $install_type => $install_type . '-plugin',
  742. ),
  743. $this->get_tgmpa_url()
  744. ),
  745. 'tgmpa-' . $install_type,
  746. 'tgmpa-nonce'
  747. );
  748. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
  749. if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) {
  750. return true;
  751. }
  752. if ( ! WP_Filesystem( $creds ) ) {
  753. request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem.
  754. return true;
  755. }
  756. /* If we arrive here, we have the filesystem. */
  757. // Prep variables for Plugin_Installer_Skin class.
  758. $extra = array();
  759. $extra['slug'] = $slug; // Needed for potentially renaming of directory name.
  760. $source = $this->get_download_url( $slug );
  761. $api = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null;
  762. $api = ( false !== $api ) ? $api : null;
  763. $url = add_query_arg(
  764. array(
  765. 'action' => $install_type . '-plugin',
  766. 'plugin' => urlencode( $slug ),
  767. ),
  768. 'update.php'
  769. );
  770. if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
  771. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  772. }
  773. $title = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing'];
  774. $skin_args = array(
  775. 'type' => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload',
  776. 'title' => sprintf( $title, $this->plugins[ $slug ]['name'] ),
  777. 'url' => esc_url_raw( $url ),
  778. 'nonce' => $install_type . '-plugin_' . $slug,
  779. 'plugin' => '',
  780. 'api' => $api,
  781. 'extra' => $extra,
  782. );
  783. unset( $title );
  784. if ( 'update' === $install_type ) {
  785. $skin_args['plugin'] = $this->plugins[ $slug ]['file_path'];
  786. $skin = new Plugin_Upgrader_Skin( $skin_args );
  787. } else {
  788. $skin = new Plugin_Installer_Skin( $skin_args );
  789. }
  790. // Create a new instance of Plugin_Upgrader.
  791. $upgrader = new Plugin_Upgrader( $skin );
  792. // Perform the action and install the plugin from the $source urldecode().
  793. add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 );
  794. if ( 'update' === $install_type ) {
  795. // Inject our info into the update transient.
  796. $to_inject = array( $slug => $this->plugins[ $slug ] );
  797. $to_inject[ $slug ]['source'] = $source;
  798. $this->inject_update_info( $to_inject );
  799. $upgrader->upgrade( $this->plugins[ $slug ]['file_path'] );
  800. } else {
  801. $upgrader->install( $source );
  802. }
  803. remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 );
  804. // Make sure we have the correct file path now the plugin is installed/updated.
  805. $this->populate_file_path( $slug );
  806. // Only activate plugins if the config option is set to true and the plugin isn't
  807. // already active (upgrade).
  808. if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) {
  809. $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method.
  810. if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) {
  811. return true; // Finish execution of the function early as we encountered an error.
  812. }
  813. }
  814. $this->show_tgmpa_version();
  815. // Display message based on if all plugins are now active or not.
  816. if ( $this->is_tgmpa_complete() ) {
  817. echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ), '</p>';
  818. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  819. } else {
  820. echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
  821. }
  822. return true;
  823. } elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) {
  824. // Activate action link was clicked.
  825. check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' );
  826. if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) {
  827. return true; // Finish execution of the function early as we encountered an error.
  828. }
  829. }
  830. return false;
  831. }
  832. /**
  833. * Inject information into the 'update_plugins' site transient as WP checks that before running an update.
  834. *
  835. * @since 2.5.0
  836. *
  837. * @param array $plugins The plugin information for the plugins which are to be updated.
  838. */
  839. public function inject_update_info( $plugins ) {
  840. $repo_updates = get_site_transient( 'update_plugins' );
  841. if ( ! is_object( $repo_updates ) ) {
  842. $repo_updates = new stdClass;
  843. }
  844. foreach ( $plugins as $slug => $plugin ) {
  845. $file_path = $plugin['file_path'];
  846. if ( empty( $repo_updates->response[ $file_path ] ) ) {
  847. $repo_updates->response[ $file_path ] = new stdClass;
  848. }
  849. // We only really need to set package, but let's do all we can in case WP changes something.
  850. $repo_updates->response[ $file_path ]->slug = $slug;
  851. $repo_updates->response[ $file_path ]->plugin = $file_path;
  852. $repo_updates->response[ $file_path ]->new_version = $plugin['version'];
  853. $repo_updates->response[ $file_path ]->package = $plugin['source'];
  854. if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) {
  855. $repo_updates->response[ $file_path ]->url = $plugin['external_url'];
  856. }
  857. }
  858. set_site_transient( 'update_plugins', $repo_updates );
  859. }
  860. /**
  861. * Adjust the plugin directory name if necessary.
  862. *
  863. * The final destination directory of a plugin is based on the subdirectory name found in the
  864. * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this
  865. * subdirectory name is not the same as the expected slug and the plugin will not be recognized
  866. * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to
  867. * the expected plugin slug.
  868. *
  869. * @since 2.5.0
  870. *
  871. * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/.
  872. * @param string $remote_source Path to upgrade/zip-file-name.tmp.
  873. * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin.
  874. * @return string $source
  875. */
  876. public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) {
  877. if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) {
  878. return $source;
  879. }
  880. // Check for single file plugins.
  881. $source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) );
  882. if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) {
  883. return $source;
  884. }
  885. // Multi-file plugin, let's see if the directory is correctly named.
  886. $desired_slug = '';
  887. // Figure out what the slug is supposed to be.
  888. if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) {
  889. $desired_slug = $upgrader->skin->options['extra']['slug'];
  890. } else {
  891. // Bulk installer contains less info, so fall back on the info registered here.
  892. foreach ( $this->plugins as $slug => $plugin ) {
  893. if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) {
  894. $desired_slug = $slug;
  895. break;
  896. }
  897. }
  898. unset( $slug, $plugin );
  899. }
  900. if ( ! empty( $desired_slug ) ) {
  901. $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) );
  902. if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) {
  903. $from_path = untrailingslashit( $source );
  904. $to_path = trailingslashit( $remote_source ) . $desired_slug;
  905. if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) {
  906. return trailingslashit( $to_path );
  907. } else {
  908. return new WP_Error( 'rename_failed', esc_html__( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) );
  909. }
  910. } elseif ( empty( $subdir_name ) ) {
  911. return new WP_Error( 'packaged_wrong', esc_html__( 'The remote plugin package consists of more than one file, but the files are not packaged in a folder.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) );
  912. }
  913. }
  914. return $source;
  915. }
  916. /**
  917. * Activate a single plugin and send feedback about the result to the screen.
  918. *
  919. * @since 2.5.0
  920. *
  921. * @param string $file_path Path within wp-plugins/ to main plugin file.
  922. * @param string $slug Plugin slug.
  923. * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false.
  924. * This determines the styling of the output messages.
  925. * @return bool False if an error was encountered, true otherwise.
  926. */
  927. protected function activate_single_plugin( $file_path, $slug, $automatic = false ) {
  928. if ( $this->can_plugin_activate( $slug ) ) {
  929. $activate = activate_plugin( $file_path );
  930. if ( is_wp_error( $activate ) ) {
  931. echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>',
  932. '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>';
  933. return false; // End it here if there is an error with activation.
  934. } else {
  935. if ( ! $automatic ) {
  936. // Make sure message doesn't display again if bulk activation is performed
  937. // immediately after a single activation.
  938. if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
  939. echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>';
  940. }
  941. } else {
  942. // Simpler message layout for use on the plugin install page.
  943. echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>';
  944. }
  945. }
  946. } elseif ( $this->is_plugin_active( $slug ) ) {
  947. // No simpler message format provided as this message should never be encountered
  948. // on the plugin install page.
  949. echo '<div id="message" class="error"><p>',
  950. sprintf(
  951. esc_html( $this->strings['plugin_already_active'] ),
  952. '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
  953. ),
  954. '</p></div>';
  955. } elseif ( $this->does_plugin_require_update( $slug ) ) {
  956. if ( ! $automatic ) {
  957. // Make sure message doesn't display again if bulk activation is performed
  958. // immediately after a single activation.
  959. if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK.
  960. echo '<div id="message" class="error"><p>',
  961. sprintf(
  962. esc_html( $this->strings['plugin_needs_higher_version'] ),
  963. '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>'
  964. ),
  965. '</p></div>';
  966. }
  967. } else {
  968. // Simpler message layout for use on the plugin install page.
  969. echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>';
  970. }
  971. }
  972. return true;
  973. }
  974. /**
  975. * Echoes required plugin notice.
  976. *
  977. * Outputs a message telling users that a specific plugin is required for
  978. * their theme. If appropriate, it includes a link to the form page where
  979. * users can install and activate the plugin.
  980. *
  981. * Returns early if we're on the Install page.
  982. *
  983. * @since 1.0.0
  984. *
  985. * @global object $current_screen
  986. *
  987. * @return null Returns early if we're on the Install page.
  988. */
  989. public function notices() {
  990. // Remove nag on the install page / Return early if the nag message has been dismissed or user < author.
  991. if ( ( $this->is_tgmpa_page() || $this->is_core_update_page() ) || get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) || ! current_user_can( apply_filters( 'tgmpa_show_admin_notice_capability', 'publish_posts' ) ) ) {
  992. return;
  993. }
  994. // Store for the plugin slugs by message type.
  995. $message = array();
  996. // Initialize counters used to determine plurality of action link texts.
  997. $install_link_count = 0;
  998. $update_link_count = 0;
  999. $activate_link_count = 0;
  1000. $total_required_action_count = 0;
  1001. foreach ( $this->plugins as $slug => $plugin ) {
  1002. if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) {
  1003. continue;
  1004. }
  1005. if ( ! $this->is_plugin_installed( $slug ) ) {
  1006. if ( current_user_can( 'install_plugins' ) ) {
  1007. $install_link_count++;
  1008. if ( true === $plugin['required'] ) {
  1009. $message['notice_can_install_required'][] = $slug;
  1010. } else {
  1011. $message['notice_can_install_recommended'][] = $slug;
  1012. }
  1013. }
  1014. if ( true === $plugin['required'] ) {
  1015. $total_required_action_count++;
  1016. }
  1017. } else {
  1018. if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) {
  1019. if ( current_user_can( 'activate_plugins' ) ) {
  1020. $activate_link_count++;
  1021. if ( true === $plugin['required'] ) {
  1022. $message['notice_can_activate_required'][] = $slug;
  1023. } else {
  1024. $message['notice_can_activate_recommended'][] = $slug;
  1025. }
  1026. }
  1027. if ( true === $plugin['required'] ) {
  1028. $total_required_action_count++;
  1029. }
  1030. }
  1031. if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
  1032. if ( current_user_can( 'update_plugins' ) ) {
  1033. $update_link_count++;
  1034. if ( $this->does_plugin_require_update( $slug ) ) {
  1035. $message['notice_ask_to_update'][] = $slug;
  1036. } elseif ( false !== $this->does_plugin_have_update( $slug ) ) {
  1037. $message['notice_ask_to_update_maybe'][] = $slug;
  1038. }
  1039. }
  1040. if ( true === $plugin['required'] ) {
  1041. $total_required_action_count++;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. unset( $slug, $plugin );
  1047. // If we have notices to display, we move forward.
  1048. if ( ! empty( $message ) || $total_required_action_count > 0 ) {
  1049. krsort( $message ); // Sort messages.
  1050. $rendered = '';
  1051. // As add_settings_error() wraps the final message in a <p> and as the final message can't be
  1052. // filtered, using <p>'s in our html would render invalid html output.
  1053. $line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n";
  1054. if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
  1055. $rendered = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] );
  1056. $rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template );
  1057. } else {
  1058. // If dismissable is false and a message is set, output it now.
  1059. if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) {
  1060. $rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) );
  1061. }
  1062. // Render the individual message lines for the notice.
  1063. foreach ( $message as $type => $plugin_group ) {
  1064. $linked_plugins = array();
  1065. // Get the external info link for a plugin if one is available.
  1066. foreach ( $plugin_group as $plugin_slug ) {
  1067. $linked_plugins[] = $this->get_info_link( $plugin_slug );
  1068. }
  1069. unset( $plugin_slug );
  1070. $count = count( $plugin_group );
  1071. $linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins );
  1072. $last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability.
  1073. $imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
  1074. $rendered .= sprintf(
  1075. $line_template,
  1076. sprintf(
  1077. translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ),
  1078. $imploded,
  1079. $count
  1080. )
  1081. );
  1082. }
  1083. unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded );
  1084. $rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template );
  1085. }
  1086. // Register the nag messages and prepare them to be processed.
  1087. add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() );
  1088. }
  1089. // Admin options pages already output settings_errors, so this is to avoid duplication.
  1090. if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) {
  1091. $this->display_settings_errors();
  1092. }
  1093. }
  1094. /**
  1095. * Generate the user action links for the admin notice.
  1096. *
  1097. * @since 2.6.0
  1098. *
  1099. * @param int $install_count Number of plugins to install.
  1100. * @param int $update_count Number of plugins to update.
  1101. * @param int $activate_count Number of plugins to activate.
  1102. * @param int $line_template Template for the HTML tag to output a line.
  1103. * @return string Action links.
  1104. */
  1105. protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) {
  1106. // Setup action links.
  1107. $action_links = array(
  1108. 'install' => '',
  1109. 'update' => '',
  1110. 'activate' => '',
  1111. 'dismiss' => $this->dismissable ? '<a href="' . esc_url( wp_nonce_url( add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ), 'tgmpa-dismiss-' . get_current_user_id() ) ) . '" class="dismiss-notice" target="_parent">' . esc_html( $this->strings['dismiss'] ) . '</a>' : '',
  1112. );
  1113. $link_template = '<a href="%2$s">%1$s</a>';
  1114. if ( current_user_can( 'install_plugins' ) ) {
  1115. if ( $install_count > 0 ) {
  1116. $action_links['install'] = sprintf(
  1117. $link_template,
  1118. translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ),
  1119. esc_url( $this->get_tgmpa_status_url( 'install' ) )
  1120. );
  1121. }
  1122. if ( $update_count > 0 ) {
  1123. $action_links['update'] = sprintf(
  1124. $link_template,
  1125. translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ),
  1126. esc_url( $this->get_tgmpa_status_url( 'update' ) )
  1127. );
  1128. }
  1129. }
  1130. if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) {
  1131. $action_links['activate'] = sprintf(
  1132. $link_template,
  1133. translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ),
  1134. esc_url( $this->get_tgmpa_status_url( 'activate' ) )
  1135. );
  1136. }
  1137. $action_links = apply_filters( 'tgmpa_notice_action_links', $action_links );
  1138. $action_links = array_filter( (array) $action_links ); // Remove any empty array items.
  1139. if ( ! empty( $action_links ) ) {
  1140. $action_links = sprintf( $line_template, implode( ' | ', $action_links ) );
  1141. return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links );
  1142. } else {
  1143. return '';
  1144. }
  1145. }
  1146. /**
  1147. * Get admin notice class.
  1148. *
  1149. * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7
  1150. * (lowest supported version by TGMPA).
  1151. *
  1152. * @since 2.6.0
  1153. *
  1154. * @return string
  1155. */
  1156. protected function get_admin_notice_class() {
  1157. if ( ! empty( $this->strings['nag_type'] ) ) {
  1158. return sanitize_html_class( strtolower( $this->strings['nag_type'] ) );
  1159. } else {
  1160. if ( version_compare( $this->wp_version, '4.2', '>=' ) ) {
  1161. return 'notice-warning';
  1162. } elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) {
  1163. return 'notice';
  1164. } else {
  1165. return 'updated';
  1166. }
  1167. }
  1168. }
  1169. /**
  1170. * Display settings errors and remove those which have been displayed to avoid duplicate messages showing
  1171. *
  1172. * @since 2.5.0
  1173. */
  1174. protected function display_settings_errors() {
  1175. global $wp_settings_errors;
  1176. settings_errors( 'tgmpa' );
  1177. foreach ( (array) $wp_settings_errors as $key => $details ) {
  1178. if ( 'tgmpa' === $details['setting'] ) {
  1179. unset( $wp_settings_errors[ $key ] );
  1180. break;
  1181. }
  1182. }
  1183. }
  1184. /**
  1185. * Register dismissal of admin notices.
  1186. *
  1187. * Acts on the dismiss link in the admin nag messages.
  1188. * If clicked, the admin notice disappears and will no longer be visible to this user.
  1189. *
  1190. * @since 2.1.0
  1191. */
  1192. public function dismiss() {
  1193. if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) {
  1194. update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 );
  1195. }
  1196. }
  1197. /**
  1198. * Add individual plugin to our collection of plugins.
  1199. *
  1200. * If the required keys are not set or the plugin has already
  1201. * been registered, the plugin is not added.
  1202. *
  1203. * @since 2.0.0
  1204. *
  1205. * @param array|null $plugin Array of plugin arguments or null if invalid argument.
  1206. * @return null Return early if incorrect argument.
  1207. */
  1208. public function register( $plugin ) {
  1209. if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) {
  1210. return;
  1211. }
  1212. if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) {
  1213. return;
  1214. }
  1215. $defaults = array(
  1216. 'name' => '', // String
  1217. 'slug' => '', // String
  1218. 'source' => 'repo', // String
  1219. 'required' => false, // Boolean
  1220. 'version' => '', // String
  1221. 'force_activation' => false, // Boolean
  1222. 'force_deactivation' => false, // Boolean
  1223. 'external_url' => '', // String
  1224. 'is_callable' => '', // String|Array.
  1225. );
  1226. // Prepare the received data.
  1227. $plugin = wp_parse_args( $plugin, $defaults );
  1228. // Standardize the received slug.
  1229. $plugin['slug'] = $this->sanitize_key( $plugin['slug'] );
  1230. // Forgive users for using string versions of booleans or floats for version number.
  1231. $plugin['version'] = (string) $plugin['version'];
  1232. $plugin['source'] = empty( $plugin['source'] ) ? 'repo' : $plugin['source'];
  1233. $plugin['required'] = TGMPA_Utils::validate_bool( $plugin['required'] );
  1234. $plugin['force_activation'] = TGMPA_Utils::validate_bool( $plugin['force_activation'] );
  1235. $plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] );
  1236. // Enrich the received data.
  1237. $plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] );
  1238. $plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] );
  1239. // Set the class properties.
  1240. $this->plugins[ $plugin['slug'] ] = $plugin;
  1241. $this->sort_order[ $plugin['slug'] ] = $plugin['name'];
  1242. // Should we add the force activation hook ?
  1243. if ( true === $plugin['force_activation'] ) {
  1244. $this->has_forced_activation = true;
  1245. }
  1246. // Should we add the force deactivation hook ?
  1247. if ( true === $plugin['force_deactivation'] ) {
  1248. $this->has_forced_deactivation = true;
  1249. }
  1250. }
  1251. /**
  1252. * Determine what type of source the plugin comes from.
  1253. *
  1254. * @since 2.5.0
  1255. *
  1256. * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path
  1257. * (= bundled) or an external URL.
  1258. * @return string 'repo', 'external', or 'bundled'
  1259. */
  1260. protected function get_plugin_source_type( $source ) {
  1261. if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) {
  1262. return 'repo';
  1263. } elseif ( preg_match( self::IS_URL_REGEX, $source ) ) {
  1264. return 'external';
  1265. } else {
  1266. return 'bundled';
  1267. }
  1268. }
  1269. /**
  1270. * Sanitizes a string key.
  1271. *
  1272. * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are*
  1273. * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase
  1274. * characters in the plugin directory path/slug. Silly them.
  1275. *
  1276. * @see https://developer.wordpress.org/reference/hooks/sanitize_key/
  1277. *
  1278. * @since 2.5.0
  1279. *
  1280. * @param string $key String key.
  1281. * @return string Sanitized key
  1282. */
  1283. public function sanitize_key( $key ) {
  1284. $raw_key = $key;
  1285. $key = preg_replace( '`[^A-Za-z0-9_-]`', '', $key );
  1286. /**
  1287. * Filter a sanitized key string.
  1288. *
  1289. * @since 2.5.0
  1290. *
  1291. * @param string $key Sanitized key.
  1292. * @param string $raw_key The key prior to sanitization.
  1293. */
  1294. return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key );
  1295. }
  1296. /**
  1297. * Amend default configuration settings.
  1298. *
  1299. * @since 2.0.0
  1300. *
  1301. * @param array $config Array of config options to pass as class properties.
  1302. */
  1303. public function config( $config ) {
  1304. $keys = array(
  1305. 'id',
  1306. 'default_path',
  1307. 'has_notices',
  1308. 'dismissable',
  1309. 'dismiss_msg',
  1310. 'menu',
  1311. 'parent_slug',
  1312. 'capability',
  1313. 'is_automatic',
  1314. 'message',
  1315. 'strings',
  1316. );
  1317. foreach ( $keys as $key ) {
  1318. if ( isset( $config[ $key ] ) ) {
  1319. if ( is_array( $config[ $key ] ) ) {
  1320. $this->$key = array_merge( $this->$key, $config[ $key ] );
  1321. } else {
  1322. $this->$key = $config[ $key ];
  1323. }
  1324. }
  1325. }
  1326. }
  1327. /**
  1328. * Amend action link after plugin installation.
  1329. *
  1330. * @since 2.0.0
  1331. *
  1332. * @param array $install_actions Existing array of actions.
  1333. * @return false|array Amended array of actions.
  1334. */
  1335. public function actions( $install_actions ) {
  1336. // Remove action links on the TGMPA install page.
  1337. if ( $this->is_tgmpa_page() ) {
  1338. return false;
  1339. }
  1340. return $install_actions;
  1341. }
  1342. /**
  1343. * Flushes the plugins cache on theme switch to prevent stale entries
  1344. * from remaining in the plugin table.
  1345. *
  1346. * @since 2.4.0
  1347. *
  1348. * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache.
  1349. * Parameter added in v2.5.0.
  1350. */
  1351. public function flush_plugins_cache( $clear_update_cache = true ) {
  1352. wp_clean_plugins_cache( $clear_update_cache );
  1353. }
  1354. /**
  1355. * Set file_path key for each installed plugin.
  1356. *
  1357. * @since 2.1.0
  1358. *
  1359. * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin.
  1360. * Parameter added in v2.5.0.
  1361. */
  1362. public function populate_file_path( $plugin_slug = '' ) {
  1363. if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) {
  1364. $this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug );
  1365. } else {
  1366. // Add file_path key for all plugins.
  1367. foreach ( $this->plugins as $slug => $values ) {
  1368. $this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug );
  1369. }
  1370. }
  1371. }
  1372. /**
  1373. * Helper function to extract the file path of the plugin file from the
  1374. * plugin slug, if the plugin is installed.
  1375. *
  1376. * @since 2.0.0
  1377. *
  1378. * @param string $slug Plugin slug (typically folder name) as provided by the developer.
  1379. * @return string Either file path for plugin if installed, or just the plugin slug.
  1380. */
  1381. protected function _get_plugin_basename_from_slug( $slug ) {
  1382. $keys = array_keys( $this->get_plugins() );
  1383. foreach ( $keys as $key ) {
  1384. if ( preg_match( '|^' . $slug . '/|', $key ) ) {
  1385. return $key;
  1386. }
  1387. }
  1388. return $slug;
  1389. }
  1390. /**
  1391. * Retrieve plugin data, given the plugin name.
  1392. *
  1393. * Loops through the registered plugins looking for $name. If it finds it,
  1394. * it returns the $data from that plugin. Otherwise, returns false.
  1395. *
  1396. * @since 2.1.0
  1397. *
  1398. * @param string $name Name of the plugin, as it was registered.
  1399. * @param string $data Optional. Array key of plugin data to return. Default is slug.
  1400. * @return string|boolean Plugin slug if found, false otherwise.
  1401. */
  1402. public function _get_plugin_data_from_name( $name, $data = 'slug' ) {
  1403. foreach ( $this->plugins as $values ) {
  1404. if ( $name === $values['name'] && isset( $values[ $data ] ) ) {
  1405. return $values[ $data ];
  1406. }
  1407. }
  1408. return false;
  1409. }
  1410. /**
  1411. * Retrieve the download URL for a package.
  1412. *
  1413. * @since 2.5.0
  1414. *
  1415. * @param string $slug Plugin slug.
  1416. * @return string Plugin download URL or path to local file or empty string if undetermined.
  1417. */
  1418. public function get_download_url( $slug ) {
  1419. $dl_source = '';
  1420. switch ( $this->plugins[ $slug ]['source_type'] ) {
  1421. case 'repo':
  1422. return $this->get_wp_repo_download_url( $slug );
  1423. case 'external':
  1424. return $this->plugins[ $slug ]['source'];
  1425. case 'bundled':
  1426. return $this->default_path . $this->plugins[ $slug ]['source'];
  1427. }
  1428. return $dl_source; // Should never happen.
  1429. }
  1430. /**
  1431. * Retrieve the download URL for a WP repo package.
  1432. *
  1433. * @since 2.5.0
  1434. *
  1435. * @param string $slug Plugin slug.
  1436. * @return string Plugin download URL.
  1437. */
  1438. protected function get_wp_repo_download_url( $slug ) {
  1439. $source = '';
  1440. $api = $this->get_plugins_api( $slug );
  1441. if ( false !== $api && isset( $api->download_link ) ) {
  1442. $source = $api->download_link;
  1443. }
  1444. return $source;
  1445. }
  1446. /**
  1447. * Try to grab information from WordPress API.
  1448. *
  1449. * @since 2.5.0
  1450. *
  1451. * @param string $slug Plugin slug.
  1452. * @return object Plugins_api response object on success, WP_Error on failure.
  1453. */
  1454. protected function get_plugins_api( $slug ) {
  1455. static $api = array(); // Cache received responses.
  1456. if ( ! isset( $api[ $slug ] ) ) {
  1457. if ( ! function_exists( 'plugins_api' ) ) {
  1458. require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
  1459. }
  1460. $response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
  1461. $api[ $slug ] = false;
  1462. if ( is_wp_error( $response ) ) {
  1463. wp_die( esc_html( $this->strings['oops'] ) );
  1464. } else {
  1465. $api[ $slug ] = $response;
  1466. }
  1467. }
  1468. return $api[ $slug ];
  1469. }
  1470. /**
  1471. * Retrieve a link to a plugin information page.
  1472. *
  1473. * @since 2.5.0
  1474. *
  1475. * @param string $slug Plugin slug.
  1476. * @return string Fully formed html link to a plugin information page if available
  1477. * or the plugin name if not.
  1478. */
  1479. public function get_info_link( $slug ) {
  1480. if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) {
  1481. $link = sprintf(
  1482. '<a href="%1$s" target="_blank">%2$s</a>',
  1483. esc_url( $this->plugins[ $slug ]['external_url'] ),
  1484. esc_html( $this->plugins[ $slug ]['name'] )
  1485. );
  1486. } elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) {
  1487. $url = add_query_arg(
  1488. array(
  1489. 'tab' => 'plugin-information',
  1490. 'plugin' => urlencode( $slug ),
  1491. 'TB_iframe' => 'true',
  1492. 'width' => '640',
  1493. 'height' => '500',
  1494. ),
  1495. self_admin_url( 'plugin-install.php' )
  1496. );
  1497. $link = sprintf(
  1498. '<a href="%1$s" class="thickbox">%2$s</a>',
  1499. esc_url( $url ),
  1500. esc_html( $this->plugins[ $slug ]['name'] )
  1501. );
  1502. } else {
  1503. $link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink.
  1504. }
  1505. return $link;
  1506. }
  1507. /**
  1508. * Determine if we're on the TGMPA Install page.
  1509. *
  1510. * @since 2.1.0
  1511. *
  1512. * @return boolean True when on the TGMPA page, false otherwise.
  1513. */
  1514. protected function is_tgmpa_page() {
  1515. return isset( $_GET['page'] ) && $this->menu === $_GET['page'];
  1516. }
  1517. /**
  1518. * Determine if we're on a WP Core installation/upgrade page.
  1519. *
  1520. * @since 2.6.0
  1521. *
  1522. * @return boolean True when on a WP Core installation/upgrade page, false otherwise.
  1523. */
  1524. protected function is_core_update_page() {
  1525. // Current screen is not always available, most notably on the customizer screen.
  1526. if ( ! function_exists( 'get_current_screen' ) ) {
  1527. return false;
  1528. }
  1529. $screen = get_current_screen();
  1530. if ( 'update-core' === $screen->base ) {
  1531. // Core update screen.
  1532. return true;
  1533. } elseif ( 'plugins' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
  1534. // Plugins bulk update screen.
  1535. return true;
  1536. } elseif ( 'update' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok.
  1537. // Individual updates (ajax call).
  1538. return true;
  1539. }
  1540. return false;
  1541. }
  1542. /**
  1543. * Retrieve the URL to the TGMPA Install page.
  1544. *
  1545. * I.e. depending on the config settings passed something along the lines of:
  1546. * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins
  1547. *
  1548. * @since 2.5.0
  1549. *
  1550. * @return string Properly encoded URL (not escaped).
  1551. */
  1552. public function get_tgmpa_url() {
  1553. static $url;
  1554. if ( ! isset( $url ) ) {
  1555. $parent = $this->parent_slug;
  1556. if ( false === strpos( $parent, '.php' ) ) {
  1557. $parent = 'admin.php';
  1558. }
  1559. $url = add_query_arg(
  1560. array(
  1561. 'page' => urlencode( $this->menu ),
  1562. ),
  1563. self_admin_url( $parent )
  1564. );
  1565. }
  1566. return $url;
  1567. }
  1568. /**
  1569. * Retrieve the URL to the TGMPA Install page for a specific plugin status (view).
  1570. *
  1571. * I.e. depending on the config settings passed something along the lines of:
  1572. * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install
  1573. *
  1574. * @since 2.5.0
  1575. *
  1576. * @param string $status Plugin status - either 'install', 'update' or 'activate'.
  1577. * @return string Properly encoded URL (not escaped).
  1578. */
  1579. public function get_tgmpa_status_url( $status ) {
  1580. return add_query_arg(
  1581. array(
  1582. 'plugin_status' => urlencode( $status ),
  1583. ),
  1584. $this->get_tgmpa_url()
  1585. );
  1586. }
  1587. /**
  1588. * Determine whether there are open actions for plugins registered with TGMPA.
  1589. *
  1590. * @since 2.5.0
  1591. *
  1592. * @return bool True if complete, i.e. no outstanding actions. False otherwise.
  1593. */
  1594. public function is_tgmpa_complete() {
  1595. $complete = true;
  1596. foreach ( $this->plugins as $slug => $plugin ) {
  1597. if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) {
  1598. $complete = false;
  1599. break;
  1600. }
  1601. }
  1602. return $complete;
  1603. }
  1604. /**
  1605. * Check if a plugin is installed. Does not take must-use plugins into account.
  1606. *
  1607. * @since 2.5.0
  1608. *
  1609. * @param string $slug Plugin slug.
  1610. * @return bool True if installed, false otherwise.
  1611. */
  1612. public function is_plugin_installed( $slug ) {
  1613. $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
  1614. return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) );
  1615. }
  1616. /**
  1617. * Check if a plugin is active.
  1618. *
  1619. * @since 2.5.0
  1620. *
  1621. * @param string $slug Plugin slug.
  1622. * @return bool True if active, false otherwise.
  1623. */
  1624. public function is_plugin_active( $slug ) {
  1625. return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) );
  1626. }
  1627. /**
  1628. * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required
  1629. * available, check whether the current install meets them.
  1630. *
  1631. * @since 2.5.0
  1632. *
  1633. * @param string $slug Plugin slug.
  1634. * @return bool True if OK to update, false otherwise.
  1635. */
  1636. public function can_plugin_update( $slug ) {
  1637. // We currently can't get reliable info on non-WP-repo plugins - issue #380.
  1638. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
  1639. return true;
  1640. }
  1641. $api = $this->get_plugins_api( $slug );
  1642. if ( false !== $api && isset( $api->requires ) ) {
  1643. return version_compare( $this->wp_version, $api->requires, '>=' );
  1644. }
  1645. // No usable info received from the plugins API, presume we can update.
  1646. return true;
  1647. }
  1648. /**
  1649. * Check to see if the plugin is 'updatetable', i.e. installed, with an update available
  1650. * and no WP version requirements blocking it.
  1651. *
  1652. * @since 2.6.0
  1653. *
  1654. * @param string $slug Plugin slug.
  1655. * @return bool True if OK to proceed with update, false otherwise.
  1656. */
  1657. public function is_plugin_updatetable( $slug ) {
  1658. if ( ! $this->is_plugin_installed( $slug ) ) {
  1659. return false;
  1660. } else {
  1661. return ( false !== $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) );
  1662. }
  1663. }
  1664. /**
  1665. * Check if a plugin can be activated, i.e. is not currently active and meets the minimum
  1666. * plugin version requirements set in TGMPA (if any).
  1667. *
  1668. * @since 2.5.0
  1669. *
  1670. * @param string $slug Plugin slug.
  1671. * @return bool True if OK to activate, false otherwise.
  1672. */
  1673. public function can_plugin_activate( $slug ) {
  1674. return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) );
  1675. }
  1676. /**
  1677. * Retrieve the version number of an installed plugin.
  1678. *
  1679. * @since 2.5.0
  1680. *
  1681. * @param string $slug Plugin slug.
  1682. * @return string Version number as string or an empty string if the plugin is not installed
  1683. * or version unknown (plugins which don't comply with the plugin header standard).
  1684. */
  1685. public function get_installed_version( $slug ) {
  1686. $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached).
  1687. if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) {
  1688. return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'];
  1689. }
  1690. return '';
  1691. }
  1692. /**
  1693. * Check whether a plugin complies with the minimum version requirements.
  1694. *
  1695. * @since 2.5.0
  1696. *
  1697. * @param string $slug Plugin slug.
  1698. * @return bool True when a plugin needs to be updated, otherwise false.
  1699. */
  1700. public function does_plugin_require_update( $slug ) {
  1701. $installed_version = $this->get_installed_version( $slug );
  1702. $minimum_version = $this->plugins[ $slug ]['version'];
  1703. return version_compare( $minimum_version, $installed_version, '>' );
  1704. }
  1705. /**
  1706. * Check whether there is an update available for a plugin.
  1707. *
  1708. * @since 2.5.0
  1709. *
  1710. * @param string $slug Plugin slug.
  1711. * @return false|string Version number string of the available update or false if no update available.
  1712. */
  1713. public function does_plugin_have_update( $slug ) {
  1714. // Presume bundled and external plugins will point to a package which meets the minimum required version.
  1715. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
  1716. if ( $this->does_plugin_require_update( $slug ) ) {
  1717. return $this->plugins[ $slug ]['version'];
  1718. }
  1719. return false;
  1720. }
  1721. $repo_updates = get_site_transient( 'update_plugins' );
  1722. if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) {
  1723. return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version;
  1724. }
  1725. return false;
  1726. }
  1727. /**
  1728. * Retrieve potential upgrade notice for a plugin.
  1729. *
  1730. * @since 2.5.0
  1731. *
  1732. * @param string $slug Plugin slug.
  1733. * @return string The upgrade notice or an empty string if no message was available or provided.
  1734. */
  1735. public function get_upgrade_notice( $slug ) {
  1736. // We currently can't get reliable info on non-WP-repo plugins - issue #380.
  1737. if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) {
  1738. return '';
  1739. }
  1740. $repo_updates = get_site_transient( 'update_plugins' );
  1741. if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) {
  1742. return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice;
  1743. }
  1744. return '';
  1745. }
  1746. /**
  1747. * Wrapper around the core WP get_plugins function, making sure it's actually available.
  1748. *
  1749. * @since 2.5.0
  1750. *
  1751. * @param string $plugin_folder Optional. Relative path to single plugin folder.
  1752. * @return array Array of installed plugins with plugin information.
  1753. */
  1754. public function get_plugins( $plugin_folder = '' ) {
  1755. if ( ! function_exists( 'get_plugins' ) ) {
  1756. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  1757. }
  1758. return get_plugins( $plugin_folder );
  1759. }
  1760. /**
  1761. * Delete dismissable nag option when theme is switched.
  1762. *
  1763. * This ensures that the user(s) is/are again reminded via nag of required
  1764. * and/or recommended plugins if they re-activate the theme.
  1765. *
  1766. * @since 2.1.1
  1767. */
  1768. public function update_dismiss() {
  1769. delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true );
  1770. }
  1771. /**
  1772. * Forces plugin activation if the parameter 'force_activation' is
  1773. * set to true.
  1774. *
  1775. * This allows theme authors to specify certain plugins that must be
  1776. * active at all times while using the current theme.
  1777. *
  1778. * Please take special care when using this parameter as it has the
  1779. * potential to be harmful if not used correctly. Setting this parameter
  1780. * to true will not allow the specified plugin to be deactivated unless
  1781. * the user switches themes.
  1782. *
  1783. * @since 2.2.0
  1784. */
  1785. public function force_activation() {
  1786. foreach ( $this->plugins as $slug => $plugin ) {
  1787. if ( true === $plugin['force_activation'] ) {
  1788. if ( ! $this->is_plugin_installed( $slug ) ) {
  1789. // Oops, plugin isn't there so iterate to next condition.
  1790. continue;
  1791. } elseif ( $this->can_plugin_activate( $slug ) ) {
  1792. // There we go, activate the plugin.
  1793. activate_plugin( $plugin['file_path'] );
  1794. }
  1795. }
  1796. }
  1797. }
  1798. /**
  1799. * Forces plugin deactivation if the parameter 'force_deactivation'
  1800. * is set to true and adds the plugin to the 'recently active' plugins list.
  1801. *
  1802. * This allows theme authors to specify certain plugins that must be
  1803. * deactivated upon switching from the current theme to another.
  1804. *
  1805. * Please take special care when using this parameter as it has the
  1806. * potential to be harmful if not used correctly.
  1807. *
  1808. * @since 2.2.0
  1809. */
  1810. public function force_deactivation() {
  1811. $deactivated = array();
  1812. foreach ( $this->plugins as $slug => $plugin ) {
  1813. /*
  1814. * Only proceed forward if the parameter is set to true and plugin is active
  1815. * as a 'normal' (not must-use) plugin.
  1816. */
  1817. if ( true === $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) ) {
  1818. deactivate_plugins( $plugin['file_path'] );
  1819. $deactivated[ $plugin['file_path'] ] = time();
  1820. }
  1821. }
  1822. if ( ! empty( $deactivated ) ) {
  1823. update_option( 'recently_activated', $deactivated + (array) get_option( 'recently_activated' ) );
  1824. }
  1825. }
  1826. /**
  1827. * Echo the current TGMPA version number to the page.
  1828. *
  1829. * @since 2.5.0
  1830. */
  1831. public function show_tgmpa_version() {
  1832. echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>',
  1833. esc_html(
  1834. sprintf(
  1835. /* translators: %s: version number */
  1836. __( 'TGMPA v%s', 'tgmpa' ),
  1837. self::TGMPA_VERSION
  1838. )
  1839. ),
  1840. '</small></strong></p>';
  1841. }
  1842. /**
  1843. * Returns the singleton instance of the class.
  1844. *
  1845. * @since 2.4.0
  1846. *
  1847. * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object.
  1848. */
  1849. public static function get_instance() {
  1850. if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
  1851. self::$instance = new self();
  1852. }
  1853. return self::$instance;
  1854. }
  1855. }
  1856. if ( ! function_exists( 'load_tgm_plugin_activation' ) ) {
  1857. /**
  1858. * Ensure only one instance of the class is ever invoked.
  1859. *
  1860. * @since 2.5.0
  1861. */
  1862. function load_tgm_plugin_activation() {
  1863. $GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance();
  1864. }
  1865. }
  1866. if ( did_action( 'plugins_loaded' ) ) {
  1867. load_tgm_plugin_activation();
  1868. } else {
  1869. add_action( 'plugins_loaded', 'load_tgm_plugin_activation' );
  1870. }
  1871. }
  1872. if ( ! function_exists( 'tgmpa' ) ) {
  1873. /**
  1874. * Helper function to register a collection of required plugins.
  1875. *
  1876. * @since 2.0.0
  1877. * @api
  1878. *
  1879. * @param array $plugins An array of plugin arrays.
  1880. * @param array $config Optional. An array of configuration values.
  1881. */
  1882. function tgmpa( $plugins, $config = array() ) {
  1883. $instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
  1884. foreach ( $plugins as $plugin ) {
  1885. call_user_func( array( $instance, 'register' ), $plugin );
  1886. }
  1887. if ( ! empty( $config ) && is_array( $config ) ) {
  1888. // Send out notices for deprecated arguments passed.
  1889. if ( isset( $config['notices'] ) ) {
  1890. _deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' );
  1891. if ( ! isset( $config['has_notices'] ) ) {
  1892. $config['has_notices'] = $config['notices'];
  1893. }
  1894. }
  1895. if ( isset( $config['parent_menu_slug'] ) ) {
  1896. _deprecated_argument( __FUNCTION__, '2.4.0', 'The `parent_menu_slug` config parameter was removed in TGMPA 2.4.0. In TGMPA 2.5.0 an alternative was (re-)introduced. Please adjust your configuration. For more information visit the website: http://tgmpluginactivation.com/configuration/#h-configuration-options.' );
  1897. }
  1898. if ( isset( $config['parent_url_slug'] ) ) {
  1899. _deprecated_argument( __FUNCTION__, '2.4.0', 'The `parent_url_slug` config parameter was removed in TGMPA 2.4.0. In TGMPA 2.5.0 an alternative was (re-)introduced. Please adjust your configuration. For more information visit the website: http://tgmpluginactivation.com/configuration/#h-configuration-options.' );
  1900. }
  1901. call_user_func( array( $instance, 'config' ), $config );
  1902. }
  1903. }
  1904. }
  1905. /**
  1906. * WP_List_Table isn't always available. If it isn't available,
  1907. * we load it here.
  1908. *
  1909. * @since 2.2.0
  1910. */
  1911. if ( ! class_exists( 'WP_List_Table' ) ) {
  1912. require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
  1913. }
  1914. if ( ! class_exists( 'TGMPA_List_Table' ) ) {
  1915. /**
  1916. * List table class for handling plugins.
  1917. *
  1918. * Extends the WP_List_Table class to provide a future-compatible
  1919. * way of listing out all required/recommended plugins.
  1920. *
  1921. * Gives users an interface similar to the Plugin Administration
  1922. * area with similar (albeit stripped down) capabilities.
  1923. *
  1924. * This class also allows for the bulk install of plugins.
  1925. *
  1926. * @since 2.2.0
  1927. *
  1928. * @package TGM-Plugin-Activation
  1929. * @author Thomas Griffin
  1930. * @author Gary Jones
  1931. */
  1932. class TGMPA_List_Table extends WP_List_Table {
  1933. /**
  1934. * TGMPA instance.
  1935. *
  1936. * @since 2.5.0
  1937. *
  1938. * @var object
  1939. */
  1940. protected $tgmpa;
  1941. /**
  1942. * The currently chosen view.
  1943. *
  1944. * @since 2.5.0
  1945. *
  1946. * @var string One of: 'all', 'install', 'update', 'activate'
  1947. */
  1948. public $view_context = 'all';
  1949. /**
  1950. * The plugin counts for the various views.
  1951. *
  1952. * @since 2.5.0
  1953. *
  1954. * @var array
  1955. */
  1956. protected $view_totals = array(
  1957. 'all' => 0,
  1958. 'install' => 0,
  1959. 'update' => 0,
  1960. 'activate' => 0,
  1961. );
  1962. /**
  1963. * References parent constructor and sets defaults for class.
  1964. *
  1965. * @since 2.2.0
  1966. */
  1967. public function __construct() {
  1968. $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
  1969. parent::__construct(
  1970. array(
  1971. 'singular' => 'plugin',
  1972. 'plural' => 'plugins',
  1973. 'ajax' => false,
  1974. )
  1975. );
  1976. if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) {
  1977. $this->view_context = sanitize_key( $_REQUEST['plugin_status'] );
  1978. }
  1979. add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) );
  1980. }
  1981. /**
  1982. * Get a list of CSS classes for the <table> tag.
  1983. *
  1984. * Overruled to prevent the 'plural' argument from being added.
  1985. *
  1986. * @since 2.5.0
  1987. *
  1988. * @return array CSS classnames.
  1989. */
  1990. public function get_table_classes() {
  1991. return array( 'widefat', 'fixed' );
  1992. }
  1993. /**
  1994. * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table.
  1995. *
  1996. * @since 2.2.0
  1997. *
  1998. * @return array $table_data Information for use in table.
  1999. */
  2000. protected function _gather_plugin_data() {
  2001. // Load thickbox for plugin links.
  2002. $this->tgmpa->admin_init();
  2003. $this->tgmpa->thickbox();
  2004. // Categorize the plugins which have open actions.
  2005. $plugins = $this->categorize_plugins_to_views();
  2006. // Set the counts for the view links.
  2007. $this->set_view_totals( $plugins );
  2008. // Prep variables for use and grab list of all installed plugins.
  2009. $table_data = array();
  2010. $i = 0;
  2011. // Redirect to the 'all' view if no plugins were found for the selected view context.
  2012. if ( empty( $plugins[ $this->view_context ] ) ) {
  2013. $this->view_context = 'all';
  2014. }
  2015. foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) {
  2016. $table_data[ $i ]['sanitized_plugin'] = $plugin['name'];
  2017. $table_data[ $i ]['slug'] = $slug;
  2018. $table_data[ $i ]['plugin'] = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>';
  2019. $table_data[ $i ]['source'] = $this->get_plugin_source_type_text( $plugin['source_type'] );
  2020. $table_data[ $i ]['type'] = $this->get_plugin_advise_type_text( $plugin['required'] );
  2021. $table_data[ $i ]['status'] = $this->get_plugin_status_text( $slug );
  2022. $table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug );
  2023. $table_data[ $i ]['minimum_version'] = $plugin['version'];
  2024. $table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug );
  2025. // Prep the upgrade notice info.
  2026. $upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug );
  2027. if ( ! empty( $upgrade_notice ) ) {
  2028. $table_data[ $i ]['upgrade_notice'] = $upgrade_notice;
  2029. add_action( "tgmpa_after_plugin_row_{$slug}", array( $this, 'wp_plugin_update_row' ), 10, 2 );
  2030. }
  2031. $table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin );
  2032. $i++;
  2033. }
  2034. return $table_data;
  2035. }
  2036. /**
  2037. * Categorize the plugins which have open actions into views for the TGMPA page.
  2038. *
  2039. * @since 2.5.0
  2040. */
  2041. protected function categorize_plugins_to_views() {
  2042. $plugins = array(
  2043. 'all' => array(), // Meaning: all plugins which still have open actions.
  2044. 'install' => array(),
  2045. 'update' => array(),
  2046. 'activate' => array(),
  2047. );
  2048. foreach ( $this->tgmpa->plugins as $slug => $plugin ) {
  2049. if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
  2050. // No need to display plugins if they are installed, up-to-date and active.
  2051. continue;
  2052. } else {
  2053. $plugins['all'][ $slug ] = $plugin;
  2054. if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
  2055. $plugins['install'][ $slug ] = $plugin;
  2056. } else {
  2057. if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
  2058. $plugins['update'][ $slug ] = $plugin;
  2059. }
  2060. if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
  2061. $plugins['activate'][ $slug ] = $plugin;
  2062. }
  2063. }
  2064. }
  2065. }
  2066. return $plugins;
  2067. }
  2068. /**
  2069. * Set the counts for the view links.
  2070. *
  2071. * @since 2.5.0
  2072. *
  2073. * @param array $plugins Plugins order by view.
  2074. */
  2075. protected function set_view_totals( $plugins ) {
  2076. foreach ( $plugins as $type => $list ) {
  2077. $this->view_totals[ $type ] = count( $list );
  2078. }
  2079. }
  2080. /**
  2081. * Get the plugin required/recommended text string.
  2082. *
  2083. * @since 2.5.0
  2084. *
  2085. * @param string $required Plugin required setting.
  2086. * @return string
  2087. */
  2088. protected function get_plugin_advise_type_text( $required ) {
  2089. if ( true === $required ) {
  2090. return __( 'Required', 'tgmpa' );
  2091. }
  2092. return __( 'Recommended', 'tgmpa' );
  2093. }
  2094. /**
  2095. * Get the plugin source type text string.
  2096. *
  2097. * @since 2.5.0
  2098. *
  2099. * @param string $type Plugin type.
  2100. * @return string
  2101. */
  2102. protected function get_plugin_source_type_text( $type ) {
  2103. $string = '';
  2104. switch ( $type ) {
  2105. case 'repo':
  2106. $string = __( 'WordPress Repository', 'tgmpa' );
  2107. break;
  2108. case 'external':
  2109. $string = __( 'External Source', 'tgmpa' );
  2110. break;
  2111. case 'bundled':
  2112. $string = __( 'Pre-Packaged', 'tgmpa' );
  2113. break;
  2114. }
  2115. return $string;
  2116. }
  2117. /**
  2118. * Determine the plugin status message.
  2119. *
  2120. * @since 2.5.0
  2121. *
  2122. * @param string $slug Plugin slug.
  2123. * @return string
  2124. */
  2125. protected function get_plugin_status_text( $slug ) {
  2126. if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) {
  2127. return __( 'Not Installed', 'tgmpa' );
  2128. }
  2129. if ( ! $this->tgmpa->is_plugin_active( $slug ) ) {
  2130. $install_status = __( 'Installed But Not Activated', 'tgmpa' );
  2131. } else {
  2132. $install_status = __( 'Active', 'tgmpa' );
  2133. }
  2134. $update_status = '';
  2135. if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) {
  2136. $update_status = __( 'Required Update not Available', 'tgmpa' );
  2137. } elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) {
  2138. $update_status = __( 'Requires Update', 'tgmpa' );
  2139. } elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) {
  2140. $update_status = __( 'Update recommended', 'tgmpa' );
  2141. }
  2142. if ( '' === $update_status ) {
  2143. return $install_status;
  2144. }
  2145. return sprintf(
  2146. /* translators: 1: install status, 2: update status */
  2147. _x( '%1$s, %2$s', 'Install/Update Status', 'tgmpa' ),
  2148. $install_status,
  2149. $update_status
  2150. );
  2151. }
  2152. /**
  2153. * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type.
  2154. *
  2155. * @since 2.5.0
  2156. *
  2157. * @param array $items Prepared table items.
  2158. * @return array Sorted table items.
  2159. */
  2160. public function sort_table_items( $items ) {
  2161. $type = array();
  2162. $name = array();
  2163. foreach ( $items as $i => $plugin ) {
  2164. $type[ $i ] = $plugin['type']; // Required / recommended.
  2165. $name[ $i ] = $plugin['sanitized_plugin'];
  2166. }
  2167. array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items );
  2168. return $items;
  2169. }
  2170. /**
  2171. * Get an associative array ( id => link ) of the views available on this table.
  2172. *
  2173. * @since 2.5.0
  2174. *
  2175. * @return array
  2176. */
  2177. public function get_views() {
  2178. $status_links = array();
  2179. foreach ( $this->view_totals as $type => $count ) {
  2180. if ( $count < 1 ) {
  2181. continue;
  2182. }
  2183. switch ( $type ) {
  2184. case 'all':
  2185. /* translators: 1: number of plugins. */
  2186. $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' );
  2187. break;
  2188. case 'install':
  2189. /* translators: 1: number of plugins. */
  2190. $text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' );
  2191. break;
  2192. case 'update':
  2193. /* translators: 1: number of plugins. */
  2194. $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' );
  2195. break;
  2196. case 'activate':
  2197. /* translators: 1: number of plugins. */
  2198. $text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' );
  2199. break;
  2200. default:
  2201. $text = '';
  2202. break;
  2203. }
  2204. if ( ! empty( $text ) ) {
  2205. $status_links[ $type ] = sprintf(
  2206. '<a href="%s"%s>%s</a>',
  2207. esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ),
  2208. ( $type === $this->view_context ) ? ' class="current"' : '',
  2209. sprintf( $text, number_format_i18n( $count ) )
  2210. );
  2211. }
  2212. }
  2213. return $status_links;
  2214. }
  2215. /**
  2216. * Create default columns to display important plugin information
  2217. * like type, action and status.
  2218. *
  2219. * @since 2.2.0
  2220. *
  2221. * @param array $item Array of item data.
  2222. * @param string $column_name The name of the column.
  2223. * @return string
  2224. */
  2225. public function column_default( $item, $column_name ) {
  2226. return $item[ $column_name ];
  2227. }
  2228. /**
  2229. * Required for bulk installing.
  2230. *
  2231. * Adds a checkbox for each plugin.
  2232. *
  2233. * @since 2.2.0
  2234. *
  2235. * @param array $item Array of item data.
  2236. * @return string The input checkbox with all necessary info.
  2237. */
  2238. public function column_cb( $item ) {
  2239. return sprintf(
  2240. '<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />',
  2241. esc_attr( $this->_args['singular'] ),
  2242. esc_attr( $item['slug'] ),
  2243. esc_attr( $item['sanitized_plugin'] )
  2244. );
  2245. }
  2246. /**
  2247. * Create default title column along with the action links.
  2248. *
  2249. * @since 2.2.0
  2250. *
  2251. * @param array $item Array of item data.
  2252. * @return string The plugin name and action links.
  2253. */
  2254. public function column_plugin( $item ) {
  2255. return sprintf(
  2256. '%1$s %2$s',
  2257. $item['plugin'],
  2258. $this->row_actions( $this->get_row_actions( $item ), true )
  2259. );
  2260. }
  2261. /**
  2262. * Create version information column.
  2263. *
  2264. * @since 2.5.0
  2265. *
  2266. * @param array $item Array of item data.
  2267. * @return string HTML-formatted version information.
  2268. */
  2269. public function column_version( $item ) {
  2270. $output = array();
  2271. if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
  2272. $installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'tgmpa' );
  2273. $color = '';
  2274. if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) {
  2275. $color = ' color: #ff0000; font-weight: bold;';
  2276. }
  2277. $output[] = sprintf(
  2278. '<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'tgmpa' ) . '</p>',
  2279. $color,
  2280. $installed
  2281. );
  2282. }
  2283. if ( ! empty( $item['minimum_version'] ) ) {
  2284. $output[] = sprintf(
  2285. '<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'tgmpa' ) . '</p>',
  2286. $item['minimum_version']
  2287. );
  2288. }
  2289. if ( ! empty( $item['available_version'] ) ) {
  2290. $color = '';
  2291. if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) {
  2292. $color = ' color: #71C671; font-weight: bold;';
  2293. }
  2294. $output[] = sprintf(
  2295. '<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'tgmpa' ) . '</p>',
  2296. $color,
  2297. $item['available_version']
  2298. );
  2299. }
  2300. if ( empty( $output ) ) {
  2301. return '&nbsp;'; // Let's not break the table layout.
  2302. } else {
  2303. return implode( "\n", $output );
  2304. }
  2305. }
  2306. /**
  2307. * Sets default message within the plugins table if no plugins
  2308. * are left for interaction.
  2309. *
  2310. * Hides the menu item to prevent the user from clicking and
  2311. * getting a permissions error.
  2312. *
  2313. * @since 2.2.0
  2314. */
  2315. public function no_items() {
  2316. echo esc_html__( 'No plugins to install, update or activate.', 'tgmpa' ) . ' <a href="' . esc_url( self_admin_url() ) . '"> ' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>';
  2317. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  2318. }
  2319. /**
  2320. * Output all the column information within the table.
  2321. *
  2322. * @since 2.2.0
  2323. *
  2324. * @return array $columns The column names.
  2325. */
  2326. public function get_columns() {
  2327. $columns = array(
  2328. 'cb' => '<input type="checkbox" />',
  2329. 'plugin' => __( 'Plugin', 'tgmpa' ),
  2330. 'source' => __( 'Source', 'tgmpa' ),
  2331. 'type' => __( 'Type', 'tgmpa' ),
  2332. );
  2333. if ( 'all' === $this->view_context || 'update' === $this->view_context ) {
  2334. $columns['version'] = __( 'Version', 'tgmpa' );
  2335. $columns['status'] = __( 'Status', 'tgmpa' );
  2336. }
  2337. return apply_filters( 'tgmpa_table_columns', $columns );
  2338. }
  2339. /**
  2340. * Get name of default primary column
  2341. *
  2342. * @since 2.5.0 / WP 4.3+ compatibility
  2343. * @access protected
  2344. *
  2345. * @return string
  2346. */
  2347. protected function get_default_primary_column_name() {
  2348. return 'plugin';
  2349. }
  2350. /**
  2351. * Get the name of the primary column.
  2352. *
  2353. * @since 2.5.0 / WP 4.3+ compatibility
  2354. * @access protected
  2355. *
  2356. * @return string The name of the primary column.
  2357. */
  2358. protected function get_primary_column_name() {
  2359. if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) {
  2360. return parent::get_primary_column_name();
  2361. } else {
  2362. return $this->get_default_primary_column_name();
  2363. }
  2364. }
  2365. /**
  2366. * Get the actions which are relevant for a specific plugin row.
  2367. *
  2368. * @since 2.5.0
  2369. *
  2370. * @param array $item Array of item data.
  2371. * @return array Array with relevant action links.
  2372. */
  2373. protected function get_row_actions( $item ) {
  2374. $actions = array();
  2375. $action_links = array();
  2376. // Display the 'Install' action link if the plugin is not yet available.
  2377. if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) {
  2378. /* translators: %2$s: plugin name in screen reader markup */
  2379. $actions['install'] = __( 'Install %2$s', 'tgmpa' );
  2380. } else {
  2381. // Display the 'Update' action link if an update is available and WP complies with plugin minimum.
  2382. if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) {
  2383. /* translators: %2$s: plugin name in screen reader markup */
  2384. $actions['update'] = __( 'Update %2$s', 'tgmpa' );
  2385. }
  2386. // Display the 'Activate' action link, but only if the plugin meets the minimum version.
  2387. if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) {
  2388. /* translators: %2$s: plugin name in screen reader markup */
  2389. $actions['activate'] = __( 'Activate %2$s', 'tgmpa' );
  2390. }
  2391. }
  2392. // Create the actual links.
  2393. foreach ( $actions as $action => $text ) {
  2394. $nonce_url = wp_nonce_url(
  2395. add_query_arg(
  2396. array(
  2397. 'plugin' => urlencode( $item['slug'] ),
  2398. 'tgmpa-' . $action => $action . '-plugin',
  2399. ),
  2400. $this->tgmpa->get_tgmpa_url()
  2401. ),
  2402. 'tgmpa-' . $action,
  2403. 'tgmpa-nonce'
  2404. );
  2405. $action_links[ $action ] = sprintf(
  2406. '<a href="%1$s">' . esc_html( $text ) . '</a>', // $text contains the second placeholder.
  2407. esc_url( $nonce_url ),
  2408. '<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>'
  2409. );
  2410. }
  2411. $prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : '';
  2412. return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context );
  2413. }
  2414. /**
  2415. * Generates content for a single row of the table.
  2416. *
  2417. * @since 2.5.0
  2418. *
  2419. * @param object $item The current item.
  2420. */
  2421. public function single_row( $item ) {
  2422. parent::single_row( $item );
  2423. /**
  2424. * Fires after each specific row in the TGMPA Plugins list table.
  2425. *
  2426. * The dynamic portion of the hook name, `$item['slug']`, refers to the slug
  2427. * for the plugin.
  2428. *
  2429. * @since 2.5.0
  2430. */
  2431. do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context );
  2432. }
  2433. /**
  2434. * Show the upgrade notice below a plugin row if there is one.
  2435. *
  2436. * @since 2.5.0
  2437. *
  2438. * @see /wp-admin/includes/update.php
  2439. *
  2440. * @param string $slug Plugin slug.
  2441. * @param array $item The information available in this table row.
  2442. * @return null Return early if upgrade notice is empty.
  2443. */
  2444. public function wp_plugin_update_row( $slug, $item ) {
  2445. if ( empty( $item['upgrade_notice'] ) ) {
  2446. return;
  2447. }
  2448. echo '
  2449. <tr class="plugin-update-tr">
  2450. <td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange">
  2451. <div class="update-message">',
  2452. esc_html__( 'Upgrade message from the plugin author:', 'tgmpa' ),
  2453. ' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong>
  2454. </div>
  2455. </td>
  2456. </tr>';
  2457. }
  2458. /**
  2459. * Extra controls to be displayed between bulk actions and pagination.
  2460. *
  2461. * @since 2.5.0
  2462. *
  2463. * @param string $which 'top' or 'bottom' table navigation.
  2464. */
  2465. public function extra_tablenav( $which ) {
  2466. if ( 'bottom' === $which ) {
  2467. $this->tgmpa->show_tgmpa_version();
  2468. }
  2469. }
  2470. /**
  2471. * Defines the bulk actions for handling registered plugins.
  2472. *
  2473. * @since 2.2.0
  2474. *
  2475. * @return array $actions The bulk actions for the plugin install table.
  2476. */
  2477. public function get_bulk_actions() {
  2478. $actions = array();
  2479. if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) {
  2480. if ( current_user_can( 'install_plugins' ) ) {
  2481. $actions['tgmpa-bulk-install'] = __( 'Install', 'tgmpa' );
  2482. }
  2483. }
  2484. if ( 'install' !== $this->view_context ) {
  2485. if ( current_user_can( 'update_plugins' ) ) {
  2486. $actions['tgmpa-bulk-update'] = __( 'Update', 'tgmpa' );
  2487. }
  2488. if ( current_user_can( 'activate_plugins' ) ) {
  2489. $actions['tgmpa-bulk-activate'] = __( 'Activate', 'tgmpa' );
  2490. }
  2491. }
  2492. return $actions;
  2493. }
  2494. /**
  2495. * Processes bulk installation and activation actions.
  2496. *
  2497. * The bulk installation process looks for the $_POST information and passes that
  2498. * through if a user has to use WP_Filesystem to enter their credentials.
  2499. *
  2500. * @since 2.2.0
  2501. */
  2502. public function process_bulk_actions() {
  2503. // Bulk installation process.
  2504. if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) {
  2505. check_admin_referer( 'bulk-' . $this->_args['plural'] );
  2506. $install_type = 'install';
  2507. if ( 'tgmpa-bulk-update' === $this->current_action() ) {
  2508. $install_type = 'update';
  2509. }
  2510. $plugins_to_install = array();
  2511. // Did user actually select any plugins to install/update ?
  2512. if ( empty( $_POST['plugin'] ) ) {
  2513. if ( 'install' === $install_type ) {
  2514. $message = __( 'No plugins were selected to be installed. No action taken.', 'tgmpa' );
  2515. } else {
  2516. $message = __( 'No plugins were selected to be updated. No action taken.', 'tgmpa' );
  2517. }
  2518. echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
  2519. return false;
  2520. }
  2521. if ( is_array( $_POST['plugin'] ) ) {
  2522. $plugins_to_install = (array) $_POST['plugin'];
  2523. } elseif ( is_string( $_POST['plugin'] ) ) {
  2524. // Received via Filesystem page - un-flatten array (WP bug #19643).
  2525. $plugins_to_install = explode( ',', $_POST['plugin'] );
  2526. }
  2527. // Sanitize the received input.
  2528. $plugins_to_install = array_map( 'urldecode', $plugins_to_install );
  2529. $plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install );
  2530. // Validate the received input.
  2531. foreach ( $plugins_to_install as $key => $slug ) {
  2532. // Check if the plugin was registered with TGMPA and remove if not.
  2533. if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) {
  2534. unset( $plugins_to_install[ $key ] );
  2535. continue;
  2536. }
  2537. // For install: make sure this is a plugin we *can* install and not one already installed.
  2538. if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) {
  2539. unset( $plugins_to_install[ $key ] );
  2540. }
  2541. // For updates: make sure this is a plugin we *can* update (update available and WP version ok).
  2542. if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) {
  2543. unset( $plugins_to_install[ $key ] );
  2544. }
  2545. }
  2546. // No need to proceed further if we have no plugins to handle.
  2547. if ( empty( $plugins_to_install ) ) {
  2548. if ( 'install' === $install_type ) {
  2549. $message = __( 'No plugins are available to be installed at this time.', 'tgmpa' );
  2550. } else {
  2551. $message = __( 'No plugins are available to be updated at this time.', 'tgmpa' );
  2552. }
  2553. echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>';
  2554. return false;
  2555. }
  2556. // Pass all necessary information if WP_Filesystem is needed.
  2557. $url = wp_nonce_url(
  2558. $this->tgmpa->get_tgmpa_url(),
  2559. 'bulk-' . $this->_args['plural']
  2560. );
  2561. // Give validated data back to $_POST which is the only place the filesystem looks for extra fields.
  2562. $_POST['plugin'] = implode( ',', $plugins_to_install ); // Work around for WP bug #19643.
  2563. $method = ''; // Leave blank so WP_Filesystem can populate it as necessary.
  2564. $fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem.
  2565. if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) {
  2566. return true; // Stop the normal page form from displaying, credential request form will be shown.
  2567. }
  2568. // Now we have some credentials, setup WP_Filesystem.
  2569. if ( ! WP_Filesystem( $creds ) ) {
  2570. // Our credentials were no good, ask the user for them again.
  2571. request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields );
  2572. return true;
  2573. }
  2574. /* If we arrive here, we have the filesystem */
  2575. // Store all information in arrays since we are processing a bulk installation.
  2576. $names = array();
  2577. $sources = array(); // Needed for installs.
  2578. $file_paths = array(); // Needed for upgrades.
  2579. $to_inject = array(); // Information to inject into the update_plugins transient.
  2580. // Prepare the data for validated plugins for the install/upgrade.
  2581. foreach ( $plugins_to_install as $slug ) {
  2582. $name = $this->tgmpa->plugins[ $slug ]['name'];
  2583. $source = $this->tgmpa->get_download_url( $slug );
  2584. if ( ! empty( $name ) && ! empty( $source ) ) {
  2585. $names[] = $name;
  2586. switch ( $install_type ) {
  2587. case 'install':
  2588. $sources[] = $source;
  2589. break;
  2590. case 'update':
  2591. $file_paths[] = $this->tgmpa->plugins[ $slug ]['file_path'];
  2592. $to_inject[ $slug ] = $this->tgmpa->plugins[ $slug ];
  2593. $to_inject[ $slug ]['source'] = $source;
  2594. break;
  2595. }
  2596. }
  2597. }
  2598. unset( $slug, $name, $source );
  2599. // Create a new instance of TGMPA_Bulk_Installer.
  2600. $installer = new TGMPA_Bulk_Installer(
  2601. new TGMPA_Bulk_Installer_Skin(
  2602. array(
  2603. 'url' => esc_url_raw( $this->tgmpa->get_tgmpa_url() ),
  2604. 'nonce' => 'bulk-' . $this->_args['plural'],
  2605. 'names' => $names,
  2606. 'install_type' => $install_type,
  2607. )
  2608. )
  2609. );
  2610. // Wrap the install process with the appropriate HTML.
  2611. echo '<div class="tgmpa">',
  2612. '<h2 style="font-size: 23px; font-weight: 400; line-height: 29px; margin: 0; padding: 9px 15px 4px 0;">', esc_html( get_admin_page_title() ), '</h2>
  2613. <div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">';
  2614. // Process the bulk installation submissions.
  2615. add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 );
  2616. if ( 'tgmpa-bulk-update' === $this->current_action() ) {
  2617. // Inject our info into the update transient.
  2618. $this->tgmpa->inject_update_info( $to_inject );
  2619. $installer->bulk_upgrade( $file_paths );
  2620. } else {
  2621. $installer->bulk_install( $sources );
  2622. }
  2623. remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1 );
  2624. echo '</div></div>';
  2625. return true;
  2626. }
  2627. // Bulk activation process.
  2628. if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
  2629. check_admin_referer( 'bulk-' . $this->_args['plural'] );
  2630. // Did user actually select any plugins to activate ?
  2631. if ( empty( $_POST['plugin'] ) ) {
  2632. echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>';
  2633. return false;
  2634. }
  2635. // Grab plugin data from $_POST.
  2636. $plugins = array();
  2637. if ( isset( $_POST['plugin'] ) ) {
  2638. $plugins = array_map( 'urldecode', (array) $_POST['plugin'] );
  2639. $plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins );
  2640. }
  2641. $plugins_to_activate = array();
  2642. $plugin_names = array();
  2643. // Grab the file paths for the selected & inactive plugins from the registration array.
  2644. foreach ( $plugins as $slug ) {
  2645. if ( $this->tgmpa->can_plugin_activate( $slug ) ) {
  2646. $plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path'];
  2647. $plugin_names[] = $this->tgmpa->plugins[ $slug ]['name'];
  2648. }
  2649. }
  2650. unset( $slug );
  2651. // Return early if there are no plugins to activate.
  2652. if ( empty( $plugins_to_activate ) ) {
  2653. echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>';
  2654. return false;
  2655. }
  2656. // Now we are good to go - let's start activating plugins.
  2657. $activate = activate_plugins( $plugins_to_activate );
  2658. if ( is_wp_error( $activate ) ) {
  2659. echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>';
  2660. } else {
  2661. $count = count( $plugin_names ); // Count so we can use _n function.
  2662. $plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names );
  2663. $last_plugin = array_pop( $plugin_names ); // Pop off last name to prep for readability.
  2664. $imploded = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin );
  2665. printf( // WPCS: xss ok.
  2666. '<div id="message" class="updated"><p>%1$s %2$s.</p></div>',
  2667. esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ),
  2668. $imploded
  2669. );
  2670. // Update recently activated plugins option.
  2671. $recent = (array) get_option( 'recently_activated' );
  2672. foreach ( $plugins_to_activate as $plugin => $time ) {
  2673. if ( isset( $recent[ $plugin ] ) ) {
  2674. unset( $recent[ $plugin ] );
  2675. }
  2676. }
  2677. update_option( 'recently_activated', $recent );
  2678. }
  2679. unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another.
  2680. return true;
  2681. }
  2682. return false;
  2683. }
  2684. /**
  2685. * Prepares all of our information to be outputted into a usable table.
  2686. *
  2687. * @since 2.2.0
  2688. */
  2689. public function prepare_items() {
  2690. $columns = $this->get_columns(); // Get all necessary column information.
  2691. $hidden = array(); // No columns to hide, but we must set as an array.
  2692. $sortable = array(); // No reason to make sortable columns.
  2693. $primary = $this->get_primary_column_name(); // Column which has the row actions.
  2694. $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers.
  2695. // Process our bulk activations here.
  2696. if ( 'tgmpa-bulk-activate' === $this->current_action() ) {
  2697. $this->process_bulk_actions();
  2698. }
  2699. // Store all of our plugin data into $items array so WP_List_Table can use it.
  2700. $this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() );
  2701. }
  2702. /* *********** DEPRECATED METHODS *********** */
  2703. /**
  2704. * Retrieve plugin data, given the plugin name.
  2705. *
  2706. * @since 2.2.0
  2707. * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead.
  2708. * @see TGM_Plugin_Activation::_get_plugin_data_from_name()
  2709. *
  2710. * @param string $name Name of the plugin, as it was registered.
  2711. * @param string $data Optional. Array key of plugin data to return. Default is slug.
  2712. * @return string|boolean Plugin slug if found, false otherwise.
  2713. */
  2714. protected function _get_plugin_data_from_name( $name, $data = 'slug' ) {
  2715. _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' );
  2716. return $this->tgmpa->_get_plugin_data_from_name( $name, $data );
  2717. }
  2718. }
  2719. }
  2720. if ( ! class_exists( 'TGM_Bulk_Installer' ) ) {
  2721. /**
  2722. * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+.
  2723. *
  2724. * @since 2.5.2
  2725. *
  2726. * {@internal The TGMPA_Bulk_Installer class was originally called TGM_Bulk_Installer.
  2727. * For more information, see that class.}}
  2728. */
  2729. class TGM_Bulk_Installer {
  2730. }
  2731. }
  2732. if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) {
  2733. /**
  2734. * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+.
  2735. *
  2736. * @since 2.5.2
  2737. *
  2738. * {@internal The TGMPA_Bulk_Installer_Skin class was originally called TGM_Bulk_Installer_Skin.
  2739. * For more information, see that class.}}
  2740. */
  2741. class TGM_Bulk_Installer_Skin {
  2742. }
  2743. }
  2744. /**
  2745. * The WP_Upgrader file isn't always available. If it isn't available,
  2746. * we load it here.
  2747. *
  2748. * We check to make sure no action or activation keys are set so that WordPress
  2749. * does not try to re-include the class when processing upgrades or installs outside
  2750. * of the class.
  2751. *
  2752. * @since 2.2.0
  2753. */
  2754. add_action( 'admin_init', 'tgmpa_load_bulk_installer' );
  2755. if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) {
  2756. /**
  2757. * Load bulk installer
  2758. */
  2759. function tgmpa_load_bulk_installer() {
  2760. // Silently fail if 2.5+ is loaded *after* an older version.
  2761. if ( ! isset( $GLOBALS['tgmpa'] ) ) {
  2762. return;
  2763. }
  2764. // Get TGMPA class instance.
  2765. $tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
  2766. if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) {
  2767. if ( ! class_exists( 'Plugin_Upgrader', false ) ) {
  2768. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  2769. }
  2770. if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) {
  2771. /**
  2772. * Installer class to handle bulk plugin installations.
  2773. *
  2774. * Extends WP_Upgrader and customizes to suit the installation of multiple
  2775. * plugins.
  2776. *
  2777. * @since 2.2.0
  2778. *
  2779. * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}}
  2780. * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer.
  2781. * This was done to prevent backward compatibility issues with v2.3.6.}}
  2782. *
  2783. * @package TGM-Plugin-Activation
  2784. * @author Thomas Griffin
  2785. * @author Gary Jones
  2786. */
  2787. class TGMPA_Bulk_Installer extends Plugin_Upgrader {
  2788. /**
  2789. * Holds result of bulk plugin installation.
  2790. *
  2791. * @since 2.2.0
  2792. *
  2793. * @var string
  2794. */
  2795. public $result;
  2796. /**
  2797. * Flag to check if bulk installation is occurring or not.
  2798. *
  2799. * @since 2.2.0
  2800. *
  2801. * @var boolean
  2802. */
  2803. public $bulk = false;
  2804. /**
  2805. * TGMPA instance
  2806. *
  2807. * @since 2.5.0
  2808. *
  2809. * @var object
  2810. */
  2811. protected $tgmpa;
  2812. /**
  2813. * Whether or not the destination directory needs to be cleared ( = on update).
  2814. *
  2815. * @since 2.5.0
  2816. *
  2817. * @var bool
  2818. */
  2819. protected $clear_destination = false;
  2820. /**
  2821. * References parent constructor and sets defaults for class.
  2822. *
  2823. * @since 2.2.0
  2824. *
  2825. * @param \Bulk_Upgrader_Skin|null $skin Installer skin.
  2826. */
  2827. public function __construct( $skin = null ) {
  2828. // Get TGMPA class instance.
  2829. $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
  2830. parent::__construct( $skin );
  2831. if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) {
  2832. $this->clear_destination = true;
  2833. }
  2834. if ( $this->tgmpa->is_automatic ) {
  2835. $this->activate_strings();
  2836. }
  2837. add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) );
  2838. }
  2839. /**
  2840. * Sets the correct activation strings for the installer skin to use.
  2841. *
  2842. * @since 2.2.0
  2843. */
  2844. public function activate_strings() {
  2845. $this->strings['activation_failed'] = __( 'Plugin activation failed.', 'tgmpa' );
  2846. $this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' );
  2847. }
  2848. /**
  2849. * Performs the actual installation of each plugin.
  2850. *
  2851. * @since 2.2.0
  2852. *
  2853. * @see WP_Upgrader::run()
  2854. *
  2855. * @param array $options The installation config options.
  2856. * @return null|array Return early if error, array of installation data on success.
  2857. */
  2858. public function run( $options ) {
  2859. $result = parent::run( $options );
  2860. // Reset the strings in case we changed one during automatic activation.
  2861. if ( $this->tgmpa->is_automatic ) {
  2862. if ( 'update' === $this->skin->options['install_type'] ) {
  2863. $this->upgrade_strings();
  2864. } else {
  2865. $this->install_strings();
  2866. }
  2867. }
  2868. return $result;
  2869. }
  2870. /**
  2871. * Processes the bulk installation of plugins.
  2872. *
  2873. * @since 2.2.0
  2874. *
  2875. * {@internal This is basically a near identical copy of the WP Core
  2876. * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with
  2877. * new installs instead of upgrades.
  2878. * For ease of future synchronizations, the adjustments are clearly commented, but no other
  2879. * comments are added. Code style has been made to comply.}}
  2880. *
  2881. * @see Plugin_Upgrader::bulk_upgrade()
  2882. * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838
  2883. * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}}
  2884. *
  2885. * @param array $plugins The plugin sources needed for installation.
  2886. * @param array $args Arbitrary passed extra arguments.
  2887. * @return array|false Install confirmation messages on success, false on failure.
  2888. */
  2889. public function bulk_install( $plugins, $args = array() ) {
  2890. // [TGMPA + ] Hook auto-activation in.
  2891. add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
  2892. $defaults = array(
  2893. 'clear_update_cache' => true,
  2894. );
  2895. $parsed_args = wp_parse_args( $args, $defaults );
  2896. $this->init();
  2897. $this->bulk = true;
  2898. $this->install_strings(); // [TGMPA + ] adjusted.
  2899. /* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */
  2900. /* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */
  2901. $this->skin->header();
  2902. // Connect to the Filesystem first.
  2903. $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
  2904. if ( ! $res ) {
  2905. $this->skin->footer();
  2906. return false;
  2907. }
  2908. $this->skin->bulk_header();
  2909. /*
  2910. * Only start maintenance mode if:
  2911. * - running Multisite and there are one or more plugins specified, OR
  2912. * - a plugin with an update available is currently active.
  2913. * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
  2914. */
  2915. $maintenance = ( is_multisite() && ! empty( $plugins ) );
  2916. /*
  2917. [TGMPA - ]
  2918. foreach ( $plugins as $plugin )
  2919. $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
  2920. */
  2921. if ( $maintenance ) {
  2922. $this->maintenance_mode( true );
  2923. }
  2924. $results = array();
  2925. $this->update_count = count( $plugins );
  2926. $this->update_current = 0;
  2927. foreach ( $plugins as $plugin ) {
  2928. $this->update_current++;
  2929. /*
  2930. [TGMPA - ]
  2931. $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
  2932. if ( !isset( $current->response[ $plugin ] ) ) {
  2933. $this->skin->set_result('up_to_date');
  2934. $this->skin->before();
  2935. $this->skin->feedback('up_to_date');
  2936. $this->skin->after();
  2937. $results[$plugin] = true;
  2938. continue;
  2939. }
  2940. // Get the URL to the zip file.
  2941. $r = $current->response[ $plugin ];
  2942. $this->skin->plugin_active = is_plugin_active($plugin);
  2943. */
  2944. $result = $this->run(
  2945. array(
  2946. 'package' => $plugin, // [TGMPA + ] adjusted.
  2947. 'destination' => WP_PLUGIN_DIR,
  2948. 'clear_destination' => false, // [TGMPA + ] adjusted.
  2949. 'clear_working' => true,
  2950. 'is_multi' => true,
  2951. 'hook_extra' => array(
  2952. 'plugin' => $plugin,
  2953. ),
  2954. )
  2955. );
  2956. $results[ $plugin ] = $this->result;
  2957. // Prevent credentials auth screen from displaying multiple times.
  2958. if ( false === $result ) {
  2959. break;
  2960. }
  2961. } //end foreach $plugins
  2962. $this->maintenance_mode( false );
  2963. /**
  2964. * Fires when the bulk upgrader process is complete.
  2965. *
  2966. * @since WP 3.6.0 / TGMPA 2.5.0
  2967. *
  2968. * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
  2969. * be a Theme_Upgrader or Core_Upgrade instance.
  2970. * @param array $data {
  2971. * Array of bulk item update data.
  2972. *
  2973. * @type string $action Type of action. Default 'update'.
  2974. * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'.
  2975. * @type bool $bulk Whether the update process is a bulk update. Default true.
  2976. * @type array $packages Array of plugin, theme, or core packages to update.
  2977. * }
  2978. */
  2979. do_action( 'upgrader_process_complete', $this, array(
  2980. 'action' => 'install', // [TGMPA + ] adjusted.
  2981. 'type' => 'plugin',
  2982. 'bulk' => true,
  2983. 'plugins' => $plugins,
  2984. ) );
  2985. $this->skin->bulk_footer();
  2986. $this->skin->footer();
  2987. // Cleanup our hooks, in case something else does a upgrade on this connection.
  2988. /* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */
  2989. // [TGMPA + ] Remove our auto-activation hook.
  2990. remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
  2991. // Force refresh of plugin update information.
  2992. wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
  2993. return $results;
  2994. }
  2995. /**
  2996. * Handle a bulk upgrade request.
  2997. *
  2998. * @since 2.5.0
  2999. *
  3000. * @see Plugin_Upgrader::bulk_upgrade()
  3001. *
  3002. * @param array $plugins The local WP file_path's of the plugins which should be upgraded.
  3003. * @param array $args Arbitrary passed extra arguments.
  3004. * @return string|bool Install confirmation messages on success, false on failure.
  3005. */
  3006. public function bulk_upgrade( $plugins, $args = array() ) {
  3007. add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
  3008. $result = parent::bulk_upgrade( $plugins, $args );
  3009. remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 );
  3010. return $result;
  3011. }
  3012. /**
  3013. * Abuse a filter to auto-activate plugins after installation.
  3014. *
  3015. * Hooked into the 'upgrader_post_install' filter hook.
  3016. *
  3017. * @since 2.5.0
  3018. *
  3019. * @param bool $bool The value we need to give back (true).
  3020. * @return bool
  3021. */
  3022. public function auto_activate( $bool ) {
  3023. // Only process the activation of installed plugins if the automatic flag is set to true.
  3024. if ( $this->tgmpa->is_automatic ) {
  3025. // Flush plugins cache so the headers of the newly installed plugins will be read correctly.
  3026. wp_clean_plugins_cache();
  3027. // Get the installed plugin file.
  3028. $plugin_info = $this->plugin_info();
  3029. // Don't try to activate on upgrade of active plugin as WP will do this already.
  3030. if ( ! is_plugin_active( $plugin_info ) ) {
  3031. $activate = activate_plugin( $plugin_info );
  3032. // Adjust the success string based on the activation result.
  3033. $this->strings['process_success'] = $this->strings['process_success'] . "<br />\n";
  3034. if ( is_wp_error( $activate ) ) {
  3035. $this->skin->error( $activate );
  3036. $this->strings['process_success'] .= $this->strings['activation_failed'];
  3037. } else {
  3038. $this->strings['process_success'] .= $this->strings['activation_success'];
  3039. }
  3040. }
  3041. }
  3042. return $bool;
  3043. }
  3044. }
  3045. }
  3046. if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) {
  3047. /**
  3048. * Installer skin to set strings for the bulk plugin installations..
  3049. *
  3050. * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple
  3051. * plugins.
  3052. *
  3053. * @since 2.2.0
  3054. *
  3055. * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to
  3056. * TGMPA_Bulk_Installer_Skin.
  3057. * This was done to prevent backward compatibility issues with v2.3.6.}}
  3058. *
  3059. * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php
  3060. *
  3061. * @package TGM-Plugin-Activation
  3062. * @author Thomas Griffin
  3063. * @author Gary Jones
  3064. */
  3065. class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin {
  3066. /**
  3067. * Holds plugin info for each individual plugin installation.
  3068. *
  3069. * @since 2.2.0
  3070. *
  3071. * @var array
  3072. */
  3073. public $plugin_info = array();
  3074. /**
  3075. * Holds names of plugins that are undergoing bulk installations.
  3076. *
  3077. * @since 2.2.0
  3078. *
  3079. * @var array
  3080. */
  3081. public $plugin_names = array();
  3082. /**
  3083. * Integer to use for iteration through each plugin installation.
  3084. *
  3085. * @since 2.2.0
  3086. *
  3087. * @var integer
  3088. */
  3089. public $i = 0;
  3090. /**
  3091. * TGMPA instance
  3092. *
  3093. * @since 2.5.0
  3094. *
  3095. * @var object
  3096. */
  3097. protected $tgmpa;
  3098. /**
  3099. * Constructor. Parses default args with new ones and extracts them for use.
  3100. *
  3101. * @since 2.2.0
  3102. *
  3103. * @param array $args Arguments to pass for use within the class.
  3104. */
  3105. public function __construct( $args = array() ) {
  3106. // Get TGMPA class instance.
  3107. $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) );
  3108. // Parse default and new args.
  3109. $defaults = array(
  3110. 'url' => '',
  3111. 'nonce' => '',
  3112. 'names' => array(),
  3113. 'install_type' => 'install',
  3114. );
  3115. $args = wp_parse_args( $args, $defaults );
  3116. // Set plugin names to $this->plugin_names property.
  3117. $this->plugin_names = $args['names'];
  3118. // Extract the new args.
  3119. parent::__construct( $args );
  3120. }
  3121. /**
  3122. * Sets install skin strings for each individual plugin.
  3123. *
  3124. * Checks to see if the automatic activation flag is set and uses the
  3125. * the proper strings accordingly.
  3126. *
  3127. * @since 2.2.0
  3128. */
  3129. public function add_strings() {
  3130. if ( 'update' === $this->options['install_type'] ) {
  3131. parent::add_strings();
  3132. /* translators: 1: plugin name, 2: action number 3: total number of actions. */
  3133. $this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
  3134. } else {
  3135. /* translators: 1: plugin name, 2: error message. */
  3136. $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' );
  3137. /* translators: 1: plugin name. */
  3138. $this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'tgmpa' );
  3139. if ( $this->tgmpa->is_automatic ) {
  3140. // Automatic activation strings.
  3141. $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation and activation process is starting. This process may take a while on some hosts, so please be patient.', 'tgmpa' );
  3142. /* translators: 1: plugin name. */
  3143. $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>';
  3144. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', 'tgmpa' );
  3145. /* translators: 1: plugin name, 2: action number 3: total number of actions. */
  3146. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
  3147. } else {
  3148. // Default installation strings.
  3149. $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation process is starting. This process may take a while on some hosts, so please be patient.', 'tgmpa' );
  3150. /* translators: 1: plugin name. */
  3151. $this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>';
  3152. $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations have been completed.', 'tgmpa' );
  3153. /* translators: 1: plugin name, 2: action number 3: total number of actions. */
  3154. $this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' );
  3155. }
  3156. }
  3157. }
  3158. /**
  3159. * Outputs the header strings and necessary JS before each plugin installation.
  3160. *
  3161. * @since 2.2.0
  3162. *
  3163. * @param string $title Unused in this implementation.
  3164. */
  3165. public function before( $title = '' ) {
  3166. if ( empty( $title ) ) {
  3167. $title = esc_html( $this->plugin_names[ $this->i ] );
  3168. }
  3169. parent::before( $title );
  3170. }
  3171. /**
  3172. * Outputs the footer strings and necessary JS after each plugin installation.
  3173. *
  3174. * Checks for any errors and outputs them if they exist, else output
  3175. * success strings.
  3176. *
  3177. * @since 2.2.0
  3178. *
  3179. * @param string $title Unused in this implementation.
  3180. */
  3181. public function after( $title = '' ) {
  3182. if ( empty( $title ) ) {
  3183. $title = esc_html( $this->plugin_names[ $this->i ] );
  3184. }
  3185. parent::after( $title );
  3186. $this->i++;
  3187. }
  3188. /**
  3189. * Outputs links after bulk plugin installation is complete.
  3190. *
  3191. * @since 2.2.0
  3192. */
  3193. public function bulk_footer() {
  3194. // Serve up the string to say installations (and possibly activations) are complete.
  3195. parent::bulk_footer();
  3196. // Flush plugins cache so we can make sure that the installed plugins list is always up to date.
  3197. wp_clean_plugins_cache();
  3198. $this->tgmpa->show_tgmpa_version();
  3199. // Display message based on if all plugins are now active or not.
  3200. $update_actions = array();
  3201. if ( $this->tgmpa->is_tgmpa_complete() ) {
  3202. // All plugins are active, so we display the complete string and hide the menu to protect users.
  3203. echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>';
  3204. $update_actions['dashboard'] = sprintf(
  3205. esc_html( $this->tgmpa->strings['complete'] ),
  3206. '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>'
  3207. );
  3208. } else {
  3209. $update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>';
  3210. }
  3211. /**
  3212. * Filter the list of action links available following bulk plugin installs/updates.
  3213. *
  3214. * @since 2.5.0
  3215. *
  3216. * @param array $update_actions Array of plugin action links.
  3217. * @param array $plugin_info Array of information for the last-handled plugin.
  3218. */
  3219. $update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info );
  3220. if ( ! empty( $update_actions ) ) {
  3221. $this->feedback( implode( ' | ', (array) $update_actions ) );
  3222. }
  3223. }
  3224. /* *********** DEPRECATED METHODS *********** */
  3225. /**
  3226. * Flush header output buffer.
  3227. *
  3228. * @since 2.2.0
  3229. * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
  3230. * @see Bulk_Upgrader_Skin::flush_output()
  3231. */
  3232. public function before_flush_output() {
  3233. _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
  3234. $this->flush_output();
  3235. }
  3236. /**
  3237. * Flush footer output buffer and iterate $this->i to make sure the
  3238. * installation strings reference the correct plugin.
  3239. *
  3240. * @since 2.2.0
  3241. * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead
  3242. * @see Bulk_Upgrader_Skin::flush_output()
  3243. */
  3244. public function after_flush_output() {
  3245. _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' );
  3246. $this->flush_output();
  3247. $this->i++;
  3248. }
  3249. }
  3250. }
  3251. }
  3252. }
  3253. }
  3254. if ( ! class_exists( 'TGMPA_Utils' ) ) {
  3255. /**
  3256. * Generic utilities for TGMPA.
  3257. *
  3258. * All methods are static, poor-dev name-spacing class wrapper.
  3259. *
  3260. * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy.
  3261. *
  3262. * @since 2.5.0
  3263. *
  3264. * @package TGM-Plugin-Activation
  3265. * @author Juliette Reinders Folmer
  3266. */
  3267. class TGMPA_Utils {
  3268. /**
  3269. * Whether the PHP filter extension is enabled.
  3270. *
  3271. * @see http://php.net/book.filter
  3272. *
  3273. * @since 2.5.0
  3274. *
  3275. * @static
  3276. *
  3277. * @var bool $has_filters True is the extension is enabled.
  3278. */
  3279. public static $has_filters;
  3280. /**
  3281. * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map().
  3282. *
  3283. * @since 2.5.0
  3284. *
  3285. * @static
  3286. *
  3287. * @param string $string Text to be wrapped.
  3288. * @return string
  3289. */
  3290. public static function wrap_in_em( $string ) {
  3291. return '<em>' . wp_kses_post( $string ) . '</em>';
  3292. }
  3293. /**
  3294. * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map().
  3295. *
  3296. * @since 2.5.0
  3297. *
  3298. * @static
  3299. *
  3300. * @param string $string Text to be wrapped.
  3301. * @return string
  3302. */
  3303. public static function wrap_in_strong( $string ) {
  3304. return '<strong>' . wp_kses_post( $string ) . '</strong>';
  3305. }
  3306. /**
  3307. * Helper function: Validate a value as boolean
  3308. *
  3309. * @since 2.5.0
  3310. *
  3311. * @static
  3312. *
  3313. * @param mixed $value Arbitrary value.
  3314. * @return bool
  3315. */
  3316. public static function validate_bool( $value ) {
  3317. if ( ! isset( self::$has_filters ) ) {
  3318. self::$has_filters = extension_loaded( 'filter' );
  3319. }
  3320. if ( self::$has_filters ) {
  3321. return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
  3322. } else {
  3323. return self::emulate_filter_bool( $value );
  3324. }
  3325. }
  3326. /**
  3327. * Helper function: Cast a value to bool
  3328. *
  3329. * @since 2.5.0
  3330. *
  3331. * @static
  3332. *
  3333. * @param mixed $value Value to cast.
  3334. * @return bool
  3335. */
  3336. protected static function emulate_filter_bool( $value ) {
  3337. // @codingStandardsIgnoreStart
  3338. static $true = array(
  3339. '1',
  3340. 'true', 'True', 'TRUE',
  3341. 'y', 'Y',
  3342. 'yes', 'Yes', 'YES',
  3343. 'on', 'On', 'ON',
  3344. );
  3345. static $false = array(
  3346. '0',
  3347. 'false', 'False', 'FALSE',
  3348. 'n', 'N',
  3349. 'no', 'No', 'NO',
  3350. 'off', 'Off', 'OFF',
  3351. );
  3352. // @codingStandardsIgnoreEnd
  3353. if ( is_bool( $value ) ) {
  3354. return $value;
  3355. } elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) {
  3356. return (bool) $value;
  3357. } elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
  3358. return (bool) $value;
  3359. } elseif ( is_string( $value ) ) {
  3360. $value = trim( $value );
  3361. if ( in_array( $value, $true, true ) ) {
  3362. return true;
  3363. } elseif ( in_array( $value, $false, true ) ) {
  3364. return false;
  3365. } else {
  3366. return false;
  3367. }
  3368. }
  3369. return false;
  3370. }
  3371. } // End of class TGMPA_Utils
  3372. } // End of class_exists wrapper