/** * hijack the normal shortlink button and * use ours instead * * @param [type] $shortlink [description] * @param [type] $id [description] * @return [type] [description] */ public function shortlink_button($shortlink, $id) { // bail if the setting isn't enabled if (false === ($enabled = YOURLSCreator_Helper::get_yourls_option('sht'))) { return $shortlink; } // check existing postmeta for YOURLS $custom = YOURLSCreator_Helper::get_yourls_meta($id); // return the custom YOURLS link or the regular one return !empty($custom) ? $custom : $shortlink; }
/** * add shortlink into head if present * * @return [type] [description] */ public function shortlink_meta() { // no shortlinks exist on non-singular items, so bail if (!is_singular()) { return; } // check options to see if it's enabled if (false === YOURLSCreator_Helper::get_yourls_option('sht')) { return; } // call the global post object global $post; // check existing postmeta for YOURLS $link = YOURLSCreator_Helper::get_yourls_meta($post->ID); // got a YOURLS? well then add it if (!empty($link)) { echo '<link href="' . esc_url($link) . '" rel="shortlink">' . "\n"; } }
/** * Display main options page structure * * @return void */ public static function yourls_settings_page() { // bail if current user cannot manage options if (!current_user_can('manage_options')) { return; } ?> <div class="wrap"> <h2><?php _e('YOURLS Link Creator Settings', 'wpyourls'); ?> </h2> <div id="poststuff" class="metabox-holder has-right-sidebar"> <?php self::settings_side(); self::settings_open(); ?> <div class="yourls-form-text"> <p><?php _e('Below are the basic settings for the YOURLS creator. A reminder, your YOURLS install cannot be public.', 'wpyourls'); ?> </p> </div> <div class="yourls-form-options"> <form method="post"> <?php // fetch our data for the settings $data = YOURLSCreator_Helper::get_yourls_option(); // filter and check each one $url = !empty($data['url']) ? $data['url'] : ''; $api = !empty($data['api']) ? $data['api'] : ''; $save = !empty($data['sav']) ? true : false; $schd = !empty($data['sch']) ? true : false; $short = !empty($data['sht']) ? true : false; $cpts = !empty($data['cpt']) ? true : false; $types = !empty($data['typ']) ? (array) $data['typ'] : array(); // load the settings fields wp_nonce_field('yourls_settings_save_nonce', 'yourls_settings_save', false, true); ?> <table class="form-table yourls-table"> <tbody> <tr> <th><?php _e('YOURLS Custom URL', 'wpyourls'); ?> </th> <td> <input type="url" class="regular-text code" value="<?php echo esc_url($url); ?> " id="yourls-url" name="yourls-options[url]"> <p class="description"><?php _e('Enter your custom YOURLS URL', 'wpyourls'); ?> </p> </td> </tr> <tr> <th><?php _e('YOURLS API Signature Key', 'wpyourls'); ?> </th> <td class="apikey-field-wrapper"> <input type="text" class="regular-text code" value="<?php echo esc_attr($api); ?> " id="yourls-api" name="yourls-options[api]" autocomplete="off"> <span class="dashicons dashicons-visibility password-toggle"></span> <p class="description"><?php _e('Found in the tools section on your YOURLS admin page.', 'wpyourls'); ?> </p> </td> </tr> <tr> <th><?php _e('Auto generate links', 'wpyourls'); ?> </th> <td class="setting-item"> <input type="checkbox" name="yourls-options[sav]" id="yourls-sav" value="true" <?php checked($save, true); ?> /> <label for="yourls-sav"><?php _e('Create a YOURLS link when a post is saved.', 'wpyourls'); ?> </label> </td> </tr> <tr> <th><?php _e('Scheduled Content', 'wpyourls'); ?> </th> <td class="setting-item"> <input type="checkbox" name="yourls-options[sch]" id="yourls-sch" value="true" <?php checked($schd, true); ?> /> <label for="yourls-sch"><?php _e('Create a YOURLS link when a scheduled post publishes.', 'wpyourls'); ?> </label> </td> </tr> <tr> <th><?php _e('Use YOURLS for shortlink', 'wpyourls'); ?> </th> <td class="setting-item"> <input type="checkbox" name="yourls-options[sht]" id="yourls-sht" value="true" <?php checked($short, true); ?> /> <label for="yourls-sht"><?php _e('Use the YOURLS link wherever wp_shortlink is fired', 'wpyourls'); ?> </label> </td> </tr> <tr class="setting-item-types"> <th><?php _e('Include Custom Post Types', 'wpyourls'); ?> </th> <td class="setting-item"> <input type="checkbox" name="yourls-options[cpt]" id="yourls-cpt" value="true" <?php checked($cpts, true); ?> /> <label for="yourls-cpt"><?php _e('Display the YOURLS creator on public custom post types', 'wpyourls'); ?> </label> </td> </tr> <tr class="secondary yourls-types" style="display:none;"> <th><?php _e('Select the types to include', 'wpyourls'); ?> </th> <td><?php echo self::post_types($types); ?> </td> </tr> </tbody> </table> <p><input type="submit" class="button-primary" value="<?php _e('Save Changes'); ?> " /></p> </form> </div> <?php self::settings_close(); ?> </div> </div> <?php }
/** * Create yourls link on publish if one doesn't exist * * @param integer $post_id [description] * * @return void */ public function yourls_on_save($post_id) { // bail if this is an import since it'll potentially mess up the process if (!empty($_POST['import_id'])) { return; } // run various checks to make sure we aren't doing anything weird if (YOURLSCreator_Helper::meta_save_check($post_id)) { return; } // bail if we aren't working with a published or scheduled post if (!in_array(get_post_status($post_id), YOURLSCreator_Helper::get_yourls_status('save'))) { return; } // make sure we're working with an approved post type if (!in_array(get_post_type($post_id), YOURLSCreator_Helper::get_yourls_types())) { return; } // bail if the API key or URL have not been entered if (false === ($api = YOURLSCreator_Helper::get_yourls_api_data())) { return; } // bail if user hasn't checked the box if (false === ($onsave = YOURLSCreator_Helper::get_yourls_option('sav'))) { return; } // check for a link and bail if one exists if (false !== ($exist = YOURLSCreator_Helper::get_yourls_meta($post_id))) { return; } // get my post URL and title $url = YOURLSCreator_Helper::prepare_api_link($post_id); $title = get_the_title($post_id); // and optional keyword $keywd = !empty($_POST['yourls-keyw']) ? YOURLSCreator_Helper::prepare_api_keyword($_POST['yourls-keyw']) : ''; // set my args for the API call $args = array('url' => esc_url($url), 'title' => sanitize_text_field($title), 'keyword' => $keywd); // make the API call $build = YOURLSCreator_Helper::run_yourls_api_call('shorturl', $args); // bail if empty data or error received if (empty($build) || false === $build['success']) { return; } // we have done our error checking and we are ready to go if (false !== $build['success'] && !empty($build['data']['shorturl'])) { // get my short URL $shorturl = esc_url($build['data']['shorturl']); // update the post meta update_post_meta($post_id, '_yourls_url', $shorturl); update_post_meta($post_id, '_yourls_clicks', '0'); // do the action after saving do_action('yourls_after_url_save', $post_id, $shorturl); } }