summaryrefslogtreecommitdiffstats
path: root/danixland-site-plugin.php
blob: 71af06e72dfa8a90c6a25f09e2ff1e147d670140 (plain)
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
<?php
/*
Plugin Name: Site Plugin for danixland.net
Description: Site specific code changes for danixland.net
*/

/**
 * Disable admin bar sitewide
 */
show_admin_bar(__return_false());

/**
 * Allow Shortcodes inside the html widget
 */
add_filter( 'widget_text', 'do_shortcode' );

/**
 * make sure the links menu in the admin is visible
 */
add_filter( 'pre_option_link_manager_enabled', '__return_true' );

/**
 * The gravatar shortcode
 *
 * @param email $email email address used to retrieve the gravatar image
 * @return string HTML the image tag which will display the gravatar image.
 *
 */
function danix_gravatar( $atts ) {
    extract(shortcode_atts(array(
        'email'        => '',
        'size'         => '200',
        'default'      => '',
        'alt'          => ''
    ), $atts));

    if( empty($email) )
        return '';

    $output = get_avatar($email, $size, $default, $alt);

    return $output;
}
add_shortcode('gravatar', 'danix_gravatar');

/**
 * The download button shortcode
 *
 * @param url $link link address.
 * @param string $linkname text that will be displayed by the button.
 * @return string HTML the link tag that will be rendered to the page.
 *
 */
function danix_download_button( $atts ) {
    extract(shortcode_atts(array(
        'link'      => '',
        'linkname'  => ''
    ), $atts));

    if ( empty($link) )
        return '';

    $output = sprintf(
        '<a href="%s" class="download_button">%s</a>',
        $link,
        $linkname
    );

    return $output;
}
add_shortcode('download', 'danix_download_button');

/**
 * The google analytics code
 *
 */
function danix_google_anal() {?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-23882460-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-23882460-1');
</script>
<?php }
add_action('wp_head', 'danix_google_anal', 10);

/**
 * The googlemaps shortcode
 *
 * @param width the width of the returned map
 * @param height the height of the returned map
 * @param src the url of the map you want to display
 * @return string HTML the map object
 * @since 0.57
 *
 */
function danix_gmaps($atts, $content = null) {
    extract(shortcode_atts(array(
      "width" => '640',
      "height" => '480',
      "src" => ''
    ), $atts));
    return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'&amp;output=embed" ></iframe>';
}
add_shortcode("googlemaps", "danix_gmaps");

function danix_get_plugins_stats() {
    $url    = 'https://api.wordpress.org/plugins/info/1.0/';
    $fields = array(
        'downloaded' => true,
        'rating' => false,
        'description' => false,
        'short_description' => false,
        'donate_link' => false,
        'tags' => false,
        'sections' => false,
        'homepage' => false,
        'added' => false,
        'last_updated' => false,
        'compatibility' => false,
        'tested' => false,
        'requires' => false,
        'downloadlink' => false,
    );
    $args = (object) array( 'author' => 'danixland', 'fields' => $fields );
    $request = array( 'action' => 'query_plugins', 'timeout' => 15, 'request' => serialize( $args) );
    $response = wp_remote_post( $url, array( 'body' => $request ) );
    $plugins_info = unserialize( $response['body'] );
    $result = array();
    $total_dl = '';
    if ( isset( $plugins_info ) ) {
        # let's build our array of data to return
        foreach ($plugins_info->plugins as $plugin) {
            $pl_name = $plugin->slug;
            $pl_dirlink = 'https://wordpress.org/plugins/' . $pl_name;
            $pl_download = $plugin->download_link;
            $pl_downloaded = $plugin->downloaded;
            $pl_voters = $plugin->num_ratings;
            $pl_stars = $plugin->ratings;
            $avg_vote = ($pl_stars[1] + $pl_stars[2] * 2 + $pl_stars[3] * 3 + $pl_stars[4] * 4 + $pl_stars[5] * 5) / $pl_voters;
            $pl_avg_vote = ( ! is_int($avg_vote) ? 0 : $avg_vote );
            $result[$pl_name] = array(
                'dir_link' => $pl_dirlink,
                'dl_link' => $pl_download,
                'downloaded' => $pl_downloaded,
                'voters' => $pl_voters,
                'avg_rating' => $pl_avg_vote
            );
            $total_dl += $pl_downloaded;
        }
        $result['total_plugins'] = count($plugins_info->plugins);
        $result['total_downloads'] = $total_dl;
        return $result;
    }
    return array();
}

// Execute cron
function danix_save_plugins_stats() {

  $plugins = danix_get_plugins_stats();
  update_option( 'danix_plugins_stats', $plugins );
}
add_action( 'danix_save_plugins_stats', 'danix_save_plugins_stats' );

// Schedule cron
if ( ! wp_next_scheduled( 'danix_save_plugins_stats' ) ) :
  wp_schedule_event( 1407110400, 'daily', 'danix_save_plugins_stats' ); // 1407110400 is 08 / 4 / 2014 @ 0:0:0 UTC
endif;

/**
 * The dotOrg plugins stats shortcode
 *
 * @param url $link link address.
 * @param string $linkname text that will be displayed by the button.
 * @return string HTML the link tag that will be rendered to the page.
 *
 */
function danix_display_plugins_stats() {
    $option_stats = get_option('danix_plugins_stats');
    $plugins = ( false === $option_stats ? danix_get_plugins_stats() : $option_stats );
    $average = '';
    foreach ($plugins as $plugin) {
        $average += $plugin['avg_rating'];
    }
    $average = $average / $plugins['total_plugins'];
    ?>
    <dl>
        <dt><?php _e("Plugins", 'sog_simplify'); ?></dt>
        <dd><?php printf( _n('I created %s plugin', 'I created %s plugins', $plugins['total_plugins'], 'sog_simplify' ), $plugins['total_plugins'] ); ?></dd><br />
        <?php foreach ($plugins as $name => $data) { 
            if (is_array($data) ) : ?>
                <dd><a href="<?php echo $data['dir_link']; ?>"><?php echo $name; ?></a></dd>
            <?php endif; ?>
        <?php } ?>
        <dt><?php _e('Total Downloads', 'sog_simplify'); ?></dt>
        <dd><?php echo $plugins['total_downloads']; ?></dd>
    </dl>
<?php }
add_shortcode('dotOrgStats', 'danix_display_plugins_stats');