updates.php (6669B)
1 <?php 2 3 function materialis_force_check_updates() { 4 materialis_force_check_plugins_update(); 5 materialis_force_check_themes_update(); 6 } 7 8 function materialis_force_check_plugins_update() { 9 $transient = get_site_transient('update_plugins'); 10 if ($transient) { 11 foreach ($transient->checked as $path => $version) { 12 if (strpos($path, "materialis-companion") !== FALSE) { 13 if (isset($transient->no_update[$path])) { 14 unset($transient->no_update[$path]); 15 unset($transient->checked[$path]); 16 set_site_transient('update_plugins', $transient); 17 } 18 break; 19 } 20 } 21 } 22 //wp_update_plugins(); 23 } 24 25 function materialis_force_check_themes_update() { 26 $transient = get_site_transient('update_themes'); 27 if ($transient) { 28 //unset($transient->response['materialis-pro']); 29 //set_site_transient('update_themes', $transient); 30 //print_r(get_site_transient('update_themes')); 31 if (!isset($transient->response['materialis-pro'])) { 32 if (isset($transient->checked['materialis-pro'])) { 33 unset($transient->checked['materialis-pro']); 34 set_site_transient('update_themes', $transient); 35 } 36 } 37 } 38 //wp_update_themes(); 39 } 40 41 42 43 function materialis_get_available_updates() { 44 45 $needs_update = array(); 46 47 $themes = get_theme_updates(); 48 49 if ($themes && isset($themes['materialis-pro'])) { 50 $theme = $themes['materialis-pro']; 51 $needs_update['themes'] = array(array( 52 "version" => $theme->update['new_version'], 53 "name" => $theme->get("Name") 54 )); 55 } 56 57 $plugins = get_plugin_updates(); 58 if ($plugins) { 59 foreach ($plugins as $file => $plugin) { 60 if ($plugin->TextDomain == 'materialis-companion') { 61 $needs_update['plugins'] = array(array( 62 "version" => $plugin->update->new_version, 63 "name" => $plugin->Name, 64 )); 65 } 66 } 67 } 68 69 return $needs_update; 70 } 71 72 function materialis_get_updates_msg(){ 73 $updates = materialis_get_available_updates(); 74 75 $msg = ""; 76 77 if (isset($updates['themes'])) { 78 for ($i=0; $i < count($updates['themes']); $i++) { 79 $update = $updates['themes'][$i]; 80 $msg .= "<h1>New version (" . $update['version'] . ") available for " . $update['name'] . "</h1>"; 81 } 82 } 83 84 if (isset($updates['plugins'])) { 85 for ($i=0; $i < count($updates['plugins']); $i++) { 86 $update = $updates['plugins'][$i]; 87 $msg .= "<h1>New version (" . $update['version'] . ") available for " . $update['name'] . "</h1>"; 88 } 89 } 90 91 if ($msg) { 92 $msg .= '<h2>Please update to the latest versions before editing in Customizer.</h2>'; 93 $msg .= '<br/>'; 94 $msg .= '<a href="' . get_admin_url(null, "update-core.php") . '" class="button button-orange">Go to updates</a> '; 95 } 96 97 return $msg; 98 } 99 100 add_action("admin_init", function () { 101 global $pagenow; 102 103 try { 104 if ('customize.php' === $pagenow) { 105 $theme = wp_get_theme(); 106 107 if ($theme->template == "materialis-pro" || ($theme->parent() && $theme->parent()->template == "materialis-pro")) { 108 materialis_force_check_themes_update(); 109 110 if (function_exists("materialis_pro_require") && !class_exists("Wp_License_Manager_Client")) { 111 materialis_pro_require('/inc/class-wp-license-manager-client.php'); 112 } 113 114 if (class_exists("Wp_License_Manager_Client")) { 115 $licence_manager = new Wp_License_Manager_Client( 116 'materialis-pro', 117 'Materialis PRO', 118 'materialis', 119 'http://onepageexpress.com/api/license-manager/v1/', 120 'theme' 121 ); 122 } 123 124 wp_update_themes(); 125 } 126 127 } 128 } catch (Exception $e) { 129 } 130 }); 131 132 $theme = wp_get_theme(); 133 if ($theme && ($theme->template == "materialis-pro" || ($theme->parent() && $theme->parent()->template == "materialis-pro"))) { 134 add_action('customize_controls_print_footer_scripts', function () { 135 ?> 136 <script type="text/javascript"> 137 CP_Customizer.addModule(function () { 138 CP_Customizer.bind(CP_Customizer.events.PREVIEW_LOADED, function () { 139 var updates_msg = <?php echo json_encode(materialis_get_updates_msg()); ?>; 140 if (updates_msg) { 141 CP_Customizer.popUpInfo('Updates available', 142 '<div class="pro-popup-preview-container">' + 143 updates_msg + 144 '</div>' 145 ); 146 }; 147 }); 148 }); 149 </script> 150 <?php 151 }, 11); 152 } 153 154 /* 155 enable theme updates, by sending the version parameter 156 */ 157 158 add_filter( 'http_request_args', function($r, $url) { 159 if (strpos($url, "materialis-pro") !== FALSE) { 160 $r['body'] = array("v" => "1.0"); 161 } 162 return $r; 163 }, PHP_INT_MAX, 2); 164 165 /* 166 fix updates apearring for pro child theme instead of pro theme 167 */ 168 169 add_filter('pre_set_site_transient_update_themes', function ($transient) { 170 //print_r($transient); 171 //die(); 172 173 if (property_exists($transient, 'response') && is_array($transient->response)) { 174 foreach ($transient->response as $slug => $value) { 175 if ($slug != "materialis-pro" && strpos($value["package"], "materialis-pro") !== false) { 176 177 $theme = wp_get_theme(); 178 if ($theme->parent() && $theme->parent()->template == "materialis-pro") { 179 // if different version, add as pro update// 180 181 if ($theme->parent()->version != $value['new_version']) { 182 $transient->response['materialis-pro'] = $value; 183 $transient->checked['materialis-pro'] = $theme->parent()->version; 184 } 185 } 186 187 unset($transient->response[$slug]); 188 189 if (isset($transient->checked[$slug])) { 190 unset($transient->checked[$slug]); 191 } 192 } 193 } 194 } 195 196 return $transient; 197 }, PHP_INT_MAX);