initial commit
[danixland-site-plugin.git] / danixland-site-plugin.php
1 <?php
2 /*
3 Plugin Name: Site Plugin for danixland.net
4 Description: Site specific code changes for danixland.net
5 */
6
7 /**
8 * Disable admin bar sitewide
9 */
10 show_admin_bar(__return_false());
11
12 /**
13 * Allow Shortcodes inside the html widget
14 */
15 add_filter( 'widget_text', 'do_shortcode' );
16
17 /**
18 * make sure the links menu in the admin is visible
19 */
20 add_filter( 'pre_option_link_manager_enabled', '__return_true' );
21
22 /**
23 * The gravatar shortcode
24 *
25 * @param email $email email address used to retrieve the gravatar image
26 * @return string HTML the image tag which will display the gravatar image.
27 *
28 */
29 function danix_gravatar( $atts ) {
30 extract(shortcode_atts(array(
31 'email' => '',
32 'size' => '200',
33 'default' => '',
34 'alt' => ''
35 ), $atts));
36
37 if( empty($email) )
38 return '';
39
40 $output = get_avatar($email, $size, $default, $alt);
41
42 return $output;
43 }
44 add_shortcode('gravatar', 'danix_gravatar');
45
46 /**
47 * The download button shortcode
48 *
49 * @param url $link link address.
50 * @param string $linkname text that will be displayed by the button.
51 * @return string HTML the link tag that will be rendered to the page.
52 *
53 */
54 function danix_download_button( $atts ) {
55 extract(shortcode_atts(array(
56 'link' => '',
57 'linkname' => ''
58 ), $atts));
59
60 if ( empty($link) )
61 return '';
62
63 $output = sprintf(
64 '<a href="%s" class="download_button">%s</a>',
65 $link,
66 $linkname
67 );
68
69 return $output;
70 }
71 add_shortcode('download', 'danix_download_button');
72
73 /**
74 * The google analytics code
75 *
76 */
77 function danix_google_anal() {?>
78 <!-- Global site tag (gtag.js) - Google Analytics -->
79 <script async src="https://www.googletagmanager.com/gtag/js?id=UA-23882460-1"></script>
80 <script>
81 window.dataLayer = window.dataLayer || [];
82 function gtag(){dataLayer.push(arguments);}
83 gtag('js', new Date());
84
85 gtag('config', 'UA-23882460-1');
86 </script>
87 <?php }
88 add_action('wp_head', 'danix_google_anal', 10);
89
90 /**
91 * The googlemaps shortcode
92 *
93 * @param width the width of the returned map
94 * @param height the height of the returned map
95 * @param src the url of the map you want to display
96 * @return string HTML the map object
97 * @since 0.57
98 *
99 */
100 function danix_gmaps($atts, $content = null) {
101 extract(shortcode_atts(array(
102 "width" => '640',
103 "height" => '480',
104 "src" => ''
105 ), $atts));
106 return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'&amp;output=embed" ></iframe>';
107 }
108 add_shortcode("googlemaps", "danix_gmaps");
109
110 function danix_get_plugins_stats() {
111 $url = 'https://api.wordpress.org/plugins/info/1.0/';
112 $fields = array(
113 'downloaded' => true,
114 'rating' => false,
115 'description' => false,
116 'short_description' => false,
117 'donate_link' => false,
118 'tags' => false,
119 'sections' => false,
120 'homepage' => false,
121 'added' => false,
122 'last_updated' => false,
123 'compatibility' => false,
124 'tested' => false,
125 'requires' => false,
126 'downloadlink' => false,
127 );
128 $args = (object) array( 'author' => 'danixland', 'fields' => $fields );
129 $request = array( 'action' => 'query_plugins', 'timeout' => 15, 'request' => serialize( $args) );
130 $response = wp_remote_post( $url, array( 'body' => $request ) );
131 $plugins_info = unserialize( $response['body'] );
132 $result = array();
133 $total_dl = '';
134 if ( isset( $plugins_info ) ) {
135 # let's build our array of data to return
136 foreach ($plugins_info->plugins as $plugin) {
137 $pl_name = $plugin->slug;
138 $pl_dirlink = 'https://wordpress.org/plugins/' . $pl_name;
139 $pl_download = $plugin->download_link;
140 $pl_downloaded = $plugin->downloaded;
141 $pl_voters = $plugin->num_ratings;
142 $pl_stars = $plugin->ratings;
143 $avg_vote = ($pl_stars[1] + $pl_stars[2] * 2 + $pl_stars[3] * 3 + $pl_stars[4] * 4 + $pl_stars[5] * 5) / $pl_voters;
144 $pl_avg_vote = ( ! is_int($avg_vote) ? 0 : $avg_vote );
145 $result[$pl_name] = array(
146 'dir_link' => $pl_dirlink,
147 'dl_link' => $pl_download,
148 'downloaded' => $pl_downloaded,
149 'voters' => $pl_voters,
150 'avg_rating' => $pl_avg_vote
151 );
152 $total_dl += $pl_downloaded;
153 }
154 $result['total_plugins'] = count($plugins_info->plugins);
155 $result['total_downloads'] = $total_dl;
156 return $result;
157 }
158 return array();
159 }
160
161 // Execute cron
162 function danix_save_plugins_stats() {
163
164 $plugins = danix_get_plugins_stats();
165 update_option( 'danix_plugins_stats', $plugins );
166 }
167 add_action( 'danix_save_plugins_stats', 'danix_save_plugins_stats' );
168
169 // Schedule cron
170 if ( ! wp_next_scheduled( 'danix_save_plugins_stats' ) ) :
171 wp_schedule_event( 1407110400, 'daily', 'danix_save_plugins_stats' ); // 1407110400 is 08 / 4 / 2014 @ 0:0:0 UTC
172 endif;
173
174 /**
175 * The dotOrg plugins stats shortcode
176 *
177 * @param url $link link address.
178 * @param string $linkname text that will be displayed by the button.
179 * @return string HTML the link tag that will be rendered to the page.
180 *
181 */
182 function danix_display_plugins_stats() {
183 $option_stats = get_option('danix_plugins_stats');
184 $plugins = ( false === $option_stats ? danix_get_plugins_stats() : $option_stats );
185 $average = '';
186 foreach ($plugins as $plugin) {
187 $average += $plugin['avg_rating'];
188 }
189 $average = $average / $plugins['total_plugins'];
190 ?>
191 <dl>
192 <dt><?php _e("Plugins", 'sog_simplify'); ?></dt>
193 <dd><?php printf( _n('I created %s plugin', 'I created %s plugins', $plugins['total_plugins'], 'sog_simplify' ), $plugins['total_plugins'] ); ?></dd><br />
194 <?php foreach ($plugins as $name => $data) {
195 if (is_array($data) ) : ?>
196 <dd><a href="<?php echo $data['dir_link']; ?>"><?php echo $name; ?></a></dd>
197 <?php endif; ?>
198 <?php } ?>
199 <dt><?php _e('Total Downloads', 'sog_simplify'); ?></dt>
200 <dd><?php echo $plugins['total_downloads']; ?></dd>
201 </dl>
202 <?php }
203 add_shortcode('dotOrgStats', 'danix_display_plugins_stats');