public function widget($args, $instance)
 {
     // Setup widget vars
     $instance = array_merge($this->default_vars, $instance);
     // Don't output anything if there's no postlink
     if ($instance['postlink'] == '') {
         return;
     }
     // Don't output the widget on the frontpage if the frontpage show_on_front is posts
     if (is_front_page() && false === is_page()) {
         return;
     }
     // Use custom post of parent is null
     if (null === $instance['parent']) {
         $instance['parent'] = get_the_ID();
     }
     // Load the PTL
     $post_type_link_manager = new SP_Connection_Manager();
     $connection = $post_type_link_manager->get_connection($instance['postlink']);
     // Return if there is no connection
     if (null == $connection) {
         return;
     }
     // Only display widget on pages where the post type is the $instance['parent'] equals the Connection post type
     if ($connection->get_parent() != get_post_type($instance['parent'])) {
         return;
     }
     // Setup the post link manager
     $post_link_manager = new SP_Post_Link_Manager();
     // Generate the widget content
     $widget_content = $post_link_manager->generate_children_list($connection->get_slug(), $instance['parent'], $instance['link'], $instance['excerpt'], 'b', $instance['thumbnail']);
     // Don't ouput the widget if there is no widget content
     if ('' == $widget_content) {
         return;
     }
     // Output the widget
     echo $args['before_widget'];
     echo $args['before_title'] . $instance['title'] . $args['after_title'] . "\n";
     echo $widget_content;
     echo $args['after_widget'];
 }
    /**
     * Setup edit screen
     *
     * @access public
     * @return void
     */
    public function screen_edit()
    {
        $connection_manager = new SP_Connection_Manager();
        if (isset($_GET['id'])) {
            $connection = $connection_manager->get_connection($_GET['id']);
        } else {
            $connection = new SP_Connection();
        }
        // Get Post Types
        $raw_post_types = get_post_types(array('_builtin' => false), 'objects');
        $post_types = array();
        $post_types['post'] = __('Posts', 'post-connector');
        $post_types['page'] = __('Pages', 'post-connector');
        if (count($raw_post_types) > 0) {
            foreach ($raw_post_types as $pt_key => $post_type) {
                if ($pt_key == 'attachments' || $pt_key == 'sub_posts_link') {
                    continue;
                }
                $post_types[$pt_key] = $post_type->labels->name;
            }
        }
        ?>
		<div class="wrap">
			<h2>Post Connector: <?php 
        if (isset($_GET['id'])) {
            _e('Edit connection', 'post-connector');
        } else {
            _e('Create a new connection', 'post-connector');
        }
        ?>
</h2>

			<div class="pc-content pc-edit-screen">
				<form method="post" action="" id="pc-connection-form">
					<?php 
        // Add nonce
        wp_nonce_field(plugin_basename(__FILE__), 'sp_edit_nonce');
        if ($connection->get_id() != 0) {
            echo "<input type='hidden' name='id' value='" . $connection->get_id() . "' />\n";
        }
        echo "<input type='hidden' name='pc-ajax-nonce' id='pc-ajax-nonce' value='" . wp_create_nonce('post-connector-ajax-nonce-omgrandomword') . "' />\n";
        ?>
					<input type="hidden" name="sp_save_check" value="true" />

					<p><?php 
        _e("To create or edit a post connection, fill in the below form and press the 'save connection' button.", 'post-connector');
        ?>
</p>

					<h3 class="pc-title"><?php 
        _e('Main settings', 'post-connector');
        ?>
</h3>

					<table class="form-table">
						<tbody>

						<tr valign="top">
							<th scope="row">
								<label for="title" class="sp_label"><?php 
        _e('Connection Title', 'post-connector');
        ?>
</label>
							</th>
							<td colspan="2" class="nowrap">
								<input type="text" name="title" value="<?php 
        echo $connection->get_title();
        ?>
" placeholder="<?php 
        _e('Title', 'post-connector');
        ?>
" class="widefat mandatory" id="title" />

								<p class="help"><?php 
        _e('The title of the connection, can be automatically shown above linked posts.', 'post-connector');
        ?>
</p>
							</td>
						</tr>

						<tr valign="top">
							<th scope="row">
								<label for="slug"><?php 
        _e('Connection Slug', 'post-connector');
        ?>
</label></th>
							<td colspan="2" class="nowrap">
								<input type="text" name="slug" value="<?php 
        echo $connection->get_slug();
        ?>
" placeholder="<?php 
        _e('Slug', 'post-connector');
        ?>
" class="widefat" id="slug"<?php 
        echo isset($_GET['id']) ? ' disabled="disabled"' : '';
        ?>
 />

								<p class="help"><?php 
        _e('Unique identifier of the connection, will automatically generate if left empty.', 'post-connector');
        ?>
</p>
							</td>
						</tr>

						<tr valign="top">
							<th scope="row">
								<label for="parent" class="sp_label"><?php 
        _e('Parent post type', 'post-connector');
        ?>
</label>
							</th>
							<td colspan="2" class="nowrap">
								<select name="parent" class="widefat mandatory" id="parent">
									<option value="0"><?php 
        _e('Select a post type', 'post-connector');
        ?>
</option>
									<?php 
        if (count($post_types) > 0) {
            foreach ($post_types as $pt_key => $pt_name) {
                echo "<option value='{$pt_key}'" . ($connection->get_parent() == $pt_key ? " selected='selected'" : "") . ">{$pt_name}</option>";
            }
        }
        ?>
								</select>
							</td>
						</tr>

						<tr valign="top">
							<th scope="row">
								<label for="child" class="sp_label"><?php 
        _e('Child post type', 'post-connector');
        ?>
</label>
							</th>
							<td colspan="2" class="nowrap">
								<select name="child" class="widefat mandatory" id="child">
									<option value="0"><?php 
        _e('Select a post type', 'post-connector');
        ?>
</option>
									<?php 
        if (count($post_types) > 0) {
            foreach ($post_types as $pt_key => $pt_name) {
                echo "<option value='{$pt_key}'" . ($connection->get_child() == $pt_key ? " selected='selected'" : "") . ">{$pt_name}</option>";
            }
        }
        ?>
								</select>
							</td>
						</tr>

						</tbody>
					</table>

					<h3 class="pc-title"><?php 
        _e('Create new link settings', 'post-connector');
        ?>
</h3>

					<table class="form-table">
						<tbody>

						<tr valign="top">
							<th scope="row"><?php 
        _e('Add New', 'post-connector');
        ?>
</th>
							<td colspan="2" class="nowrap">
								<label for="add_new" class=""><input type="checkbox" name="add_new" id="add_new" value="1"<?php 
        echo $connection->get_add_new() == '1' ? " checked='checked'" : "";
        ?>
 /> <?php 
        _e('Allow users to create and link new posts.', 'post-connector');
        ?>
								</label>
							</td>
						</tr>

						<tr valign="top">
							<th scope="row"><?php 
        _e('Add Existing', 'post-connector');
        ?>
</th>
							<td colspan="2" class="nowrap">
								<label for="add_existing" class=""><input type="checkbox" name="add_existing" id="add_existing" value="1"<?php 
        echo $connection->get_add_existing() == '1' ? " checked='checked'" : "";
        ?>
 /> <?php 
        _e('Allow users to link existing posts.', 'post-connector');
        ?>
								</label>
							</td>
						</tr>

						</tbody>
					</table>

					<h3 class="pc-title"><?php 
        _e('Automatically display linked children', 'post-connector');
        ?>
</h3>

					<table class="form-table">
						<tbody>

						<tr valign="top">
							<th scope="row"><?php 
        _e('Display linked children?', 'post-connector');
        ?>
</th>
							<td colspan="2" class="nowrap">
								<label for="after_post_display_children" class=""><input type="checkbox" name="after_post_display_children" id="after_post_display_children" value="1"<?php 
        echo $connection->get_after_post_display_children() == '1' ? " checked='checked'" : "";
        ?>
 /> <?php 
        _e('Display the linked child posts under each parent post.', 'post-connector');
        ?>
								</label>
							</td>
						</tr>

                        <?php 
        /**
         * Action: 'pc_connection_edit_form_display_linked_children' - Allow adding custom fields to display linked children section
         *
         * @api SP_Connection $connection The Connection that is currently edited
         */
        do_action('pc_connection_edit_form_display_linked_children', $connection);
        ?>

						</tbody>
					</table>

					<?php 
        /**
         * Action: 'pc_connection_edit_form' - Allow adding custom fields to connection edit screen
         *
         * @api SP_Connection $connection The Connection that is currently edited
         */
        do_action('pc_connection_edit_form', $connection);
        ?>

					<p class="submit">
						<input name="save" type="submit" class="button-primary" id="publish" accesskey="p" value="<?php 
        _e('Save Connection', 'post-connector');
        ?>
">
					</p>
				</form>
			</div>
			<?php 
        $this->sidebar();
        ?>
		</div>
	<?php 
    }
 /**
  * An update is required, do it
  *
  * @param $current_version
  */
 private function do_update($current_version)
 {
     /**
      * Move the current version option, a tricky upgrade.
      * This upgrade routine was introduced in version 1.5.2
      */
     if ($current_version == 1) {
         // Get the original option and save it as the current version.
         // If there is no original option the $current_version will remain 1.
         $current_version = get_option('sp_current_version', 1);
         // Only resave the version if it changed.
         if (1 != $current_version) {
             // Set the new version option
             update_option(SP_Constants::OPTION_CURRENT_PREMIUM_VERSION, $current_version);
             // Delete the old version option
             delete_option('sp_current_version');
         }
     }
     // < 1.5.2
     if ($current_version < 22) {
         /**
          * Upgrade to version 1.5.2
          *
          * - Save the license key in the new option and remove the old license option
          */
         // Resave the license key
         $license_key = get_option('post-connector_license', '');
         if ('' != $license_key) {
             update_option('post-connector-premium_license', $license_key);
             delete_option('connector_license');
         }
     }
     // < 1.4.0
     if ($current_version < 18) {
         /**
          * Upgrade to version 1.4.0.0
          *
          * - Change the title of all post link to the post type link slug, see #123 for more info.
          * - Remove 'sp_install_version' option from database, this option was and will never be used.
          */
         // -- Remove 'sp_install_version' option from database, this option was and will never be used.
         delete_option('sp_install_version');
         // -- Change the title of all post link to the post type link slug, see #123 for more info.
         // Post Type Link Manager
         $post_type_link_manager = new SP_Connection_Manager();
         // Add post title check filter
         add_filter('posts_clauses', array($this, 'alter_slug_update_query'), 10, 2);
         // Update all link titles to new slug setup
         $post_links = get_posts(array('post_type' => SP_Constants::CPT_LINK, 'posts_per_page' => -1, 'suppress_filters' => false));
         // Remove the check filter
         remove_filter('posts_clauses', array($this, 'alter_slug_update_query'));
         if (count($post_links) > 0) {
             foreach ($post_links as $post_link) {
                 // Get Post Type Link ID
                 $ptl_id = get_post_meta($post_link->ID, 'sp_pt_link', true);
                 // Check if PTL ID is != ''
                 if ('' != $ptl_id) {
                     // Get the Connection object
                     $ptl = $post_type_link_manager->get_connection($ptl_id);
                     // Update the post link with the new title
                     wp_update_post(array('ID' => $post_link->ID, 'post_title' => 'sp_' . $ptl->get_slug()));
                 }
             }
         }
     }
     // < 1.5.0
     if ($current_version < 20) {
         /**
          * Upgrade to version 1.5.0
          *
          * - Change the title of all post link to the post type link slug, see #123 for more info.
          * - Remove 'sp_install_version' option from database, this option was and will never be used.
          */
         // Migrate license key and status
         $license_manager = new Yoast_Plugin_License_Manager(new SP_Product_Post_Connector());
         $license_manager->set_license_key(get_option('subposts_license_key', ''));
         $license_manager->set_license_status(get_option('subposts_license_status', ''));
     }
 }