Fix attachment output

@version 1.0
@since 0.8.2

@param string $link permalink URI.
@param int    $post_id Post ID.

@return string
436706
public function attachment_link( $link, $post_id ) {
		/**
		 * WP_Rewrite.
		 *
		 * @var WP_Rewrite $wp_rewrite
		 */
		global $wp_rewrite;

		if ( ! $wp_rewrite->permalink_structure ) {
			return $link;
		}

		$post = get_post( $post_id );
		if ( ! $post->post_parent ) {
			return $link;
		}

		$post_parent = get_post( $post->post_parent );
		if ( ! $post_parent ) {
			return $link;
		}

		$pt_object = get_post_type_object( $post_parent->post_type );

		if ( empty( $pt_object->rewrite ) ) {
			return $link;
		}

		if ( ! in_array( $post->post_type, CPTP_Util::get_post_types(), true ) ) {
			return $link;
		}

		$permalink   = CPTP_Util::get_permalink_structure( $post_parent->post_type );
		$post_type   = get_post_type_object( $post_parent->post_type );

		if ( empty( $post_type->_builtin ) ) {
			if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $link, 'attachment/' ) ) {

				$link = str_replace( $post->post_name, 'attachment/' . $post->post_name, $link );
			}
		}

		return $link;
	}
49322
public function getAttachmentURL($url, $post_id) {
	    if ($this->skipGifs) {
	        $mimeType = get_post_mime_type($post_id);
	        if ($mimeType == 'image/gif') {
                /** @var StorageTool $storageTool */
                $storageTool = ToolsManager::instance()->tools['storage'];

                $gifURL = $storageTool->getAttachmentURL($url, $post_id);
                return $gifURL;
            }
        }

		$res = $this->buildImgixImage($post_id, 'full');
		if(!$res || !is_array($res)) {
			return $url;
		}

		$new_url = $res[0];
		if(!$new_url) {
			return $url;
		}

		return $new_url;
	}
609856
public function outputColumnContent($post_id)
    {
        $meta = get_post_meta($post_id, $this->id, true);

        echo wp_get_attachment_image($meta, [100, 100]);
    }
605753
public function deletePost($post_id)
	{
		$token = $this->oauth->getToken();

		// Set parameters.
		$parameters = array(
			'oauth_token' => $token['key']
		);

		// Set the success response code.
		$this->oauth->setOption('success_code', 204);

		// Set the API base
		$base = '/v1/posts/' . $post_id;

		// Build the request path.
		$path = $this->getOption('api.url') . $base;

		// Send the request.
		$response = $this->oauth->oauthRequest($path, 'DELETE', $parameters);

		return $response;
	}
1027426
public function add_link(string $id_option, string $id_link): ?string
  {
    $id = $this->db->select_one($this->class_cfg['table'], $this->fields['id'], [
      $this->fields['id_user'] => $this->id_user,
      $this->fields['id_option'] => $id_option
    ]);
    if ( $id ){
      if ( $this->db->update($this->class_cfg['table'], [
        $this->fields['id_link'] => $id_link
      ], ['id' => $id]) ){
        return $id;
      }
    }
    else if ( $this->db->insert($this->class_cfg['table'], [
      $this->fields['id_user'] => $this->id_user,
      $this->fields['id_option'] => $id_option,
      $this->fields['id_link'] => $id_link
    ]) ){
      return $this->db->last_id();
    }
    return null;
  }
972061
public function attachment() {
		if ( $this->_post->post_parent ) {
			if ( $parent_post = get_post( $this->_post->post_parent ) ) {
				$this->addHierarchyOfSingularCrumbs( $parent_post );
				$this->addCrumb(
					get_the_title( $parent_post ),
					get_permalink( $parent_post )
				);
			}
		}
		$this->addCrumb(
			$this->label('attachment', get_the_title( $this->_post ) ),
			get_permalink( $this->_post )
		);
	}
745268
private function loopBody(int $post_id, int $count): string
    {
        $post = $this->post($post_id);

        return

        $this->openPostTag($post, $count).
        $this->thumb('side', $post, $count).
        $this->openHeader($post, $count).
        $this->postInfo('title', 'before', $post, $count).
        $this->title($post, $count).
        $this->postInfo('title', 'after', $post, $count).
        $this->closeHeader($post, $count).
        $this->thumb('top', $post, $count).
        $this->content($post, $count).
        $this->excerpt($post, $count).
        $this->openFooter($post, $count).
        $this->postInfo('excerpt', 'after', $post, $count).
        $this->closeFooter($post, $count).
        $this->closePostTag($post, $count);
    }
1051855
public function brokenLinksColumnSorting()
    {
        add_filter('posts_fields', function ($fields, $query) {
            if ($query->get('orderby') !== 'broken-links') {
                return $fields;
            }

            global $wpdb;

            $fields .= ", (
                SELECT COUNT(*)
                FROM " . self::$dbTable . "
                WHERE post_id = {$wpdb->posts}.ID
            ) AS broken_links_count";

            return $fields;
        }, 10, 2);

        add_filter('posts_orderby', function ($orderby, $query) {
            if ($query->get('orderby') !== 'broken-links') {
                return $orderby;
            }

            $orderby = "broken_links_count {$query->get('order')}, " . $orderby;
            return $orderby;
        }, 10, 2);
    }
