function add_mobbr_button_to_content($content) { $options = get_mobbr_plugin_options(); $can_show_posts = isset($options['placement_options']) && in_array(MOBBR_BUTTON_PLACEMENT_POSTS, $options['placement_options']); $can_show_pages = isset($options['placement_options']) && in_array(MOBBR_BUTTON_PLACEMENT_PAGES, $options['placement_options']); $can_show_button = is_single() && $can_show_posts || is_page() && $can_show_pages; $widget = new MobbrWidget(); if ($can_show_button && isset($options['button_position']) && $options['button_position'] == MOBBR_BUTTON_POSITION_TOP) { $widget->widget(null, null); } echo $content; if ($can_show_button && isset($options['button_position']) && $options['button_position'] == MOBBR_BUTTON_POSITION_BOTTOM) { $widget->widget(null, null); } }
public function widget($args, $instance) { $options = get_mobbr_plugin_options(); if ($options['require_auth'] && !is_user_logged_in()) { return; } if (is_home() && isset($options['button_position']) && $options['button_position'] == MOBBR_BUTTON_POSITION_WIDGET) { return; } global $MOBBR_SUPPORTED_CURRENCIES; $currency = isset($instance["currency"]) && $instance["currency"] && in_array($instance["currency"], $MOBBR_SUPPORTED_CURRENCIES) ? $instance["currency"] : "USD"; $url = get_post_url(); echo "<script type='text/javascript'>mobbr.setLightboxUrl('" . MOBBR_LIGHTBOX_URL . "');</script>"; if ($options['button_style'] == MOBBR_BUTTON_STYLE_CUSTOM) { echo "<button class='mobbr-payment-button' onClick=\"mobbr.makePayment('{$url}')\">{$options['button_text']}</button>"; } else { echo "<script type='text/javascript'>mobbr.button('{$url}', '{$currency}');</script>"; } }
function ajax_save_post_participation_metadata() { $success = false; $error_msg = 'Missing parameters'; if (isset($_POST['url']) && isset($_POST['participants']) && is_array($_POST['participants'])) { $url = $_POST['url']; $post_id = url_to_postid($url); $participants = $_POST['participants']; if ($post_id) { $options = get_mobbr_plugin_options(); delete_post_meta($post_id, '_mobbr_participants'); foreach ($participants as $participant) { if (!isset($participant['id']) || !isset($participant['share'])) { continue; } $id = $participant['id']; $share = (int) $participant['share']; $role = isset($participant['role']) ? sanitize_text_field($participant['role']) : MOBBR_ROLE_TASK_CONTRIBUTOR; if ($id && (filter_var(str_replace("mailto:", "", $id), FILTER_VALIDATE_EMAIL) || preg_match(MOBBR_REGEX_URL, $id)) && $share > 0 && $share < 100 && ($role != MOBBR_ROLE_WEBSITE_OWNER || "mailto:" . $options['email'] != $id)) { $data = array('id' => $id, 'share' => $share, 'role' => $role); if (add_post_meta($post_id, '_mobbr_participants', json_encode($data))) { $success = true; } } if (!$success && $role == 'owner' && count($participants) == 1) { // Allows payer to choose the owner as the only recipient $success = true; } } } else { $error_msg = "Couldn't find post"; } } if ($success) { echo json_encode(array('status' => true, 'msg' => 'Metadata saved successfully')); } else { header("HTTP/1.0 400 Bad Request"); echo json_encode(array('status' => false, 'msg' => $error_msg)); } wp_die(); }
function get_mobbr_participation() { $title = get_the_title(); $options = get_mobbr_plugin_options(); $owner = array("id" => "mailto:{$options['email']}", "role" => MOBBR_ROLE_WEBSITE_OWNER, "share" => "{$options['share']}%"); global $wp_query; $post_id = $wp_query->post->ID; $amount = (int) get_post_meta($post_id, 'taskfee', true); $currency = get_post_meta($post_id, 'taskcurrency', true); $task_url = get_post_meta($post_id, 'taskurl', true); wp_reset_query(); $content = strip_shortcodes(in_the_loop() ? get_the_content() : $wp_query->post->post_content); if (!$task_url && preg_match(MOBBR_REGEX_URL, $title . ' ' . $content, $matches)) { $task_url = $matches[0]; } if ((!$amount || !$currency) && preg_match(MOBBR_REGEX_TASK_AMOUNT, $title . ' ' . $content, $matches)) { $number = null; if (!$amount) { if (isset($matches['amount']) && $matches['amount']) { $number = $matches['amount']; } else { if (isset($matches['amount2']) && $matches['amount2']) { $number = $matches['amount2']; } } if ($number) { $amount = str_replace(",", preg_match("/\\d+\\.\\d+/i", $number) ? "" : ".", $number); } } global $MOBBR_EURO_SYMBOLS; if (isset($matches['currency']) && $matches['currency'] && in_array($matches['currency'], $MOBBR_EURO_SYMBOLS) || isset($matches['currency2']) && $matches['currency2'] && in_array($matches['currency2'], $MOBBR_EURO_SYMBOLS)) { $currency = 'EUR'; } } $amount = floatval($amount); global $MOBBR_SUPPORTED_CURRENCIES; if (!$currency || !in_array($currency, $MOBBR_SUPPORTED_CURRENCIES)) { $currency = 'USD'; } $script_type = 'payment'; $script_lang = 'EN'; $script_title = $title; $desc = strlen($content) > 200 ? substr($content, 0, 200) . "..." : $content; $script_desc = htmlentities(strip_tags($desc), ENT_QUOTES); $script_keywords = array('tunga.io', 'tunga'); $script_participants = array(); if ($options['share'] > 0) { $script_participants[] = $owner; } $use_local_script = true; if ($task_url) { $req = wp_remote_get(MOBBR_URI_INFO_ENDPOINT . "?url=" . urlencode($task_url), array('headers' => array('Accept' => 'application/json'))); if (!is_wp_error($req) && $req && $req['response']['code'] == 200) { $response = json_decode($req['body'], true); $task_script = $response['result']['script']; if (isset($task_script['type'])) { $script_type = $task_script['type']; } if (isset($task_script['language'])) { $script_lang = $task_script['language']; } if (isset($task_script['title'])) { $script_title = $task_script['title']; } if (isset($task_script['description'])) { $script_desc = $task_script['description']; } if (isset($task_script['keywords']) && is_array($task_script['keywords'])) { $script_keywords = array_merge($script_keywords, $task_script['keywords']); } if (isset($task_script['participants']) && is_array($task_script['participants'])) { $task_participants = $task_script['participants']; if ($options['share'] < 100) { $absolute_shares = array(); $relative_shares = array(); $absolute_participants = array(); $relative_participants = array(); foreach ($task_participants as $key => $participant) { if (preg_match("/%\$/", $participant['share'])) { $share = intval(str_replace("%", "", $participant['share'])); if ($share > 0) { $absolute_shares[] = $share; $new_participant = $participant; $new_participant['share'] = $share; $absolute_participants[] = $new_participant; } } else { $share = intval($participant['share']); if ($share > 0) { $relative_shares[] = $share; $new_participant = $participant; $new_participant['share'] = $share; $relative_participants[] = $new_participant; } } } $additional_participants = array(); $total_absolutes = array_sum($absolute_shares); $total_relatives = array_sum($relative_shares); if ($total_absolutes >= 100 || $total_relatives == 0) { $additional_participants = $absolute_participants; } else { if ($total_absolutes == 0) { $additional_participants = $relative_participants; } else { $additional_participants = $absolute_participants; foreach ($relative_participants as $participant) { $share = round($participant['share'] * (100 - $total_absolutes) / $total_relatives, 0); if ($share > 0) { $new_participant = $participant; $new_participant['share'] = $share; $additional_participants[] = $new_participant; } } } } if (count($additional_participants)) { $script_participants = array_merge($script_participants, $additional_participants); } } $use_local_script = false; } } } if ($use_local_script) { $local_script_participants = array(); $participants_meta = get_post_meta($wp_query->post->ID, '_mobbr_participants'); if (is_array($participants_meta)) { foreach (get_post_meta($wp_query->post->ID, '_mobbr_participants') as $participant) { array_push($local_script_participants, json_decode($participant, true)); } $script_participants = array_merge($script_participants, $local_script_participants); } } $participation = array("type" => $script_type, "language" => $script_lang, "title" => $script_title, "description" => $script_desc, "keywords" => $script_keywords, "participants" => $script_participants, "extras" => array("editable" => $use_local_script && $options['share'] < 100, "task_url" => $task_url, "amount" => $amount, "currency" => $currency)); return $participation; }
function mobbr_plugin_options_validate($input) { $options = get_mobbr_plugin_options(); $options['require_auth'] = isset($input['require_auth']); if (isset($input['placement_options']) && is_array($options['placement_options'])) { $options['placement_options'] = $input['placement_options']; } else { $options['placement_options'] = array(); } if (isset($input['button_style']) && $input['button_style']) { $options['button_style'] = sanitize_text_field($input['button_style']); } if (isset($input['button_position']) && $input['button_position']) { $options['button_position'] = sanitize_text_field($input['button_position']); } if (isset($input['button_text']) && $input['button_text']) { $options['button_text'] = sanitize_text_field($input['button_text']); } if (isset($input['email']) && is_email($input['email'])) { $options['email'] = sanitize_email($input['email']); } if (isset($input['share'])) { $share = (int) $input['share']; if ($share >= 0 && $share <= 100) { $options['share'] = $share; } } return $options; }
Author: David Semakula Version: 0.2 Author URI: https://github.com/davidsemakula */ require_once "mobbr.config.php"; require_once "mobbr.utils.php"; require_once "mobbr.head.php"; require_once "mobbr.widget.php"; require_once "mobbr.admin.php"; require_once "mobbr.filters.php"; require_once "mobbr.metabox.php"; require_once "mobbr.ajax.php"; add_action('wp_head', 'mobbr_participation_meta'); add_action('wp_head', 'mobbr_script'); add_action("widgets_init", function () { $options = get_mobbr_plugin_options(); if (isset($options['button_position']) && $options['button_position'] == MOBBR_BUTTON_POSITION_WIDGET) { register_widget("MobbrWidget"); } }); add_filter('the_content', 'add_mobbr_button_to_content', 99); add_action('admin_menu', 'plugin_admin_add_page'); add_action('admin_init', 'plugin_admin_init'); add_action('add_meta_boxes', 'mobbr_plugin_add_meta_boxes'); add_action('save_post', 'mobbr_plugin_save_meta_boxes'); add_action('wp_ajax_add_post_meta', 'ajax_save_post_participation_metadata'); $options = get_mobbr_plugin_options(); if ($options['require_auth']) { add_action('wp_ajax_nopriv_add_post_meta', 'ajax_save_post_participation_metadata'); } add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'mobbr_plugin_action_links');