· 8 years ago · Jan 30, 2018, 09:06 PM
1<?php
2
3/**
4 * The public-facing functionality of the plugin.
5 *
6 * @link www.authorurl.com
7 * @since 1.0.0
8 *
9 * @package Crypto_Goal
10 * @subpackage Crypto_Goal/public
11 */
12
13/**
14 * The public-facing functionality of the plugin.
15 *
16 * Defines the plugin name, version, and two examples hooks for how to
17 * enqueue the public-facing stylesheet and JavaScript.
18 *
19 * @package Crypto_Goal
20 * @subpackage Crypto_Goal/public
21 * @author Author Name <authoremail@gmail.com>
22 */
23class Crypto_Goal_Public {
24
25 /**
26 * The ID of this plugin.
27 *
28 * @since 1.0.0
29 * @access private
30 * @var string $plugin_name The ID of this plugin.
31 */
32 private $plugin_name;
33
34 /**
35 * The version of this plugin.
36 *
37 * @since 1.0.0
38 * @access private
39 * @var string $version The current version of this plugin.
40 */
41 private $version;
42
43 /**
44 * Initialize the class and set its properties.
45 *
46 * @since 1.0.0
47 * @param string $plugin_name The name of the plugin.
48 * @param string $version The version of this plugin.
49 */
50 public function __construct( $plugin_name, $version ) {
51
52 $this->plugin_name = $plugin_name;
53 $this->version = $version;
54
55 }
56
57 /**
58 * Register the stylesheets for the public-facing side of the site.
59 *
60 * @since 1.0.0
61 */
62 public function enqueue_styles() {
63
64 /**
65 * This function is provided for demonstration purposes only.
66 *
67 * An instance of this class should be passed to the run() function
68 * defined in Crypto_Goal_Loader as all of the hooks are defined
69 * in that particular class.
70 *
71 * The Crypto_Goal_Loader will then create the relationship
72 * between the defined hooks and the functions defined in this
73 * class.
74 */
75
76 wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/crypto-goal-public.css', array(), $this->version, 'all' );
77
78 }
79
80 /**
81 * Register the JavaScript for the public-facing side of the site.
82 *
83 * @since 1.0.0
84 */
85 public function enqueue_scripts() {
86
87 /**
88 * This function is provided for demonstration purposes only.
89 *
90 * An instance of this class should be passed to the run() function
91 * defined in Crypto_Goal_Loader as all of the hooks are defined
92 * in that particular class.
93 *
94 * The Crypto_Goal_Loader will then create the relationship
95 * between the defined hooks and the functions defined in this
96 * class.
97 */
98
99 wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/crypto-goal-public.js', array( 'jquery' ), $this->version, false );
100
101 }
102
103 public function funding_campaign_template($template){
104 global $post;
105
106 // Is this a "my-custom-post-type" post?
107 if ($post->post_type == "funding_campaign"){
108
109 //Your plugin path
110 $plugin_path = plugin_dir_path( __FILE__ );
111
112 // The name of custom post type single template
113 $template_name = 'funding_campaign_template.php';
114
115 // A specific single template for my custom post type exists in theme folder? Or it also doesn't exist in my plugin?
116 if($template === get_stylesheet_directory() . '/' . $template_name
117 || !file_exists($plugin_path . $template_name)) {
118
119 //Then return "single.php" or "single-my-custom-post-type.php" from theme directory.
120 return $template;
121 }
122
123 // If not, return my plugin custom post type template.
124 return $plugin_path . $template_name;
125 }
126
127 //This is not my custom post type, do nothing with $template
128 return $template;
129 }
130
131}
132add_shortcode( 'crypto_campaign_table' , 'crypto_campaign_table' );
133function crypto_campaign_table(){
134 ?>
135 <style>
136 .percentfunded {
137 position: relative;
138 top: -28px;
139 width: 100%;
140 text-align: center;
141}
142</style>
143 <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
144<?php
145 $posts = new WP_Query([
146 'post_type' => 'funding_campaign',
147 'post_status' => 'publish',
148 'numberposts' => -1
149 // 'order' => 'ASC'
150 ]);
151 $query = new WP_Query(array(
152 'post_type' => 'funding_campaign',
153 'post_status' => 'publish',
154 'numberposts' => -1
155 ));
156 echo "<table>";
157 echo "<tr>
158 <th style='text-align:center' >Campaign</th>
159 <th style='text-align:center' >Total Raised / Goal</th>
160
161 <th style='text-align:center' >Wallet Address</th>
162 <th>Dev</th>
163 </tr>";
164 while ($query->have_posts()) {
165 echo "<tr>";
166 $query->the_post();
167 $post_id = get_the_ID();
168 $to_post = get_permalink($post_id);
169 $wallet_addr = get_post_meta($post_id, 'wallet_address', true);
170 $url="https://chainz.cryptoid.info/tzc/api.dws?q=getbalance&a=".$wallet_addr;
171 $ch = curl_init();
172
173 // define options
174 $optArray = array(
175 CURLOPT_URL => $url,
176 CURLOPT_RETURNTRANSFER => true
177 );
178
179 // apply those options
180 curl_setopt_array($ch, $optArray);
181
182 // execute request and get response
183 $result = curl_exec($ch);
184 $campaign_total = $result;
185 $percent_funded = ((int)$campaign_total/(int)get_post_meta($post_id, 'campaign_goal', true))*100;
186 if ($percent_funded>100){
187 $real = $percent_funded;
188 $percent_funded=100;
189 $ccolor="green";
190 $perc_color="white";
191 $wallet_addr = "Fully Funded - Thank you!";
192
193 } elseif ($percent_funded==100){
194 $percent_funded=100;
195 $real = $percent_funded;
196 $ccolor="green";
197 $perc_color="white";
198 $wallet_addr = "Fully Funded - Thank you!";
199
200 } elseif ($percent_funded>50 && $percent_funded < 99){
201 $real = $percent_funded;
202 $ccolor="orange";
203 $perc_color="black";
204 } else {
205 $real = $percent_funded;
206 $ccolor="red";
207 $perc_color="black";
208 }
209 echo "<td style='text-align:center'><a href='".$to_post."'>".get_the_title($post_id)."</a></td>";
210 echo "<td style='text-align:center'><strong>".round($campaign_total,0)."/".get_post_meta($post_id, 'campaign_goal', true)." TZC</strong><div class='w3-light-grey' style='border:1px solid #ccc'>
211 <div class='w3-".$ccolor."' style='height:24px;width:".$percent_funded."%'></div></div><div style='color:".$perc_color."' class='percentfunded'>".round($real, 0)."%</div></td>";
212
213 echo "<td style='text-align:center' >".$wallet_addr."</td>";
214 echo "<td >".get_post_meta($post_id, 'dev', true)."</td>";
215 echo "</tr>";
216 }
217 echo "</table>";
218
219 wp_reset_query();
220}