32982
protected function prepareAttachments(Post $post)
	{
		$attachments = array();
		$attachments['message'] = $post->getMessage();
		if($post->getLink())
		{
			$attachments['link'] = $post->getLink();
		}
		//TODO: Image web path required
		if($post->getImages())
		{
			$images = $post->getImages();
			$attachments['picture'] = array_shift($images);
		}
		return $attachments;
	}
1284585
public function replaceMedia($postId)
    {
        if (empty($_POST['media-replacer-replace-with']) || !is_numeric($_POST['media-replacer-replace-with'])) {
            return;
        }

        $uploadDir = wp_upload_dir();

        $filename = $uploadDir['basedir'] . '/' . get_post_meta($postId, '_wp_attached_file', true);
        $file = pathinfo($filename);
        $replacementFile = $uploadDir['basedir'] . '/' . get_post_meta($_POST['media-replacer-replace-with'], '_wp_attached_file', true);

        $backup = $this->createBackup($filename);
        $replaced = $this->replaceFile($filename, $replacementFile);

        if ($replaced) {
            unlink($replacementFile);
        }

        // Update attachment metadata
        $meta = wp_generate_attachment_metadata($postId, $filename);
        wp_update_attachment_metadata($postId, $meta);

        // Update revision meta
        $revisions = get_post_meta($postId, self::$revisionMetaKey, true);
        if (!$revisions) {
            $revisions = array();
        }

        $revisions[time()] = $backup;
        update_post_meta($postId, self::$revisionMetaKey, $revisions);

        // Remove replacement attachment post
        $this->removeAttachment($_POST['media-replacer-replace-with']);
    }
257263
private function get_category( $post_id ) {
		$category_data = get_the_category( $post_id );
		$category_arr  = [];
		foreach ( $category_data as $cat ) {
			array_push( $category_arr, $cat->term_id );
		}

		return $category_arr;
	}
1063192
function swift_get_secure_attachment_url( $post_id, $expires = 900, $size = null ) {
		return $this->swift_get_attachment_url( $post_id, $expires, $size = null );
	}
1091212
public function deleteBrokenLinks($postId)
    {
        global $wpdb;
        $tableName = self::$dbTable;
        $wpdb->delete($tableName, array('post_id' => $postId), array('%d'));
    }
919839
function checkLink()
    {
        if (!isset($_POST['link'])) {
            $this->sendError();
        }

        $link = strtolower(str_replace([" ",'"',"'",';','.',','], ["-",""], preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities(trim($_POST['link'])))));

        $base = new Model\Base;
        $status = $base->checkLink($link, $_POST['aID']) === false ? 'ok' : 'error';

        $this->send(['status'=>$status,'link'=>$link]);
    }
948146
public function delete_data( $post_id ) {
		$table = $this->get_related_table_name();
		$this->delete_misc( $post_id );

		return $this->app->db->delete( $table, [
			'post_id' => $post_id,
		] );
	}
837012
public static function getAttachmentIdFromUrl($attachmentUrl = '')
    {
        if (empty($attachmentUrl)) {
            return;
        }
        global $wpdb;
        $attachmentId = false;
        // Get the upload directory paths
        $upload_dir_paths = wp_upload_dir();
        // Make sure the upload path base directory exists in the attachment URL,
        // to verify that we're working with a media library image
        if (false !== strpos($attachmentUrl, $upload_dir_paths['baseurl'])) {
            // If this is the URL of an auto-generated thumbnail, get the URL of the original image
            $attachmentUrl = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachmentUrl);
            // Remove the upload path base directory from the attachment URL
            $attachmentUrl = str_replace($upload_dir_paths['baseurl'] . '/', '', $attachmentUrl);
            // Finally, run a custom database query to get the attachment ID from the modified
            // attachment URL
            $attachmentId = $wpdb->get_var(
                $wpdb->prepare(
                    "SELECT wposts.ID
					FROM {$wpdb->posts} wposts, {$wpdb->postmeta} wpostmeta
					WHERE wposts.ID = wpostmeta.post_id
					AND wpostmeta.meta_key = '_wp_attached_file'
					AND wpostmeta.meta_value = '%s'
					AND wposts.post_type = 'attachment'", $attachmentUrl));
        }
        return $attachmentId;
    }
343870
function mf_get_post_values( $post_id ) {
    global $wpdb;

    $raw = $wpdb->get_results(
      "SELECT
        mfpm.meta_id,
        mfpm.field_name,
        mfpm.field_count,
        mfpm.group_count,
        pm.meta_value
      FROM
        ".MF_TABLE_POST_META." as mfpm
      LEFT JOIN
       ".$wpdb->postmeta." as pm
      ON
        ( mfpm.meta_id = pm.meta_id )
      WHERE
        mfpm.post_id = ".$post_id
    );

    $data = array();

    foreach( $raw as $key => $field ){
      $data[$field->field_name][$field->group_count][$field->field_count] = $field->meta_value;
    }

    return $data;
  }