1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
|
<?php /** * WooCommerce Admin Helper * * @package WooCommerce\Admin\Helper */
use Automattic\Jetpack\Constants; use Automattic\WooCommerce\Admin\PluginsHelper; use Automattic\WooCommerce\Utilities\FeaturesUtil;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * WC_Helper Class * * The main entry-point for all things related to the Helper. */ class WC_Helper { /** * A log object returned by wc_get_logger(). * * @var $log */ public static $log;
/** * Get an absolute path to the requested helper view. * * @param string $view The requested view file. * * @return string The absolute path to the view file. */ public static function get_view_filename( $view ) { return __DIR__ . "/views/$view"; }
/** * Loads the helper class, runs on init. */ public static function load() { self::includes();
add_action( 'current_screen', array( __CLASS__, 'current_screen' ) ); add_action( 'woocommerce_helper_output', array( __CLASS__, 'render_helper_output' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) ); add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
do_action( 'woocommerce_helper_loaded' ); }
/** * Include supporting helper classes. */ protected static function includes() { include_once __DIR__ . '/class-wc-helper-options.php'; include_once __DIR__ . '/class-wc-helper-api.php'; include_once __DIR__ . '/class-wc-woo-update-manager-plugin.php'; include_once __DIR__ . '/class-wc-helper-updater.php'; include_once __DIR__ . '/class-wc-plugin-api-updater.php'; include_once __DIR__ . '/class-wc-helper-compat.php'; include_once __DIR__ . '/class-wc-helper-admin.php'; include_once __DIR__ . '/class-wc-helper-subscriptions-api.php'; include_once __DIR__ . '/class-wc-helper-orders-api.php'; include_once __DIR__ . '/class-wc-product-usage-notice.php'; }
/** * Render the helper section content based on context. */ public static function render_helper_output() { $auth = WC_Helper_Options::get( 'auth' ); $auth_user_data = WC_Helper_Options::get( 'auth_user_data' );
// Return success/error notices. $notices = self::_get_return_notices();
// No active connection. if ( ! self::is_site_connected() ) { $connect_url = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'wc-helper-connect' => 1, 'wc-helper-nonce' => wp_create_nonce( 'connect' ), ), admin_url( 'admin.php' ) );
include self::get_view_filename( 'html-oauth-start.php' ); return; } $disconnect_url = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'wc-helper-disconnect' => 1, 'wc-helper-nonce' => wp_create_nonce( 'disconnect' ), ), admin_url( 'admin.php' ) );
$current_filter = self::get_current_filter(); $refresh_url = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'filter' => $current_filter, 'wc-helper-refresh' => 1, 'wc-helper-nonce' => wp_create_nonce( 'refresh' ), ), admin_url( 'admin.php' ) );
// Installed plugins and themes, with or without an active subscription. $woo_plugins = self::get_local_woo_plugins(); $woo_themes = self::get_local_woo_themes();
$subscriptions_list_data = self::get_subscription_list_data(); $subscriptions = array_filter( $subscriptions_list_data, function ( $subscription ) { return ! empty( $subscription['product_key'] ); } ); $updates = WC_Helper_Updater::get_update_data(); $subscriptions_product_ids = wp_list_pluck( $subscriptions, 'product_id' );
foreach ( $subscriptions as &$subscription ) { $subscription['activate_url'] = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'filter' => $current_filter, 'wc-helper-activate' => 1, 'wc-helper-product-key' => $subscription['product_key'], 'wc-helper-product-id' => $subscription['product_id'], 'wc-helper-nonce' => wp_create_nonce( 'activate:' . $subscription['product_key'] ), ), admin_url( 'admin.php' ) );
$subscription['deactivate_url'] = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'filter' => $current_filter, 'wc-helper-deactivate' => 1, 'wc-helper-product-key' => $subscription['product_key'], 'wc-helper-product-id' => $subscription['product_id'], 'wc-helper-nonce' => wp_create_nonce( 'deactivate:' . $subscription['product_key'] ), ), admin_url( 'admin.php' ) );
$subscription['update_url'] = admin_url( 'update-core.php' );
$local = wp_list_filter( array_merge( $woo_plugins, $woo_themes ), array( '_product_id' => $subscription['product_id'] ) );
if ( ! empty( $local ) ) { $local = array_shift( $local ); if ( 'plugin' === $local['_type'] ) { // A magic update_url. $subscription['update_url'] = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $local['_filename'], 'upgrade-plugin_' . $local['_filename'] );
} elseif ( 'theme' === $local['_type'] ) { // Another magic update_url. $subscription['update_url'] = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-theme&theme=' . $local['_stylesheet'] ), 'upgrade-theme_' . $local['_stylesheet'] ); } }
$subscription['download_primary'] = true; $subscription['download_url'] = 'https://woocommerce.com/my-account/downloads/'; if ( ! $subscription['local']['installed'] && ! empty( $updates[ $subscription['product_id'] ] ) ) { $subscription['download_url'] = $updates[ $subscription['product_id'] ]['package']; }
$subscription['actions'] = array();
if ( $subscription['has_update'] && ! $subscription['expired'] ) { $action = array( /* translators: %s: version number */ 'message' => sprintf( __( 'Version %s is <strong>available</strong>.', 'woocommerce' ), esc_html( $updates[ $subscription['product_id'] ]['version'] ) ), 'button_label' => __( 'Update', 'woocommerce' ), 'button_url' => $subscription['update_url'], 'status' => 'update-available', 'icon' => 'dashicons-update', );
// Subscription is not active on this site. if ( ! $subscription['active'] ) { $action['message'] .= ' ' . __( 'To enable this update you need to <strong>activate</strong> this subscription.', 'woocommerce' ); $action['button_label'] = null; $action['button_url'] = null; }
$subscription['actions'][] = $action; }
if ( $subscription['has_update'] && $subscription['expired'] ) { $action = array( /* translators: %s: version number */ 'message' => sprintf( __( 'Version %s is <strong>available</strong>.', 'woocommerce' ), esc_html( $updates[ $subscription['product_id'] ]['version'] ) ), 'status' => 'expired', 'icon' => 'dashicons-info', );
$action['message'] .= ' ' . __( 'To enable this update you need to <strong>purchase</strong> a new subscription.', 'woocommerce' ); $action['button_label'] = __( 'Purchase', 'woocommerce' ); $action['button_url'] = self::add_utm_params_to_url_for_subscription_link( $subscription['product_url'], 'purchase' );
$subscription['actions'][] = $action; } elseif ( $subscription['expired'] && ! empty( $subscription['master_user_email'] ) ) { $action = array( 'message' => sprintf( __( 'This subscription has expired. Contact the owner to <strong>renew</strong> the subscription to receive updates and support.', 'woocommerce' ) ), 'status' => 'expired', 'icon' => 'dashicons-info', );
$subscription['actions'][] = $action; } elseif ( $subscription['expired'] ) { $action = array( 'message' => sprintf( __( 'This subscription has expired. Please <strong>renew</strong> to receive updates and support.', 'woocommerce' ) ), 'button_label' => __( 'Renew', 'woocommerce' ), 'button_url' => self::add_utm_params_to_url_for_subscription_link( 'https://woocommerce.com/my-account/my-subscriptions/', 'renew' ), 'status' => 'expired', 'icon' => 'dashicons-info', );
$subscription['actions'][] = $action; }
if ( $subscription['expiring'] && ! $subscription['autorenew'] ) { $action = array( 'message' => __( 'Subscription is <strong>expiring</strong> soon.', 'woocommerce' ), 'button_label' => __( 'Enable auto-renew', 'woocommerce' ), 'button_url' => self::add_utm_params_to_url_for_subscription_link( 'https://woocommerce.com/my-account/my-subscriptions/', 'auto-renew' ), 'status' => 'expired', 'icon' => 'dashicons-info', );
$subscription['download_primary'] = false; $subscription['actions'][] = $action; } elseif ( $subscription['expiring'] ) { $action = array( 'message' => sprintf( __( 'This subscription is expiring soon. Please <strong>renew</strong> to continue receiving updates and support.', 'woocommerce' ) ), 'button_label' => __( 'Renew', 'woocommerce' ), 'button_url' => self::add_utm_params_to_url_for_subscription_link( 'https://woocommerce.com/my-account/my-subscriptions/', 'renew' ), 'status' => 'expired', 'icon' => 'dashicons-info', );
$subscription['download_primary'] = false; $subscription['actions'][] = $action; }
// Mark the first action primary. foreach ( $subscription['actions'] as $key => $action ) { if ( ! empty( $action['button_label'] ) ) { $subscription['actions'][ $key ]['primary'] = true; break; } } }
// Break the by-ref. unset( $subscription );
// Installed products without a subscription. $no_subscriptions = array(); foreach ( array_merge( $woo_plugins, $woo_themes ) as $filename => $data ) { if ( in_array( $data['_product_id'], $subscriptions_product_ids ) ) { continue; }
$data['_product_url'] = '#'; $data['_has_update'] = false;
if ( ! empty( $updates[ $data['_product_id'] ] ) ) { $data['_has_update'] = version_compare( $updates[ $data['_product_id'] ]['version'], $data['Version'], '>' );
if ( ! empty( $updates[ $data['_product_id'] ]['url'] ) ) { $data['_product_url'] = $updates[ $data['_product_id'] ]['url']; } elseif ( ! empty( $data['PluginURI'] ) ) { $data['_product_url'] = $data['PluginURI']; } }
$data['_actions'] = array();
if ( $data['_has_update'] ) { $action = array( /* translators: %s: version number */ 'message' => sprintf( __( 'Version %s is <strong>available</strong>. To enable this update you need to <strong>purchase</strong> a new subscription.', 'woocommerce' ), esc_html( $updates[ $data['_product_id'] ]['version'] ) ), 'button_label' => __( 'Purchase', 'woocommerce' ), 'button_url' => self::add_utm_params_to_url_for_subscription_link( $data['_product_url'], 'purchase' ), 'status' => 'expired', 'icon' => 'dashicons-info', );
$data['_actions'][] = $action; } else { $action = array( /* translators: 1: subscriptions docs 2: subscriptions docs */ 'message' => sprintf( __( 'To receive updates and support for this extension, you need to <strong>purchase</strong> a new subscription or consolidate your extensions to one connected account by <strong><a href="%1$s" title="Sharing Docs">sharing</a> or <a href="%2$s" title="Transferring Docs">transferring</a></strong> this extension to this connected account.', 'woocommerce' ), 'https://woocommerce.com/document/managing-woocommerce-com-subscriptions/#section-10', 'https://woocommerce.com/document/managing-woocommerce-com-subscriptions/#section-5' ), 'button_label' => __( 'Purchase', 'woocommerce' ), 'button_url' => self::add_utm_params_to_url_for_subscription_link( $data['_product_url'], 'purchase' ), 'status' => 'expired', 'icon' => 'dashicons-info', );
$data['_actions'][] = $action; }
$no_subscriptions[ $filename ] = $data; }
// Update the user id if it came from a migrated connection. if ( empty( $auth['user_id'] ) ) { $auth['user_id'] = get_current_user_id(); WC_Helper_Options::update( 'auth', $auth ); }
// Sort alphabetically. uasort( $subscriptions, array( __CLASS__, '_sort_by_product_name' ) ); uasort( $no_subscriptions, array( __CLASS__, '_sort_by_name' ) );
// Filters. self::get_filters_counts( $subscriptions ); // Warm it up. self::_filter( $subscriptions, self::get_current_filter() );
// We have an active connection. include self::get_view_filename( 'html-main.php' ); return; }
/** * Add tracking parameters to buttons (Renew, Purchase, etc.) on subscriptions page * * @param string $url URL to product page or to https://woocommerce.com/my-account/my-subscriptions/. * @param string $utm_content value of utm_content query parameter used for tracking * * @return string URL including utm parameters for tracking */ public static function add_utm_params_to_url_for_subscription_link( $url, $utm_content ) { $utm_params = 'utm_source=subscriptionsscreen&' . 'utm_medium=product&' . 'utm_campaign=wcaddons&' . 'utm_content=' . $utm_content;
// there are already some URL parameters if ( strpos( $url, '?' ) ) { return $url . '&' . $utm_params; }
return $url . '?' . $utm_params; }
/** * Get available subscriptions filters. * * @return array An array of filter keys and labels. */ public static function get_filters() { $filters = array( 'all' => __( 'All', 'woocommerce' ), 'active' => __( 'Active', 'woocommerce' ), 'inactive' => __( 'Inactive', 'woocommerce' ), 'installed' => __( 'Installed', 'woocommerce' ), 'update-available' => __( 'Update Available', 'woocommerce' ), 'expiring' => __( 'Expiring Soon', 'woocommerce' ), 'expired' => __( 'Expired', 'woocommerce' ), 'download' => __( 'Download', 'woocommerce' ), );
return $filters; }
/** * Get counts data for the filters array. * * @param array $subscriptions The array of all available subscriptions. * * @return array Filter counts (filter => count). */ public static function get_filters_counts( $subscriptions = null ) { static $filters;
if ( isset( $filters ) ) { return $filters; }
$filters = array_fill_keys( array_keys( self::get_filters() ), 0 ); if ( ! is_array( $subscriptions ) || empty( $subscriptions ) ) { return array(); }
foreach ( $filters as $key => $count ) { $_subs = $subscriptions; self::_filter( $_subs, $key ); $filters[ $key ] = count( $_subs ); }
return $filters; }
/** * Get current filter. * * @return string The current filter. */ public static function get_current_filter() { $current_filter = 'all'; $valid_filters = array_keys( self::get_filters() );
if ( ! empty( $_GET['filter'] ) && in_array( wp_unslash( $_GET['filter'] ), $valid_filters ) ) { $current_filter = wc_clean( wp_unslash( $_GET['filter'] ) ); }
return $current_filter; }
/** * Filter an array of subscriptions by $filter. * * @param array $subscriptions The subscriptions array, passed by ref. * @param string $filter The filter. */ private static function _filter( &$subscriptions, $filter ) { switch ( $filter ) { case 'active': $subscriptions = wp_list_filter( $subscriptions, array( 'active' => true ) ); break;
case 'inactive': $subscriptions = wp_list_filter( $subscriptions, array( 'active' => false ) ); break;
case 'installed': foreach ( $subscriptions as $key => $subscription ) { if ( empty( $subscription['local']['installed'] ) ) { unset( $subscriptions[ $key ] ); } } break;
case 'update-available': $subscriptions = wp_list_filter( $subscriptions, array( 'has_update' => true ) ); break;
case 'expiring': $subscriptions = wp_list_filter( $subscriptions, array( 'expiring' => true ) ); break;
case 'expired': $subscriptions = wp_list_filter( $subscriptions, array( 'expired' => true ) ); break;
case 'download': foreach ( $subscriptions as $key => $subscription ) { if ( $subscription['local']['installed'] || $subscription['expired'] ) { unset( $subscriptions[ $key ] ); } } break; } }
/** * Enqueue admin scripts and styles. */ public static function admin_enqueue_scripts() { $screen = get_current_screen(); $screen_id = $screen ? $screen->id : ''; $wc_screen_id = 'woocommerce';
if ( $wc_screen_id . '_page_wc-addons' === $screen_id && isset( $_GET['section'] ) && 'helper' === $_GET['section'] ) { wp_enqueue_style( 'woocommerce-helper', WC()->plugin_url() . '/assets/css/helper.css', array(), Constants::get_constant( 'WC_VERSION' ) ); wp_style_add_data( 'woocommerce-helper', 'rtl', 'replace' ); } }
/** * Various success/error notices. * * Runs during admin page render, so no headers/redirects here. * * @return array Array pairs of message/type strings with notices. */ private static function _get_return_notices() { $return_status = isset( $_GET['wc-helper-status'] ) ? wc_clean( wp_unslash( $_GET['wc-helper-status'] ) ) : null; $notices = array();
switch ( $return_status ) { case 'activate-success': $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0; $subscription = self::_get_subscriptions_from_product_id( $product_id ); $notices[] = array( 'type' => 'updated', 'message' => sprintf( /* translators: %s: product name */ __( '%s activated successfully. You will now receive updates for this product.', 'woocommerce' ), '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>' ), ); break;
case 'activate-error': $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0; $subscription = self::_get_subscriptions_from_product_id( $product_id ); $notices[] = array( 'type' => 'error', 'message' => sprintf( /* translators: %s: product name */ __( 'An error has occurred when activating %s. Please try again later.', 'woocommerce' ), '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>' ), ); break;
case 'deactivate-success': $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0; $subscription = self::_get_subscriptions_from_product_id( $product_id ); $local = self::_get_local_from_product_id( $product_id );
$message = sprintf( /* translators: %s: product name */ __( 'Subscription for %s deactivated successfully. You will no longer receive updates for this product.', 'woocommerce' ), '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>' );
if ( $local && is_plugin_active( $local['_filename'] ) && current_user_can( 'activate_plugins' ) ) { $deactivate_plugin_url = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'filter' => self::get_current_filter(), 'wc-helper-deactivate-plugin' => 1, 'wc-helper-product-id' => $subscription['product_id'], 'wc-helper-nonce' => wp_create_nonce( 'deactivate-plugin:' . $subscription['product_id'] ), ), admin_url( 'admin.php' ) );
$message = sprintf( /* translators: %1$s: product name, %2$s: deactivate url */ __( 'Subscription for %1$s deactivated successfully. You will no longer receive updates for this product. <a href="%2$s">Click here</a> if you wish to deactivate the plugin as well.', 'woocommerce' ), '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>', esc_url( $deactivate_plugin_url ) ); }
$notices[] = array( 'message' => $message, 'type' => 'updated', ); break;
case 'deactivate-error': $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0; $subscription = self::_get_subscriptions_from_product_id( $product_id ); $notices[] = array( 'type' => 'error', 'message' => sprintf( /* translators: %s: product name */ __( 'An error has occurred when deactivating the subscription for %s. Please try again later.', 'woocommerce' ), '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>' ), ); break;
case 'deactivate-plugin-success': $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0; $subscription = self::_get_subscriptions_from_product_id( $product_id ); $notices[] = array( 'type' => 'updated', 'message' => sprintf( /* translators: %s: product name */ __( 'The extension %s has been deactivated successfully.', 'woocommerce' ), '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>' ), ); break;
case 'deactivate-plugin-error': $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0; $subscription = self::_get_subscriptions_from_product_id( $product_id ); $notices[] = array( 'type' => 'error', 'message' => sprintf( /* translators: %1$s: product name, %2$s: plugins screen url */ __( 'An error has occurred when deactivating the extension %1$s. Please proceed to the <a href="%2$s">Plugins screen</a> to deactivate it manually.', 'woocommerce' ), '<strong>' . esc_html( $subscription['product_name'] ) . '</strong>', admin_url( 'plugins.php' ) ), ); break;
case 'helper-connected': $notices[] = array( 'message' => __( 'You have successfully connected your store to WooCommerce.com', 'woocommerce' ), 'type' => 'updated', ); break;
case 'helper-disconnected': $notices[] = array( 'message' => __( 'You have successfully disconnected your store from WooCommerce.com', 'woocommerce' ), 'type' => 'updated', ); break;
case 'helper-refreshed': $notices[] = array( 'message' => __( 'Authentication and subscription caches refreshed successfully.', 'woocommerce' ), 'type' => 'updated', ); break; }
return $notices; }
/** * Various early-phase actions with possible redirects. * * @param object $screen WP screen object. */ public static function current_screen( $screen ) { $wc_screen_id = 'woocommerce';
if ( $wc_screen_id . '_page_wc-addons' !== $screen->id ) { return; }
self::maybe_redirect_to_new_marketplace_installer();
if ( empty( $_GET['section'] ) || 'helper' !== $_GET['section'] ) { return; }
if ( ! empty( $_GET['wc-helper-connect'] ) ) { return self::_helper_auth_connect(); }
if ( ! empty( $_GET['wc-helper-return'] ) ) { return self::_helper_auth_return(); }
if ( ! empty( $_GET['wc-helper-disconnect'] ) ) { return self::_helper_auth_disconnect(); }
if ( ! empty( $_GET['wc-helper-refresh'] ) ) { return self::_helper_auth_refresh(); }
if ( ! empty( $_GET['wc-helper-activate'] ) ) { return self::_helper_subscription_activate(); }
if ( ! empty( $_GET['wc-helper-deactivate'] ) ) { return self::helper_subscription_deactivate(); }
if ( ! empty( $_GET['wc-helper-deactivate-plugin'] ) ) { return self::_helper_plugin_deactivate(); } }
/** * Maybe redirect to the new Marketplace installer. */ private static function maybe_redirect_to_new_marketplace_installer() { // phpcs:disable WordPress.Security.NonceVerification.Recommended // Redirect requires the "install" URL parameter to be passed. if ( empty( $_GET['install'] ) ) { return; }
wp_safe_redirect( self::get_helper_redirect_url( array( 'page' => 'wc-addons', 'section' => 'helper', ) ) ); }
/** * Get helper redirect URL. * * @param array $args Query args. * @return string */ private static function get_helper_redirect_url( $args = array() ) { global $current_screen;
// phpcs:disable WordPress.Security.NonceVerification.Recommended $redirect_admin_url = isset( $_GET['redirect_admin_url'] ) ? esc_url_raw( urldecode( // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized wp_unslash( $_GET['redirect_admin_url'] ) ) ) : ''; $install_product_key = isset( $_GET['install'] ) ? sanitize_text_field( wp_unslash( $_GET['install'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification.Recommended
if ( 'woocommerce_page_wc-addons' === $current_screen->id && FeaturesUtil::feature_is_enabled( 'marketplace' ) && ( false === empty( $redirect_admin_url ) || false === empty( $install_product_key ) ) ) { if ( strpos( $redirect_admin_url, admin_url( 'admin.php' ) ) === 0 ) { $new_url = $redirect_admin_url; } else { $new_url = add_query_arg( array( 'page' => 'wc-admin', 'tab' => 'my-subscriptions', 'path' => rawurlencode( '/extensions' ), ), admin_url( 'admin.php' ) ); }
if ( ! empty( $install_product_key ) ) { $new_url = add_query_arg( array( 'install' => $install_product_key, ), $new_url ); } return $new_url; }
return add_query_arg( $args, admin_url( 'admin.php' ) ); }
/** * Initiate a new OAuth connection. */ private static function _helper_auth_connect() { if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'connect' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized self::log( 'Could not verify nonce in _helper_auth_connect' ); wp_die( 'Could not verify nonce' ); }
$redirect_url_args = array( 'page' => 'wc-addons', 'section' => 'helper', 'wc-helper-return' => 1, 'wc-helper-nonce' => wp_create_nonce( 'connect' ), );
if ( isset( $_GET['install'] ) ) { $redirect_url_args['install'] = sanitize_text_field( wp_unslash( $_GET['install'] ) ); }
if ( isset( $_GET['utm_source'] ) ) { $redirect_url_args['utm_source'] = wc_clean( wp_unslash( $_GET['utm_source'] ) ); }
if ( isset( $_GET['utm_campaign'] ) ) { $redirect_url_args['utm_campaign'] = wc_clean( wp_unslash( $_GET['utm_campaign'] ) ); }
$redirect_uri = add_query_arg( $redirect_url_args, admin_url( 'admin.php' ) );
$request = WC_Helper_API::post( 'oauth/request_token', array( 'body' => array( 'home_url' => home_url(), 'redirect_uri' => $redirect_uri, ), ) );
$code = wp_remote_retrieve_response_code( $request );
if ( 200 !== $code ) { self::log( sprintf( 'Call to oauth/request_token returned a non-200 response code (%d)', $code ) ); wp_die( 'Something went wrong' ); }
$secret = json_decode( wp_remote_retrieve_body( $request ) ); if ( empty( $secret ) ) { self::log( sprintf( 'Call to oauth/request_token returned an invalid body: %s', wp_remote_retrieve_body( $request ) ) ); wp_die( 'Something went wrong' ); }
/** * Fires when the Helper connection process is initiated. */ do_action( 'woocommerce_helper_connect_start' );
$connect_url = add_query_arg( array( 'home_url' => rawurlencode( home_url() ), 'redirect_uri' => rawurlencode( $redirect_uri ), 'secret' => rawurlencode( $secret ), 'redirect_admin_url' => isset( $_GET['redirect_admin_url'] ) ? rawurlencode( esc_url_raw( urldecode( // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized wp_unslash( $_GET['redirect_admin_url'] ) ) ) ) : '', 'wum-installed' => WC_Woo_Update_Manager_Plugin::is_plugin_installed() ? '1' : '0', ), WC_Helper_API::url( 'oauth/authorize' ) );
wp_redirect( esc_url_raw( $connect_url ) ); die(); }
/** * Return from WooCommerce.com OAuth flow. */ private static function _helper_auth_return() { if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'connect' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized self::log( 'Could not verify nonce in _helper_auth_return' ); wp_die( 'Something went wrong' ); }
// Bail if the user clicked deny. if ( ! empty( $_GET['deny'] ) ) { /** * Fires when the Helper connection process is denied/cancelled. */ do_action( 'woocommerce_helper_denied' );
wp_safe_redirect( self::get_helper_redirect_url( array( 'page' => 'wc-addons', 'section' => 'helper', ) ) ); die(); }
// We do need a request token... if ( empty( $_GET['request_token'] ) ) { self::log( 'Request token not found in _helper_auth_return' ); wp_die( 'Something went wrong' ); }
// Obtain an access token. $request = WC_Helper_API::post( 'oauth/access_token', array( 'timeout' => 30, 'body' => array( 'request_token' => wp_unslash( $_GET['request_token'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized 'home_url' => home_url(), ), ) );
$code = wp_remote_retrieve_response_code( $request );
if ( 200 !== $code ) { self::log( sprintf( 'Call to oauth/access_token returned a non-200 response code (%d)', $code ) ); wp_die( 'Something went wrong' ); }
$access_token = json_decode( wp_remote_retrieve_body( $request ), true ); if ( ! $access_token ) { self::log( sprintf( 'Call to oauth/access_token returned an invalid body: %s', wp_remote_retrieve_body( $request ) ) ); wp_die( 'Something went wrong' ); }
self::update_auth_option( $access_token['access_token'], $access_token['access_token_secret'], $access_token['site_id'] );
/** * Fires when the Helper connection process has completed successfully. */ do_action( 'woocommerce_helper_connected' );
// Enable tracking when connected. if ( class_exists( 'WC_Tracker' ) ) { $prev_value = get_option( 'woocommerce_allow_tracking', 'no' ); update_option( 'woocommerce_allow_tracking', 'yes' ); WC_Tracker::send_tracking_data( true );
// Track woocommerce_allow_tracking_toggled in case was set as 'no' before. if ( class_exists( 'WC_Tracks' ) && 'no' === $prev_value ) { WC_Tracks::track_woocommerce_allow_tracking_toggled( $prev_value, 'yes', 'wccom_connect' ); } }
// If connecting through in-app purchase, redirects back to WooCommerce.com // for product installation. if ( ! empty( $_GET['wccom-install-url'] ) ) { wp_redirect( wp_unslash( $_GET['wccom-install-url'] ) ); exit; }
wp_safe_redirect( self::get_helper_redirect_url( array( 'page' => 'wc-addons', 'section' => 'helper', 'wc-helper-status' => 'helper-connected', ) ) ); die(); }
/** * Disconnect from WooCommerce.com, clear OAuth tokens. */ private static function _helper_auth_disconnect() { if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'disconnect' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized self::log( 'Could not verify nonce in _helper_auth_disconnect' ); wp_die( 'Could not verify nonce' ); }
/** * Fires when the Helper has been disconnected. */ do_action( 'woocommerce_helper_disconnected' );
$redirect_uri = self::get_helper_redirect_url( array( 'page' => 'wc-addons', 'section' => 'helper', 'wc-helper-status' => 'helper-disconnected', ) );
self::disconnect();
wp_safe_redirect( $redirect_uri ); die(); }
/** * User hit the Refresh button, clear all caches. */ private static function _helper_auth_refresh() { if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'refresh' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized self::log( 'Could not verify nonce in _helper_auth_refresh' ); wp_die( 'Could not verify nonce' ); }
self::refresh_helper_subscriptions();
$redirect_uri = self::get_helper_redirect_url( array( 'page' => 'wc-addons', 'section' => 'helper', 'filter' => self::get_current_filter(), 'wc-helper-status' => 'helper-refreshed', ) );
wp_safe_redirect( $redirect_uri ); die(); }
/** * Flush helper authentication cache. */ public static function refresh_helper_subscriptions() { /** * Fires when Helper subscriptions are refreshed. * * @since 8.3.0 */ do_action( 'woocommerce_helper_subscriptions_refresh' ); self::_flush_authentication_cache(); self::_flush_subscriptions_cache(); self::_flush_updates_cache(); self::flush_product_usage_notice_rules_cache(); }
/** * Active a product subscription. */ private static function _helper_subscription_activate() { $product_key = isset( $_GET['wc-helper-product-key'] ) ? wc_clean( wp_unslash( $_GET['wc-helper-product-key'] ) ) : ''; $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0;
if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'activate:' . $product_key ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized self::log( 'Could not verify nonce in _helper_subscription_activate' ); wp_die( 'Could not verify nonce' ); }
try { $activated = self::activate_helper_subscription( $product_key, $product_id ); } catch ( Exception $e ) { $activated = false; }
$redirect_uri = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'filter' => self::get_current_filter(), 'wc-helper-status' => $activated ? 'activate-success' : 'activate-error', 'wc-helper-product-id' => $product_id, ), admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_uri ); die(); }
/** * Activate helper subscription. * * @throws Exception If the subscription could not be activated or found. * @param string $product_key Subscription product key. * @return bool True if activated, false otherwise. */ public static function activate_helper_subscription( $product_key ) { $subscription = self::get_subscription( $product_key ); if ( ! $subscription ) { throw new Exception( __( 'Subscription not found', 'woocommerce' ) ); } $product_id = $subscription['product_id'];
// Activate subscription. list( $activation_response, $activated, $body ) = self::wccom_activate( $product_key );
if ( $activated ) { /** * Fires when the Helper activates a product successfully. * * @param int $product_id Product ID being activated. * @param string $product_key Subscription product key. * @param array $activation_response The response object from wp_safe_remote_request(). */ do_action( 'woocommerce_helper_subscription_activate_success', $product_id, $product_key, $activation_response ); } else { /** * Fires when the Helper fails to activate a product. * * @param int $product_id Product ID being activated. * @param string $product_key Subscription product key. * @param array $activation_response The response object from wp_safe_remote_request(). */ do_action( 'woocommerce_helper_subscription_activate_error', $product_id, $product_key, $activation_response ); throw new Exception( $body['message'] ?? __( 'Unknown error', 'woocommerce' ) ); }
// Attempt to activate this plugin. $local = self::_get_local_from_product_id( $product_id ); if ( $local && 'plugin' === $local['_type'] && current_user_can( 'activate_plugins' ) && ! is_plugin_active( $local['_filename'] ) ) { activate_plugin( $local['_filename'] ); }
self::_flush_subscriptions_cache(); self::_flush_updates_cache();
return $activated; }
/** * Deactivate a product subscription. */ private static function helper_subscription_deactivate() { $product_key = isset( $_GET['wc-helper-product-key'] ) ? wc_clean( wp_unslash( $_GET['wc-helper-product-key'] ) ) : ''; $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0;
if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'deactivate:' . $product_key ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized self::log( 'Could not verify nonce in helper_subscription_deactivate' ); wp_die( 'Could not verify nonce' ); }
try { $deactivated = self::deactivate_helper_subscription( $product_key ); } catch ( Exception $e ) { $deactivated = false; }
$redirect_uri = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'filter' => self::get_current_filter(), 'wc-helper-status' => $deactivated ? 'deactivate-success' : 'deactivate-error', 'wc-helper-product-id' => $product_id, ), admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_uri ); die(); }
/** * Deactivate a product subscription. * * @throws Exception If the subscription could not be deactivated or found. * @param string $product_key Subscription product key. * @return bool True if deactivated, false otherwise. */ public static function deactivate_helper_subscription( $product_key ) { $subscription = self::get_subscription( $product_key ); if ( ! $subscription ) { throw new Exception( __( 'Subscription not found', 'woocommerce' ) ); } $product_id = $subscription['product_id'];
$deactivation_response = WC_Helper_API::post( 'deactivate', array( 'authenticated' => true, 'body' => wp_json_encode( array( 'product_key' => $product_key, ) ), ) );
$code = wp_remote_retrieve_response_code( $deactivation_response ); $deactivated = 200 === $code;
if ( $deactivated ) { /** * Fires when the Helper activates a product successfully. * * @param int $product_id Product ID being deactivated. * @param string $product_key Subscription product key. * @param array $deactivation_response The response object from wp_safe_remote_request(). */ do_action( 'woocommerce_helper_subscription_deactivate_success', $product_id, $product_key, $deactivation_response ); } else { self::log( sprintf( 'Deactivate API call returned a non-200 response code (%d)', $code ) );
/** * Fires when the Helper fails to activate a product. * * @param int $product_id Product ID being deactivated. * @param string $product_key Subscription product key. * @param array $deactivation_response The response object from wp_safe_remote_request(). */ do_action( 'woocommerce_helper_subscription_deactivate_error', $product_id, $product_key, $deactivation_response );
$body = json_decode( wp_remote_retrieve_body( $deactivation_response ), true ); throw new Exception( $body['message'] ?? __( 'Unknown error', 'woocommerce' ) ); }
self::_flush_subscriptions_cache();
return $deactivated; }
/** * Get a subscriptions install URL. * * @param string $product_key Subscription product key. * @param string $product_slug Subscription product slug. * @return string */ public static function get_subscription_install_url( $product_key, $product_slug ) { $install_url = add_query_arg( array( 'product-key' => rawurlencode( $product_key ), ), self::get_install_base_url() . "{$product_slug}/" );
return WC_Helper_API::add_auth_parameters( $install_url ); }
/** * Deactivate a plugin. */ private static function _helper_plugin_deactivate() { $product_id = isset( $_GET['wc-helper-product-id'] ) ? absint( $_GET['wc-helper-product-id'] ) : 0; $deactivated = false;
if ( empty( $_GET['wc-helper-nonce'] ) || ! wp_verify_nonce( wp_unslash( $_GET['wc-helper-nonce'] ), 'deactivate-plugin:' . $product_id ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized self::log( 'Could not verify nonce in _helper_plugin_deactivate' ); wp_die( 'Could not verify nonce' ); }
if ( ! current_user_can( 'activate_plugins' ) ) { wp_die( 'You are not allowed to manage plugins on this site.' ); }
$local = wp_list_filter( array_merge( self::get_local_woo_plugins(), self::get_local_woo_themes() ), array( '_product_id' => $product_id ) );
// Attempt to deactivate this plugin or theme. if ( ! empty( $local ) ) { $local = array_shift( $local ); if ( is_plugin_active( $local['_filename'] ) ) { deactivate_plugins( $local['_filename'] ); }
$deactivated = ! is_plugin_active( $local['_filename'] ); }
$redirect_uri = add_query_arg( array( 'page' => 'wc-addons', 'section' => 'helper', 'filter' => self::get_current_filter(), 'wc-helper-status' => $deactivated ? 'deactivate-plugin-success' : 'deactivate-plugin-error', 'wc-helper-product-id' => $product_id, ), admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_uri ); die(); }
/** * Get a local plugin/theme entry from product_id. * * @param int $product_id The product id. * * @return array|bool The array containing the local plugin/theme data or false. */ private static function _get_local_from_product_id( $product_id ) { $local = wp_list_filter( array_merge( self::get_local_woo_plugins(), self::get_local_woo_themes() ), array( '_product_id' => $product_id ) );
if ( ! empty( $local ) ) { return array_shift( $local ); }
return false; }
/** * Checks whether current site has product subscription of a given ID. * * @since 3.7.0 * * @param int $product_id The product id. * * @return bool Returns true if product subscription exists, false otherwise. */ public static function has_product_subscription( $product_id ) { $subscription = self::_get_subscriptions_from_product_id( $product_id, true ); return ! empty( $subscription ); }
/** * Get the user's connected subscriptions that are installed on the current * site. * * @return array */ public static function get_installed_subscriptions() { static $installed_subscriptions = null;
// Cache installed_subscriptions in the current request. if ( is_null( $installed_subscriptions ) ) { $auth = WC_Helper_Options::get( 'auth' ); $site_id = isset( $auth['site_id'] ) ? absint( $auth['site_id'] ) : 0; if ( 0 === $site_id ) { $installed_subscriptions = array(); return $installed_subscriptions; }
$installed_subscriptions = array_filter( self::get_subscriptions(), function ( $subscription ) use ( $site_id ) { return in_array( $site_id, $subscription['connections'], true ); } ); }
return $installed_subscriptions; }
/** * Get the user's unconnected subscriptions. * * @return array */ public static function get_unconnected_subscriptions() { static $unconnected_subscriptions = null;
// Cache unconnected_subscriptions in the current request. if ( is_null( $unconnected_subscriptions ) ) { $auth = WC_Helper_Options::get( 'auth' ); $site_id = isset( $auth['site_id'] ) ? absint( $auth['site_id'] ) : 0; if ( 0 === $site_id ) { $unconnected_subscriptions = array(); return $unconnected_subscriptions; }
$unconnected_subscriptions = array_filter( self::get_subscriptions(), function ( $subscription ) use ( $site_id ) { return empty( $subscription['connections'] ); } ); }
return $unconnected_subscriptions; }
/** * Get subscription state of a given product ID. * * @since TBD * * @param int $product_id The product id. * * @return array Array of state_name => (bool) state */ public static function get_product_subscription_state( $product_id ) { $product_subscriptions = wp_list_filter( self::get_installed_subscriptions(), array( 'product_id' => $product_id ) );
$subscription = ! empty( $product_subscriptions ) ? array_shift( $product_subscriptions ) : array();
return array( 'unregistered' => empty( $subscription ), 'expired' => ( isset( $subscription['expired'] ) && $subscription['expired'] ), 'expiring' => ( isset( $subscription['expiring'] ) && $subscription['expiring'] ), 'key' => $subscription['product_key'] ?? '', 'order_id' => $subscription['order_id'] ?? '', ); }
/** * Get a subscription entry from product_id. If multiple subscriptions are * found with the same product id and $single is set to true, will return the * first one in the list, so you can use this method to get things like extension * name, version, etc. * * @param int $product_id The product id. * @param bool $single Whether to return a single subscription or all matching a product id. * * @return array|bool The array containing sub data or false. */ private static function _get_subscriptions_from_product_id( $product_id, $single = true ) { $subscriptions = wp_list_filter( self::get_subscriptions(), array( 'product_id' => $product_id ) ); if ( ! empty( $subscriptions ) ) { return $single ? array_shift( $subscriptions ) : $subscriptions; } return false; }
/** * Get locally installed plugins * * @return array */ public static function get_local_plugins() { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugins = get_plugins();
$output_plugins = array(); foreach ( $plugins as $filename => $data ) { array_push( $output_plugins, array( '_filename' => $filename, '_type' => 'plugin', 'slug' => dirname( $filename ), 'Version' => $data['Version'], ) ); }
return $output_plugins; }
/** * Get locally installed themes. * * @return array */ public static function get_local_themes() { if ( ! function_exists( 'wp_get_themes' ) ) { require_once ABSPATH . 'wp-admin/includes/theme.php'; } $themes = wp_get_themes();
$output_themes = array(); foreach ( $themes as $theme ) { array_push( $output_themes, array( '_filename' => $theme->get_stylesheet() . '/style.css', '_stylesheet' => $theme->get_stylesheet(), '_type' => 'theme', 'slug' => $theme->get_stylesheet(), 'Version' => $theme->get( 'Version' ), ) ); } return $output_themes; }
/** * Obtain a list of data about locally installed Woo extensions. */ public static function get_local_woo_plugins() { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; }
$plugins = get_plugins();
/** * Check if plugins have WC headers, if not then clear cache and fetch again. * WC Headers will not be present if `wc_enable_wc_plugin_headers` hook was added after a `get_plugins` call -- for example when WC is activated/updated. * Also, get_plugins call is expensive, so we should clear this cache very conservatively. */ if ( ! empty( $plugins ) && ! array_key_exists( 'Woo', current( $plugins ) ) ) { wp_clean_plugins_cache( false ); $plugins = get_plugins(); }
$woo_plugins = array();
// Backwards compatibility for woothemes_queue_update(). $_compat = array(); if ( ! empty( $GLOBALS['woothemes_queued_updates'] ) ) { foreach ( $GLOBALS['woothemes_queued_updates'] as $_compat_plugin ) { $_compat[ $_compat_plugin->file ] = array( 'product_id' => $_compat_plugin->product_id, 'file_id' => $_compat_plugin->file_id, ); } }
foreach ( $plugins as $filename => $data ) { if ( empty( $data['Woo'] ) && ! empty( $_compat[ $filename ] ) ) { $data['Woo'] = sprintf( '%d:%s', $_compat[ $filename ]['product_id'], $_compat[ $filename ]['file_id'] ); }
if ( empty( $data['Woo'] ) ) { continue; }
list( $product_id, $file_id ) = explode( ':', $data['Woo'] ); if ( empty( $product_id ) || empty( $file_id ) ) { continue; }
// Omit the WooCommerce plugin used on Woo Express sites. if ( 'WooCommerce' === $data['Name'] ) { continue; }
$data['_filename'] = $filename; $data['_product_id'] = absint( $product_id ); $data['_file_id'] = $file_id; $data['_type'] = 'plugin'; $data['slug'] = dirname( $filename ); $woo_plugins[ $filename ] = $data; }
return $woo_plugins; }
/** * Get locally installed Woo themes. */ public static function get_local_woo_themes() { $themes = wp_get_themes(); $woo_themes = array();
foreach ( $themes as $theme ) { $header = $theme->get( 'Woo' );
// Backwards compatibility for theme_info.txt. if ( ! $header ) { $txt = $theme->get_stylesheet_directory() . '/theme_info.txt'; if ( is_readable( $txt ) ) { $txt = file_get_contents( $txt ); $txt = preg_split( '#\s#', $txt ); if ( is_array( $txt ) && count( $txt ) >= 2 ) { $header = sprintf( '%d:%s', $txt[0], $txt[1] ); } } }
if ( empty( $header ) ) { continue; }
list( $product_id, $file_id ) = explode( ':', $header ); if ( empty( $product_id ) || empty( $file_id ) ) { continue; }
$data = array( 'Name' => $theme->get( 'Name' ), 'Version' => $theme->get( 'Version' ), 'Woo' => $header,
'_filename' => $theme->get_stylesheet() . '/style.css', '_stylesheet' => $theme->get_stylesheet(), '_product_id' => absint( $product_id ), '_file_id' => $file_id, '_type' => 'theme', 'slug' => dirname( $theme->get_stylesheet() ), );
$woo_themes[ $data['_filename'] ] = $data; }
return $woo_themes; }
/** * Get rules for displaying notice regarding marketplace product usage. * * @return array */ public static function get_product_usage_notice_rules() { $cache_key = '_woocommerce_helper_product_usage_notice_rules'; $data = get_transient( $cache_key ); if ( false !== $data ) { return $data; }
$request = WC_Helper_API::get( 'product-usage-notice-rules', array( 'authenticated' => false, 'timeout' => 2, ) );
// Retry in 15 minutes for non-200 response. if ( wp_remote_retrieve_response_code( $request ) !== 200 ) { set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS ); return array(); }
$data = json_decode( wp_remote_retrieve_body( $request ), true ); if ( empty( $data ) || ! is_array( $data ) ) { $data = array(); }
set_transient( $cache_key, $data, 1 * HOUR_IN_SECONDS ); return $data; }
/** * Verify request hash created by WooCommerce.com. * * @param string $request_hash request hash to be verified. * @return bool */ public static function verify_request_hash( string $request_hash ): bool { $request = WC_Helper_API::get( 'verify-request-hash', array( 'authenticated' => true, 'query_string' => '?request_hash=' . $request_hash, ) );
if ( wp_remote_retrieve_response_code( $request ) !== 200 ) { return false; }
$data = json_decode( wp_remote_retrieve_body( $request ), true );
return isset( $data['success'] ) && true === $data['success']; }
/** * Get the connected user's subscriptions. * * @return array */ public static function get_subscriptions() { $cache_key = '_woocommerce_helper_subscriptions'; $data = get_transient( $cache_key ); if ( false !== $data ) { return $data; }
$request_uri = wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $source = ''; if ( stripos( $request_uri, 'wc-addons' ) ) : $source = 'my-subscriptions'; elseif ( stripos( $request_uri, 'plugins.php' ) ) : $source = 'plugins'; elseif ( stripos( $request_uri, 'wc-admin' ) ) : $source = 'inbox-notes'; elseif ( stripos( $request_uri, 'admin-ajax.php' ) ) : $source = 'heartbeat-api'; elseif ( stripos( $request_uri, 'installer' ) ) : $source = 'wccom-site-installer'; elseif ( defined( 'WP_CLI' ) && WP_CLI ) : $source = 'wc-cli'; endif;
// Obtain the connected user info. $request = WC_Helper_API::get( 'subscriptions', array( 'authenticated' => true, 'query_string' => '' !== $source ? esc_url( '?source=' . $source ) : '', ) );
if ( wp_remote_retrieve_response_code( $request ) !== 200 ) { set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS ); return array(); }
$data = json_decode( wp_remote_retrieve_body( $request ), true ); if ( empty( $data ) || ! is_array( $data ) ) { $data = array(); }
set_transient( $cache_key, $data, 1 * HOUR_IN_SECONDS ); return $data; }
/** * Get subscription data for a given product key. * * @param string $product_key Subscription product key. * @return array|bool The array containing sub data or false. */ public static function get_subscription( $product_key ) { $subscriptions = wp_list_filter( self::get_subscriptions(), array( 'product_key' => $product_key ) );
if ( empty( $subscriptions ) ) { return false; }
$subscription = array_shift( $subscriptions ); $subscription['local'] = self::get_subscription_local_data( $subscription ); return $subscription; }
/** * Get the connected user's subscription list data. Here, we merge connected * subscriptions with locally installed Woo plugins and themes. We also * add in information about available updates. * * Used by the My Subscriptions page. * * @return array */ public static function get_subscription_list_data() { // First, connected subscriptions. $subscriptions = self::get_subscriptions();
// Then, installed plugins and themes, with or without an active subscription. $woo_plugins = self::get_local_woo_plugins(); $woo_themes = self::get_local_woo_themes();
// Get the product IDs of the subscriptions. $subscriptions_product_ids = wp_list_pluck( $subscriptions, 'product_id' );
// Get the site ID. $auth = WC_Helper_Options::get( 'auth' ); $site_id = isset( $auth['site_id'] ) ? absint( $auth['site_id'] ) : 0;
// Now, merge installed products without a subscription. foreach ( array_merge( $woo_plugins, $woo_themes ) as $filename => $data ) { if ( in_array( $data['_product_id'], $subscriptions_product_ids, true ) ) { continue; }
// We add these as subscriptions to the previous connected subscriptions list. $subscriptions[] = array( 'product_key' => '', 'product_id' => $data['_product_id'], 'product_name' => $data['Name'], 'product_url' => $data['PluginURI'] ?? '', 'zip_slug' => $data['slug'], 'documentation_url' => '', 'key_type' => '', 'key_type_label' => '', 'lifetime' => false, 'product_status' => 'publish', // Connections is empty because this is not a connected subscription. 'connections' => array(), 'expires' => 0, 'expired' => true, 'expiring' => false, 'sites_max' => 0, 'sites_active' => 0, 'autorenew' => false, 'maxed' => false, ); }
// Fetch updates so we can refine subscriptions with information about updates. $updates = WC_Helper_Updater::get_update_data();
// Add local data to merged subscriptions list (both locally installed and purchased). foreach ( $subscriptions as &$subscription ) { $subscription['active'] = in_array( $site_id, $subscription['connections'], true );
$subscription['local'] = self::get_subscription_local_data( $subscription );
$subscription['has_update'] = false; if ( $subscription['local']['installed'] && ! empty( $updates[ $subscription['product_id'] ] ) ) { $subscription['has_update'] = version_compare( $updates[ $subscription['product_id'] ]['version'], $subscription['local']['version'], '>' ); }
if ( ! empty( $updates[ $subscription['product_id'] ] ) ) { $subscription['version'] = $updates[ $subscription['product_id'] ]['version']; }
// If the update endpoint returns a URL, we prefer it over the default PluginURI. if ( ! empty( $updates[ $subscription['product_id'] ]['url'] ) ) { $subscription['product_url'] = $updates[ $subscription['product_id'] ]['url']; } }
// Sort subscriptions by name and expiration date. usort( $subscriptions, function ( $a, $b ) { $compare_value = strcasecmp( $a['product_name'], $b['product_name'] ); if ( 0 === $compare_value ) { return strcasecmp( $a['expires'], $b['expires'] ); } return $compare_value; } );
// Add subscription install flags after the active and local data is set. foreach ( $subscriptions as &$subscription ) { $subscription['subscription_available'] = self::is_subscription_available( $subscription, $subscriptions ); $subscription['subscription_installed'] = self::is_subscription_installed( $subscription, $subscriptions ); }
// Break the by-ref. unset( $subscription );
return $subscriptions; }
/** * Check if a subscription is available to use. * That is, is not already active and hasn't expired, and there are no other subscriptions * for this product already active on this site. * * @param array $subscription The subscription we're checking. * @param array $subscriptions The list of all the user's subscriptions. * @return bool True if multiple licenses exist, false otherwise. */ public static function is_subscription_available( $subscription, $subscriptions ) { if ( true === $subscription['active'] ) { return false; }
if ( true === $subscription['expired'] ) { return false; }
$product_subscriptions = wp_list_filter( $subscriptions, array( 'product_id' => $subscription['product_id'], 'active' => true, ) );
// If there are no subscriptions for this product already active on this site, then it's available. if ( empty( $product_subscriptions ) ) { return true; }
return false; }
/** * Check if product relating to a subscription is installed. * This method will return true if the product is installed, but will exclude subscriptions for the same product that are not in use. * If a product is installed and inactive, this will ensure that one subscription is marked as installed. * * @param array $subscription The subscription we're checking. * @param array $subscriptions The list of all the user's subscriptions. * @return bool True if installed, false otherwise. */ public static function is_subscription_installed( $subscription, $subscriptions ) { if ( false === $subscription['local']['installed'] ) { return false; }
// If the subscription is active, then it's installed. if ( true === $subscription['active'] ) { return true; }
$product_subscriptions = wp_list_filter( $subscriptions, array( 'product_id' => $subscription['product_id'], ) ); if ( empty( $product_subscriptions ) ) { return false; }
// If there are no other subscriptions for this product, then it's installed. if ( 1 === count( $product_subscriptions ) ) { return true; }
$active_subscription = wp_list_filter( $product_subscriptions, array( 'active' => true, ) );
// If there is another active subscription, this subscription is not installed. // If the current subscription is active, it would already return true above. if ( ! empty( $active_subscription ) ) { return false; }
// Find subscriptions that can be activated. $product_subscriptions_without_maxed_connections = wp_list_filter( $product_subscriptions, array( 'maxed' => false, ) );
if ( 0 < count( $product_subscriptions_without_maxed_connections ) ) { // Pick the first subscription available for activation. $product_subscription = array_shift( $product_subscriptions_without_maxed_connections ); } else { // If there are multiple subscriptions, but no active subscriptions, then mark the first one as installed. $product_subscription = array_shift( $product_subscriptions ); }
if ( $product_subscription['product_key'] === $subscription['product_key'] ) { return true; }
return false; }
/** * Add local data to a subscription. * * @param array $subscription The subscription data. * @return array The subscription data with local data added. */ public static function get_subscription_local_data( array $subscription ) { $local_plugins = self::get_local_plugins(); $local_themes = self::get_local_themes();
$installed_product = wp_list_filter( array_merge( $local_plugins, $local_themes ), array( 'slug' => $subscription['zip_slug'] ) ); $installed_product = array_shift( $installed_product );
if ( empty( $installed_product ) ) { return array( 'installed' => false, 'active' => false, 'version' => null, 'type' => null, 'slug' => null, 'path' => null, ); }
$local_data = array( 'installed' => true, 'active' => false, 'version' => $installed_product['Version'], 'type' => $installed_product['_type'], 'slug' => null, 'path' => $installed_product['_filename'], );
if ( 'plugin' === $installed_product['_type'] ) { $local_data['slug'] = $installed_product['slug']; if ( is_plugin_active( $installed_product['_filename'] ) ) { $local_data['active'] = true; } elseif ( is_multisite() && is_plugin_active_for_network( $installed_product['_filename'] ) ) { $local_data['active'] = true; } } elseif ( 'theme' === $installed_product['_type'] ) { $local_data['slug'] = $installed_product['_stylesheet']; if ( in_array( $installed_product['_stylesheet'], array( get_stylesheet(), get_template() ), true ) ) { $local_data['active'] = true; } }
return $local_data; }
/** * Runs when any plugin is activated. * * Depending on the activated plugin attempts to look through available * subscriptions and auto-activate one if possible, so the user does not * need to visit the Helper UI at all after installing a new extension. * * @param string $filename The filename of the activated plugin. */ public static function activated_plugin( $filename ) { $plugins = self::get_local_woo_plugins();
// Not a local woo plugin. if ( empty( $plugins[ $filename ] ) ) { return; }
// Make sure we have a connection. $auth = WC_Helper_Options::get( 'auth' ); if ( empty( $auth ) ) { return; }
$plugin = $plugins[ $filename ]; $product_id = $plugin['_product_id']; $subscription = self::get_available_subscription( $product_id );
// No valid subscription found. if ( ! $subscription ) { return; }
$product_key = $subscription['product_key']; list( $activation_response, $activated, $body ) = self::wccom_activate( $product_key );
if ( $activated ) { self::log( 'Auto-activated a subscription for ' . $filename ); /** * Fires when the Helper activates a product successfully. * * @param int $product_id Product ID being activated. * @param string $product_key Subscription product key. * @param array $activation_response The response object from wp_safe_remote_request(). * @since 9.7 */ do_action( 'woocommerce_helper_subscription_activate_success', $product_id, $product_key, $activation_response ); } else { self::log( 'Could not activate a subscription upon plugin activation: ' . $filename );
/** * Fires when the Helper fails to activate a product. * * @param int $product_id Product ID being activated. * @param string $product_key Subscription product key. * @param array $activation_response The response object from wp_safe_remote_request(). * @since 9.7 */ do_action( 'woocommerce_helper_subscription_activate_error', $product_id, $product_key, $activation_response ); }
self::_flush_subscriptions_cache(); self::_flush_updates_cache(); }
/** * Connect theme to the WCCOM. * * Depending on the activated theme attempts to look through available * subscriptions and auto-activate one if possible, so the user does not * need to visit the Helper UI at all after installing a new extension. * * @param string $product_id The product id of the activated theme. */ public static function connect_theme( $product_id ) { // Make sure we have a connection. $auth = WC_Helper_Options::get( 'auth' ); if ( empty( $auth ) ) { return; }
wp_clean_themes_cache( false ); $themes = self::get_local_woo_themes();
$themes = array_filter( $themes, function ( $theme ) use ( $product_id ) { return $theme['_product_id'] === $product_id; } );
if ( empty( $themes ) ) { return; }
$theme = reset( $themes );
$subscription = self::get_available_subscription( $product_id );
// No valid subscription found. if ( ! $subscription ) { return; }
$product_key = $subscription['product_key']; list( $activation_response, $activated, $body ) = self::wccom_activate( $product_key );
if ( $activated ) { self::log( 'Auto-activated a subscription for ' . $theme['Name'] ); /** * Fires when the Helper activates a product successfully. * * @param int $product_id Product ID being activated. * @param string $product_key Subscription product key. * @param array $activation_response The response object from wp_safe_remote_request(). */ do_action( 'woocommerce_helper_subscription_activate_success', $product_id, $product_key, $activation_response ); } else { self::log( 'Could not activate a subscription for theme: ' . $theme['Name'] );
/** * Fires when the Helper fails to activate a product. * * @param int $product_id Product ID being activated. * @param string $product_key Subscription product key. * @param array $activation_response The response object from wp_safe_remote_request(). */ do_action( 'woocommerce_helper_subscription_activate_error', $product_id, $product_key, $activation_response ); }
self::_flush_subscriptions_cache(); self::_flush_updates_cache(); }
/** * Runs when any plugin is deactivated. * * When a user deactivates a plugin, attempt to deactivate any subscriptions * associated with the extension. * * @param string $filename The filename of the deactivated plugin. */ public static function deactivated_plugin( $filename ) { $plugins = self::get_local_woo_plugins();
// Not a local woo plugin. if ( empty( $plugins[ $filename ] ) ) { return; }
// Make sure we have a connection. $auth = WC_Helper_Options::get( 'auth' ); if ( empty( $auth ) ) { return; }
$plugin = $plugins[ $filename ]; $product_id = $plugin['_product_id']; $subscriptions = self::_get_subscriptions_from_product_id( $product_id, false ); $site_id = absint( $auth['site_id'] );
// No valid subscriptions for this product. if ( empty( $subscriptions ) ) { return; }
$deactivated = 0;
foreach ( $subscriptions as $subscription ) { // Don't touch subscriptions that aren't activated on this site. if ( ! in_array( $site_id, $subscription['connections'], true ) ) { continue; }
$product_key = $subscription['product_key']; $deactivation_response = WC_Helper_API::post( 'deactivate', array( 'authenticated' => true, 'body' => wp_json_encode( array( 'product_key' => $product_key, ) ), ) );
if ( wp_remote_retrieve_response_code( $deactivation_response ) === 200 ) { ++$deactivated;
/** * Fires when the Helper activates a product successfully. * * @param int $product_id Product ID being deactivated. * @param string $product_key Subscription product key. * @param array $deactivation_response The response object from wp_safe_remote_request(). */ do_action( 'woocommerce_helper_subscription_deactivate_success', $product_id, $product_key, $deactivation_response ); } else { /** * Fires when the Helper fails to activate a product. * * @param int $product_id Product ID being deactivated. * @param string $product_key Subscription product key. * @param array $deactivation_response The response object from wp_safe_remote_request(). */ do_action( 'woocommerce_helper_subscription_deactivate_error', $product_id, $product_key, $deactivation_response ); } }
if ( $deactivated ) { self::log( sprintf( 'Auto-deactivated %d subscription(s) for %s', $deactivated, $filename ) ); self::_flush_subscriptions_cache(); self::_flush_updates_cache(); } }
/** * Various Helper-related admin notices. */ public static function admin_notices() { if ( apply_filters( 'woocommerce_helper_suppress_admin_notices', false ) ) { return; }
$screen = get_current_screen(); $screen_id = $screen ? $screen->id : '';
if ( 'update-core' !== $screen_id ) { return; }
// Don't nag if Woo doesn't have an update available. if ( ! self::_woo_core_update_available() ) { return; }
// Add a note about available extension updates if Woo core has an update available. $notice = self::_get_extensions_update_notice(); if ( ! empty( $notice ) ) { echo '<div class="updated woocommerce-message"><p>' . $notice . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } }
/** * Get an update notice if one or more Woo extensions has an update available. * * @return string|null The update notice or null if everything is up to date. */ private static function _get_extensions_update_notice() { $plugins = self::get_local_woo_plugins(); $updates = WC_Helper_Updater::get_update_data(); $available = 0;
foreach ( $plugins as $data ) { if ( empty( $updates[ $data['_product_id'] ] ) ) { continue; }
$product_id = $data['_product_id']; if ( version_compare( $updates[ $product_id ]['version'], $data['Version'], '>' ) ) { ++$available; } }
if ( ! $available ) { return; }
return sprintf( /* translators: %1$s: helper url, %2$d: number of extensions */ _n( 'Note: You currently have <a href="%1$s">%2$d paid extension</a> which should be updated first before updating WooCommerce.', 'Note: You currently have <a href="%1$s">%2$d paid extensions</a> which should be updated first before updating WooCommerce.', $available, 'woocommerce' ), admin_url( 'admin.php?page=wc-addons§ion=helper' ), $available ); }
/** * Whether WooCommerce has an update available. * * @return bool True if a Woo core update is available. */ private static function _woo_core_update_available() { $updates = get_site_transient( 'update_plugins' ); if ( empty( $updates->response ) ) { return false; }
if ( empty( $updates->response['woocommerce/woocommerce.php'] ) ) { return false; }
$data = $updates->response['woocommerce/woocommerce.php']; if ( version_compare( Constants::get_constant( 'WC_VERSION' ), $data->new_version, '>=' ) ) { return false; }
return true; }
/** * Flush subscriptions cache. */ public static function _flush_subscriptions_cache() { delete_transient( '_woocommerce_helper_subscriptions' ); }
/** * Flush product-usage-notice-rules cache. */ public static function flush_product_usage_notice_rules_cache() { delete_transient( '_woocommerce_helper_product_usage_notice_rules' ); }
/** * Flush auth cache. */ public static function _flush_authentication_cache() { $request = WC_Helper_API::get( 'oauth/me', array( 'authenticated' => true, 'timeout' => 30, ) );
if ( wp_remote_retrieve_response_code( $request ) !== 200 ) { return false; }
$user_data = json_decode( wp_remote_retrieve_body( $request ), true ); if ( ! $user_data ) { return false; }
WC_Helper_Options::update( 'auth_user_data', array( 'name' => $user_data['name'], 'email' => $user_data['email'], ) );
return true; }
/** * Flush updates cache. */ private static function _flush_updates_cache() { WC_Helper_Updater::flush_updates_cache(); }
/** * Sort subscriptions by the product_name. * * @param array $a Subscription array. * @param array $b Subscription array. * * @return int */ public static function _sort_by_product_name( $a, $b ) { return strcmp( $a['product_name'], $b['product_name'] ); }
/** * Sort subscriptions by the Name. * * @param array $a Product array. * @param array $b Product array. * * @return int */ public static function _sort_by_name( $a, $b ) { return strcmp( $a['Name'], $b['Name'] ); }
/** * Log a helper event. * * @param string $message Log message. * @param string $level Optional, defaults to info, valid levels: emergency|alert|critical|error|warning|notice|info|debug. */ public static function log( $message, $level = 'info' ) { if ( ! Constants::is_true( 'WP_DEBUG' ) ) { return; }
if ( ! isset( self::$log ) ) { self::$log = wc_get_logger(); }
self::$log->log( $level, $message, array( 'source' => 'helper' ) ); }
/** * Handles WC Helper disconnect tasks. * * @return void */ public static function disconnect() { WC_Helper_API::post( 'oauth/invalidate_token', array( 'authenticated' => true, ) );
$data = WC_Helper_Options::get( 'auth_user_data' ); WC_Helper_Options::update( 'last_disconnected_user_data', $data ); // Ignore all previously dismissed disconnect notices. delete_metadata( 'user', 0, PluginsHelper::DISMISS_DISCONNECT_NOTICE, '', true );
WC_Helper_Options::update( 'auth', array() ); WC_Helper_Options::update( 'auth_user_data', array() );
self::_flush_subscriptions_cache(); self::_flush_updates_cache(); self::flush_product_usage_notice_rules_cache(); }
/** * Checks if `access_token` exists in `auth` option. * * @return bool */ public static function is_site_connected(): bool { $auth = WC_Helper_Options::get( 'auth' );
// If `access_token` is empty, there's no active connection. return ! empty( $auth['access_token'] ); }
/** * Allows to connect with WCCOM using application password. used it to connect via CLI * * @param string $password The application password. * * @return void|WP_Error */ public static function connect_with_password( string $password ) { $request = WC_Helper_API::post( 'connect', array( 'headers' => array( 'X-API-Key' => $password, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'home_url' => home_url() ) ), 'authenticated' => false, ) );
$code = wp_remote_retrieve_response_code( $request );
if ( $code === 403 ) { $message = 'Invalid password'; self::log( $message );
return new WP_Error( 'connect-with-password-invalid-password', $message ); } elseif ( $code !== 200 ) { $message = sprintf( 'Call to /connect returned a non-200 response code (%d)', $code ); self::log( $message );
return new WP_Error( 'connect-with-password-' . $code, $message ); }
$access_data = json_decode( wp_remote_retrieve_body( $request ), true ); if ( empty( $access_data['access_token'] ) || empty( $access_data['access_token_secret'] ) ) { $message = sprintf( 'Call to /connect returned an invalid body: %s', wp_remote_retrieve_body( $request ) ); self::log( $message );
return new WP_Error( 'connect-with-password-invalid-response', $message ); }
self::update_auth_option( $access_data['access_token'], $access_data['access_token_secret'], $access_data['site_id'] ); }
/** * Updates auth options and flushes cache * * @param string $access_token The access token. * @param string $access_token_secret The secret access token. * @param int $site_id The site id returned by the API. * * @return void */ public static function update_auth_option( string $access_token, string $access_token_secret, int $site_id ): void { WC_Helper_Options::update( 'auth', array( 'access_token' => $access_token, 'access_token_secret' => $access_token_secret, 'site_id' => $site_id, 'user_id' => get_current_user_id(), 'updated' => time(), ) );
// Obtain the connected user info. if ( ! self::_flush_authentication_cache() ) { self::log( 'Could not obtain connected user info in _helper_auth_return.' ); WC_Helper_Options::update( 'auth', array() ); wp_die( 'Something went wrong. Could not obtain connected user info in _helper_auth_return.' ); }
self::_flush_subscriptions_cache(); self::_flush_updates_cache(); self::flush_product_usage_notice_rules_cache(); }
/** * Get WooCommerce.com base URL. * * @return string */ public static function get_woocommerce_com_base_url() { /** * Filter the base URL used to install the Woo hosted plugins. * * @since 8.7.0 */ return trailingslashit( apply_filters( 'woo_com_base_url', 'https://woocommerce.com/' ) ); }
/** * Get base URL for plugin auto installer. * * @return string */ public static function get_install_base_url() { return self::get_woocommerce_com_base_url() . 'auto-install-init/'; }
/** * Retrieve notice for connected store. * * @return array An array containing notice data. */ public static function get_notices() { $cache_key = '_woocommerce_helper_notices'; $cached_data = get_transient( $cache_key );
if ( false !== $cached_data ) { return $cached_data; }
// Fetch notice data for connected store. $request = WC_Helper_API::get( 'notices', array( 'authenticated' => true, ) );
if ( 200 !== wp_remote_retrieve_response_code( $request ) ) { set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS ); return array(); }
$data = json_decode( wp_remote_retrieve_body( $request ), true );
if ( empty( $data ) || ! is_array( $data ) ) { $data = array(); }
set_transient( $cache_key, $data, 1 * HOUR_IN_SECONDS ); return $data; }
/** * Activate the product subscription to WCCOM * * @param string $product_key the product key to be activated. * * @return array */ protected static function wccom_activate( $product_key ): array { $activation_response = WC_Helper_API::post( 'activate', array( 'authenticated' => true, 'body' => wp_json_encode( array( 'product_key' => $product_key, ) ), ) );
$activated = wp_remote_retrieve_response_code( $activation_response ) === 200; $body = json_decode( wp_remote_retrieve_body( $activation_response ), true );
if ( ! $activated && ! empty( $body['code'] ) && 'already_connected' === $body['code'] ) { $activated = true; }
return array( $activation_response, $activated, $body ); }
/** * Get subscriptions for a product if it is available * * @param string|int $product_id the product id to get subscriptions for. * * @return mixed|null */ protected static function get_available_subscription( $product_id ) { $subscriptions = self::_get_subscriptions_from_product_id( $product_id, false );
// No valid subscriptions for this product. if ( empty( $subscriptions ) ) { return null; }
$subscription = null; foreach ( $subscriptions as $_sub ) {
// Don't attempt to activate expired subscriptions. if ( $_sub['expired'] ) { continue; }
// No more sites available in this subscription. if ( isset( $_sub['maxed'] ) && $_sub['maxed'] ) { continue; }
// Looks good. $subscription = $_sub; break; }
return $subscription; } }
WC_Helper::load();
|